aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit/backend/Storage.ml
blob: d93467e885d367d060f0049791cf3d047b5b01d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
open Rnt

module Make (S : Abstract.Storage.STORAGE) (C : Helpers.Storage.CONFIGURATOR) = struct
  open Alcotest
  module H = Helpers.Storage.Make (S) (C)

  let storage_and_retrieval conn =
    let key =
      Concepts.Value.String "value" |> Concepts.Encoding.blob_of_value |> Concepts.Hash.hash_of_blob
    in
    let value = Concepts.Value.Integer 42 in
    let blob = Concepts.Encoding.blob_of_value value in
    let v, v' =
      begin
        let open Utilities.Result in
        let* tx = S.start conn in
        let* v = S.get tx (S.Hash key) in
        let* () = S.put tx (S.Hash key) blob in
        let* v' = S.get tx (S.Hash key) in
        Ok Concepts.Encoding.(Option.map value_of_blob v, Option.map value_of_blob v')
      end
      |> Helpers.condition_as_failure
    in
    check (option Helpers.value) "`get` before `put` should return no value" None v;
    check (option Helpers.value) "`get` after `put` should return the inserted value" (Some value)
      v'

  let creates_missing_directories () =
    Helpers.with_temporary_directory "storage-test" (fun base ->
        let path = Filename.concat (Filename.concat base "nested") "db" in
        S.connect (C.configure path) |> Helpers.condition_as_failure |> S.disconnect;
        check bool "the storage directory exists" true (Sys.is_directory path) )

  let suite prefix =
    ( "storage/" ^ prefix,
      [ test_case "storage-and-retrieval" `Quick
          (H.with_connection storage_and_retrieval "storage-test");
        test_case "creates-missing-directories" `Quick creates_missing_directories ] )
end

module LMDB = Make (Rnt.Backend.Storage.LMDB) (Helpers.Storage.LMDB_Configurator)

let suites () = [LMDB.suite "lmdb"]