use crate::configuration;
use crate::testing::temp;
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path;
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
struct TestConfiguration {
foo: String,
}
static TEST_IDENTIFIER: Lazy<configuration::Identifier> = Lazy::new(|| configuration::Identifier {
application: "bdrck_config".to_owned(),
name: "test".to_owned(),
});
#[test]
fn test_persistence() {
crate::init().unwrap();
let file = temp::File::new_file().unwrap();
let path: path::PathBuf = file.path().to_owned();
fs::remove_file(path.as_path()).unwrap();
let default = TestConfiguration {
foo: "this is test data".to_owned(),
};
configuration::new(
TEST_IDENTIFIER.clone(),
default.clone(),
Some(path.as_path()),
)
.ok()
.unwrap();
assert_eq!(default, configuration::get(&TEST_IDENTIFIER).ok().unwrap());
let updated = TestConfiguration {
foo: "this is some other test data".to_owned(),
};
configuration::set(&TEST_IDENTIFIER, updated.clone())
.ok()
.unwrap();
assert_eq!(updated, configuration::get(&TEST_IDENTIFIER).ok().unwrap());
configuration::remove::<TestConfiguration>(&TEST_IDENTIFIER)
.ok()
.unwrap();
configuration::new(
TEST_IDENTIFIER.clone(),
default.clone(),
Some(path.as_path()),
)
.ok()
.unwrap();
assert_eq!(updated, configuration::get(&TEST_IDENTIFIER).ok().unwrap());
configuration::reset::<TestConfiguration>(&TEST_IDENTIFIER)
.ok()
.unwrap();
assert_eq!(default, configuration::get(&TEST_IDENTIFIER).ok().unwrap());
}