Skip to content

Commit 97ed843

Browse files
authored
test: convert more test from unittest to pytest (#2089)
Fairly straight forward conversions with minimal changes.
1 parent 745017e commit 97ed843

File tree

4 files changed

+52
-73
lines changed

4 files changed

+52
-73
lines changed

test/test_parsers/test_parser.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
import unittest
2-
31
from rdflib.graph import Graph
42
from rdflib.namespace import RDF, RDFS
53
from rdflib.term import Literal, URIRef
64

75

8-
class ParserTestCase(unittest.TestCase):
6+
class TestParser:
97
backend = "default"
108
path = "store"
119

12-
def setUp(self):
10+
def setup_method(self):
1311
self.graph = Graph(store=self.backend)
1412
self.graph.open(self.path)
1513

16-
def tearDown(self):
14+
def teardown_method(self):
1715
self.graph.close()
1816

1917
def testNoPathWithHash(self):
@@ -38,12 +36,12 @@ def testNoPathWithHash(self):
3836

3937
subject = URIRef("http://example.org#")
4038
label = g.value(subject, RDFS.label)
41-
self.assertEqual(label, Literal("testing"))
39+
assert label == Literal("testing")
4240
type = g.value(subject, RDF.type)
43-
self.assertEqual(type, RDFS.Class)
41+
assert type == RDFS.Class
4442

4543

46-
class TestGitHubIssues(unittest.TestCase):
44+
class TestGitHubIssues:
4745
def test_issue_1228_a(self):
4846
data = """
4947
PREFIX sdo: <https://schema.org/>
@@ -53,8 +51,8 @@ def test_issue_1228_a(self):
5351
"""
5452

5553
g = Graph().parse(data=data, format="ttl")
56-
self.assertNotIn("1982-01-01", data)
57-
self.assertNotIn("1982-01-01", g.serialize(format="ttl"))
54+
assert "1982-01-01" not in data
55+
assert "1982-01-01" not in g.serialize(format="ttl")
5856

5957
def test_issue_1228_b(self):
6058
data = """\
@@ -70,8 +68,8 @@ def test_issue_1228_b(self):
7068
</rdf:RDF>"""
7169

7270
g = Graph().parse(data=data, format="xml")
73-
self.assertNotIn("1982-01-01", data)
74-
self.assertNotIn("1982-01-01", g.serialize(format="xml"))
71+
assert "1982-01-01" not in data
72+
assert "1982-01-01" not in g.serialize(format="xml")
7573

7674
def test_issue_806(self):
7775
data = (
@@ -82,8 +80,4 @@ def test_issue_806(self):
8280
g = Graph()
8381
g.parse(data=data, format="nt")
8482
for _, _, o in g:
85-
self.assertNotIn("1891-01-01", o.n3())
86-
87-
88-
if __name__ == "__main__":
89-
unittest.main()
83+
assert "1891-01-01" not in o.n3()

test/test_parsers/test_trix_parse.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
#!/usr/bin/env python
22
import os
3-
import unittest
43
from test.data import TEST_DATA_DIR
54

65
from rdflib.graph import ConjunctiveGraph
76

87

9-
class TestTrixParse(unittest.TestCase):
10-
def setUp(self):
8+
class TestTrixParse:
9+
def setup_method(self):
1110
pass
1211

13-
def tearDown(self):
12+
def teardown_method(self):
1413
pass
1514

1615
def testAperture(self):
@@ -26,8 +25,8 @@ def testAperture(self):
2625
# print list(g.contexts())
2726
t = sum(map(len, g.contexts()))
2827

29-
self.assertEqual(t, 24)
30-
self.assertEqual(len(c), 4)
28+
assert t == 24
29+
assert len(c) == 4
3130

3231
# print "Parsed %d triples"%t
3332

@@ -54,7 +53,3 @@ def testNG4j(self):
5453
g.parse(trix_path, format="trix")
5554

5655
# print "Parsed %d triples"%len(g)
57-
58-
59-
if __name__ == "__main__":
60-
unittest.main()

test/test_store/test_store_sparqlstore.py

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import re
22
import socket
3-
import unittest
43
from http.server import BaseHTTPRequestHandler, HTTPServer
54
from test.utils import helper
65
from test.utils.httpservermock import (
@@ -55,30 +54,28 @@ def test_graph_modify_fails(
5554
), f"exception text {msg!r} does not match regex {pattern_str!r}"
5655

5756

58-
class SPARQLStoreFakeDBPediaTestCase(unittest.TestCase):
57+
class TestSPARQLStoreFakeDBPedia:
5958
store_name = "SPARQLStore"
6059
path: ClassVar[str]
6160
httpmock: ClassVar[ServedBaseHTTPServerMock]
6261

6362
@classmethod
64-
def setUpClass(cls) -> None:
65-
super().setUpClass()
63+
def setup_class(cls) -> None:
6664
cls.httpmock = ServedBaseHTTPServerMock()
6765
cls.path = f"{cls.httpmock.url}/sparql"
6866

6967
@classmethod
70-
def tearDownClass(cls) -> None:
71-
super().tearDownClass()
68+
def teardown_class(cls) -> None:
7269
cls.httpmock.stop()
7370

74-
def setUp(self):
71+
def setup_method(self):
7572
self.httpmock.reset()
7673
self.graph = Graph(store="SPARQLStore")
7774
self.graph.open(self.path, create=True)
7875
ns = list(self.graph.namespaces())
7976
assert len(ns) > 0, ns
8077

81-
def tearDown(self):
78+
def teardown_method(self):
8279
self.graph.close()
8380

8481
def test_Query(self):
@@ -121,10 +118,10 @@ def unpacker(query, default_graph=None, named_graph=None):
121118
(mquery, _, _) = unpacker(*args, *kwargs)
122119
for _, uri in self.graph.namespaces():
123120
assert mquery.count(f"<{uri}>") == 1
124-
self.assertEqual(self.httpmock.mocks[MethodName.GET].call_count, 1)
121+
assert self.httpmock.mocks[MethodName.GET].call_count == 1
125122
req = self.httpmock.requests[MethodName.GET].pop(0)
126-
self.assertRegex(req.path, r"^/sparql")
127-
self.assertIn(query, req.path_query["query"][0])
123+
assert re.match(r"^/sparql", req.path)
124+
assert query in req.path_query["query"][0]
128125

129126
def test_initNs(self):
130127
query = """\
@@ -184,10 +181,10 @@ def test_initNs(self):
184181
for i in res:
185182
assert type(i[0]) == Literal, i[0].n3()
186183

187-
self.assertEqual(self.httpmock.mocks[MethodName.GET].call_count, 1)
184+
assert self.httpmock.mocks[MethodName.GET].call_count == 1
188185
req = self.httpmock.requests[MethodName.GET].pop(0)
189-
self.assertRegex(req.path, r"^/sparql")
190-
self.assertIn(query, req.path_query["query"][0])
186+
assert re.match(r"^/sparql", req.path)
187+
assert query in req.path_query["query"][0]
191188

192189
def test_noinitNs(self):
193190
query = """\
@@ -206,12 +203,12 @@ def test_noinitNs(self):
206203
{"Content-Type": ["text/plain"]},
207204
)
208205
)
209-
with self.assertRaises(ValueError):
206+
with pytest.raises(ValueError):
210207
self.graph.query(query)
211-
self.assertEqual(self.httpmock.mocks[MethodName.GET].call_count, 1)
208+
assert self.httpmock.mocks[MethodName.GET].call_count == 1
212209
req = self.httpmock.requests[MethodName.GET].pop(0)
213-
self.assertRegex(req.path, r"^/sparql")
214-
self.assertIn(query, req.path_query["query"][0])
210+
assert re.match(r"^/sparql", req.path)
211+
assert query in req.path_query["query"][0]
215212

216213
def test_query_with_added_prolog(self):
217214
prologue = """\
@@ -271,10 +268,10 @@ def test_query_with_added_prolog(self):
271268
res = helper.query_with_retry(self.graph, prologue + query)
272269
for i in res:
273270
assert type(i[0]) == Literal, i[0].n3()
274-
self.assertEqual(self.httpmock.mocks[MethodName.GET].call_count, 1)
271+
assert self.httpmock.mocks[MethodName.GET].call_count == 1
275272
req = self.httpmock.requests[MethodName.GET].pop(0)
276-
self.assertRegex(req.path, r"^/sparql")
277-
self.assertIn(query, req.path_query["query"][0])
273+
assert re.match(r"^/sparql", req.path)
274+
assert query in req.path_query["query"][0]
278275

279276
def test_query_with_added_rdf_prolog(self):
280277
prologue = """\
@@ -335,10 +332,10 @@ def test_query_with_added_rdf_prolog(self):
335332
res = helper.query_with_retry(self.graph, prologue + query)
336333
for i in res:
337334
assert type(i[0]) == Literal, i[0].n3()
338-
self.assertEqual(self.httpmock.mocks[MethodName.GET].call_count, 1)
335+
assert self.httpmock.mocks[MethodName.GET].call_count == 1
339336
req = self.httpmock.requests[MethodName.GET].pop(0)
340-
self.assertRegex(req.path, r"^/sparql")
341-
self.assertIn(query, req.path_query["query"][0])
337+
assert re.match(r"^/sparql", req.path)
338+
assert query in req.path_query["query"][0]
342339

343340
def test_counting_graph_and_store_queries(self):
344341
query = """
@@ -401,15 +398,15 @@ def test_counting_graph_and_store_queries(self):
401398

402399
assert count == 5, "SPARQLStore() didn't return 5 records"
403400

404-
self.assertEqual(self.httpmock.mocks[MethodName.GET].call_count, 2)
401+
assert self.httpmock.mocks[MethodName.GET].call_count == 2
405402
for _ in range(2):
406403
req = self.httpmock.requests[MethodName.GET].pop(0)
407-
self.assertRegex(req.path, r"^/sparql")
408-
self.assertIn(query, req.path_query["query"][0])
404+
assert re.match(r"^/sparql", req.path)
405+
assert query in req.path_query["query"][0]
409406

410407

411-
class SPARQLStoreUpdateTestCase(unittest.TestCase):
412-
def setUp(self):
408+
class TestSPARQLStoreUpdate:
409+
def setup_method(self):
413410
port = self.setup_mocked_endpoint()
414411
self.graph = Graph(store="SPARQLUpdateStore", identifier=URIRef("urn:ex"))
415412
self.graph.open(
@@ -442,7 +439,7 @@ def setup_mocked_endpoint(self):
442439
)
443440
return port
444441

445-
def tearDown(self):
442+
def teardown_method(self):
446443
self.graph.close()
447444

448445
def test_Query(self):
@@ -497,7 +494,7 @@ def do_GET(self):
497494
return
498495

499496

500-
class SPARQLMockTests(unittest.TestCase):
497+
class TestSPARQLMock:
501498
def test_query(self):
502499
triples = {
503500
(RDFS.Resource, RDF.type, RDFS.Class),
@@ -538,7 +535,3 @@ def test_query(self):
538535

539536
for _, uri in graph.namespaces():
540537
assert query.count(f"<{uri}>") == 1
541-
542-
543-
if __name__ == "__main__":
544-
unittest.main()

test/test_store/test_store_sparqlupdatestore_mock.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import unittest
21
from test.utils.httpservermock import (
32
MethodName,
43
MockHTTPResponse,
@@ -12,31 +11,29 @@
1211
EG = Namespace("http://example.org/")
1312

1413

15-
class TestSPARQLConnector(unittest.TestCase):
14+
class TestSPARQLConnector:
1615
query_path: ClassVar[str]
1716
query_endpoint: ClassVar[str]
1817
update_path: ClassVar[str]
1918
update_endpoint: ClassVar[str]
2019
httpmock: ClassVar[ServedBaseHTTPServerMock]
2120

2221
@classmethod
23-
def setUpClass(cls) -> None:
24-
super().setUpClass()
22+
def setup_class(cls) -> None:
2523
cls.httpmock = ServedBaseHTTPServerMock()
2624
cls.query_path = "/db/sparql"
2725
cls.query_endpoint = f"{cls.httpmock.url}{cls.query_path}"
2826
cls.update_path = "/db/update"
2927
cls.update_endpoint = f"{cls.httpmock.url}{cls.update_path}"
3028

3129
@classmethod
32-
def tearDownClass(cls) -> None:
33-
super().tearDownClass()
30+
def teardown_class(cls) -> None:
3431
cls.httpmock.stop()
3532

36-
def setUp(self):
33+
def setup_method(self):
3734
self.httpmock.reset()
3835

39-
def tearDown(self):
36+
def teardown_method(self):
4037
pass
4138

4239
def test_graph_update(self):
@@ -57,7 +54,7 @@ def test_graph_update(self):
5754
# at the moment this is the only supported way for SPARQLUpdateStore
5855
# to do updates.
5956
graph.update(update_statement)
60-
self.assertEqual(self.httpmock.call_count, 1)
57+
assert self.httpmock.call_count == 1
6158
req = self.httpmock.requests[MethodName.POST].pop(0)
62-
self.assertEqual(req.parsed_path.path, self.update_path)
63-
self.assertIn("application/sparql-update", req.headers.get("content-type"))
59+
assert req.parsed_path.path == self.update_path
60+
assert "application/sparql-update" in req.headers.get("content-type")

0 commit comments

Comments
 (0)