Compare commits

..

1 Commits

Author SHA1 Message Date
5602ee7c0a Implement database feature
A simple JSON-based database that works through loading full self-copy
into the memory. Should be more than enough for the needs of kf modding.
2020-11-28 23:35:27 +07:00
4 changed files with 41 additions and 161 deletions

130
Cargo.lock generated
View File

@ -12,15 +12,9 @@ dependencies = [
"custom_error 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "custom_error 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.59 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.59 (registry+https://github.com/rust-lang/crates.io-index)",
"serial_test 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"simplelog 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "simplelog 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "0.1.10" version = "0.1.10"
@ -38,14 +32,6 @@ dependencies = [
"winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "cloudabi"
version = "0.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "custom_error" name = "custom_error"
version = "1.8.0" version = "1.8.0"
@ -56,24 +42,11 @@ name = "itoa"
version = "0.4.6" version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.80" version = "0.2.80"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lock_api"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"scopeguard 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.11" version = "0.4.11"
@ -99,59 +72,11 @@ dependencies = [
"autocfg 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "autocfg 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "parking_lot"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lock_api 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot_core 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "parking_lot_core"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.80 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.57 (registry+https://github.com/rust-lang/crates.io-index)",
"smallvec 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "proc-macro2"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "quote"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "redox_syscall"
version = "0.1.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.5" version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.117" version = "1.0.117"
@ -167,26 +92,6 @@ dependencies = [
"serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "serial_test"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)",
"serial_test_derive 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serial_test_derive"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.52 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "simplelog" name = "simplelog"
version = "0.8.0" version = "0.8.0"
@ -197,21 +102,6 @@ dependencies = [
"termcolor 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "termcolor 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "smallvec"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "syn"
version = "1.0.52"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "termcolor" name = "termcolor"
version = "1.1.2" version = "1.1.2"
@ -230,11 +120,6 @@ dependencies = [
"winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "unicode-xid"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.10.0+wasi-snapshot-preview1" version = "0.10.0+wasi-snapshot-preview1"
@ -269,35 +154,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata] [metadata]
"checksum autocfg 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" "checksum autocfg 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
"checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
"checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
"checksum chrono 0.4.19 (registry+https://github.com/rust-lang/crates.io-index)" = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" "checksum chrono 0.4.19 (registry+https://github.com/rust-lang/crates.io-index)" = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
"checksum custom_error 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "51ac5e99a7fea3ee8a03fa4721a47e2efd3fbb38358fc61192a54d4c6f866c12" "checksum custom_error 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "51ac5e99a7fea3ee8a03fa4721a47e2efd3fbb38358fc61192a54d4c6f866c12"
"checksum itoa 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" "checksum itoa 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"
"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
"checksum libc 0.2.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614" "checksum libc 0.2.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614"
"checksum lock_api 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "c4da24a77a3d8a6d4862d95f72e6fdb9c09a643ecdb402d754004a557f2bec75"
"checksum log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)" = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" "checksum log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)" = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b"
"checksum num-integer 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)" = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" "checksum num-integer 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)" = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
"checksum num-traits 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" "checksum num-traits 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
"checksum parking_lot 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d3a704eb390aafdc107b0e392f56a82b668e3a71366993b5340f5833fd62505e"
"checksum parking_lot_core 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d58c7c768d4ba344e3e8d72518ac13e259d7c7ade24167003b8488e10b6740a3"
"checksum proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)" = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
"checksum quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
"checksum redox_syscall 0.1.57 (registry+https://github.com/rust-lang/crates.io-index)" = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
"checksum ryu 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" "checksum ryu 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
"checksum scopeguard 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
"checksum serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)" = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a" "checksum serde 1.0.117 (registry+https://github.com/rust-lang/crates.io-index)" = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a"
"checksum serde_json 1.0.59 (registry+https://github.com/rust-lang/crates.io-index)" = "dcac07dbffa1c65e7f816ab9eba78eb142c6d44410f4eeba1e26e4f5dfa56b95" "checksum serde_json 1.0.59 (registry+https://github.com/rust-lang/crates.io-index)" = "dcac07dbffa1c65e7f816ab9eba78eb142c6d44410f4eeba1e26e4f5dfa56b95"
"checksum serial_test 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fef5f7c7434b2f2c598adc6f9494648a1e41274a75c0ba4056f680ae0c117fd6"
"checksum serial_test_derive 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d08338d8024b227c62bd68a12c7c9883f5c66780abaef15c550dc56f46ee6515"
"checksum simplelog 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2b2736f58087298a448859961d3f4a0850b832e72619d75adc69da7993c2cd3c" "checksum simplelog 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2b2736f58087298a448859961d3f4a0850b832e72619d75adc69da7993c2cd3c"
"checksum smallvec 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7acad6f34eb9e8a259d3283d1e8c1d34d7415943d4895f65cc73813c7396fc85"
"checksum syn 1.0.52 (registry+https://github.com/rust-lang/crates.io-index)" = "6c1e438504729046a5cfae47f97c30d6d083c7d91d94603efdae3477fc070d4c"
"checksum termcolor 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" "checksum termcolor 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
"checksum time 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)" = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" "checksum time 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)" = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255"
"checksum unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
"checksum wasi 0.10.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" "checksum wasi 0.10.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
"checksum winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" "checksum winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"

View File

@ -11,4 +11,3 @@ simplelog = "0.8"
log = "0.4" log = "0.4"
serde_json = "1.0" serde_json = "1.0"
custom_error = "1.8.0" custom_error = "1.8.0"
serial_test = "0.4"

View File

@ -15,6 +15,8 @@ custom_error! { pub IOError
NotDirectory{path: String} = "Path to the database should point at a directory: {path}", NotDirectory{path: String} = "Path to the database should point at a directory: {path}",
} }
/// Reads database data from a directory at the specified path
/// as a vector of `Group`s.
pub fn read(db_path: &path::Path) -> Result<Vec<Group>, Box<dyn Error>> { pub fn read(db_path: &path::Path) -> Result<Vec<Group>, Box<dyn Error>> {
if !db_path.is_dir() { if !db_path.is_dir() {
error!( error!(
@ -45,6 +47,9 @@ pub fn read(db_path: &path::Path) -> Result<Vec<Group>, Box<dyn Error>> {
Ok(groups) Ok(groups)
} }
/// Writes data of the given database into the directory specified by the path.
/// Does not clear it from any previously existing files,
/// you can use `clear_dir()` for that.
pub fn write(db_path: &path::Path, db: &Database) -> Result<(), Box<dyn Error>> { pub fn write(db_path: &path::Path, db: &Database) -> Result<(), Box<dyn Error>> {
if db_path.exists() && !db_path.is_dir() { if db_path.exists() && !db_path.is_dir() {
error!( error!(
@ -72,6 +77,11 @@ pub fn write(db_path: &path::Path, db: &Database) -> Result<(), Box<dyn Error>>
Ok(()) Ok(())
} }
/// Given a path to directory that was used for database storage -
/// clears it's data.
///
/// This means removing any '.json' files from all immediate subdirectories and
/// then removing any subdirectories that were or became empty.
pub fn clear_dir(db_path: &path::Path) -> Result<(), Box<dyn Error>> { pub fn clear_dir(db_path: &path::Path) -> Result<(), Box<dyn Error>> {
info!( info!(
"Clearing directory {} from database files.", "Clearing directory {} from database files.",
@ -108,6 +118,8 @@ pub fn clear_dir(db_path: &path::Path) -> Result<(), Box<dyn Error>> {
Ok(()) Ok(())
} }
/// helper function to read a group from a given subdirectory:
/// loads data from all containing '.json' files.
fn read_group(group_path: &path::Path) -> Result<Option<Group>, Box<dyn Error>> { fn read_group(group_path: &path::Path) -> Result<Option<Group>, Box<dyn Error>> {
let mut files = HashMap::new(); let mut files = HashMap::new();
for entry in fs::read_dir(group_path)? { for entry in fs::read_dir(group_path)? {
@ -132,6 +144,8 @@ fn read_group(group_path: &path::Path) -> Result<Option<Group>, Box<dyn Error>>
Ok(None) Ok(None)
} }
/// Checks if given path points at a directory that can represent
/// a database group (has a valid name).
fn check_valid_group_dir(dir_path: &path::Path) -> bool { fn check_valid_group_dir(dir_path: &path::Path) -> bool {
if !dir_path.is_dir() { if !dir_path.is_dir() {
warn!( warn!(
@ -150,6 +164,8 @@ fn check_valid_group_dir(dir_path: &path::Path) -> bool {
true true
} }
/// Checks if given path points at a file that can represent
/// a database group (has a valid name).
fn check_valid_data_file(file_path: &path::Path) -> bool { fn check_valid_data_file(file_path: &path::Path) -> bool {
if file_path.is_dir() { if file_path.is_dir() {
warn!( warn!(

View File

@ -3,34 +3,32 @@ use serde_json::json;
use std::fs; use std::fs;
use std::path; use std::path;
use serial_test::serial;
const TEST_DB_PATH: &str = "./fixtures/database"; const TEST_DB_PATH: &str = "./fixtures/database";
const TEST_DB_COPY_PATH: &str = "./fixtures/copy"; const TEST_DB_MOVED_PATH: &str = "./fixtures/moved";
const TEST_DB_MOVED_COPY_PATH: &str = "./fixtures/copy_moved";
const NO_DB_MESSAGE: &str = "Can not find/load test database"; const NO_DB_MESSAGE: &str = "Can not find/load test database";
struct TestCleanup; struct TestCleanup {
path: String,
clear_moved: bool,
}
impl Drop for TestCleanup { impl Drop for TestCleanup {
fn drop(&mut self) { fn drop(&mut self) {
clear_test_db(); let _ = fs::remove_dir_all(&self.path);
if self.clear_moved {
let _ = fs::remove_dir_all(TEST_DB_MOVED_PATH);
}
} }
} }
fn prepare_db_copy() -> TestCleanup { fn prepare_db_copy(copy_id: &str, clear_moved: bool) -> (String, TestCleanup) {
clear_test_db(); let path = format!("{}_{}", TEST_DB_PATH, copy_id);
let original_db = Database::load(path::Path::new(TEST_DB_PATH)).expect(NO_DB_MESSAGE); let original_db = Database::load(path::Path::new(TEST_DB_PATH)).expect(NO_DB_MESSAGE);
original_db original_db
.write_copy(path::Path::new(TEST_DB_COPY_PATH)) .write_copy(path::Path::new(&path))
.expect("Should be able to create a new copy of the fixture database."); .expect("Should be able to create a new copy of the fixture database.");
TestCleanup (path.clone(), TestCleanup { path: path.to_owned(), clear_moved: clear_moved })
}
fn clear_test_db() {
let _ = fs::remove_dir_all(TEST_DB_COPY_PATH);
let _ = fs::remove_dir_all(TEST_DB_MOVED_COPY_PATH);
} }
#[test] #[test]
@ -322,17 +320,16 @@ fn db_remove_value() {
} }
#[test] #[test]
#[serial]
fn db_save() { fn db_save() {
let _cleanup = prepare_db_copy(); let (path, _cleanup) = prepare_db_copy("db_save", false);
// Change something up and save // Change something up and save
let mut db = Database::load(path::Path::new(TEST_DB_COPY_PATH)).expect(NO_DB_MESSAGE); let mut db = Database::load(path::Path::new(&path)).expect(NO_DB_MESSAGE);
db.remove_group("administration") db.remove_group("administration")
.expect(r#"Should be able to remove "administration" group"#); .expect(r#"Should be able to remove "administration" group"#);
db.save() db.save()
.expect("Should be able to save copy of the database."); .expect("Should be able to save copy of the database.");
// Reload and check changes // Reload and check changes
let db = Database::load(path::Path::new(TEST_DB_COPY_PATH)).expect(NO_DB_MESSAGE); let db = Database::load(path::Path::new(&path)).expect(NO_DB_MESSAGE);
assert_eq!(db.group_names().len(), 1); assert_eq!(db.group_names().len(), 1);
assert_eq!(db.group_names().get(0), Some(&"game".to_owned())); assert_eq!(db.group_names().get(0), Some(&"game".to_owned()));
assert_eq!(db.file_names_in("game").len(), 2); assert_eq!(db.file_names_in("game").len(), 2);
@ -341,23 +338,22 @@ fn db_save() {
} }
#[test] #[test]
#[serial]
fn db_change_path() { fn db_change_path() {
let _cleanup = prepare_db_copy(); let (path, _cleanup) = prepare_db_copy("db_change_path", true);
// Change something up and move // Change something up and move
let mut db = Database::load(path::Path::new(TEST_DB_COPY_PATH)).expect(NO_DB_MESSAGE); let mut db = Database::load(path::Path::new(&path)).expect(NO_DB_MESSAGE);
db.remove_group("administration") db.remove_group("administration")
.expect(r#"Should be able to remove "administration" group"#); .expect(r#"Should be able to remove "administration" group"#);
db.file_mut("game", "perks") db.file_mut("game", "perks")
.unwrap() .unwrap()
.insert("", json!({"var":7})) .insert("", json!({"var":7}))
.expect("Should be able to insert into root."); .expect("Should be able to insert into root.");
db.change_path(path::Path::new(TEST_DB_MOVED_COPY_PATH)) db.change_path(path::Path::new(TEST_DB_MOVED_PATH))
.expect("Should be able to change database's path."); .expect("Should be able to change database's path.");
assert!(!path::Path::new(TEST_DB_COPY_PATH).exists()); assert!(!path::Path::new(&path).exists());
assert!(path::Path::new(TEST_DB_MOVED_COPY_PATH).exists()); assert!(path::Path::new(TEST_DB_MOVED_PATH).exists());
// Reload and check the changes // Reload and check the changes
let db = Database::load(path::Path::new(TEST_DB_MOVED_COPY_PATH)).expect(NO_DB_MESSAGE); let db = Database::load(path::Path::new(TEST_DB_MOVED_PATH)).expect(NO_DB_MESSAGE);
assert_eq!(db.group_names().len(), 1); assert_eq!(db.group_names().len(), 1);
assert_eq!(db.group_names().get(0), Some(&"game".to_owned())); assert_eq!(db.group_names().get(0), Some(&"game".to_owned()));
assert_eq!(db.file_names_in("game").len(), 2); assert_eq!(db.file_names_in("game").len(), 2);
@ -370,10 +366,9 @@ fn db_change_path() {
} }
#[test] #[test]
#[serial]
fn db_erase() { fn db_erase() {
let _cleanup = prepare_db_copy(); let (path, _cleanup) = prepare_db_copy("db_erase", false);
let db = Database::load(path::Path::new(TEST_DB_COPY_PATH)).expect(NO_DB_MESSAGE); let db = Database::load(path::Path::new(&path)).expect(NO_DB_MESSAGE);
db.erase().expect("Should be able to erase data."); db.erase().expect("Should be able to erase data.");
assert!(!path::Path::new(TEST_DB_COPY_PATH).exists()); assert!(!path::Path::new(&path).exists());
} }