Cotton code
50 lines · 2.4 KB · RustRaw
use cotton_store::{migrate::migrate_store, Driver};fn database() -> Driver {    let db = Driver::open_in_memory().unwrap();    migrate_store(&db).unwrap();    db.exec("INSERT INTO cotton_workspaces (id,name,owner_user_id,created_at,kind,namespace) VALUES ('money-test','Synthetic Money','synthetic-person','2026-10-01','personal','money-test')").unwrap();    db}/// RULE: A money row has one status, actual or expected, so a forecast can never also count as money that moved.#[test]fn a_row_is_never_both_actual_and_expected() {    let db = database();    for status in ["actual", "expected"] {        db.run("INSERT INTO cotton_money_transactions (id,workspace_id,occurred_on,status,created_at,updated_at) VALUES (?,'money-test','2026-10-01',?,'2026-10-01','2026-10-01')", &[status.to_string().into(),status.to_string().into()]).unwrap();    }    for status in ["actual,expected", "posted", "proposed", ""] {        assert!(db            .run(                "UPDATE cotton_money_transactions SET status=? WHERE id='actual'",                &[status.to_string().into()]            )            .is_err());    }    assert_eq!(        db.get(            "SELECT COUNT(*) AS n FROM cotton_money_transactions WHERE status='actual'",            &[]        )        .unwrap()        .unwrap()        .int("n"),        Some(1)    );}/// RULE: A booked statement line is permanent evidence; deleting it must fail even when a caller bypasses the Money tools.#[test]fn a_booked_statement_line_is_never_deleted() {    let db = database();    db.exec("PRAGMA foreign_keys=OFF; INSERT INTO cotton_money_transactions (id,workspace_id,occurred_on,created_at,updated_at) VALUES ('tx','money-test','2026-10-01','2026-10-01','2026-10-01');        INSERT INTO cotton_money_evidence (id,workspace_id,transaction_id,kind,ref_id,excerpt,attached_by,created_at) VALUES ('proof','money-test','tx','statement_line','line','Synthetic payment','import','2026-10-01');        INSERT INTO cotton_money_statement_lines (id,workspace_id,statement_id,page,line_no,posted_on,text,amount_minor,direction,fingerprint,created_at) VALUES ('line','money-test','statement',1,1,'2026-10-01','Synthetic payment',100,'out','synthetic-line','2026-10-01'); PRAGMA foreign_keys=ON").unwrap();    assert!(db        .run(            "DELETE FROM cotton_money_statement_lines WHERE id='line'",            &[]        )        .is_err());}