Cotton code
37 lines · 2.3 KB · RustRaw
use super::*;use cotton_store::fixtures::{file_at_version, seed_workspace_row, NOW, WORKSPACE};use serde_json::json;fn fixture() -> Driver {    let db = file_at_version(cotton_store::migrate::SCHEMA_VERSION);    seed_workspace_row(&db);    db}fn imported(db: &Driver, id: &str, at: &str) {    db.run("INSERT INTO cotton_call_log(id,workspace_id,source,external_id,provider,direction,outcome,started_at,created_at,updated_at) VALUES(?,?,'ios_call_history',?,'facetime_audio','out','answered',?,?,?)", &[id.to_string().into(),WORKSPACE.to_string().into(),id.to_string().into(),at.to_string().into(),NOW.to_string().into(),NOW.to_string().into()]).unwrap();}/// RULE: The Calls view keeps imported provenance.#[test]fn imported_history_is_readable() {    let db = fixture();    imported(&db, "imported", NOW);    let found = read_calls(&db, WORKSPACE).unwrap();    assert_eq!(found[0]["source"], "ios_call_history");    assert_eq!(found[0]["external_id"], "imported");}/// RULE: An imported call retains its stored person reference while a gone person reads as absent, so restoring the person restores the link without rewriting call history.#[test]fn imported_person_links_disappear_and_return_with_the_person() {    let db = fixture();    imported(&db, "linked-call", NOW);    db.run("INSERT INTO cotton_people(id,workspace_id,full_name,created_at,updated_at) VALUES('linked-person',?,'Invented Person',?,?)", &[WORKSPACE.to_string().into(),NOW.to_string().into(),NOW.to_string().into()]).unwrap();    db.run("UPDATE cotton_call_log SET person_id='linked-person' WHERE id='linked-call'", &[]).unwrap();    assert_eq!(read_calls_for_person(&db, WORKSPACE, "linked-person").unwrap().len(), 1);    db.run("UPDATE cotton_people SET archived_at=? WHERE id='linked-person'", &[NOW.to_string().into()]).unwrap();    assert!(read_calls_for_person(&db, WORKSPACE, "linked-person").unwrap().is_empty());    assert_eq!(read_calls(&db, WORKSPACE).unwrap()[0]["person_id"], Value::Null);    assert_eq!(db.get("SELECT person_id FROM cotton_call_log WHERE id='linked-call'", &[]).unwrap().unwrap().text("person_id"), Some("linked-person"));    db.run("UPDATE cotton_people SET archived_at=NULL WHERE id='linked-person'", &[]).unwrap();    assert_eq!(read_calls_for_person(&db, WORKSPACE, "linked-person").unwrap().len(), 1);}