Something went wrong. Try again.
SQLite file format and SQL engine in pure OCaml
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
(** Tests for {!Sqlite.Private.Model}: the schema shapes, built and read without a database.
The point of the module is that none of this needs a file: a schema value can be constructed and asked what it declares. These pin the parts a caller reads back -- the origin letter [PRAGMA index_list] reports, and the constraint violations the engine raises by name. *)
module M = Sqlite.Private.Model
(* PRAGMA index_list reports where an index came from with one letter, and a caller matches on it: "c" for a CREATE INDEX, "u" for a UNIQUE constraint, "pk" for the PRIMARY KEY. *)let test_origin_letters () = Alcotest.(check string) "explicit CREATE INDEX" "c" (M.origin_name M.Created); Alcotest.(check string) "UNIQUE constraint" "u" (M.origin_name M.Unique_constraint); Alcotest.(check string) "PRIMARY KEY" "pk" (M.origin_name M.Primary_key)
(* The key-value membership cache, without a pager. Unloaded is unknown membership, and row hooks on it leave it unknown: a store or a drop on a cache that has not been read must not make it claim a key is there or not. *)module C = M.Kv_cache
let lookup = Alcotest.testable (fun ppf -> function | C.Unloaded -> Fmt.string ppf "Unloaded" | C.Missing -> Fmt.string ppf "Missing" | C.Rowid r -> Fmt.pf ppf "Rowid %Ld" r) ( = )
let test_kv_cache_cold () = let cold = C.unloaded () in Alcotest.check lookup "unread" C.Unloaded (C.lookup cold "old"); C.stored cold ~key:"new" ~rowid:7L; C.dropped cold ~key:"old"; Alcotest.check lookup "a store leaves it unloaded" C.Unloaded (C.lookup cold "new"); Alcotest.check lookup "a drop leaves it unloaded" C.Unloaded (C.lookup cold "old")
(* A complete membership answers Missing for an absent key and the greatest rowid of a key stored twice; a drop of a key held once removes it, and a drop of one held by several rows leaves the cache unloaded, since which row is left needs a rescan. *)let test_kv_cache_loaded () = let c = C.empty () in Alcotest.check lookup "absent" C.Missing (C.lookup c "missing"); C.stored c ~key:"one" ~rowid:10L; C.stored c ~key:"two" ~rowid:20L; Alcotest.check lookup "one" (C.Rowid 10L) (C.lookup c "one"); Alcotest.check lookup "two" (C.Rowid 20L) (C.lookup c "two"); C.stored c ~key:"one" ~rowid:10L; C.dropped c ~key:"one"; Alcotest.check lookup "the same row stored again, then dropped" C.Missing (C.lookup c "one"); Alcotest.check lookup "another key kept" (C.Rowid 20L) (C.lookup c "two"); C.stored c ~key:"two" ~rowid:3L; Alcotest.check lookup "a lower duplicate keeps the greatest" (C.Rowid 20L) (C.lookup c "two"); C.stored c ~key:"two" ~rowid:30L; C.stored c ~key:"two" ~rowid:2L; Alcotest.check lookup "a greater duplicate wins" (C.Rowid 30L) (C.lookup c "two"); C.dropped c ~key:"two"; Alcotest.check lookup "a drop among duplicates unloads" C.Unloaded (C.lookup c "two")
(* Invalidation discards the old map, and what is stored after it is not believed until a scan replaces the cache. *)let test_kv_cache_invalidate () = let c = C.empty () in C.stored c ~key:"previous" ~rowid:8L; C.invalidate c; Alcotest.check lookup "a held key" C.Unloaded (C.lookup c "previous"); Alcotest.check lookup "an absent key" C.Unloaded (C.lookup c "absent"); C.stored c ~key:"replacement" ~rowid:9L; Alcotest.check lookup "a later store" C.Unloaded (C.lookup c "replacement")
let suite = ( "model", [ Alcotest.test_case "index origin letters" `Quick test_origin_letters; Alcotest.test_case "an unloaded key-value cache stays unloaded" `Quick test_kv_cache_cold; Alcotest.test_case "a loaded key-value cache" `Quick test_kv_cache_loaded; Alcotest.test_case "an invalidated key-value cache" `Quick test_kv_cache_invalidate; ] )