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
|
import os
from test.data import TEST_DATA_DIR, bob, cheese, hates, likes, michel, pizza, tarek
from rdflib import Dataset, URIRef
timblcardn3 = open(os.path.join(TEST_DATA_DIR, "timbl-card.n3")).read()
def add_stuff(graph):
graph.add((tarek, likes, pizza))
graph.add((tarek, likes, cheese))
graph.add((tarek, likes, bob))
graph.add((tarek, likes, michel))
graph.add((michel, likes, pizza))
graph.add((michel, likes, cheese))
graph.add((michel, likes, tarek))
graph.add((bob, likes, cheese))
graph.add((bob, hates, pizza))
graph.add((bob, hates, michel))
graph.add((bob, likes, tarek))
def test_unique_subjects():
graph = Dataset()
add_stuff(graph)
assert len([sub for sub in graph.subjects()]) == 11
assert len([sub for sub in graph.subjects(unique=True)]) == 3
def test_unique_predicates():
graph = Dataset()
add_stuff(graph)
assert len([pred for pred in graph.predicates()]) == 11
assert len([pred for pred in graph.predicates(unique=True)]) == 2
def test_unique_objects():
graph = Dataset()
add_stuff(graph)
assert len([obj for obj in graph.objects()]) == 11
assert len([obj for obj in graph.objects(unique=True)]) == 5
def test_unique_subject_predicates():
graph = Dataset()
add_stuff(graph)
assert len([sub for sub in graph.subject_predicates()]) == 11
assert len([sub for sub in graph.subject_predicates(unique=True)]) == 4
def test_unique_predicate_objects():
graph = Dataset()
add_stuff(graph)
assert len([pred for pred in graph.predicate_objects()]) == 11
assert len([pred for pred in graph.predicate_objects(unique=True)]) == 7
def test_unique_subject_objects():
graph = Dataset()
add_stuff(graph)
assert len([obj for obj in graph.subject_objects()]) == 11
assert len([obj for obj in graph.subject_objects(unique=True)]) == 11
no_of_statements_in_card = 86
no_of_unique_subjects = 20
no_of_unique_predicates = 58
no_of_unique_objects = 62
def test_parse_berners_lee_card_into_dataset_default():
# Workaround pending completion of identifier-as-context work
# current W-I-P allows parsing direct to Dataset default context
# and doesn't require the dubious creation of a graph with the
# same context identifier as the Dataset default context.
# graph = Dataset()
g = Dataset()
graph = g.graph(URIRef("urn:x-rdflib:default"))
graph.parse(data=timblcardn3, format="n3")
assert len(list(graph.subjects())) == no_of_statements_in_card
assert len(list(graph.subjects(unique=True))) == no_of_unique_subjects
assert len(list(graph.predicates(unique=True))) == no_of_unique_predicates
assert len(list(graph.objects(unique=True))) == no_of_unique_objects
def test_parse_berners_lee_card_into_dataset_context():
g = Dataset()
graph = g.graph()
graph.parse(data=timblcardn3, format="n3")
assert len(list(graph.subjects())) == no_of_statements_in_card
assert len(list(graph.subjects(unique=True))) == no_of_unique_subjects
assert len(list(graph.predicates(unique=True))) == no_of_unique_predicates
assert len(list(graph.objects(unique=True))) == no_of_unique_objects
|