Cotton code
44 lines · 2.7 KB · RustRaw
//! Synthetic coverage for SQL membership used by Money evidence narrowing.mod support;use cotton_tools::mcp::read_db::{Filter, Query, ReadDb};use rusqlite::types::Value as SqlValue;use support::harness::{synthetic_uuid, Harness};// RULE: Evidence membership scopes its inner query as well as its outer query so another workspace's evidence cannot hide an unbooked line.#[test]fn membership_scopes_both_queries_and_excludes_null_members() {    let h = Harness::new();    let foreign_workspace = synthetic_uuid("foreign-workspace");    h.run("INSERT INTO cotton_workspaces (id,name,owner_user_id,created_at,_hlc) VALUES (?,'Synthetic workspace',?,'2026-01-01','')", &[SqlValue::Text(foreign_workspace.clone()),SqlValue::Text(synthetic_uuid("foreign-owner"))]);    for (id, workspace, name) in [        (synthetic_uuid("own-bank"), h.workspace_id.clone(), "Own bank"),        (synthetic_uuid("foreign-bank"), foreign_workspace, "Other bank"),    ] {        h.run("INSERT INTO cotton_money_accounts (id,workspace_id,name,kind,subtype,currency,created_at,updated_at) VALUES (?, ?, ?, 'bank','bank','JPY','2026-01-01','2026-01-01')", &[SqlValue::Text(id),SqlValue::Text(workspace),SqlValue::Text(name.into())]);    }    let db = ReadDb::new(&h.db, &h.workspace_id);    let members = Query::on("cotton_money_accounts").columns(&["currency"]).eq("name", "Other bank".to_string());    for (negate, expected) in [(false, 0), (true, 1)] {        let query = Query::on("cotton_money_accounts").filter(Filter::InQuery {column:"currency".into(), query:Box::new(members.clone()), negate});        assert_eq!(db.select(&query).unwrap().len(), expected);    }    let nulls = Query::on("cotton_money_accounts").columns(&["archived_at"]);    let query = Query::on("cotton_money_accounts").filter(Filter::InQuery {column:"id".into(), query:Box::new(nulls), negate:true});    assert_eq!(db.select(&query).unwrap().len(), 1);}// RULE: Membership validates identifiers and refuses a paginated set rather than silently changing the relationship it filters.#[test]fn membership_refuses_invalid_or_paginated_member_queries() {    let h = Harness::new();    let db = ReadDb::new(&h.db, &h.workspace_id);    for member in [        Query::on("no_such_table").columns(&["id"]),        Query::on("cotton_money_accounts").columns(&["id); DELETE FROM cotton_money_accounts; --"]),        Query::on("cotton_money_accounts").columns(&["id","name"]),        Query::on("cotton_money_accounts").columns(&["id"]).limit(1),    ] {        let query = Query::on("cotton_money_accounts").filter(Filter::InQuery {column:"id".into(), query:Box::new(member), negate:false});        assert!(db.select(&query).is_err());    }}