Cotton code
134 lines · 5.5 KB · RustRaw
use serde_json::{json, Value};use super::*;use crate::definitions::builtin_table_definition;/// A table of one text title column plus `more`, with `extra` keys set on it —/// the Swift `TablePickingTests.table(_:_:add:readonlyTitle:)` fixture.fn table(id: &str, name: &str, more: Value, extra: Value) -> TableDefinition {    let mut columns = vec![json!({ "key": "title", "type": "text" })];    columns.extend(more.as_array().cloned().unwrap_or_default());    let mut value = json!({        "id": id, "name": name, "plural": format!("{name}s"), "picture": null, "builtin": false,        "engine": null, "columns": columns, "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"] }],        "instructions": "A fixture."    });    for (key, held) in extra.as_object().unwrap() {        if key == "title_readonly" {            value["columns"][0]["readonly"] = held.clone();        } else {            value[key] = held.clone();        }    }    serde_json::from_value(value).expect("parses")}fn plain(id: &str, name: &str) -> TableDefinition {    table(id, name, json!([]), json!({}))}fn offered(all: &[TableDefinition]) -> Vec<&str> {    fileable_tables(all, "thought")        .into_iter()        .map(|t| t.id.as_str())        .collect()}/// RULE: A thought is filed into every table that takes a record by its words — never the table it is filed from, one nothing is typed into, or one whose title is read-only — the usual ones first, the rest by name.#[test]fn offers_tables_that_take_words_usual_ones_first() {    let all = [        plain("thought", "Thought"),        plain("lesson", "Lesson"),        table("person", "Person", json!([]), json!({ "filing_rank": 4 })),        table("thread", "Chat", json!([]), json!({ "add": false })),        table("note", "Note", json!([]), json!({ "filing_rank": 2 })),        table("call", "Call", json!([]), json!({ "title_readonly": true })),        plain("book", "Book"),        table("todo", "Todo", json!([]), json!({ "filing_rank": 1 })),    ];    assert_eq!(offered(&all), ["todo", "note", "person", "book", "lesson"]);    assert_eq!(fileable(&all[0], "thought"), Err(Unfileable::Source));    assert_eq!(fileable(&all[3], "thought"), Err(Unfileable::NotAdded));    assert_eq!(fileable(&all[5], "thought"), Err(Unfileable::TitleNotTyped));}/// RULE: A thought is never filed into a table that needs more than the words — an event needs when it starts — so a filing never ends in a refusal; a required column with a default is no bar.#[test]fn skips_tables_that_need_more_than_the_words() {    let event = table(        "event",        "Event",        json!([{ "key": "starts_at", "type": "date", "required": true }]),        json!({}),    );    let goal = table(        "goal",        "Goal",        json!([{ "key": "status", "type": "text", "required": true, "default": "active" }]),        json!({}),    );    let all = [event, goal, table("todo", "Todo", json!([]), json!({ "filing_rank": 1 }))];    assert_eq!(offered(&all), ["todo", "goal"]);    assert_eq!(        fileable(&all[0], "thought"),        Err(Unfileable::NeedsMore {            column: "starts_at".into()        })    );}/// RULE: A thought is never filed into a table whose record belongs to one parent through a link the person never sets (a chapter's book) — it is made from the parent — but a todo or a note is made by its own verb, whatever it links, and a link to a picture (a person's avatar) is no parent.#[test]fn skips_tables_that_belong_to_a_parent() {    let chapter = table(        "chapter",        "Chapter",        json!([{ "key": "book_id", "type": "link", "link_to": "book", "readonly": true }]),        json!({}),    );    let todo = table(        "todo",        "Todo",        json!([{ "key": "recurrence_id", "type": "link", "link_to": "todo_recurrence", "readonly": true }]),        json!({ "filing_rank": 1, "made_by_verb": true }),    );    let event = table(        "event",        "Event",        json!([{ "key": "people", "type": "link", "link_to": "person", "readonly": true, "multiple": true }]),        json!({ "filing_rank": 3 }),    );    let person = table(        "person",        "Person",        json!([{ "key": "avatar_photo_id", "type": "link", "link_to": "photo", "readonly": true, "media": true }]),        json!({ "filing_rank": 4 }),    );    let all = [chapter, todo, event, person];    assert_eq!(offered(&all), ["todo", "event", "person"]);    assert_eq!(        fileable(&all[0], "thought"),        Err(Unfileable::BelongsToAParent {            column: "book_id".into()        })    );}/// RULE: an editable alias on a non-addable engine projection is not a title/// that can create a record from a thought. Renaming an existing account must/// not turn its alias into a creation input or change its typed-title refusal.#[test]fn a_non_addable_projection_has_no_creation_title_even_when_its_alias_is_editable() {    let mut projected = plain("projection", "Projected");    projected.derived = Some(true);    projected.add = Some(false);    assert_eq!(fileable(&projected, "thought"), Err(Unfileable::TitleNotTyped));    let mut ordinary = plain("archive", "Archive");    ordinary.add = Some(false);    assert_eq!(fileable(&ordinary, "thought"), Err(Unfileable::NotAdded));}