Skip to content

Commit

Permalink
Convert some more graph tests to pytest (#1982)
Browse files Browse the repository at this point in the history
This patch converts some additional graph tests to pytest, I'm doing
this in preperation for adding more tests to `test_diff.py`.
  • Loading branch information
aucampia committed Jun 5, 2022
1 parent 148e4a2 commit a3e6f67
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 117 deletions.
24 changes: 12 additions & 12 deletions test/test_graph/test_batch_add.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import unittest
import pytest

from rdflib.graph import BatchAddGraph, Graph
from rdflib.term import URIRef


class TestBatchAddGraph(unittest.TestCase):
class TestBatchAddGraph:
def test_batch_size_zero_denied(self):
with self.assertRaises(ValueError):
with pytest.raises(ValueError):
BatchAddGraph(Graph(), batch_size=0)

def test_batch_size_none_denied(self):
with self.assertRaises(ValueError):
with pytest.raises(ValueError):
BatchAddGraph(Graph(), batch_size=None)

def test_batch_size_one_denied(self):
with self.assertRaises(ValueError):
with pytest.raises(ValueError):
BatchAddGraph(Graph(), batch_size=1)

def test_batch_size_negative_denied(self):
with self.assertRaises(ValueError):
with pytest.raises(ValueError):
BatchAddGraph(Graph(), batch_size=-12)

def test_exit_submits_partial_batch(self):
trip = (URIRef("a"), URIRef("b"), URIRef("c"))
g = Graph()
with BatchAddGraph(g, batch_size=10) as cut:
cut.add(trip)
self.assertIn(trip, g)
assert trip in g

def test_add_more_than_batch_size(self):
trips = [(URIRef("a"), URIRef("b%d" % i), URIRef("c%d" % i)) for i in range(12)]
g = Graph()
with BatchAddGraph(g, batch_size=10) as cut:
for trip in trips:
cut.add(trip)
self.assertEqual(12, len(g))
assert 12 == len(g)

def test_add_quad_for_non_conjunctive_empty(self):
"""
Expand All @@ -44,13 +44,13 @@ def test_add_quad_for_non_conjunctive_empty(self):
badg = Graph(identifier="http://example.org/badness")
with BatchAddGraph(g) as cut:
cut.add((URIRef("a"), URIRef("b"), URIRef("c"), badg))
self.assertEqual(0, len(g))
assert 0 == len(g)

def test_add_quad_for_non_conjunctive_pass_on_context_matches(self):
g = Graph()
with BatchAddGraph(g) as cut:
cut.add((URIRef("a"), URIRef("b"), URIRef("c"), g))
self.assertEqual(1, len(g))
assert 1 == len(g)

def test_no_addN_on_exception(self):
"""
Expand All @@ -69,7 +69,7 @@ def test_no_addN_on_exception(self):
except Exception as e:
if str(e) != "myexc":
pass
self.assertEqual(10, len(g))
assert 10 == len(g)

def test_addN_batching_addN(self):
class MockGraph(object):
Expand All @@ -86,4 +86,4 @@ def addN(self, quads):

with BatchAddGraph(g, batch_size=10, batch_addn=True) as cut:
cut.addN(quads)
self.assertEqual(g.counts, [10, 2])
assert g.counts == [10, 2]
3 changes: 1 addition & 2 deletions test/test_graph/test_canonicalization.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import unittest
from collections import Counter
from io import StringIO
from test.utils import GraphHelper
Expand Down Expand Up @@ -521,7 +520,7 @@ def test_issue725_collapsing_bnodes_2():
_TripleSet = Set["_TripleType"]


class TestConsistency(unittest.TestCase):
class TestConsistency:
@pytest.mark.xfail
def test_consistent_ids(self) -> None:
"""
Expand Down
169 changes: 81 additions & 88 deletions test/test_graph/test_container.py
Original file line number Diff line number Diff line change
@@ -1,88 +1,81 @@
import unittest

from rdflib import Graph
from rdflib.container import Alt, Bag, Seq
from rdflib.term import BNode, Literal


class TestContainer(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.g = Graph()
cls.c1 = Bag(cls.g, BNode())
cls.c2 = Bag(
cls.g, BNode(), [Literal("1"), Literal("2"), Literal("3"), Literal("4")]
)
cls.c3 = Alt(
cls.g, BNode(), [Literal("1"), Literal("2"), Literal("3"), Literal("4")]
)
cls.c4 = Seq(
cls.g, BNode(), [Literal("1"), Literal("2"), Literal("3"), Literal("4")]
)

def testA(self):
self.assertEqual(len(self.c1) == 0, True)

def testB(self):
self.assertEqual(len(self.c2) == 4, True)

def testC(self):
self.c2.append(Literal("5"))
del self.c2[2]
self.assertEqual(len(self.c2) == 4, True)

def testD(self):
self.assertEqual(self.c2.index(Literal("5")) == 4, True)

def testE(self):
self.assertEqual(self.c2[2] == Literal("3"), True)

def testF(self):
self.c2[2] = Literal("9")
self.assertEqual(self.c2[2] == Literal("9"), True)

def testG(self):
self.c2.clear()
self.assertEqual(len(self.c2) == 0, True)

def testH(self):
self.c2.append_multiple([Literal("80"), Literal("90")])
self.assertEqual(self.c2[1] == Literal("80"), True)

def testI(self):
self.assertEqual(self.c2[2] == Literal("90"), True)

def testJ(self):
self.assertEqual(len(self.c2) == 2, True)

def testK(self):
self.assertEqual(self.c2.end() == 2, True)

def testL(self):
self.assertEqual(
self.c3.anyone()
in [Literal("1"), Literal("2"), Literal("3"), Literal("4")],
True,
)

def testM(self):
self.c4.add_at_position(3, Literal("60"))
self.assertEqual(len(self.c4) == 5, True)

def testN(self):
self.assertEqual(self.c4.index(Literal("60")) == 3, True)

def testO(self):
self.assertEqual(self.c4.index(Literal("3")) == 4, True)

def testP(self):
self.assertEqual(self.c4.index(Literal("4")) == 5, True)

def testQ(self):
self.assertEqual(
self.c2.index(Literal("1000")) == 3, False
) # there is no Literal("1000") in the Bag


if __name__ == "__main__":
unittest.main()
from rdflib import Graph
from rdflib.container import Alt, Bag, Seq
from rdflib.term import BNode, Literal


class TestContainer:
@classmethod
def setup_class(cls):
cls.g = Graph()
cls.c1 = Bag(cls.g, BNode())
cls.c2 = Bag(
cls.g, BNode(), [Literal("1"), Literal("2"), Literal("3"), Literal("4")]
)
cls.c3 = Alt(
cls.g, BNode(), [Literal("1"), Literal("2"), Literal("3"), Literal("4")]
)
cls.c4 = Seq(
cls.g, BNode(), [Literal("1"), Literal("2"), Literal("3"), Literal("4")]
)

def testA(self):
assert len(self.c1) == 0

def testB(self):
assert len(self.c2) == 4

def testC(self):
self.c2.append(Literal("5"))
del self.c2[2]
assert len(self.c2) == 4

def testD(self):
assert self.c2.index(Literal("5")) == 4

def testE(self):
assert self.c2[2] == Literal("3")

def testF(self):
self.c2[2] = Literal("9")
assert self.c2[2] == Literal("9")

def testG(self):
self.c2.clear()
assert len(self.c2) == 0

def testH(self):
self.c2.append_multiple([Literal("80"), Literal("90")])
assert self.c2[1] == Literal("80")

def testI(self):
assert self.c2[2] == Literal("90")

def testJ(self):
assert len(self.c2) == 2

def testK(self):
assert self.c2.end() == 2

def testL(self):
assert self.c3.anyone() in [
Literal("1"),
Literal("2"),
Literal("3"),
Literal("4"),
]

def testM(self):
self.c4.add_at_position(3, Literal("60"))
assert len(self.c4) == 5

def testN(self):
assert self.c4.index(Literal("60")) == 3

def testO(self):
assert self.c4.index(Literal("3")) == 4

def testP(self):
assert self.c4.index(Literal("4")) == 5

def testQ(self):
assert self.c2.index(Literal("1000")) != 3
26 changes: 11 additions & 15 deletions test/test_graph/test_diff.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
from test.utils import GraphHelper
from typing import TYPE_CHECKING, Set
from unittest.case import expectedFailure

import pytest

import rdflib
from rdflib import Graph
Expand All @@ -18,26 +18,26 @@
_TripleSetT = Set["_TripleType"]


class TestDiff(unittest.TestCase):
class TestDiff:
"""Unicode literals for graph_diff test
(issue 151)"""

def testA(self):
def test_a(self):
"""with bnode"""
g = rdflib.Graph()
g.add((rdflib.BNode(), rdflib.URIRef("urn:p"), rdflib.Literal("\xe9")))

diff = graph_diff(g, g)
graph_diff(g, g)

def testB(self):
def test_b(self):
"""Curiously, this one passes, even before the fix in issue 151"""

g = rdflib.Graph()
g.add((rdflib.URIRef("urn:a"), rdflib.URIRef("urn:p"), rdflib.Literal("\xe9")))

diff = graph_diff(g, g)
graph_diff(g, g)

@expectedFailure
@pytest.mark.xfail()
def test_subsets(self) -> None:
"""
This test verifies that `graph_diff` returns the correct values
Expand Down Expand Up @@ -73,10 +73,6 @@ def test_subsets(self) -> None:

result = graph_diff(g0, g1)
in_both, in_first, in_second = GraphHelper.triple_sets(result)
self.assertFalse(in_first)
self.assertTrue(in_second)
self.assertTrue(in_both)


if __name__ == "__main__":
unittest.main()
assert in_first == set()
assert len(in_second) > 0
assert len(in_both) > 0

0 comments on commit a3e6f67

Please sign in to comment.