Minimal SQLite key-value store for OCaml
0
fork

Configure Feed

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

fix(lint): replace Printf.sprintf with Fmt.str in tests

Linter auto-fix: use Fmt.str instead of Printf.sprintf in sdnv,
space-packet, sqlite, and tc test files.

+26 -31
+26 -31
test/test_sqlite.ml
··· 9 9 let cwd = Eio.Stdenv.cwd env in 10 10 let tmp_dir = Eio.Path.(cwd / "_build" / "test_sqlite") in 11 11 (try Eio.Path.mkdirs ~exists_ok:true ~perm:0o755 tmp_dir with Eio.Io _ -> ()); 12 - let path = 13 - Eio.Path.(tmp_dir / Printf.sprintf "test_%d.db" (Random.int 1_000_000)) 14 - in 12 + let path = Eio.Path.(tmp_dir / Fmt.str "test_%d.db" (Random.int 1_000_000)) in 15 13 Eio.Switch.run @@ fun sw -> 16 14 let db = Sqlite.v ~sw path in 17 15 Fun.protect ~finally:(fun () -> Sqlite.close db) (fun () -> f fs db) ··· 166 164 Sqlite.put db key "value"; 167 165 let result = Sqlite.find db key in 168 166 Alcotest.(check (option string)) 169 - (Printf.sprintf "injection key %S safe" key) 167 + (Fmt.str "injection key %S safe" key) 170 168 (Some "value") result) 171 169 malicious_keys 172 170 ··· 180 178 Sqlite.put db "key" value; 181 179 let result = Sqlite.find db "key" in 182 180 Alcotest.(check (option string)) 183 - (Printf.sprintf "injection value safe") 181 + (Fmt.str "injection value safe") 184 182 (Some value) result) 185 183 malicious_values 186 184 ··· 201 199 (fun name -> 202 200 try 203 201 let _ = Sqlite.Table.create db ~name in 204 - Alcotest.fail (Printf.sprintf "should reject invalid name: %S" name) 202 + Alcotest.fail (Fmt.str "should reject invalid name: %S" name) 205 203 with Invalid_argument _ -> ()) 206 204 invalid_names 207 205 ··· 216 214 Sqlite.Table.put table "key" "value"; 217 215 let result = Sqlite.Table.find table "key" in 218 216 Alcotest.(check (option string)) 219 - (Printf.sprintf "valid table %S works" name) 217 + (Fmt.str "valid table %S works" name) 220 218 (Some "value") result) 221 219 valid_names 222 220 ··· 230 228 Sqlite.put db key "value"; 231 229 let result = Sqlite.find db key in 232 230 Alcotest.(check (option string)) 233 - (Printf.sprintf "unicode key %S" key) 231 + (Fmt.str "unicode key %S" key) 234 232 (Some "value") result) 235 233 unicode_keys 236 234 ··· 260 258 let tmp_dir = Eio.Path.(cwd / "_build" / "test_sqlite") in 261 259 (try Eio.Path.mkdirs ~exists_ok:true ~perm:0o755 tmp_dir with Eio.Io _ -> ()); 262 260 let path = 263 - Eio.Path.(tmp_dir / Printf.sprintf "persist_%d.db" (Random.int 1_000_000)) 261 + Eio.Path.(tmp_dir / Fmt.str "persist_%d.db" (Random.int 1_000_000)) 264 262 in 265 263 (* Create and write *) 266 264 Eio.Switch.run (fun sw -> ··· 283 281 let tmp_dir = Eio.Path.(cwd / "_build" / "test_sqlite") in 284 282 (try Eio.Path.mkdirs ~exists_ok:true ~perm:0o755 tmp_dir with Eio.Io _ -> ()); 285 283 let path = 286 - Eio.Path.( 287 - tmp_dir / Printf.sprintf "persist_del_%d.db" (Random.int 1_000_000)) 284 + Eio.Path.(tmp_dir / Fmt.str "persist_del_%d.db" (Random.int 1_000_000)) 288 285 in 289 286 (* Create, write, delete *) 290 287 Eio.Switch.run (fun sw -> ··· 308 305 let tmp_dir = Eio.Path.(cwd / "_build" / "test_sqlite") in 309 306 (try Eio.Path.mkdirs ~exists_ok:true ~perm:0o755 tmp_dir with Eio.Io _ -> ()); 310 307 let path = 311 - Eio.Path.( 312 - tmp_dir / Printf.sprintf "persist_tbl_%d.db" (Random.int 1_000_000)) 308 + Eio.Path.(tmp_dir / Fmt.str "persist_tbl_%d.db" (Random.int 1_000_000)) 313 309 in 314 310 (* Create with tables *) 315 311 Eio.Switch.run (fun sw -> ··· 373 369 List.iter 374 370 (fun len -> 375 371 let key = String.make len 'x' in 376 - let value = Printf.sprintf "value_%d" len in 372 + let value = Fmt.str "value_%d" len in 377 373 Sqlite.put db key value; 378 374 let result = Sqlite.find db key in 379 375 Alcotest.(check (option string)) 380 - (Printf.sprintf "key length %d" len) 376 + (Fmt.str "key length %d" len) 381 377 (Some value) result) 382 378 lengths 383 379 ··· 388 384 let n = 1000 in 389 385 (* Insert many keys *) 390 386 for i = 0 to n - 1 do 391 - Sqlite.put db (Printf.sprintf "key_%05d" i) (Printf.sprintf "value_%d" i) 387 + Sqlite.put db (Fmt.str "key_%05d" i) (Fmt.str "value_%d" i) 392 388 done; 393 389 (* Verify all present *) 394 390 for i = 0 to n - 1 do 395 - let result = Sqlite.find db (Printf.sprintf "key_%05d" i) in 391 + let result = Sqlite.find db (Fmt.str "key_%05d" i) in 396 392 Alcotest.(check (option string)) 397 - (Printf.sprintf "key %d present" i) 398 - (Some (Printf.sprintf "value_%d" i)) 393 + (Fmt.str "key %d present" i) 394 + (Some (Fmt.str "value_%d" i)) 399 395 result 400 396 done 401 397 ··· 404 400 let n = 100 in 405 401 (* Update same key many times *) 406 402 for i = 0 to n - 1 do 407 - Sqlite.put db "key" (Printf.sprintf "value_%d" i) 403 + Sqlite.put db "key" (Fmt.str "value_%d" i) 408 404 done; 409 405 let result = Sqlite.find db "key" in 410 406 Alcotest.(check (option string)) 411 407 "final value" 412 - (Some (Printf.sprintf "value_%d" (n - 1))) 408 + (Some (Fmt.str "value_%d" (n - 1))) 413 409 result 414 410 415 411 let test_interleaved_operations () = 416 412 with_temp_db @@ fun _fs db -> 417 413 (* Mix of puts, gets, deletes *) 418 414 for i = 0 to 99 do 419 - Sqlite.put db (Printf.sprintf "a_%d" i) "value"; 420 - Sqlite.put db (Printf.sprintf "b_%d" i) "value"; 421 - if i mod 2 = 0 then Sqlite.delete db (Printf.sprintf "a_%d" i) 415 + Sqlite.put db (Fmt.str "a_%d" i) "value"; 416 + Sqlite.put db (Fmt.str "b_%d" i) "value"; 417 + if i mod 2 = 0 then Sqlite.delete db (Fmt.str "a_%d" i) 422 418 done; 423 419 (* Verify state *) 424 420 let a_count = ref 0 in ··· 436 432 let n = 20 in 437 433 (* Create many tables *) 438 434 let tables = 439 - Array.init n (fun i -> 440 - Sqlite.Table.create db ~name:(Printf.sprintf "table%d" i)) 435 + Array.init n (fun i -> Sqlite.Table.create db ~name:(Fmt.str "table%d" i)) 441 436 in 442 437 (* Write to all tables *) 443 438 Array.iteri 444 - (fun i t -> Sqlite.Table.put t "key" (Printf.sprintf "value_%d" i)) 439 + (fun i t -> Sqlite.Table.put t "key" (Fmt.str "value_%d" i)) 445 440 tables; 446 441 (* Verify isolation *) 447 442 Array.iteri 448 443 (fun i t -> 449 444 let result = Sqlite.Table.find t "key" in 450 445 Alcotest.(check (option string)) 451 - (Printf.sprintf "table %d" i) 452 - (Some (Printf.sprintf "value_%d" i)) 446 + (Fmt.str "table %d" i) 447 + (Some (Fmt.str "value_%d" i)) 453 448 result) 454 449 tables 455 450 ··· 469 464 let sizes = [ 100; 200; 300; 400; 500 ] in 470 465 List.iter 471 466 (fun size -> 472 - let key = Printf.sprintf "key_%d" size in 467 + let key = Fmt.str "key_%d" size in 473 468 let value = String.make size 'v' in 474 469 Sqlite.put db key value; 475 470 let result = Sqlite.find db key in 476 471 Alcotest.(check (option string)) 477 - (Printf.sprintf "boundary size %d" size) 472 + (Fmt.str "boundary size %d" size) 478 473 (Some value) result) 479 474 sizes 480 475