Cotton code
64 lines · 2.6 KB · RustRaw
use super::*;#[test]fn every_soft_ref_parent_is_a_spec_table_and_the_lookup_finds_each() {    for (child, column, parent) in SOFT_REFS {        assert!(            TABLE_SPECS.iter().any(|spec| spec.name == *parent),            "{child}.{column} names {parent}, which has no spec"        );        assert_eq!(parent_of(child, column), Some(*parent));    }    assert_eq!(        parent_of("cotton_money_evidence", "transaction_id"),        None,        "an owning ref is not soft"    );    for (child, column, _) in SOFT_REF_DATABASES {        assert!(parent_of(child, column).is_some(), "{child}.{column} has a database pair and is not soft");    }    assert_eq!(        soft_columns("cotton_sessions").collect::<Vec<_>>(),        vec![            ("parent_session_id", "cotton_sessions"),            ("project_id", "cotton_memory_projects"),            ("todo_id", "cotton_todos"),        ]    );}#[test]fn the_fragment_tests_the_parent_rows_gone_columns_and_nothing_else() {    // The Bin rise gives People both archived and trashed states.    assert_eq!(        soft_names("c.person_id", "cotton_people"),        "(c.person_id IS NOT NULL AND EXISTS (SELECT 1 FROM cotton_people _soft WHERE _soft.id = c.person_id AND _soft.trashed_at IS NULL AND _soft.archived_at IS NULL))"    );    // A parent with none: present whenever it exists.    assert_eq!(        soft_names("s.parent_session_id", "cotton_sessions"),        "(s.parent_session_id IS NOT NULL AND EXISTS (SELECT 1 FROM cotton_sessions _soft WHERE _soft.id = s.parent_session_id AND _soft.trashed_at IS NULL))"    );    // A reference that may cross databases is named while its pair is set.    assert_eq!(        soft_names("s.todo_id", "cotton_todos"),        "(s.todo_id IS NOT NULL AND (s.todo_database IS NOT NULL OR EXISTS (SELECT 1 FROM cotton_todos _soft WHERE _soft.id = s.todo_id AND _soft.trashed_at IS NULL)))"    );    // A parent with two.    assert!(soft_names("m.reply_to_id", "cotton_msg_messages").contains("_soft.deleted_at IS NULL"));    assert!(!soft_names("m.reply_to_id", "cotton_msg_messages").contains("hidden_at"));    assert_eq!(        soft_absent("c.person_id", "cotton_people"),        format!("NOT {}", soft_names("c.person_id", "cotton_people"))    );    assert_eq!(        soft_value("c.person_id", "cotton_people"),        format!("CASE WHEN {} THEN c.person_id END", soft_names("c.person_id", "cotton_people"))    );}#[test]#[should_panic(expected = "qualified")]fn an_unqualified_column_is_refused_because_a_self_reference_would_read_the_wrong_row() {    soft_names("parent_session_id", "cotton_sessions");}