90 lines · 4.4 KB · RustRaw
use super::*;use cotton_store::driver::Driver;use rusqlite::types::Value as SqlValue;type Case = (Option<i64>, Option<i64>, Option<i64>, Option<&'static str>, Option<&'static str>);/// (month, day, year, whole, the birthday said). Synthetic dates only.const CASES: &[Case] = &[ // The phone's own parts. (Some(8), Some(17), Some(1992), None, Some("1992-08-17")), (Some(8), Some(17), None, None, Some("--08-17")), // The object's whole date, the parts empty. (None, None, None, Some("1988-11-20"), Some("1988-11-20")), (None, None, None, Some("--03-02"), Some("--03-02")), (None, None, None, Some("1988-11-20T00:00:00.000Z"), Some("1988-11-20")), // Both: the parts win. (Some(4), Some(2), None, Some("1988-11-20"), Some("--04-02")), // Half the parts, or parts out of range, fall to the whole date. (Some(4), None, None, Some("--03-02"), Some("--03-02")), (Some(13), Some(2), None, Some("1988-11-20"), Some("1988-11-20")), // Nothing that reads as a day. (None, None, None, None, None), (None, None, None, Some(""), None), (None, None, None, Some("someday"), None), (None, None, None, Some("1988-13-20"), None), (None, None, None, Some("--03-32"), None), (None, None, None, Some("1988/11/20"), None), (Some(2), None, Some(1990), None, None),];fn file() -> Driver { let db = Driver::open_in_memory().expect("an in-memory file opens"); db.exec("CREATE TABLE cotton_people (id INTEGER PRIMARY KEY, birth_month INTEGER, birth_day INTEGER, birth_year INTEGER, birthday TEXT)") .expect("a person table"); db}fn value<T: Into<SqlValue>>(value: Option<T>) -> SqlValue { value.map_or(SqlValue::Null, Into::into)}/// RULE: a person's birthday is the phone's own month and day (and year) where they are set, else the date the object keeps whole, else none — said as "1992-08-17", or "--08-17" with no year.#[test]fn the_parts_win_else_the_whole_date_else_none() { for (month, day, year, whole, said) in CASES { let birthday = Birthday::of(*month, *day, *year, *whole); assert_eq!(birthday.map(|b| b.said()).as_deref(), *said, "{month:?} {day:?} {year:?} {whole:?}"); }}/// RULE: the SQL a list narrows and sorts a person's birthday by answers what a row read answers, case by case, so a view, a reminder and a phone read never disagree.#[test]fn the_sql_twin_answers_what_the_row_read_answers() { let db = file(); for (id, (month, day, year, whole, _)) in CASES.iter().enumerate() { db.run( "INSERT INTO cotton_people (id, birth_month, birth_day, birth_year, birthday) VALUES (?,?,?,?,?)", &[SqlValue::Integer(id as i64), value(*month), value(*day), value(*year), value(whole.map(str::to_string))], ) .expect("a person"); } let sql = format!( "SELECT {}, {} AS said FROM cotton_people ORDER BY id", columns_sql(PEOPLE_TABLE, true), birthday_sql(PEOPLE_TABLE, true) ); let rows = db.all(&sql, &[]).expect("the read"); assert_eq!(rows.len(), CASES.len()); for (row, (month, day, year, whole, said)) in rows.iter().zip(CASES) { let read = Birthday::of_row(row).map(|b| b.said()); assert_eq!(read.as_deref(), *said, "row read: {month:?} {day:?} {year:?} {whole:?}"); assert_eq!(row.text("said"), *said, "SQL: {month:?} {day:?} {year:?} {whole:?}"); }}/// RULE: a file that never kept the object's whole date reads the birthday from the parts alone, naming no column it does not have.#[test]fn a_file_without_the_whole_date_reads_the_parts_alone() { let db = Driver::open_in_memory().expect("an in-memory file opens"); db.exec("CREATE TABLE cotton_people (id INTEGER PRIMARY KEY, birth_month INTEGER, birth_day INTEGER, birth_year INTEGER)") .expect("a person table"); db.exec("INSERT INTO cotton_people VALUES (1, 8, 17, NULL), (2, NULL, NULL, NULL)").expect("people"); assert!(!whole_held(&db).expect("the served columns read")); let sql = format!("SELECT {}, {} AS said FROM cotton_people ORDER BY id", columns_sql(PEOPLE_TABLE, false), birthday_sql(PEOPLE_TABLE, false)); let rows = db.all(&sql, &[]).expect("the read"); assert_eq!(rows[0].text("said"), Some("--08-17")); assert_eq!(Birthday::of_row(&rows[0]), Some(Birthday { month: 8, day: 17, year: None })); assert_eq!(rows[1].text("said"), None); assert_eq!(Birthday::of_row(&rows[1]), None);}