An embedded, single-file key-value store for OCaml, inspired by BoltDB and LMDB.
at main 1.8 kB view raw
1(** Tests for I/O module *) 2 3let test_create_database () = 4 let path = Filename.temp_file "lithos_test" ".db" in 5 Unix.unlink path; 6 let cleanup () = 7 try Unix.unlink path with 8 | _ -> () 9 in 10 11 let open Lithos.Io in 12 match create_database path 4096 with 13 | Ok () -> 14 Alcotest.(check bool) "File exists" true (Sys.file_exists path); 15 cleanup () 16 | Error err -> 17 cleanup (); 18 Alcotest.fail (Lithos.Error.to_string err) 19;; 20 21let test_read_header () = 22 let path = Filename.temp_file "lithos_test" ".db" in 23 Unix.unlink path; 24 let cleanup () = 25 try Unix.unlink path with 26 | _ -> () 27 in 28 29 let open Lithos.Io in 30 match create_database path 4096 with 31 | Error err -> 32 cleanup (); 33 Alcotest.fail (Lithos.Error.to_string err) 34 | Ok () -> 35 (match read_header path with 36 | Ok meta -> 37 Alcotest.(check int) "Page size" 4096 meta.Lithos.Types.page_size; 38 Alcotest.(check int) "Version" 1 meta.Lithos.Types.version; 39 cleanup () 40 | Error err -> 41 cleanup (); 42 Alcotest.fail (Lithos.Error.to_string err)) 43;; 44 45let test_invalid_database () = 46 let path = Filename.temp_file "lithos_test" ".txt" in 47 let cleanup () = 48 try Unix.unlink path with 49 | _ -> () 50 in 51 52 let oc = open_out path in 53 output_string oc "not a database file"; 54 close_out oc; 55 56 let open Lithos.Io in 57 match read_header path with 58 | Ok _ -> 59 cleanup (); 60 Alcotest.fail "Should have failed on invalid database" 61 | Error (Invalid_database _) -> cleanup () 62 | Error err -> 63 cleanup (); 64 Alcotest.fail ("Wrong error: " ^ Lithos.Error.to_string err) 65;; 66 67let suite = 68 [ "create_database", `Quick, test_create_database 69 ; "read_header", `Quick, test_read_header 70 ; "invalid_database", `Quick, test_invalid_database 71 ] 72;;