File: test_conjunctivegraph_operator_combinations.py

package info (click to toggle)
rdflib 7.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 77,852 kB
  • sloc: python: 59,555; sh: 153; makefile: 83; ruby: 74; xml: 45
file content (111 lines) | stat: -rw-r--r-- 3,058 bytes parent folder | download | duplicates (2)
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
import os

from rdflib import ConjunctiveGraph, Graph
from test.data import CHEESE, LIKES, MICHEL, PIZZA, TAREK, TEST_DATA_DIR

sportquadstrig = open(os.path.join(TEST_DATA_DIR, "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


def test_operators_returning_correct_type():
    g1 = ConjunctiveGraph()
    g2 = ConjunctiveGraph()
    assert type(g1 + g2) is ConjunctiveGraph
    assert type(g1 - g2) is ConjunctiveGraph
    assert type(g1 * g2) is ConjunctiveGraph
    assert type(g1 ^ g2) is ConjunctiveGraph