Cotton code
62 lines · 2.8 KB · RustRaw
//! A link or view that stands in one kind of database (`when: {database}`)://! the definitions a file resolves leave it out in a database of the other//! kind, so the app and an assistant never draw it there. Every row invented.mod support;use rusqlite::types::Value as SqlValue;use serde_json::json;use support::harness::Harness;const NOW: &str = "2026-10-17T00:00:00.000Z";fn define_lessons(h: &Harness) {    let lesson = json!({        "id": "lesson", "name": "Lesson", "plural": "Lessons", "picture": null,        "builtin": false, "engine": null,        "columns": [{ "key": "title", "type": "text", "required": true }, { "key": "paid", "type": "bool" }],        "title": "title", "date": null, "project": null, "order": null,        "slip": { "title": "title", "subtitle": null, "trailing": null, "photo": null, "toggle": null },        "tile": { "face": "slip-list" },        "page": [{ "heading": null, "columns": ["title"] }],        "links": [            { "label": "Paid", "table": "lesson", "where": { "paid": true } },            { "label": "By month", "table": "lesson", "when": { "database": "project" } },            { "label": "Mine", "table": "lesson", "when": { "database": "personal" } }        ],        "instructions": "A lesson is one class."    });    h.run(        "INSERT INTO cotton_table_definitions (id, workspace_id, definition, builtin, created_at, updated_at) \         VALUES (?, ?, ?, 0, ?, ?)",        &[            SqlValue::Text("lesson".into()),            SqlValue::Text(h.workspace_id.clone()),            SqlValue::Text(lesson.to_string()),            SqlValue::Text(NOW.into()),            SqlValue::Text(NOW.into()),        ],    );}fn links(h: &Harness) -> Vec<String> {    let workspace = h.workspace_id.clone();    let definitions =        h.with(|db| cotton_tools::definitions::table_definitions_from_driver(db, &workspace)).expect("definitions");    let lesson = definitions.definitions.iter().find(|table| table.id == "lesson").expect("lessons resolve");    lesson.links.iter().flatten().map(|link| link.label.clone()).collect()}/// RULE: A link whose `when` names a kind of database stands only in a database of that kind — the personal database never reads a project's Months, and a project never reads the personal one's — and a link with no `when` stands in both.#[test]fn a_link_stands_only_in_the_kind_of_database_its_when_names() {    let h = Harness::new();    h.run("UPDATE cotton_workspaces SET kind = 'personal' WHERE id = ?", &[SqlValue::Text(h.workspace_id.clone())]);    define_lessons(&h);    assert_eq!(links(&h), ["Paid", "Mine"]);    let h = Harness::new();    h.run("UPDATE cotton_workspaces SET kind = 'shared' WHERE id = ?", &[SqlValue::Text(h.workspace_id.clone())]);    define_lessons(&h);    assert_eq!(links(&h), ["Paid", "By month"], "a project's database");}