Minimal SQLite key-value store for OCaml
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add ocaml-kvn and ocaml-ocm: KVN parser and OCM message support

ocaml-kvn: CCSDS Key-Value Notation parser.
ocaml-ocm: CCSDS 505.0-B-2 Orbit Comprehensive Message parser.
Tested against TraCSS LANDSAT 5 OCM file.

+69
+69
fuzz/fuzz_sqlite.ml
··· 165 165 (Option.get (Sqlite.find db key)) 166 166 value2 167 167 168 + (* INSERT operations *) 169 + 170 + (** Insert roundtrip — insert then read_table returns same values. *) 171 + let test_insert_roundtrip text_val = 172 + let text_val = truncate text_val in 173 + let db = Sqlite.in_memory () in 174 + Sqlite.create_table db ~sql:"CREATE TABLE t (a TEXT, b INTEGER)"; 175 + let rowid = 176 + Sqlite.insert db ~table:"t" [ Sqlite.Vtext text_val; Sqlite.Vint 42L ] 177 + in 178 + let rows = Sqlite.read_table db "t" in 179 + check (List.length rows = 1); 180 + let rid, values = List.hd rows in 181 + check (rid = rowid); 182 + match values with 183 + | [ Sqlite.Vtext s; Sqlite.Vint 42L ] -> 184 + check_eq ~pp:Format.pp_print_string ~eq:( = ) s text_val 185 + | _ -> check false 186 + 187 + (** Multiple inserts produce distinct rowids. *) 188 + let test_insert_distinct_rowids n = 189 + let n = (abs n mod 50) + 1 in 190 + let db = Sqlite.in_memory () in 191 + Sqlite.create_table db ~sql:"CREATE TABLE t (x TEXT)"; 192 + let rowids = 193 + List.init n (fun i -> 194 + Sqlite.insert db ~table:"t" [ Sqlite.Vtext (string_of_int i) ]) 195 + in 196 + let unique = List.sort_uniq Int64.compare rowids in 197 + check (List.length unique = n) 198 + 199 + (** Insert with INTEGER PRIMARY KEY uses explicit rowid. *) 200 + let test_insert_explicit_pk n = 201 + let n = Int64.of_int ((abs n mod 100_000) + 1) in 202 + let db = Sqlite.in_memory () in 203 + Sqlite.create_table db ~sql:"CREATE TABLE t (id INTEGER PRIMARY KEY, v TEXT)"; 204 + let rowid = 205 + Sqlite.insert db ~table:"t" [ Sqlite.Vint n; Sqlite.Vtext "val" ] 206 + in 207 + check (rowid = n); 208 + let rows = Sqlite.read_table db "t" in 209 + let rid, _ = List.hd rows in 210 + check (rid = n) 211 + 212 + (** Insert must not crash on arbitrary text data. *) 213 + let test_insert_crash text_val = 214 + let text_val = truncate text_val in 215 + let db = Sqlite.in_memory () in 216 + Sqlite.create_table db ~sql:"CREATE TABLE t (a TEXT)"; 217 + try ignore (Sqlite.insert db ~table:"t" [ Sqlite.Vtext text_val ]) 218 + with _exn -> () 219 + 220 + (** Insert with blob data roundtrips correctly. *) 221 + let test_insert_blob_roundtrip blob = 222 + let blob = truncate blob in 223 + let db = Sqlite.in_memory () in 224 + Sqlite.create_table db ~sql:"CREATE TABLE t (data BLOB)"; 225 + let _ = Sqlite.insert db ~table:"t" [ Sqlite.Vblob blob ] in 226 + let rows = Sqlite.read_table db "t" in 227 + let _, values = List.hd rows in 228 + match values with 229 + | [ Sqlite.Vblob b ] -> check_eq ~pp:Format.pp_print_string ~eq:( = ) b blob 230 + | _ -> check false 231 + 168 232 (* Register all tests *) 169 233 170 234 let suite = ··· 185 249 test_case "both empty" [ const () ] test_both_empty; 186 250 test_case "multiple puts" [ bytes; list bytes ] test_multiple_puts; 187 251 test_case "put delete put" [ bytes; bytes; bytes ] test_put_delete_put; 252 + test_case "insert roundtrip" [ bytes ] test_insert_roundtrip; 253 + test_case "insert distinct rowids" [ int ] test_insert_distinct_rowids; 254 + test_case "insert explicit pk" [ int ] test_insert_explicit_pk; 255 + test_case "insert crash safety" [ bytes ] test_insert_crash; 256 + test_case "insert blob roundtrip" [ bytes ] test_insert_blob_roundtrip; 188 257 ] )