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());
configuration::remove::<TestConfiguration>(&TEST_IDENTIFIER)
.ok()
.unwrap();
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
struct OtherConfiguration {
bar: i32,
}
fn err_id(name: &str) -> configuration::Identifier {
configuration::Identifier {
application: "bdrck_config_errors".to_owned(),
name: name.to_owned(),
}
}
#[test]
fn test_operations_on_unknown_identifier() {
crate::init().unwrap();
let id = err_id("never_registered");
assert!(configuration::get::<TestConfiguration>(&id).is_err());
assert!(
configuration::set::<TestConfiguration>(
&id,
TestConfiguration {
foo: "x".to_owned()
},
)
.is_err()
);
assert!(configuration::reset::<TestConfiguration>(&id).is_err());
assert!(configuration::persist::<TestConfiguration>(&id).is_err());
assert!(configuration::remove::<TestConfiguration>(&id).is_err());
}
#[test]
fn test_operations_with_wrong_type() {
crate::init().unwrap();
let id = err_id("wrong_type");
let file = temp::File::new_file().unwrap();
let path = file.path().to_owned();
fs::remove_file(path.as_path()).unwrap();
configuration::new(
id.clone(),
TestConfiguration {
foo: "v".to_owned(),
},
Some(path.as_path()),
)
.unwrap();
assert!(configuration::get::<OtherConfiguration>(&id).is_err());
assert!(configuration::set::<OtherConfiguration>(&id, OtherConfiguration { bar: 42 }).is_err());
assert!(configuration::reset::<OtherConfiguration>(&id).is_err());
assert!(configuration::persist::<OtherConfiguration>(&id).is_err());
assert!(configuration::remove::<OtherConfiguration>(&id).is_err());
configuration::remove::<TestConfiguration>(&id)
.ok()
.unwrap();
}