Skip to content

Commit

Permalink
Merge pull request #1806 from gjhiggins/migrate-more-tests-to-pytest
Browse files Browse the repository at this point in the history
Migrate more tests to pytest

Merging with only one review as this is a fairly straight forward migration to pytest and coverage remains the same.
  • Loading branch information
aucampia authored Apr 13, 2022
2 parents ab727dd + f76097c commit e19e1cc
Show file tree
Hide file tree
Showing 16 changed files with 168 additions and 286 deletions.
7 changes: 1 addition & 6 deletions test/test_graph/test_canonicalization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from collections import Counter
from typing import Set, Tuple
from unittest.case import expectedFailure

import pytest
from rdflib.term import Node
Expand Down Expand Up @@ -524,7 +523,7 @@ def test_issue725_collapsing_bnodes_2():


class TestConsistency(unittest.TestCase):
@expectedFailure
@pytest.mark.xfail
def test_consistent_ids(self) -> None:
"""
This test verifies that `to_canonical_graph` creates consistent
Expand Down Expand Up @@ -566,7 +565,3 @@ def test_consistent_ids(self) -> None:
assert cg0_ts.issubset(
cg1_ts
), "canonical triple set cg0_ts should be a subset of canonical triple set cg1_ts"


if __name__ == "__main__":
unittest.main()
67 changes: 20 additions & 47 deletions test/test_graph/test_slice.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from rdflib import Graph, URIRef
import unittest
from test.data import tarek, likes, pizza, cheese, michel, bob, hates


class GraphSlice(unittest.TestCase):
def testSlice(self):
class TestGraphSlice():
def test_slice(self):
"""
We pervert the slice object,
and use start, stop, step as subject, predicate, object
Expand All @@ -12,70 +12,43 @@ def testSlice(self):
"""

def sl(x, y):
return self.assertEqual(len(list(x)), y)
return len(list(x)) == y

def soe(x, y):
return self.assertEqual(set([a[2] for a in x]), set(y)) # equals objects
return set([a[2] for a in x]) == set(y) # equals objects

g = self.graph
g = Graph()
g.add((tarek, likes, pizza))
g.add((tarek, likes, cheese))
g.add((michel, likes, pizza))
g.add((michel, likes, cheese))
g.add((bob, likes, cheese))
g.add((bob, hates, pizza))
g.add((bob, hates, michel)) # gasp!

# Single terms are all trivial:

# single index slices by subject, i.e. return triples((x,None,None))
# tell me everything about "tarek"
sl(g[self.tarek], 2)
sl(g[tarek], 2)

# single slice slices by s,p,o, with : used to split
# tell me everything about "tarek" (same as above)
sl(g[self.tarek : :], 2)
sl(g[tarek : :], 2)

# give me every "likes" relationship
sl(g[: self.likes :], 5)
sl(g[: likes :], 5)

# give me every relationship to pizza
sl(g[:: self.pizza], 3)
sl(g[:: pizza], 3)

# give me everyone who likes pizza
sl(g[: self.likes : self.pizza], 2)
sl(g[: likes : pizza], 2)

# does tarek like pizza?
self.assertTrue(g[self.tarek : self.likes : self.pizza])
assert g[tarek : likes : pizza] is True

# More intesting is using paths

# everything hated or liked
sl(g[: self.hates | self.likes], 7)

def setUp(self):
self.graph = Graph()

self.michel = URIRef("michel")
self.tarek = URIRef("tarek")
self.bob = URIRef("bob")
self.likes = URIRef("likes")
self.hates = URIRef("hates")
self.pizza = URIRef("pizza")
self.cheese = URIRef("cheese")

self.addStuff()

def addStuff(self):
tarek = self.tarek
michel = self.michel
bob = self.bob
likes = self.likes
hates = self.hates
pizza = self.pizza
cheese = self.cheese

self.graph.add((tarek, likes, pizza))
self.graph.add((tarek, likes, cheese))
self.graph.add((michel, likes, pizza))
self.graph.add((michel, likes, cheese))
self.graph.add((bob, likes, cheese))
self.graph.add((bob, hates, pizza))
self.graph.add((bob, hates, michel)) # gasp!


if __name__ == "__main__":
unittest.main()
sl(g[: hates | likes], 7)
40 changes: 16 additions & 24 deletions test/test_literal/test_datetime.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sys
import unittest

from datetime import datetime

Expand All @@ -11,67 +10,60 @@
from rdflib.namespace import XSD


class TestRelativeBase(unittest.TestCase):
class TestRelativeBase:
def test_equality(self):
x = Literal(
"2008-12-01T18:02:00Z",
datatype=URIRef("http://www.w3.org/2001/XMLSchema#dateTime"),
)
self.assertEqual(x == x, True)
assert x == x

def test_microseconds(self):
dt1 = datetime(2009, 6, 15, 23, 37, 6, 522630)
l = Literal(dt1)

# datetime with microseconds should be cast as a literal with using
# XML Schema dateTime as the literal datatype
self.assertEqual(str(l), "2009-06-15T23:37:06.522630")
self.assertEqual(l.datatype, XSD.dateTime)
assert str(l) == "2009-06-15T23:37:06.522630"
assert l.datatype == XSD.dateTime

dt2 = l.toPython()
self.assertEqual(dt2, dt1)
assert dt2 == dt1

def test_to_python(self):
dt = "2008-12-01T18:02:00"
l = Literal(dt, datatype=URIRef("http://www.w3.org/2001/XMLSchema#dateTime"))

self.assertTrue(isinstance(l.toPython(), datetime))
self.assertEqual(l.toPython().isoformat(), dt)
assert isinstance(l.toPython(), datetime)
assert l.toPython().isoformat() == dt

def test_timezone_z(self):
dt = "2008-12-01T18:02:00.522630Z"
l = Literal(dt, datatype=URIRef("http://www.w3.org/2001/XMLSchema#dateTime"))

self.assertTrue(isinstance(l.toPython(), datetime))
self.assertEqual(
datetime_isoformat(
l.toPython(), DATE_EXT_COMPLETE + "T" + "%H:%M:%S.%f" + TZ_EXT
),
dt,
assert isinstance(l.toPython(), datetime)
assert datetime_isoformat(
l.toPython() == DATE_EXT_COMPLETE + "T" + "%H:%M:%S.%f" + TZ_EXT, dt
)
self.assertEqual(l.toPython().isoformat(), "2008-12-01T18:02:00.522630+00:00")
assert l.toPython().isoformat() == "2008-12-01T18:02:00.522630+00:00"

def test_timezone_offset(self):
dt = "2010-02-10T12:36:00+03:00"
l = Literal(dt, datatype=URIRef("http://www.w3.org/2001/XMLSchema#dateTime"))

self.assertTrue(isinstance(l.toPython(), datetime))
self.assertEqual(l.toPython().isoformat(), dt)
assert isinstance(l.toPython(), datetime)
assert l.toPython().isoformat() == dt

def test_timezone_offset_to_utc(self):
dt = "2010-02-10T12:36:00+03:00"
l = Literal(dt, datatype=URIRef("http://www.w3.org/2001/XMLSchema#dateTime"))

utc_dt = l.toPython().astimezone(UTC)
self.assertEqual(datetime_isoformat(utc_dt), "2010-02-10T09:36:00Z")
assert datetime_isoformat(utc_dt) == "2010-02-10T09:36:00Z"

def test_timezone_offset_millisecond(self):
dt = "2011-01-16T19:39:18.239743+01:00"
l = Literal(dt, datatype=URIRef("http://www.w3.org/2001/XMLSchema#dateTime"))

self.assertTrue(isinstance(l.toPython(), datetime))
self.assertEqual(l.toPython().isoformat(), dt)


if __name__ == "__main__":
unittest.main()
assert isinstance(l.toPython(), datetime)
assert l.toPython().isoformat() == dt
36 changes: 14 additions & 22 deletions test/test_literal/test_duration.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import unittest
from datetime import timedelta

from isodate import Duration, parse_duration
Expand All @@ -7,40 +6,33 @@
from rdflib.term import Literal


class TestDuration(unittest.TestCase):
class TestDuration:
def test_to_python_timedelta(self):
l = Literal("P4DT5H6M7S", datatype=XSD.dayTimeDuration)
self.assertTrue(isinstance(l.toPython(), timedelta))
self.assertEqual(l.toPython(), parse_duration("P4DT5H6M7S"))
assert isinstance(l.toPython(), timedelta)
assert l.toPython() == parse_duration("P4DT5H6M7S")

def test_to_python_ym_duration(self):
l = Literal("P1Y2M", datatype=XSD.yearMonthDuration)
self.assertTrue(isinstance(l.toPython(), Duration))
self.assertEqual(l.toPython(), parse_duration("P1Y2M"))
assert isinstance(l.toPython(), Duration)
assert l.toPython() == parse_duration("P1Y2M")

def test_to_python_ymdhms_duration(self):
l = Literal("P1Y2M4DT5H6M7S", datatype=XSD.duration)
self.assertTrue(isinstance(l.toPython(), Duration))
self.assertEqual(l.toPython(), parse_duration("P1Y2M4DT5H6M7S"))
assert isinstance(l.toPython(), Duration)
assert l.toPython() == parse_duration("P1Y2M4DT5H6M7S")

def test_equality(self):
def test_equalityself(self):
x = Literal("P1Y2M3W4DT5H6M7S", datatype=XSD.duration)
y = Literal("P1Y2M25DT5H6M7S", datatype=XSD.duration)
self.assertTrue(x == y)
assert x == y

def test_duration_le(self):
self.assertTrue(
Literal("P4DT5H6M7S", datatype=XSD.duration)
< Literal("P8DT10H12M14S", datatype=XSD.duration)
assert Literal("P4DT5H6M7S", datatype=XSD.duration) < Literal(
"P8DT10H12M14S", datatype=XSD.duration
)

def test_duration_sum(self):
self.assertEqual(
Literal("P1Y2M4DT5H6M7S", datatype=XSD.duration)
+ Literal("P1Y2M4DT5H6M7S", datatype=XSD.duration).toPython(),
Literal("P2Y4M8DT10H12M14S", datatype=XSD.duration),
)


if __name__ == "__main__":
unittest.main()
assert Literal("P1Y2M4DT5H6M7S", datatype=XSD.duration) + Literal(
"P1Y2M4DT5H6M7S", datatype=XSD.duration
).toPython() == Literal("P2Y4M8DT10H12M14S", datatype=XSD.duration)
19 changes: 6 additions & 13 deletions test/test_literal/test_normalized_string.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
import unittest

from rdflib import Literal
from rdflib.namespace import XSD


class NormalizedStringTest(unittest.TestCase):
class TestNormalizedString:
def test1(self):
lit2 = Literal("\two\nw", datatype=XSD.normalizedString)
lit = Literal("\two\nw", datatype=XSD.string)
self.assertFalse(str(lit) == str(lit2))
assert str(lit) != str(lit2)

def test2(self):
lit = Literal(
"\tBeing a Doctor Is\n\ta Full-Time Job\r", datatype=XSD.normalizedString
)
st = Literal(" Being a Doctor Is a Full-Time Job ", datatype=XSD.string)
self.assertFalse(Literal.eq(st, lit))
self.assertEqual(str(lit), str(st))
assert Literal.eq(st, lit) is False
assert str(lit) == str(st)

def test3(self):
lit = Literal("hey\nthere", datatype=XSD.normalizedString).n3()
self.assertTrue(
lit == '"hey there"^^<http://www.w3.org/2001/XMLSchema#normalizedString>'
)
assert lit == '"hey there"^^<http://www.w3.org/2001/XMLSchema#normalizedString>'

def test4(self):
lit = Literal(
"hey\nthere\ta tab\rcarriage return", datatype=XSD.normalizedString
)
expected = Literal("""hey there a tab carriage return""", datatype=XSD.string)
self.assertEqual(str(lit), str(expected))

assert str(lit) == str(expected)

if __name__ == "__main__":
unittest.main()
Loading

0 comments on commit e19e1cc

Please sign in to comment.