(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) (* Tests for {!Sql.Private.Table}: a table as a write computes its rows, read from the storage schema its CREATE TABLE declares. The expected values are the sqlite3 CLI's (3.54) for the same statements. *) module Table = Sql.Private.Table module Ast = Sql.Ast let schema sql = match Sql.Private.Lexer.parse sql with | Ok ct -> Table.v (Sql.Private.Engine.table_schema ~source:sql ct) | Error e -> Alcotest.failf "%s: %s" sql e let values = Alcotest.(list (testable Catalog.Value.pp Catalog.Value.equal)) let value = Alcotest.testable Catalog.Value.pp Catalog.Value.equal let storage = let pp_storage ppf = function | Table.Strict st -> Fmt.pf ppf "STRICT %a" Sql.Private.Strict.pp st | Affinity aff -> Fmt.pf ppf "affinity %s" (match aff with | `Blob -> "BLOB" | `Integer -> "INTEGER" | `Numeric -> "NUMERIC" | `Real -> "REAL" | `Text -> "TEXT") in Alcotest.(list (testable pp_storage ( = ))) (* Each column is paired with the type it must hold: the one it declares in a STRICT table, nothing in an ordinary one. *) let test_storage_pairs_each_column_with_its_type () = let strict = schema "CREATE TABLE t (a INT, b TEXT, c ANY) STRICT" in Alcotest.check storage "declared types" [ Table.Strict Sql.Private.Strict.Int; Table.Strict Sql.Private.Strict.Text; Table.Strict Sql.Private.Strict.Any; ] (Table.storage strict); let plain = schema "CREATE TABLE t (a INT, b VARCHAR(10), c)" in Alcotest.check storage "the affinity of the declared type" [ Table.Affinity `Integer; Affinity `Text; Affinity `Blob ] (Table.storage plain) (* A column of a STRICT table must name one of the six types SQLite knows; one that names none, or names something else, is refused by name. *) let test_a_strict_column_must_name_one_of_the_six_types () = let storage sql = ignore (Table.storage (schema sql) : Table.storage list) in Alcotest.check_raises "no datatype" (Catalog.Error (Prepare "missing datatype for t.a")) (fun () -> storage "CREATE TABLE t (a) STRICT"); Alcotest.check_raises "not one of the six" (Catalog.Error (Prepare "unknown datatype for t.a: \"VARCHAR(10)\"")) (fun () -> storage "CREATE TABLE t (a VARCHAR(10)) STRICT") (* A value takes the column's affinity on the way in, so a NUMERIC column holds 42 rather than '42' and a TEXT column holds '42' rather than 42. *) let test_a_value_takes_the_column_affinity_on_the_way_in () = let s = schema "CREATE TABLE t (a NUMERIC, b TEXT)" in Alcotest.check values "converted" [ Ast.Int 42L; Ast.Text "42" ] (Table.coerce s [ Ast.Text "42"; Ast.Int 42L ]) (* A STRICT column converts by its declared type's affinity and then holds only that type. *) let test_a_strict_column_holds_only_its_type () = let s = schema "CREATE TABLE t (a INT) STRICT" in Alcotest.check value "numeric text converts" (Ast.Int 42L) (Table.store_value s 0 (Ast.Text "42")); Alcotest.check_raises "and nothing else" (Catalog.Error (Constraint (Datatype { table = "t"; column = "a"; declared = "INT"; stored = "TEXT" }))) (fun () -> ignore (Table.store_value s 0 (Ast.Text "abc"))) (* A row is one value per column, so a list of another width is no row, and it is refused naming the table and both widths, which List.map2's own [Invalid_argument "List.map2"] would not. *) let test_storing_a_row_needs_the_whole_row () = let s = schema "CREATE TABLE t (a INT, b TEXT, c BLOB)" in Alcotest.check_raises "a row short of the table" (Invalid_argument "table t declares 3 columns but the row holds 2 values") (fun () -> ignore (Table.coerce s [ Ast.Int 1L; Ast.Text "x" ])); Alcotest.check_raises "and one wider than it" (Invalid_argument "table t declares 3 columns but the row holds 4 values") (fun () -> ignore (Table.coerce s [ Ast.Int 1L; Ast.Text "x"; Ast.Null; Ast.Int 2L ])) (* The DEFAULT of each column, evaluated once. A constant expression is folded here rather than at every read. *) let test_defaults_are_the_columns_own () = let s = schema "CREATE TABLE t (a INT, b TEXT DEFAULT 'd', c INT DEFAULT (2 + 3))" in Alcotest.check values "folded" [ Ast.Null; Ast.Text "d"; Ast.Int 5L ] (Array.to_list (Table.defaults s)) let apply s row = Table.generate s row (* Both kinds of generated column are recomputed on a write from the row's other columns, and a later one may read an earlier one: [s] reads [b], which the same pass has just set from [a]. The computed value takes the column's affinity, so [a + 1] on an INTEGER column is an integer. *) let test_generate_computes_both_in_order () = let s = schema "CREATE TABLE t (a INT, b INT AS (a + 1) STORED, c INT AS (b + 1) \ VIRTUAL)" in Alcotest.check values "b from a, then c from b" [ Ast.Int 1L; Ast.Int 2L; Ast.Int 3L ] (apply s [ Ast.Int 1L; Ast.Null; Ast.Null ]) (* The order is the one the expressions read each other in, not the declared one: a STORED column reading a VIRTUAL one declared after it, and a STORED one reading a later STORED one, see their values (insert.c, sqlite3ComputeGeneratedColumns; sqlite3 3.54.1 stores 1|20|2 and 1|11|10). Computed in declared order, both would read NULL. *) let test_generate_follows_the_reads () = let d = schema "CREATE TABLE d (a, s AS (v * 10) STORED, v AS (a + 1))" in Alcotest.check values "s reads the later v" [ Ast.Int 1L; Ast.Int 20L; Ast.Int 2L ] (apply d [ Ast.Int 1L; Ast.Null; Ast.Null ]); let d2 = schema "CREATE TABLE d2 (a, s2 AS (s1 + 1) STORED, s1 AS (a * 10) STORED)" in Alcotest.check values "s2 reads the later s1" [ Ast.Int 1L; Ast.Int 11L; Ast.Int 10L ] (apply d2 [ Ast.Int 1L; Ast.Null; Ast.Null ]) (* The INTEGER PRIMARY KEY is set to the rowid before any generated column is computed, so one over it reads the key the row is stored under, not the NULL the INSERT wrote (insert.c, sqlite3Insert: "in case one of the generated columns is derived from the INTEGER PRIMARY KEY"). *) let test_generate_reads_the_rowid () = let s = schema "CREATE TABLE t (id INTEGER PRIMARY KEY, s AS (id) STORED, v AS (id * 2))" in Alcotest.check values "s and v from the rowid" [ Ast.Int 7L; Ast.Int 7L; Ast.Int 14L ] (Table.generate s ~rowid:7L [ Ast.Null; Ast.Null; Ast.Null ]) (* A computed value is stored by its column's affinity, exactly as a written one is: a TEXT expression on an INTEGER column comes back an integer. *) let test_generate_takes_the_column_affinity () = let s = schema "CREATE TABLE t (a TEXT, b INT AS (a) STORED)" in Alcotest.check values "b holds the integer 42, not the text" [ Ast.Text "42"; Ast.Int 42L ] (apply s [ Ast.Text "42"; Ast.Null ]) (* A table with no generated column is returned untouched -- the common case, which the empty-list guard answers without walking the row. *) let test_generate_is_identity_without_generated_columns () = let s = schema "CREATE TABLE t (a INT, b TEXT)" in let row = [ Ast.Int 1L; Ast.Text "x" ] in Alcotest.check values "unchanged" row (apply s row) (* An INSERT's tuple through the writer of its column list [named] over [t (a, b, c)] (or [t (a, b)]), every column untyped so a value is stored as it stands. *) let write ?(cols = "a, b") ~named ?(default_for = fun _ -> Ast.Null) vals = let w = Table.writer (Fmt.kstr schema "CREATE TABLE t (%s)" cols) ~has_rowid:true ~named in Table.write w ~default_for vals (* With no column list, the tuple must match the table arity positionally and is returned as it stands. *) let test_write_positional_passes_through () = Alcotest.check values "the whole tuple, in order" [ Ast.Int 1L; Ast.Text "x" ] (fst (write ~named:[] [ Ast.Int 1L; Ast.Text "x" ])); Alcotest.check_raises "a tuple of the wrong arity is refused" (Catalog.Error (Prepare "table t has 2 columns but 1 values were supplied")) (fun () -> ignore (write ~named:[] [ Ast.Int 1L ])) (* A named column list fills the columns it names and takes [default_for] for the rest, in declared-column order; a rowid name is the key and leaves the row. *) let test_write_named_fills_the_rest_with_defaults () = let cols = "a, b, c" and default_for _ = Ast.Int 0L in Alcotest.check values "b named, a and c defaulted" [ Ast.Int 0L; Ast.Text "x"; Ast.Int 0L ] (fst (write ~cols ~named:[ "b" ] ~default_for [ Ast.Text "x" ])); Alcotest.(check (option (testable Catalog.Value.pp Catalog.Value.equal))) "the rowid named is the key" (Some (Ast.Int 9L)) (snd (write ~cols ~named:[ "rowid"; "b" ] [ Ast.Int 9L; Ast.Text "x" ])); Alcotest.check values "the rowid named alone writes every default" [ Ast.Int 0L; Ast.Int 0L; Ast.Int 0L ] (fst (write ~cols ~named:[ "rowid" ] ~default_for [ Ast.Int 9L ])); Alcotest.check_raises "a column the table does not have is refused" (Catalog.Error (Prepare "table t has no column named z")) (fun () -> ignore (write ~cols ~named:[ "z" ] [ Ast.Int 1L ])); Alcotest.check_raises "a name/value count mismatch is refused" (Catalog.Error (Prepare "1 values for 2 columns")) (fun () -> ignore (write ~cols ~named:[ "a"; "b" ] [ Ast.Int 1L ])) (* A table resolves a column list once: the writer of the list it was last asked for is the one it answers with again, whether the caller hands the same list or one spelled the same, so a statement run a row at a time resolves its list for the first row alone. Another list, or the other answer to [has_rowid], is resolved for itself, and the writer of a table is no other table's. An implementation that resolves per call answers a fresh writer each time. *) let test_a_column_list_is_resolved_once () = let t = schema "CREATE TABLE t (a, b, c)" in let named = [ "c"; "a" ] in let w = Table.writer t ~has_rowid:true ~named in let same what w' = Alcotest.(check bool) what true (w == w') and other what w' = Alcotest.(check bool) what false (w == w') in same "the same list" (Table.writer t ~has_rowid:true ~named); same "a list spelled the same" (Table.writer t ~has_rowid:true ~named:[ "c"; "a" ]); other "without a rowid" (Table.writer t ~has_rowid:false ~named); other "another list" (Table.writer t ~has_rowid:true ~named:[ "a"; "c" ]); other "another table of the same text" (Table.writer (schema "CREATE TABLE t (a, b, c)") ~has_rowid:true ~named); (* each list still writes its own row, whichever was resolved last *) let row named vals = fst (Table.write (Table.writer t ~has_rowid:true ~named) ~default_for:(fun _ -> Ast.Null) vals) in Alcotest.check values "c, a" [ Ast.Int 2L; Ast.Null; Ast.Int 1L ] (row [ "c"; "a" ] [ Ast.Int 1L; Ast.Int 2L ]); Alcotest.check values "a, c" [ Ast.Int 1L; Ast.Null; Ast.Int 2L ] (row [ "a"; "c" ] [ Ast.Int 1L; Ast.Int 2L ]); Alcotest.check values "c, a again" [ Ast.Int 2L; Ast.Null; Ast.Int 1L ] (row [ "c"; "a" ] [ Ast.Int 1L; Ast.Int 2L ]); Alcotest.check values "no list" [ Ast.Int 1L; Ast.Int 2L; Ast.Int 3L ] (row [] [ Ast.Int 1L; Ast.Int 2L; Ast.Int 3L ]) let suite = ( "table", [ Alcotest.test_case "a column list is resolved once" `Quick test_a_column_list_is_resolved_once; Alcotest.test_case "storage pairs each column with its type" `Quick test_storage_pairs_each_column_with_its_type; Alcotest.test_case "a strict column must name one of the six types" `Quick test_a_strict_column_must_name_one_of_the_six_types; Alcotest.test_case "a value takes the column affinity on the way in" `Quick test_a_value_takes_the_column_affinity_on_the_way_in; Alcotest.test_case "a strict column holds only its type" `Quick test_a_strict_column_holds_only_its_type; Alcotest.test_case "a row needs the whole row" `Quick test_storing_a_row_needs_the_whole_row; Alcotest.test_case "defaults are the columns own" `Quick test_defaults_are_the_columns_own; Alcotest.test_case "generate computes both in order" `Quick test_generate_computes_both_in_order; Alcotest.test_case "generate follows the reads" `Quick test_generate_follows_the_reads; Alcotest.test_case "generate reads the rowid" `Quick test_generate_reads_the_rowid; Alcotest.test_case "generate takes the column affinity" `Quick test_generate_takes_the_column_affinity; Alcotest.test_case "generate is identity without generated columns" `Quick test_generate_is_identity_without_generated_columns; Alcotest.test_case "a writer passes a positional tuple through" `Quick test_write_positional_passes_through; Alcotest.test_case "a writer fills the unnamed columns with defaults" `Quick test_write_named_fills_the_rest_with_defaults; ] )