aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit/kernel/SubstantialRelation.ml
blob: 9d6068527f699ae8b7c25b88c23dd82c43d407ce (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
44
45
46
47
48
49
50
51
52
53
54
55
open Rnt

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

  module H = Helpers.Storage.Make (S) (C)
  module SR = Rnt.Kernel.SubstantialRelation.Make (S)

  let hash s =
    Concepts.Blob.blob_of_bytes (Bytes.of_string s) |> Concepts.Hash.hash_of_blob

  let alaric =
    { Concepts.Tuple.type_ = "employee";
      attributes =
        BatMap.String.empty
        |> BatMap.String.add "name" (Concepts.Value.String "Alaric")
        |> BatMap.String.add "age" (Concepts.Value.Integer 42) }

  let assert_and_enumerate conn =
    let tuple = Concepts.Tuple.Representation.to_blob alaric in
    let before, after, enumerated =
      begin
        let open Utilities.Result in
        let* tx = S.start conn in
        let* relation = SR.empty tx ~schematics:(hash "schema") () in
        let* before = SR.contains_tuple tx relation tuple in
        let* relation = SR.assert_tuple tx relation tuple in
        let* after = SR.contains_tuple tx relation tuple in
        let* () = S.commit tx in
        let* cursor = SR.enumerate conn relation in
        let* scan = Protocols.Cursor.require cursor in
        let* enumerated = Protocols.Cursor.drain scan () in
        Protocols.Handle.release cursor;
        Ok (before, after, BatFingerTree.to_list enumerated)
      end
      |> Helpers.condition_as_failure
    in
    check bool "absent before assertion" false before;
    check bool "present after assertion" true after;
    check int "one tuple enumerated" 1 (List.length enumerated);
    check bool "the tuple that was asserted" true
      (List.for_all
         (fun tuple ->
           Concepts.Hash.hash_equals (Concepts.Tuple.hash tuple) (Concepts.Tuple.hash alaric))
         enumerated)

  let suite prefix =
    ( "substantial-relation/" ^ prefix,
      [ test_case "assert-and-enumerate" `Quick
          (H.with_connection assert_and_enumerate "substantial-relation-test") ] )
end

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

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