Fix catchall V0 schemas, add more tests

This commit is contained in:
xenofem 2022-08-16 03:52:33 -04:00
parent ebffd7a922
commit 805c6cf15e
2 changed files with 40 additions and 3 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "jsondb"
version = "0.3.1"
version = "0.3.2"
edition = "2021"
authors = ["xenofem <xenofem@xeno.science>"]
license = "MIT"

View file

@ -100,8 +100,10 @@ impl<T: SchemaV0> Schema for T {
if version != 0 {
return Err(Error::UnknownVersion(version));
}
Ok(serde_json::from_str::<VersionedData<Self>>(s)?.data)
} else {
Ok(serde_json::from_str(s)?)
}
Ok(serde_json::from_str(s)?)
}
}
@ -297,7 +299,7 @@ impl<T: Schema> JsonDb<T> {
#[cfg(test)]
mod tests {
use std::fs::File;
use std::{collections::HashMap, fs::File};
use serde::{Deserialize, Serialize};
use serde_json::Value;
@ -520,4 +522,39 @@ mod tests {
assert_eq!(&value["last_updated"], "2022-08-15T17:47:18Z");
}
}
#[test]
fn catchall_schema() {
#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
struct Catchall(HashMap<String, String>);
impl SchemaV0 for Catchall {}
let mut data = HashMap::new();
data.insert("hello".into(), "world".into());
data.insert("catch".into(), "all".into());
assert_eq!(
Catchall::parse(r#"{"version":0,"hello":"world","catch":"all"}"#).unwrap(),
Catchall(data)
)
}
#[test]
fn unversioned() {
#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
struct Data {
number: usize,
truth: bool,
}
impl SchemaV0 for Data {
const EXPECT_VERSION_NUMBER: bool = false;
}
assert_eq!(
Data::parse(r#"{"number":42,"truth":true}"#).unwrap(),
Data {
number: 42,
truth: true,
},
);
}
}