Cotton code
67 lines · 3.8 KB · RustRaw
use super::*;use serde_json::json;fn definition() -> TableDefinition {    let definition = TableDefinition::from_json(&json!({        "id":"address","name":"Address","plural":"Addresses","title":"city",        "builtin":false,"slip":{"title":"city"},"tile":{"face":"slip-list"},"page":[],"instructions":"An address.",        "columns":[            {"key":"city","type":"place","place":{"id":"city_place","country":"country","zone":"timezone"}},            {"key":"city_place","type":"text","readonly":true},            {"key":"country","type":"text","readonly":true},            {"key":"timezone","type":"text","readonly":true}        ]    }).to_string()).unwrap();    definition.validate().unwrap();    definition}/// RULE: Picking writes one dataset id and its typed facts together; city text alone never guesses a place.#[test]fn resolves_all_place_columns_in_one_patch() {    let definition = definition();    let input = json!({"city":{"place":"geonames:1269843"}});    let patch = validate_patch(&definition, &input).unwrap();    let resolved = place::resolve(&definition, &patch, |id| {        assert_eq!(id, "geonames:1269843");        Ok(place::PickedPlace {id:id.into(),name:"Hyderabad".into(),country:"IN".into(),zone:"Asia/Kolkata".into(),lat:Some(17.38),lng:Some(78.47),version:Some("v1".into())})    }).unwrap();    assert_eq!(resolved, json!({"city":"Hyderabad","city_place":"geonames:1269843","country":"IN","timezone":"Asia/Kolkata"}).as_object().unwrap().clone());    let patch = validate_patch(&definition, &json!({"city":"A place I know"})).unwrap();    let resolved = place::resolve(&definition, &patch, |_| panic!("text cannot trigger a lookup")).unwrap();    assert_eq!(resolved["city"], "A place I know");    assert!(resolved["city_place"].is_null());}/// RULE: A caller cannot forge a place's readonly companion facts; only the dataset resolver may fill them.#[test]fn refuses_direct_companions_and_malformed_picks() {    let definition = definition();    assert!(place::resolve(&definition, &json!({"country":"IN"}).as_object().unwrap().clone(), |_| unreachable!()).is_err());    assert!(validate_patch(&definition, &json!({"city":{"place":"geonames:1","name":"Forged"}})).is_err());    assert!(validate_patch(&definition, &json!({"city":{"place":"http://elsewhere"}})).is_err());}/// RULE: Imported plain city text invalidates the old place id in the same minted change; picked facts and exact Undo images retain their link.#[test]fn raw_engine_text_cannot_retain_a_previous_places_facts() {    let definition = definition();    let columns = vec![("city".into(), definition.column("city").unwrap().place.clone().unwrap())];    let mut raw = json!({"city":"A new unlisted town"}).as_object().unwrap().clone();    place::invalidate_unpicked(&columns, &mut raw);    assert!(raw["city_place"].is_null() && raw["country"].is_null() && raw["timezone"].is_null());    let mut picked = json!({"city":"Hyderabad","city_place":"geonames:1269843","country":"IN","timezone":"Asia/Kolkata"}).as_object().unwrap().clone();    let before = picked.clone();    place::invalidate_unpicked(&columns, &mut picked);    assert_eq!(picked,before);}/// RULE: Keeping an unlisted place requires nonempty personal words and an explicit dataset neighbour; companion facts and arbitrary object keys cannot be forged.#[test]fn kept_places_require_personal_words_and_a_dataset_neighbour() {    let definition = definition();    assert!(validate_patch(&definition, &json!({"city":{"kept":"Invented courtyard","near":"geonames:1269843"}})).is_ok());    for input in [json!({"kept":" ","near":"geonames:1269843"}),json!({"kept":"Courtyard","near":"location:22222222-2222-4222-8222-222222222222"}),json!({"kept":"Courtyard","near":"geonames:1","country":"GB"})] {        assert!(validate_patch(&definition,&json!({"city":input})).is_err());    }}