diff options
author | arie <arie@alleycat.cc> | 2021-06-29 10:18:32 +0200 |
---|---|---|
committer | arie <arie@alleycat.cc> | 2021-06-29 10:18:32 +0200 |
commit | e6bbf3a31f2d7c9a6975af6631d50771c7b01fa8 (patch) | |
tree | 4a0720d8def0b7dd51503af1e8e2ab3d75777b92 /test/turtle/main.ml | |
parent | 49a0b6e0034fc411963bf3cc2bc1c6f046dcc389 (diff) |
Add turtle tests. Almost done, some small strange behavior with literal.
Diffstat (limited to 'test/turtle/main.ml')
-rw-r--r-- | test/turtle/main.ml | 307 |
1 files changed, 265 insertions, 42 deletions
diff --git a/test/turtle/main.ml b/test/turtle/main.ml index 11e5036..5f2d97f 100644 --- a/test/turtle/main.ml +++ b/test/turtle/main.ml @@ -4,44 +4,6 @@ module Turtle = Rdf_turtle.Ast open Turtle open Alcotest -(* TODO The following four functions create a 'testable' for the graph type. - * Usually, this testable is located in another file: rdf_alcotest, - * but since I'm not sure at all wheteher the 'equal' function for graphs is - * a reasonable one, and the pp function is also rather ad hoc, I put it here. - * It would be nice to have a good implementation for the equal function. - * Right now, it first transforms the graph into a list of tiples, and then - * checks whether the lists are equal up to ordering. This list_equal_up_to_order - * function is also used in the test file for rdf_json. *) - -(* Check whether two lists have the same elements *) -let list_equal_up_to_order equal l1 l2 = - (l1 |> List.fold_left - (fun acc y -> - if not acc then false - else if not (List.exists (fun x -> equal y x) l2) then false - else true) - true) - && - (l2 |> List.fold_left - (fun acc y -> - if not acc then false - else if not (List.exists (fun x -> equal y x) l1) then false - else true) - true) - -let graph_equal g1 g2 = - let graph_to_list g = - g - |> Rdf.Graph.to_triples - |> List.of_seq - in - list_equal_up_to_order Rdf.Triple.equal (graph_to_list g1) (graph_to_list g2) - -let graph_pp ppf g = - Fmt.pf ppf "%s" (Rdf_turtle.encode g) - -let graph_testable = Alcotest.testable graph_pp graph_equal - let parse p = Angstrom.parse_string ~consume:Angstrom.Consume.All @@ -487,23 +449,284 @@ let turtle_test_case = * immediately clear where the problems lie, since the decode function is a composition of other * (already quite complicated) funtions. *) let turtle_to_graph_test_case = - let triple_of_iris_of_strs sub pred obj = +(* A triple of three Iri's *) + let triple_of_iri_iri_iri sub pred obj = (Rdf.Triple.make (Rdf.Triple.Subject.of_iri @@ Rdf.Iri.of_string sub) (Rdf.Triple.Predicate.of_iri @@ Rdf.Iri.of_string pred) (Rdf.Triple.Object.of_iri @@ Rdf.Iri.of_string obj)) in +(* A triple of an iri, an iri, and a blank_node *) + let triple_of_iri_iri_bnode sub pred obj = + (Rdf.Triple.make + (Rdf.Triple.Subject.of_iri @@ Rdf.Iri.of_string sub) + (Rdf.Triple.Predicate.of_iri @@ Rdf.Iri.of_string pred) + (Rdf.Triple.Object.of_blank_node @@ Rdf.Blank_node.of_string obj)) + in +(* A triple of a blank_node, an iri, and an iri *) + let triple_of_bnode_iri_iri sub pred obj = + (Rdf.Triple.make + (Rdf.Triple.Subject.of_blank_node @@ Rdf.Blank_node.of_string sub) + (Rdf.Triple.Predicate.of_iri @@ Rdf.Iri.of_string pred) + (Rdf.Triple.Object.of_iri @@ Rdf.Iri.of_string obj)) + in +(* A triple of a blank_node, an iri, and a blank_node *) + let triple_of_bnode_iri_bnode sub pred obj = + (Rdf.Triple.make + (Rdf.Triple.Subject.of_blank_node @@ Rdf.Blank_node.of_string sub) + (Rdf.Triple.Predicate.of_iri @@ Rdf.Iri.of_string pred) + (Rdf.Triple.Object.of_blank_node @@ Rdf.Blank_node.of_string obj)) + in +(* A triple of a blank_node, an iri, and a literal + * The language and value are optional since a lot of literals are given by + * their value, and nothing else. *) + let obj_literal ?obj_language ?obj_datatype obj_value = + match obj_language, obj_datatype with + | (Some lang), (Some datatype) -> + let obj_datatype_iri = Rdf.Iri.of_string datatype in + Rdf.Literal.make + obj_value + ~language:lang + obj_datatype_iri + | (None), Some datatype -> + let obj_datatype_iri = Rdf.Iri.of_string datatype in + Rdf.Literal.make obj_value obj_datatype_iri + | Some lang, None -> + let obj_datatype_iri = Rdf.Iri.of_string "http://www.w3.org/2001/XMLSchema#string" in + Rdf.Literal.make + obj_value + ~language:lang + obj_datatype_iri + | None, None -> + Fmt.pr "DO YOU HEAR ME?"; + let obj_datatype_iri = Rdf.Iri.of_string "http://www.w3.org/2001/XMLSchema#string" in + Rdf.Literal.make obj_value obj_datatype_iri + in +(* A triple of an iri, an iri, and a literal *) + let triple_of_iri_iri_literal sub pred ?obj_language ?obj_datatype obj_value = + let obj_literal = obj_literal ?obj_language ?obj_datatype obj_value in + (Rdf.Triple.make + (Rdf.Triple.Subject.of_iri @@ Rdf.Iri.of_string sub) + (Rdf.Triple.Predicate.of_iri @@ Rdf.Iri.of_string pred) + (Rdf.Triple.Object.of_literal @@ obj_literal)) + in + let triple_of_bnode_iri_literal sub pred ?obj_language ?obj_datatype obj_value = + let obj_literal = obj_literal ?obj_language ?obj_datatype obj_value in + (Rdf.Triple.make + (Rdf.Triple.Subject.of_blank_node @@ Rdf.Blank_node.of_string sub) + (Rdf.Triple.Predicate.of_iri @@ Rdf.Iri.of_string pred) + (Rdf.Triple.Object.of_literal @@ obj_literal)) + in + let nil = "http://www.w3.org/1999/02/22-rdf-syntax-ns#nil" in + let first = "http://www.w3.org/1999/02/22-rdf-syntax-ns#first" in + let rest = "http://www.w3.org/1999/02/22-rdf-syntax-ns#rest" in + let a_pred = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" in let cases = [ "<sub> <pred> <obj> .", - Rdf.Graph.add Rdf.Graph.empty - (triple_of_iris_of_strs "sub" "pred" "obj") + Rdf.Graph.empty + |> Rdf.Graph.add (triple_of_iri_iri_iri "sub" "pred" "obj") + ; + (* SUBJECT = COLLECTION *) + "() <http://a.example/p> <http://a.example/o> .", + Rdf.Graph.empty + |> Rdf.Graph.add (triple_of_iri_iri_iri nil "http://a.example/p" "http://a.example/o") + ; + "(\"hello\") <http://a.example/p> <http://a.example/o> .", + Rdf.Graph.empty + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid1" "http://a.example/p" "http://a.example/o") + |> Rdf.Graph.add (triple_of_bnode_iri_literal "genid1" first "hello") + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid1" rest nil) + ; + "('1' '2') <http://a.example/p> <http://a.example/o> .", + Rdf.Graph.empty + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid1" "http://a.example/p" "http://a.example/o") + |> Rdf.Graph.add (triple_of_bnode_iri_literal "genid1" first "1") + |> Rdf.Graph.add (triple_of_bnode_iri_bnode "genid1" rest "genid2") + |> Rdf.Graph.add (triple_of_bnode_iri_literal "genid2" first "2") + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid2" rest nil) + ; + "('1' '2' '3') <http://a.example/p> <http://a.example/o> .", + Rdf.Graph.empty + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid1" "http://a.example/p" "http://a.example/o") + |> Rdf.Graph.add (triple_of_bnode_iri_literal "genid1" first "1") + |> Rdf.Graph.add (triple_of_bnode_iri_bnode "genid1" rest "genid2") + |> Rdf.Graph.add (triple_of_bnode_iri_literal "genid2" first "2") + |> Rdf.Graph.add (triple_of_bnode_iri_bnode "genid2" rest "genid3") + |> Rdf.Graph.add (triple_of_bnode_iri_literal "genid3" first "3") + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid3" rest nil) + ; + (* OBJECT = COLLECTION *) + "<http://a.example/s> <http://a.example/p> () .", + Rdf.Graph.empty + |> Rdf.Graph.add (triple_of_iri_iri_iri "http://a.example/s" "http://a.example/p" nil) + ; + "<http://a.example/s> <http://a.example/p> ('1') .", + Rdf.Graph.empty + |> Rdf.Graph.add (triple_of_iri_iri_bnode "http://a.example/s" "http://a.example/p" "genid1") + |> Rdf.Graph.add (triple_of_bnode_iri_literal "genid1" first "1") + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid1" rest nil) + ; + "<http://a.example/s> <http://a.example/p> ('1' '2') .", + Rdf.Graph.empty + |> Rdf.Graph.add (triple_of_iri_iri_bnode "http://a.example/s" "http://a.example/p" "genid1") + |> Rdf.Graph.add (triple_of_bnode_iri_literal "genid1" first "1") + |> Rdf.Graph.add (triple_of_bnode_iri_bnode "genid1" rest "genid2") + |> Rdf.Graph.add (triple_of_bnode_iri_literal "genid2" first "2") + |> Rdf.Graph.add (triple_of_bnode_iri_iri"genid2" rest nil) + ; + "<http://a.example/s> <http://a.example/p> ('1' '2' '3') .", + Rdf.Graph.empty + |> Rdf.Graph.add (triple_of_iri_iri_bnode "http://a.example/s" "http://a.example/p" "genid1") + |> Rdf.Graph.add (triple_of_bnode_iri_literal "genid1" first "1") + |> Rdf.Graph.add (triple_of_bnode_iri_bnode "genid1" rest "genid2") + |> Rdf.Graph.add (triple_of_bnode_iri_literal "genid2" first "2") + |> Rdf.Graph.add (triple_of_bnode_iri_bnode"genid2" rest "genid3") + |> Rdf.Graph.add (triple_of_bnode_iri_literal "genid3" first "3") + |> Rdf.Graph.add (triple_of_bnode_iri_iri"genid3" rest nil) + ; + (* SUBJECT = BNODPS, this works *) + "[] <http://a.example/p> <http://a.example/o> .", + Rdf.Graph.empty + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid1" "http://a.example/p" "http://a.example/o") + ; + " + @prefix as: <https://www.w3.org/ns/activitystreams#> . + [ a as:Person ; ] + <http://a.example/p> <http://a.example/o> .", + Rdf.Graph.empty + |> Rdf.Graph.add (triple_of_bnode_iri_iri + "genid1" + a_pred + "https://www.w3.org/ns/activitystreams#Person") + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid1" "http://a.example/p" "http://a.example/o") + ; + " + @prefix as: <https://www.w3.org/ns/activitystreams#> . + @prefix ldp: <http://www.w3.org/ns/ldp#> . + [ a as:Person ; + ldp:inbox <http://localhost:4000/users/alice/inbox> ; + ] + <http://a.example/p> <http://a.example/o> .", + Rdf.Graph.empty + |> Rdf.Graph.add (triple_of_bnode_iri_iri + "genid1" + a_pred + "https://www.w3.org/ns/activitystreams#Person") + |> Rdf.Graph.add (triple_of_bnode_iri_iri + "genid1" + "http://www.w3.org/ns/ldp#inbox" + "http://localhost:4000/users/alice/inbox") + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid1" "http://a.example/p" "http://a.example/o") + ; + (* TRIPLE / OBJECT = BNODPS *) + "[<pred> <obj>] .", + Rdf.Graph.empty + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid1" "pred" "obj") + ; + "<sub> <pred> [] .", + Rdf.Graph.empty + |> Rdf.Graph.add (triple_of_iri_iri_bnode "sub" "pred" "genid1") + ; + "<sub> <pred> [<pred2> <obj2>] .", + Rdf.Graph.empty + |> Rdf.Graph.add (triple_of_iri_iri_bnode "sub" "pred" "genid1") + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid1" "pred2" "obj2") + ; + "<sub> <pred> [<pred2> <obj2>, <obj3> ; <pred3> <obj4>, <obj5>] .", + Rdf.Graph.empty + |> Rdf.Graph.add (triple_of_iri_iri_bnode "sub" "pred" "genid1") + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid1" "pred2" "obj2") + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid1" "pred2" "obj3") + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid1" "pred3" "obj4") + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid1" "pred3" "obj5") + ; + (* NESTED BNODPS (in subject) *) + "[ <http://a.example/p1> [ <http://a.example/p2> <http://a.example/o2> ] ; <http://a.example/p> <http://a.example/o> ].", + Rdf.Graph.empty + |> Rdf.Graph.add (triple_of_bnode_iri_bnode "genid1" "http://a.example/p1" "genid2") + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid2" "http://a.example/p2" "http://a.example/o2") + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid1" "http://a.example/p" "http://a.example/o") + ; + (* BNODPS (as triple) CONTAINING COLLECTION *) + "[ <http://a.example/p1> ('1') ] .", + Rdf.Graph.empty + |> Rdf.Graph.add (triple_of_bnode_iri_bnode "genid1" "http://a.example/p1" "genid2") + |> Rdf.Graph.add (triple_of_bnode_iri_literal "genid2" first "1") + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid2" rest nil) + ; + (* NESTED COLLECTIONS *) + "<http://a.example/s> <http://a.example/p> (('1')) .", + Rdf.Graph.empty + |> Rdf.Graph.add (triple_of_iri_iri_bnode "http://a.example/s" "http://a.example/p" "genid1") + |> Rdf.Graph.add (triple_of_bnode_iri_bnode "genid1" first "genid2") + |> Rdf.Graph.add (triple_of_bnode_iri_literal "genid2" first "1") + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid2" rest nil) + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid1" rest nil) + ; + "<http://a.example/s> <http://a.example/p> ('1' ('2')) .", + Rdf.Graph.empty + |> Rdf.Graph.add (triple_of_iri_iri_bnode "http://a.example/s" "http://a.example/p" "genid1") + |> Rdf.Graph.add (triple_of_bnode_iri_literal "genid1" first "1") + |> Rdf.Graph.add (triple_of_bnode_iri_bnode "genid1" rest "genid2") + |> Rdf.Graph.add (triple_of_bnode_iri_bnode "genid2" first "genid3") + |> Rdf.Graph.add (triple_of_bnode_iri_literal "genid3" first "2") + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid3" rest nil) + |> Rdf.Graph.add (triple_of_bnode_iri_iri "genid2" rest nil) + ; + (* A MORE REALISTIC EXAMPLE *) + "@prefix as: <https://www.w3.org/ns/activitystreams#> . + @prefix foaf: <http://xmlns.com/foaf/0.1/> . + @prefix ldp: <http://www.w3.org/ns/ldp#> . + @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . + @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . + @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . + + <http://localhost:4000/users/alice> + a as:Person ; + ldp:inbox <http://localhost:4000/users/alice/inbox> ; + as:outbox <http://localhost:4000/users/alice/outbox> ; + as:preferredUsername \"alice\" . + " , + Rdf.Graph.empty + |> Rdf.Graph.add (triple_of_iri_iri_iri + "http://localhost:4000/users/alice" + a_pred + "https://www.w3.org/ns/activitystreams#Person") + |> Rdf.Graph.add (triple_of_iri_iri_iri + "http://localhost:4000/users/alice" + "http://www.w3.org/ns/ldp#inbox" + "http://localhost:4000/users/alice/inbox") + |> Rdf.Graph.add (triple_of_iri_iri_iri + "http://localhost:4000/users/alice" + "https://www.w3.org/ns/activitystreams#outbox" + "http://localhost:4000/users/alice/outbox") + |> Rdf.Graph.add (triple_of_iri_iri_literal + "http://localhost:4000/users/alice" + "https://www.w3.org/ns/activitystreams#preferredUsername" + "alice") ; + (* TODO *) + (* " @base <http://example.org/> . *) + (* @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . *) + (* @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . *) + (* @prefix foaf: <http://xmlns.com/foaf/0.1/> . *) + (* *) + (* <#green-goblin> *) + (* rel:enemyOf <#spiderman> ; #LET us put a comment here *) + (* a foaf:Person ; *) + (* foaf:name \"Green Goblin\" . *) + (* *) + (* @prefix rel: <http://www.perceive.net/schemas/relationship/> . *) + (* <#spiderman> *) + (* rel:enemyOf <#green-goblin> ; *) + (* a foaf:Person ; *) + (* foaf:name \"Spiderman\", \"Человек-паук\"@ru . ", *) ] in test_case "turtle parser and transform" `Quick (fun () -> List.iter (fun (enc, v) -> - check (graph_testable) + check (Rdf_alcotest.graph) "can parse and transform" (Rdf_turtle.decode enc) (v)) |