use super::*; fn fixture_path() -> std::path::PathBuf { cotton_store::manifest_dir().join("tests/fixtures/book_filled_from.json") } /// RULE: the phone and the object stamp one closed list of sources: the /// shared fixture is this file's `lists`, and the Worker's /// `test/books.test.ts` holds `BOOK_SOURCES` to the same. Regenerate with /// COTTON_WRITE_BOOK_FILLED_FROM=1. #[test] fn the_shared_list_is_this() { if std::env::var_os("COTTON_WRITE_BOOK_FILLED_FROM").is_some() { std::fs::write(fixture_path(), format!("{}\n", serde_json::to_string_pretty(&lists()).expect("JSON"))).expect("the fixture is written"); } let held: Value = serde_json::from_str(&std::fs::read_to_string(fixture_path()).expect("the shared fixture")).expect("the fixture is JSON"); assert_eq!(held, lists(), "tests/fixtures/book_filled_from.json drifted from cotton_core::books::SOURCES"); } #[test] fn a_stamp_names_each_column_beside_what_was_said() { let first = stamped(None, ["author", "year"], FROM_OPF); assert_eq!(serde_json::from_str::(&first).unwrap(), json!({"author": "opf", "year": "opf"})); let more = stamped(Some(&first), ["pages", "year"], FROM_OPEN_LIBRARY); assert_eq!(serde_json::from_str::(&more).unwrap(), json!({"author": "opf", "year": "open_library", "pages": "open_library"})); assert_eq!(stamped(Some("not json"), ["isbn"], FROM_GOODREADS), r#"{"isbn":"goodreads"}"#); } /// RULE: the book table carries `filled_from` shaped as film_episode's. #[test] fn the_book_table_holds_filled_from_as_film_does() { let column = |table: &str| -> Value { let path = cotton_store::manifest_dir().join(format!("kits/personal/tables/{table}.json")); let def: Value = serde_json::from_str(&std::fs::read_to_string(path).expect("the table")).expect("JSON"); def["columns"].as_array().expect("columns").iter().find(|c| c["key"] == "filled_from").cloned().unwrap_or(Value::Null) }; assert_ne!(column("book"), Value::Null); assert_eq!(column("book"), column("film_episode")); }