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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
from rdflib import Graph, URIRef, Literal, BNode, XSD
from rdflib.plugins.sparql import prepareQuery
from rdflib.compare import isomorphic
import rdflib
from .testutils import eq_
from pprint import pprint
import io
def test_dateTime_dateTime_subs_issue():
"""
Test for query mentioned in the Issue #629
https://github.com/RDFLib/rdflib/issues/629
"""
data1 = """
@prefix : <#>.
@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#>.
:C a rdfs:Class.
:start a rdf:Property;
rdfs:domain :C;
rdfs:range xsd:dateTime.
:end a rdf:Property;
rdfs:domain :C;
rdfs:range xsd:dateTime.
:c1 a :C;
:start "2016-01-01T20:00:00"^^xsd:dateTime;
:end "2016-01-02T20:01:00"^^xsd:dateTime.
:c2 a :C;
:start "2016-01-01T20:05:00"^^xsd:dateTime;
:end "2016-01-01T20:30:00"^^xsd:dateTime.
"""
graph1 = Graph()
f = io.StringIO(data1)
graph1.parse(f, format="n3")
result = graph1.query(
"""
SELECT ?c ?duration
WHERE {
?c :start ?start;
:end ?end.
BIND(?end - ?start AS ?duration)
}
"""
)
answer = list(result)
answer = sorted(answer)
# expected result will be a list of 2 tuples
# FirstElement of these tuples will be a node with a path of directory of saved project
# Second Element while represent the actual durations
expectedFirstDuration = rdflib.term.Literal(
"P1DT1M",
datatype=rdflib.term.URIRef("http://www.w3.org/2001/XMLSchema#duration"),
)
expectedSecondDuration = rdflib.term.Literal(
"PT25M",
datatype=rdflib.term.URIRef("http://www.w3.org/2001/XMLSchema#duration"),
)
eq_(answer[0][1], expectedFirstDuration)
eq_(answer[1][1], expectedSecondDuration)
def test_dateTime_duration_subs():
"""
Test cases for subtraction operation between dateTime and duration
"""
data = """
@prefix : <#>.
@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#>.
"""
graph = Graph()
f = io.StringIO(data)
graph.parse(f, format="n3")
## 1st Test Case
result1 = graph.query(
"""
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT (?d - ?duration AS ?next_year)
WHERE {
VALUES (?duration ?d) {
("P1Y"^^xsd:yearMonthDuration"2019-05-28T12:14:45Z"^^xsd:dateTime)
("P1Y"^^xsd:yearMonthDuration"2019-05-28"^^xsd:date)
}
}
"""
)
expected = []
expected.append(
rdflib.term.Literal(
"2018-05-28T12:14:45+00:00",
datatype=rdflib.term.URIRef("http://www.w3.org/2001/XMLSchema#dateTime"),
)
)
expected.append(
rdflib.term.Literal(
"2018-05-28",
datatype=rdflib.term.URIRef("http://www.w3.org/2001/XMLSchema#date"),
)
)
eq_(list(result1)[0][0], expected[0])
eq_(list(result1)[1][0], expected[1])
## 2nd Test Case
result2 = graph.query(
"""
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT (?d - ?duration AS ?next_year)
WHERE {
VALUES (?duration ?d) {
("P3DT1H15M"^^xsd:dayTimeDuration "2000-10-30T11:12:00"^^xsd:dateTime)
("P3DT1H15M"^^xsd:dayTimeDuration "2000-10-30"^^xsd:date)
}
}
"""
)
expected = []
expected.append(
rdflib.term.Literal(
"2000-10-27T09:57:00",
datatype=rdflib.term.URIRef("http://www.w3.org/2001/XMLSchema#dateTime"),
)
)
expected.append(
rdflib.term.Literal(
"2000-10-27",
datatype=rdflib.term.URIRef("http://www.w3.org/2001/XMLSchema#date"),
)
)
eq_(list(result2)[0][0], expected[0])
eq_(list(result2)[1][0], expected[1])
def test_dateTime_duration_add():
"""
Test cases for addition operation between dateTime and duration
"""
data = """
@prefix : <#>.
@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#>.
"""
graph = Graph()
f = io.StringIO(data)
graph.parse(f, format="n3")
## 1st Test case
result1 = graph.query(
"""
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT (?d + ?duration AS ?next_year)
WHERE {
VALUES (?duration ?d) {
("P1Y"^^xsd:yearMonthDuration"2019-05-28T12:14:45Z"^^xsd:dateTime)
("P1Y"^^xsd:yearMonthDuration"2019-05-28"^^xsd:date)
}
}
"""
)
# print(list(result1))
expected = []
expected.append(
rdflib.term.Literal(
"2020-05-28T12:14:45+00:00",
datatype=rdflib.term.URIRef("http://www.w3.org/2001/XMLSchema#dateTime"),
)
)
expected.append(
rdflib.term.Literal(
"2020-05-28",
datatype=rdflib.term.URIRef("http://www.w3.org/2001/XMLSchema#date"),
)
)
eq_(list(result1)[0][0], expected[0])
eq_(list(result1)[1][0], expected[1])
## 2nd Test case
result2 = graph.query(
"""
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT (?d + ?duration AS ?next_year)
WHERE {
VALUES (?duration ?d) {
("P3DT1H15M"^^xsd:dayTimeDuration "2000-10-30T11:12:00"^^xsd:dateTime)
("P3DT1H15M"^^xsd:dayTimeDuration "2000-10-30"^^xsd:date)
}
}
"""
)
# print(list(result2))
expected = []
expected.append(
rdflib.term.Literal(
"2000-11-02T12:27:00",
datatype=rdflib.term.URIRef("http://www.w3.org/2001/XMLSchema#dateTime"),
)
)
expected.append(
rdflib.term.Literal(
"2000-11-02",
datatype=rdflib.term.URIRef("http://www.w3.org/2001/XMLSchema#date"),
)
)
eq_(list(result2)[0][0], expected[0])
eq_(list(result2)[1][0], expected[1])
def test_dateTime_dateTime_subs():
"""
Test cases for subtraction operation between dateTime and dateTime
"""
data = """
@prefix : <#>.
@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#>.
"""
graph = Graph()
f = io.StringIO(data)
graph.parse(f, format="n3")
result1 = graph.query(
"""
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT (?l - ?r AS ?duration)
WHERE {
VALUES (?l ?r) {
("2000-10-30T06:12:00-05:00"^^xsd:dateTime "1999-11-28T09:00:00Z"^^xsd:dateTime)
("2000-10-30"^^xsd:date"1999-11-28"^^xsd:date)
}
}
"""
)
expected1 = rdflib.term.Literal(
"P337DT2H12M",
datatype=rdflib.term.URIRef("http://www.w3.org/2001/XMLSchema#duration"),
)
expected2 = rdflib.term.Literal(
"P337D",
datatype=rdflib.term.URIRef("http://www.w3.org/2001/XMLSchema#duration"),
)
eq_(list(result1)[0][0], expected1)
eq_(list(result1)[1][0], expected2)
|