-
Notifications
You must be signed in to change notification settings - Fork 72
/
GraphTest.scala
115 lines (86 loc) · 3.46 KB
/
GraphTest.scala
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
112
113
114
115
package org.w3.banana
import org.scalatest.wordspec.AnyWordSpec
import org.scalatest.matchers.should.Matchers
class GraphTest[Rdf <: RDF](implicit ops: RDFOps[Rdf]) extends AnyWordSpec with Matchers {
import ops._
def exuri(foo: String = "foo"): Rdf#URI = URI("http://example.com/" + foo)
val foo1gr: Rdf#Graph = Graph(Triple(exuri(), rdf("foo"), Literal("foo")))
val bar1gr: Rdf#Graph = Graph(Triple(exuri(), rdf("bar"), Literal("bar")))
def exbnode(n: Int = 1): Rdf#Node = BNode("ex" + n)
def bnNameGr(n: Int = 1, name: String) = Graph(Triple(exbnode(n), rdf("knows"), Literal(name)))
val foo = (
exuri()
-- rdf("foo") ->- "foo"
-- rdf("bar") ->- "bar"
).graph
val fooReference = (
exuri()
-- rdf("foo") ->- "foo"
-- rdf("bar") ->- "bar"
).graph
val bar = (
exuri()
-- rdf("bar") ->- "bar"
-- rdf("baz") ->- "baz"
).graph
val barReference = (
exuri()
-- rdf("bar") ->- "bar"
-- rdf("baz") ->- "baz"
).graph
val foobar = (
exuri()
-- rdf("foo") ->- "foo"
-- rdf("bar") ->- "bar"
-- rdf("baz") ->- "baz"
).graph
"union test" should {
"union must compute the union of two graphs, and should not touch the graphs" in {
val result = union(foo :: bar :: Nil)
isomorphism(foo, fooReference) shouldEqual true
isomorphism(bar, barReference) shouldEqual true
(! isomorphism(foo, bar)) shouldEqual true
graphSize(result) shouldEqual graphSize(foobar)
isomorphism(foobar, result) shouldEqual true
}
"union of Nil must return an empty graph" in {
val result: Rdf#Graph = union(Nil)
isomorphism(result, emptyGraph) shouldEqual true
graphSize(result) shouldEqual 0
}
"union of a single graph must return an isomorphic graph" in {
val result = union(foo :: Nil)
isomorphism(result, foo) shouldEqual true
}
}
"diff tests" should {
"removing one triple in a 1 triple graph must return the empty graph" in {
isomorphism(diff(foo1gr, foo1gr), emptyGraph) shouldEqual true
}
"removing one triple in a 2 triple graph must leave the other triple in the graph" in {
val d = diff(foo1gr union bar1gr, foo1gr)
graphSize(d) shouldEqual graphSize(bar1gr)
isomorphism(d, bar1gr) shouldEqual true
val oneBNGraph = bnNameGr(1, "Henry") union bnNameGr(1, "Alexandre")
val d2 = diff(oneBNGraph, bnNameGr(1, "Alexandre"))
graphSize(d2) shouldEqual 1
isomorphism(d2, bnNameGr(1, "Henry")) shouldEqual true
val twoBNGraph = bnNameGr(1, "Henry") union bnNameGr(2, "Alexandre")
val d3 = diff(twoBNGraph, bnNameGr(2, "Alexandre"))
isomorphism(d3, bnNameGr(1, "Henry")) shouldEqual true
}
"removing a triple that is not present in a 2 triple graph must return the same graph" in {
val d = diff(foo1gr union bar1gr, bnNameGr(1, "George"))
graphSize(d) shouldEqual graphSize(foo1gr union bar1gr)
isomorphism(d, foo1gr union bar1gr) shouldEqual true
val oneBNGraph = bnNameGr(1, "Henry") union bnNameGr(1, "Alexandre")
val d2 = diff(oneBNGraph, bnNameGr(1, "{}"))
isomorphism(d2, oneBNGraph) shouldEqual true
val twoBNGraph = bnNameGr(1, "Henry") union bnNameGr(2, "Alexandre")
val d3 = diff(twoBNGraph, foo1gr)
isomorphism(d3, twoBNGraph) shouldEqual true
val d4 = diff(twoBNGraph, bnNameGr(1, "And now for something completely different"))
isomorphism(d4, twoBNGraph) shouldEqual true
}
}
}