(* A CREATE TABLE is refused in sqlite3's words and order before a store is asked to hold it: the name is located first (build.c, sqlite3StartTable), then each column as it is read, its name, DEFAULT and COLLATE (sqlite3AddColumn, sqlite3AddDefaultValue, sqlite3AddCollateType), then the table's constraints. Each expected sentence is the sqlite3 CLI's (3.54.1) for the same statement. *) let refused cat sql msg = Alcotest.check_raises sql (Catalog.Error (Prepare msg)) (fun () -> ignore (Sql.exec (Sql.v cat) sql)) let store () = let cat = Catalog.of_mem [] in ignore (Sql.exec (Sql.v cat) "CREATE TABLE t (a)"); cat let test_a_column_is_refused_as_it_is_read () = let cat = store () in refused cat "CREATE TABLE u (a COLLATE nosuch, a)" "no such collation sequence: nosuch"; refused cat "CREATE TABLE u (a DEFAULT (x), b COLLATE nosuch)" "default value of column [a] is not constant"; refused cat "CREATE TABLE u (a, A COLLATE nosuch)" "duplicate column name: A" let test_the_name_is_located_first () = let cat = store () in refused cat "CREATE TABLE t (a COLLATE nosuch, a)" "table t already exists" let test_a_constraint_names_a_declared_column () = let cat = store () in refused cat "CREATE TABLE u (a, UNIQUE (c))" "no such column: c"; refused cat "CREATE TABLE u (a, FOREIGN KEY (z) REFERENCES t)" "unknown column \"z\" in foreign key definition"; refused cat "CREATE TABLE u (a, b AS (1) PRIMARY KEY)" "generated columns cannot be part of the PRIMARY KEY" (* sqlite3 matches a foreign key's width as it reads the key (build.c, sqlite3CreateForeignKey): a table's FOREIGN KEY names as many parent columns as child ones, ahead of resolving its child columns, and a column's REFERENCES names one parent column, refused as the column is read, under the parent's name as written. *) let test_a_foreign_key_names_as_many_parent_columns () = let cat = store () in let mismatch = "number of columns in foreign key does not match the number of columns in \ the referenced table" in refused cat "CREATE TABLE u (a, b, FOREIGN KEY (a, b) REFERENCES t (a))" mismatch; refused cat "CREATE TABLE u (a, FOREIGN KEY (a, z) REFERENCES t (a))" mismatch; refused cat "CREATE TABLE u (a, FOREIGN KEY (z) REFERENCES t, FOREIGN KEY (a) \ REFERENCES t (a, b))" "unknown column \"z\" in foreign key definition"; refused cat "CREATE TABLE u (a REFERENCES t (a, b), b)" "foreign key on a should reference only one column of table t"; refused cat "CREATE TABLE u (a REFERENCES t (a, b), a)" "foreign key on a should reference only one column of table t"; refused cat "CREATE TABLE u (a REFERENCES \"t\" (a, b))" "foreign key on a should reference only one column of table \"t\"" (* lang_createtable.html section 3.2: a DEFAULT is a constant, a literal or a call over constants, and a column reference is not. *) let test_a_default_is_a_constant () = let constant = Sql.Private.Create_table.constant_default in Alcotest.(check bool) "a literal is constant" true (constant (Sql.Ast.Lit (Sql.Ast.Int 1L))); Alcotest.(check bool) "a call over constants is" true (constant (Sql.Ast.Func ("abs", [ Sql.Ast.Lit (Sql.Ast.Int (-5L)) ]))); Alcotest.(check bool) "a column reference is not" false (constant (Sql.Ast.Col (None, "a"))) let suite = ( "create_table", [ Alcotest.test_case "a column is refused as it is read" `Quick test_a_column_is_refused_as_it_is_read; Alcotest.test_case "the name is located first" `Quick test_the_name_is_located_first; Alcotest.test_case "a constraint names a declared column" `Quick test_a_constraint_names_a_declared_column; Alcotest.test_case "a foreign key names as many parent columns" `Quick test_a_foreign_key_names_as_many_parent_columns; Alcotest.test_case "a default is a constant" `Quick test_a_default_is_a_constant; ] )