aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorpukkamustard <pukkamustard@posteo.net>2021-06-11 13:12:32 +0200
committerpukkamustard <pukkamustard@posteo.net>2021-06-11 13:12:32 +0200
commit39d9d6be28d30d5fae609b7d2dc4aac40c9395e4 (patch)
treeeb1f9cee3c3bfa578a4cfe54e9fcbcf2d78dd04e /test
parentb0b0135133ba5c3a799bf26d49f0f06c16e5e8ad (diff)
Rdf_fragment_graph: Fix bugs and add tests.
Equality was buggy as the same statement could be added multiple times.
Diffstat (limited to 'test')
-rw-r--r--test/core/dune3
-rw-r--r--test/fragment_graph/dune8
-rw-r--r--test/fragment_graph/main.ml274
3 files changed, 284 insertions, 1 deletions
diff --git a/test/core/dune b/test/core/dune
index 4d8d3b4..098a94b 100644
--- a/test/core/dune
+++ b/test/core/dune
@@ -1,6 +1,7 @@
(executables
(names main)
- (libraries rdf))
+ (libraries rdf rdf_gen rdf_alcotest
+ alcotest qcheck qcheck-alcotest))
(rule
(alias runtest)
diff --git a/test/fragment_graph/dune b/test/fragment_graph/dune
new file mode 100644
index 0000000..48a77d8
--- /dev/null
+++ b/test/fragment_graph/dune
@@ -0,0 +1,8 @@
+(executables
+ (names main)
+ (libraries rdf_fragment_graph rdf_gen rdf_alcotest
+ alcotest qcheck qcheck-alcotest))
+
+(rule
+ (alias runtest)
+ (action (run ./main.exe)))
diff --git a/test/fragment_graph/main.ml b/test/fragment_graph/main.ml
new file mode 100644
index 0000000..d7e0460
--- /dev/null
+++ b/test/fragment_graph/main.ml
@@ -0,0 +1,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
+ ]
+ ]