Cotton code
67 lines · 3.1 KB · RustRaw
use cotton_core::definitions::{automations, TableDefinition, ToolDefinition};use serde_json::{json, Value};fn table(id: &str, columns: Value) -> TableDefinition {    let mut value: Value = serde_json::from_str(include_str!(        "../../cotton-tools/tests/fixtures/classroom/tables/student.json"    ))    .unwrap();    value["id"] = json!(id);    value["columns"] = columns;    serde_json::from_value(value).unwrap()}// RULE: Ledger rules bind typed account, purchase, date and override columns, and accept only computed balances so sync never overwrites a debit.#[test]fn ledger_binds_generic_tables_and_refuses_stored_balances() {    let mut tables = vec![        table(            "credit",            json!([                {"key":"account","type":"link","link_to":"account"},                {"key":"left","type":"number","computed":{"rollup":{"table":"movement","link":"credit","op":"count"}}},                {"key":"starts","type":"date"}, {"key":"expires","type":"date"}            ]),        ),        table(            "movement",            json!([                {"key":"account","type":"link","link_to":"account"},                {"key":"credit","type":"link","link_to":"credit"},                {"key":"on","type":"date"}, {"key":"override","type":"bool"}            ]),        ),    ];    let mut tool: ToolDefinition = serde_json::from_value(json!({"id":"ledger","name":"Ledger","tables":["credit","movement"],"instructions":"","engines":["rules@1"],"automations":[{        "id":"allocate","name":"Allocate","when":{"table":"movement","column":"account","to":"any"},"then":[{"ledger":{            "table":"credit","account":"account","on":"on","purchase":"credit","override_column":"override","purchase_account":"account","remaining":"left","expires":"expires","starts":"starts","refusal":"No credit left"        }}]    }],"author":{"device":"test","name":"Test"},"signature":null})).unwrap();    assert!(automations::validate_tables(&tool, &tables).is_ok());    // RULE: The history setting binds provenance text on both sides of the ledger.    tool.automations[0]["then"][0]["ledger"]["history"] = json!("origin");    assert!(automations::validate_tables(&tool, &tables)        .unwrap_err()        .contains("origin"));    for table in &mut tables {        table            .columns            .push(serde_json::from_value(json!({"key":"origin","type":"text"})).unwrap());    }    assert!(automations::validate_tables(&tool, &tables).is_ok());    tables[0].columns[1].computed = None;    assert!(automations::validate_tables(&tool, &tables)        .unwrap_err()        .contains("computed"));    tables[0].columns[1].computed = Some(        serde_json::from_value(json!({"rollup":{"table":"movement","link":"credit","op":"count"}}))            .unwrap(),    );    tables[1].columns[0].link_to = Some("somebody_else".into());    assert!(automations::validate_tables(&tool, &tables).is_err());    tables[1].columns[0].link_to = Some("account".into());    tool.engines.clear();    assert!(automations::validate_tables(&tool, &tables)        .unwrap_err()        .contains("rules@1"));}