Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test of ConjunctiveGraph operators #1647

Merged
merged 2 commits into from Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions test/consistent_test_data/sportquads.trig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix ex: <http://example.org/graph/> .
@prefix ont: <http://example.com/ontology/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

ex:students {
<http://example.com/resource/student_10> a ont:Student ;
foaf:name "Venus Williams" .

<http://example.com/resource/student_20> a ont:Student ;
foaf:name "Demi Moore" .
}

ex:sports {
<http://example.com/resource/sport_100> a ont:Sport ;
rdfs:label "Tennis" .
}

ex:practise {
<http://example.com/resource/student_10> ont:practises <http://example.com/resource/sport_100> .
}
123 changes: 123 additions & 0 deletions test/test_conjunctivegraph_operator_combinations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import os
from rdflib import (
Graph,
ConjunctiveGraph,
URIRef,
)


michel = URIRef("urn:example:michel")
tarek = URIRef("urn:example:tarek")
bob = URIRef("urn:example:bob")
likes = URIRef("urn:example:likes")
hates = URIRef("urn:example:hates")
pizza = URIRef("urn:example:pizza")
cheese = URIRef("urn:example:cheese")

c1 = URIRef("urn:example:context-1")
c2 = URIRef("urn:example:context-2")

sportquadstrig = open(
os.path.join(os.path.dirname(__file__), "consistent_test_data", "sportquads.trig")
).read()


def test_operators_with_conjunctivegraph_and_graph():

cg = ConjunctiveGraph()
cg.add((tarek, likes, pizza))
cg.add((tarek, likes, michel))

g = Graph()
g.add([tarek, likes, pizza])
g.add([tarek, likes, cheese])

assert len(cg + g) == 3 # adds cheese as liking

assert len(cg - g) == 1 # removes pizza

assert len(cg * g) == 1 # only pizza

assert len(cg ^ g) == 2 # removes pizza, adds cheese


def test_reversed_operators_with_conjunctivegraph_and_graph():

cg = ConjunctiveGraph()
cg.add((tarek, likes, pizza))
cg.add((tarek, likes, michel))

g = Graph()
g.add([tarek, likes, pizza])
g.add([tarek, likes, cheese])

assert len(g + cg) == 3 # adds cheese as liking

assert len(g - cg) == 1 # removes pizza

assert len(g * cg) == 1 # only pizza

assert len(g ^ cg) == 2 # removes pizza, adds cheese


def test_reversed_operators_with_conjunctivegraph_with_contexts_and_graph():

cg = ConjunctiveGraph()
cg.add((tarek, likes, pizza))
cg.add((tarek, likes, michel))
cg.parse(data=sportquadstrig, format="trig")

g = Graph()
g.add([tarek, likes, pizza])
g.add([tarek, likes, cheese])

assert len(g + cg) == 10 # adds cheese as liking plus sevenquads

assert len(list((g + cg).triples((None, None, None)))) == 10

assert len(g - cg) == 1 # removes pizza

assert len(g * cg) == 1 # only pizza

assert len(g ^ cg) == 9 # removes pizza, adds cheese and sevenquads


def test_operators_with_two_conjunctivegraphs():

cg1 = ConjunctiveGraph()
cg1.add([tarek, likes, pizza])
cg1.add([tarek, likes, michel])

cg2 = ConjunctiveGraph()
cg2.add([tarek, likes, pizza])
cg2.add([tarek, likes, cheese])

assert len(cg1 + cg2) == 3 # adds cheese as liking

assert len(cg1 - cg2) == 1 # removes pizza from cg1

assert len(cg1 * cg2) == 1 # only pizza

assert len(cg1 + cg2) == 3 # adds cheese as liking

assert len(cg1 ^ cg2) == 2 # removes pizza, adds cheese


def test_operators_with_two_conjunctivegraphs_one_with_contexts():

cg1 = ConjunctiveGraph()
cg1.add([tarek, likes, pizza])
cg1.add([tarek, likes, michel])

cg2 = ConjunctiveGraph()
cg2.add([tarek, likes, pizza])
cg2.add([tarek, likes, cheese])
cg2.parse(data=sportquadstrig, format="trig")

assert len(cg1 + cg2) == 10 # adds cheese as liking and all seven quads

assert len(cg1 - cg2) == 1 # removes pizza

assert len(cg1 * cg2) == 1 # only pizza

assert len(cg1 ^ cg2) == 9 # removes pizza