aboutsummaryrefslogtreecommitdiff
path: root/test/fragment_graph/main.ml
blob: d7e04607f938c5c4a5bad816c9c6fc66291f773b (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
(*
 * SPDX-FileCopyrightText: 2021 petites singularités <ps-dream@lesoiseaux.io>
 * SPDX-FileCopyrightText: 2021 pukkamustard <pukkamustard@posteo.net>
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later
 *)

module FragmentGraph = Rdf_fragment_graph.Make(struct
    let hash s =
      "urn:dummy_hash:" ^ (string_of_int @@ Hashtbl.hash s)
      |> Rdf.Iri.of_string
  end)

module Gen = struct

  let fragment_reference =
    QCheck.Gen.(string ~gen:(char_range 'a' 'z'))


  let predicate =
    QCheck.choose
      [
        QCheck.Gen.(
          fragment_reference
          >|= (fun id -> FragmentGraph.Predicate.FragmentReference id)
        )
        |> QCheck.make ;

        Rdf_gen.iri
        |> QCheck.map FragmentGraph.Predicate.of_iri
      ]

  let object' =
    QCheck.choose
      [
        QCheck.Gen.(
          fragment_reference
          >|= (fun id -> FragmentGraph.Object.FragmentReference id)
        )
        |> QCheck.make ;

        Rdf_gen.iri
        |> QCheck.map FragmentGraph.Object.of_iri;

        Rdf_gen.literal
        |> QCheck.map FragmentGraph.Object.of_literal
      ]

  let statement =
    QCheck.pair predicate object'

  let fragment_statement =
    QCheck.triple (QCheck.make fragment_reference) predicate object'

  let fragment_graph =
    QCheck.(
      pair
        (list statement)
        (list fragment_statement)
      |> map
        ~rev:(fun fragment_graph ->
            (FragmentGraph.statements fragment_graph,
             FragmentGraph.fragment_statements fragment_graph))
        (fun (statements, fragment_statements) ->
           List.fold_left (fun fg (p, o) -> FragmentGraph.add_statement p o fg)
             (List.fold_left (fun fg (f, p, o) -> FragmentGraph.add_fragment_statement f p o fg)
                FragmentGraph.empty
                fragment_statements)
             statements
        )
    )
end

let fragment_graph_testable =
  Alcotest.testable  FragmentGraph.pp FragmentGraph.equal


let ex = Rdf.Namespace.make_namespace "http://example.com/"

let equal_unit_test =
  let open Alcotest in
  test_case "unit tests"
    `Quick
    (fun () ->
       check bool
         "empty fragment graphs are equal"
         true
         FragmentGraph.(equal empty empty);

       check bool
         "empty fragment graph is not equal to a fragment graph with one statement"
         false
         FragmentGraph.(equal empty
                          FragmentGraph.(
                            empty
                            |> add_statement
                              (Predicate.of_iri @@ ex "foo")
                              (Object.of_iri @@ ex "bar"))
                       );

       check bool
         "fragment graph with one statement is equal"
         true
         FragmentGraph.(equal
                          FragmentGraph.(
                            empty
                            |> add_statement
                              (Predicate.of_iri @@ ex "foo")
                              (Object.of_iri @@ ex "bar"))

                          FragmentGraph.(
                            empty
                            |> add_statement
                              (Predicate.of_iri @@ ex "foo")
                              (Object.of_iri @@ ex "bar")));

       check bool
         "fragment graph with one statement is equal"
         true
         FragmentGraph.(equal
                          (empty
                           |> add_statement
                             (Predicate.of_iri @@ ex "foo")
                             (Object.of_literal @@
                              Rdf.Literal.make "hi" (ex "blups"))
                           |> add_statement
                             (Predicate.of_iri @@ ex "foo")
                             (Object.of_literal @@
                              Rdf.Literal.make "hi" (ex "blups")))

                          (empty
                           |> add_statement
                             (Predicate.of_iri @@ ex "foo")
                             (Object.of_literal @@
                              Rdf.Literal.make "hi" (ex "blups"))
                          ));

       check bool
         "fragment graph with different statements is not equal"
         false
         FragmentGraph.(equal
                          FragmentGraph.(
                            empty
                            |> add_statement
                              (Predicate.of_iri @@ ex "foo")
                              (Object.of_iri @@ ex "bar"))

                          FragmentGraph.(
                            empty
                            |> add_statement
                              (Predicate.of_iri @@ ex "foo")
                              (Object.of_iri @@ ex "bar2")));

       check bool
         "fragment graph with one fragment statements is equal to itself"
         true
         FragmentGraph.(equal
                          FragmentGraph.(
                            empty
                            |> add_fragment_statement
                              "hi"
                              (Predicate.of_iri @@ ex "foo")
                              (Object.of_iri @@ ex "bar"))

                          FragmentGraph.(
                            empty
                            |> add_fragment_statement
                              "hi"
                              (Predicate.of_iri @@ ex "foo")
                              (Object.of_iri @@ ex "bar")));

       check bool
         "fragment graphs with different fragment statements are not equal"
         false
         FragmentGraph.(equal
                          FragmentGraph.(
                            empty
                            |> add_fragment_statement
                              "hi"
                              (Predicate.of_iri @@ ex "foo")
                              (Object.of_iri @@ ex "bar"))

                          FragmentGraph.(
                            empty
                            |> add_fragment_statement
                              "hi"
                              (Predicate.of_iri @@ ex "foo")
                              (Object.of_iri @@ ex "bar2")))

    )

let canoncial_encode_decode_test =
  QCheck.Test.make
    ~name:"encode/decode canonical serialization"
    Gen.fragment_graph
    (fun fragment_graph ->
       let b = (fragment_graph
                |> FragmentGraph.canonical
                |> FragmentGraph.of_canonical
                |> Result.get_ok
               ) in

       if FragmentGraph.equal fragment_graph b then
         ()
       else
         Format.printf "NOT EQUAL: \n%a\n%a\n"

           FragmentGraph.pp fragment_graph
           FragmentGraph.pp b;

       Alcotest.check fragment_graph_testable
         "encoded/decoded value is same as original"
         fragment_graph b ;
       true)

let canonical_unit_test =
  let check test_label fragment_graph =
    Alcotest.check fragment_graph_testable
      test_label
      fragment_graph
      (fragment_graph
       |> FragmentGraph.canonical
       |> FragmentGraph.of_canonical
       |> Result.get_ok
      )
  in
  Alcotest.test_case "unit tests"
    `Quick
    (fun () ->
       check "empty" FragmentGraph.empty;

       check "single statement" @@
       FragmentGraph.(
         empty
         |> add_statement
           (Predicate.of_iri @@ ex "foo")
           (Object.of_iri @@ ex "bar")
       );

       check "single fragment statement" @@
       FragmentGraph.(
         empty
         |> add_fragment_statement
           "hi"
           (Predicate.of_iri @@ ex "foo")
           (Object.of_iri @@ ex "bar");
       );

       check "single fragment statement" @@
       FragmentGraph.(
         empty
         |> add_statement
           (Predicate.of_iri @@ ex "foo")
           (Object.of_iri @@ ex "bar")
         |> add_fragment_statement
           "hi"
           (Predicate.of_iri @@ ex "foo")
           (Object.of_literal @@ Rdf.(Literal.make "hi"
                                      @@ Namespace.xsd "something"))
       )


    )

let () =
  Alcotest.run "rdf_fragment_graph" [
    "equal", [
      equal_unit_test
    ];
    "canonical serializaton", [
      canonical_unit_test;
      QCheck_alcotest.to_alcotest canoncial_encode_decode_test
    ]
  ]