(** Tests for I/O module *) let test_create_database () = let path = Filename.temp_file "lithos_test" ".db" in Unix.unlink path; let cleanup () = try Unix.unlink path with | _ -> () in let open Lithos.Io in match create_database path 4096 with | Ok () -> Alcotest.(check bool) "File exists" true (Sys.file_exists path); cleanup () | Error err -> cleanup (); Alcotest.fail (Lithos.Error.to_string err) ;; let test_read_header () = let path = Filename.temp_file "lithos_test" ".db" in Unix.unlink path; let cleanup () = try Unix.unlink path with | _ -> () in let open Lithos.Io in match create_database path 4096 with | Error err -> cleanup (); Alcotest.fail (Lithos.Error.to_string err) | Ok () -> (match read_header path with | Ok meta -> Alcotest.(check int) "Page size" 4096 meta.Lithos.Types.page_size; Alcotest.(check int) "Version" 1 meta.Lithos.Types.version; cleanup () | Error err -> cleanup (); Alcotest.fail (Lithos.Error.to_string err)) ;; let test_invalid_database () = let path = Filename.temp_file "lithos_test" ".txt" in let cleanup () = try Unix.unlink path with | _ -> () in let oc = open_out path in output_string oc "not a database file"; close_out oc; let open Lithos.Io in match read_header path with | Ok _ -> cleanup (); Alcotest.fail "Should have failed on invalid database" | Error (Invalid_database _) -> cleanup () | Error err -> cleanup (); Alcotest.fail ("Wrong error: " ^ Lithos.Error.to_string err) ;; let suite = [ "create_database", `Quick, test_create_database ; "read_header", `Quick, test_read_header ; "invalid_database", `Quick, test_invalid_database ] ;;