From a7863d25dc47730f4ef8bdca91c4e35c7f7b1ccf Mon Sep 17 00:00:00 2001 From: Nicholas Car Date: Fri, 13 Mar 2020 21:16:15 +1000 Subject: adds cbd() function to Graph() and tests and dependency on black --- test/test_graph_cbd.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 test/test_graph_cbd.py (limited to 'test') diff --git a/test/test_graph_cbd.py b/test/test_graph_cbd.py new file mode 100644 index 00000000..88adf8be --- /dev/null +++ b/test/test_graph_cbd.py @@ -0,0 +1,63 @@ +import unittest +from rdflib import Graph, Namespace + + +class CbdTestCase(unittest.TestCase): + """Tests the Graph class' cbd() function""" + + def setUp(self): + self.g = Graph() + # adding example data for testing + self.g.parse( + data="""PREFIX ex: + PREFIX rdf: + + ex:R1 + a rdf:Resource ; + ex:hasChild ex:R2 , ex:R3 . + + ex:R2 + ex:propOne ex:P1 ; + ex:propTwo ex:P2 . + + ex:R3 + ex:propOne ex:P3 ; + ex:propTwo ex:P4 ; + ex:propThree [ + a rdf:Resource ; + ex:propFour "Some Literal" ; + ex:propFive ex:P5 ; + ex:propSix [ + ex:propSeven ex:P7 ; + ] ; + ] . + """, + format="turtle", + ) + + self.EX = Namespace("http://ex/") + self.g.bind("ex", self.EX) + + def testCbd(self): + self.assertEqual( + len(self.g.cbd(self.EX.R1)), 3, "cbd() for R1 should return 3 triples" + ) + + self.assertEqual( + len(self.g.cbd(self.EX.R2)), 2, "cbd() for R3 should return 2 triples" + ) + + self.assertEqual( + len(self.g.cbd(self.EX.R3)), 8, "cbd() for R3 should return 8 triples" + ) + + self.assertEqual( + len(self.g.cbd(self.EX.R4)), 0, "cbd() for R4 should return 0 triples" + ) + + def tearDown(self): + self.g.close() + + +if __name__ == "__main__": + unittest.main() -- cgit v1.2.1 From 0b39e5efc4b6a1eab886b8adf522870c3532e86b Mon Sep 17 00:00:00 2001 From: Nicholas Car Date: Fri, 13 Mar 2020 21:51:03 +1000 Subject: adds capability to include reified triples in cdb() extraction, as per spec, and tests for it --- test/test_graph_cbd.py | 99 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 80 insertions(+), 19 deletions(-) (limited to 'test') diff --git a/test/test_graph_cbd.py b/test/test_graph_cbd.py index 88adf8be..aedc9dd0 100644 --- a/test/test_graph_cbd.py +++ b/test/test_graph_cbd.py @@ -9,29 +9,30 @@ class CbdTestCase(unittest.TestCase): self.g = Graph() # adding example data for testing self.g.parse( - data="""PREFIX ex: - PREFIX rdf: - - ex:R1 + data=""" + PREFIX ex: + PREFIX rdf: + + ex:R1 a rdf:Resource ; ex:hasChild ex:R2 , ex:R3 . - - ex:R2 + + ex:R2 ex:propOne ex:P1 ; ex:propTwo ex:P2 . - - ex:R3 - ex:propOne ex:P3 ; - ex:propTwo ex:P4 ; - ex:propThree [ - a rdf:Resource ; - ex:propFour "Some Literal" ; - ex:propFive ex:P5 ; - ex:propSix [ - ex:propSeven ex:P7 ; - ] ; - ] . - """, + + ex:R3 + ex:propOne ex:P3 ; + ex:propTwo ex:P4 ; + ex:propThree [ + a rdf:Resource ; + ex:propFour "Some Literal" ; + ex:propFive ex:P5 ; + ex:propSix [ + ex:propSeven ex:P7 ; + ] ; + ] . + """, format="turtle", ) @@ -55,6 +56,66 @@ class CbdTestCase(unittest.TestCase): len(self.g.cbd(self.EX.R4)), 0, "cbd() for R4 should return 0 triples" ) + def testCbdReified(self): + # add some reified triples to the testing graph + self.g.parse( + data=""" + PREFIX ex: + PREFIX rdf: + + ex:R5 + ex:propOne ex:P1 ; + ex:propTwo ex:P2 ; + ex:propRei ex:Pre1 . + + ex:S + a rdf:Statement ; + rdf:subject ex:R5 ; + rdf:predicate ex:propRei ; + rdf:object ex:Pre1 ; + ex:otherReiProp ex:Pre2 . + """, + format="turtle", + ) + + # this cbd() call should get the 3 basic triples with ex:R5 as subject as well as 5 more from the reified + # statement + self.assertEqual( + len(self.g.cbd(self.EX.R5)), (3 + 5), "cbd() for R5 should return 8 triples" + ) + + # add crazy reified triples to the testing graph + self.g.parse( + data=""" + PREFIX ex: + PREFIX rdf: + + ex:R6 + ex:propOne ex:P1 ; + ex:propTwo ex:P2 ; + ex:propRei ex:Pre1 . + + ex:S1 + a rdf:Statement ; + rdf:subject ex:R6 ; + rdf:predicate ex:propRei ; + rdf:object ex:Pre1 ; + ex:otherReiProp ex:Pre3 . + + ex:S2 + rdf:subject ex:R6 ; + rdf:predicate ex:propRei2 ; + rdf:object ex:Pre2 ; + ex:otherReiProp ex:Pre4 ; + ex:otherReiProp ex:Pre5 . + """, + format="turtle", + ) + + self.assertEqual( + len(self.g.cbd(self.EX.R6)), (3 + 5 + 5), "cbd() for R6 should return 12 triples" + ) + def tearDown(self): self.g.close() -- cgit v1.2.1 From e9bbf2a7d29b897ef85a92c4072a3362cb054ea4 Mon Sep 17 00:00:00 2001 From: Nicholas Car Date: Tue, 17 Mar 2020 04:26:45 +1000 Subject: added all code from PR 388 --- test/test_hex_binary.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ test/test_literal.py | 26 ++++++++++++++++++++- 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 test/test_hex_binary.py (limited to 'test') diff --git a/test/test_hex_binary.py b/test/test_hex_binary.py new file mode 100644 index 00000000..711ff0bd --- /dev/null +++ b/test/test_hex_binary.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- + +import unittest +import binascii +from rdflib import Literal, XSD +import six + + +class HexBinaryTestCase(unittest.TestCase): + + def test_int(self): + self._test_integer(5) + self._test_integer(3452) + self._test_integer(4886) + + def _test_integer(self, i): + hex_i = format(i, "x") + # Make it has a even-length (Byte) + len_hex_i = len(hex_i) + hex_i = hex_i.zfill(len_hex_i + len_hex_i % 2) + + l = Literal(hex_i, datatype=XSD.hexBinary) + bin_i = l.toPython() + self.assertEquals(int(binascii.hexlify(bin_i), 16), i) + + if six.PY2: + self.assertEquals(unicode(l), hex_i) + else: + self.assertEquals(l, hex_i) + self.assertEquals(int(hex_i, 16), i) + if six.PY2: + self.assertEquals(int(unicode(l), 16), i) + else: + self.assertEquals(int(l, 16), i) + self.assertEquals(int(str(l), 16), i) + + def test_unicode(self): + str1 = u"Test utf-8 string éàë" + # u hexstring + hex_str1 = binascii.hexlify(str1.encode('utf-8')).decode() + l1 = Literal(hex_str1, datatype=XSD.hexBinary) + b_str1 = l1.toPython() + self.assertEquals(b_str1.decode('utf-8'), str1) + if six.PY2: + self.assertEquals(unicode(l1), hex_str1) + else: + self.assertEquals(l1, hex_str1) + + # b hexstring + hex_str1b = binascii.hexlify(str1.encode('utf-8')) + l1b = Literal(hex_str1b, datatype=XSD.hexBinary) + b_str1b = l1b.toPython() + self.assertEquals(b_str1, b_str1b) + self.assertEquals(b_str1b.decode('utf-8'), str1) + if six.PY2: + self.assertEquals(unicode(l1b), hex_str1) + else: + self.assertEquals(l1b, hex_str1) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/test_literal.py b/test/test_literal.py index feb9d72d..dae2d187 100644 --- a/test/test_literal.py +++ b/test/test_literal.py @@ -2,7 +2,7 @@ import unittest import rdflib # needed for eval(repr(...)) below from rdflib.term import Literal, URIRef, _XSD_DOUBLE, bind -from six import integer_types, PY3 +from six import integer_types, PY3, string_types def uformat(s): @@ -139,6 +139,30 @@ class TestBindings(unittest.TestCase): self.assertEqual(lb.value, vb) self.assertEqual(lb.datatype, dtB) + def testSpecificBinding(self): + + def lexify(s): + return "--%s--" % s + + def unlexify(s): + return s[2:-2] + + datatype = rdflib.URIRef('urn:dt:mystring') + + #Datatype-specific rule + bind(datatype, string_types, unlexify, lexify, datatype_specific=True) + + s = "Hello" + normal_l = Literal(s) + self.assertEqual(str(normal_l), s) + self.assertEqual(normal_l.toPython(), s) + self.assertEqual(normal_l.datatype, None) + + specific_l = Literal("--%s--" % s, datatype=datatype) + self.assertEqual(str(specific_l), lexify(s)) + self.assertEqual(specific_l.toPython(), s) + self.assertEqual(specific_l.datatype, datatype) + if __name__ == "__main__": unittest.main() -- cgit v1.2.1 From a37a9979cd5e35b732f9476083f9f1cb133a4561 Mon Sep 17 00:00:00 2001 From: Nicholas Car Date: Thu, 19 Mar 2020 13:35:33 +1000 Subject: test suite for add() --- test/test_term.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'test') diff --git a/test/test_term.py b/test/test_term.py index c222a8d2..fcf9f460 100644 --- a/test/test_term.py +++ b/test/test_term.py @@ -123,6 +123,56 @@ class TestLiteral(unittest.TestCase): random.shuffle(l2) self.assertListEqual(l1, sorted(l2)) + def test_literal_add(self): + a = Literal(1) + b01 = Literal(1) + b02 = Literal(-1) + b03 = Literal(1.1) + b04 = Literal(-1.1) + b05 = Literal("1", datatype=XSD.integer) + b06 = Literal("-1", datatype=XSD.integer) + b07 = Literal("+1", datatype=XSD.integer) + b08 = Literal("1.1", datatype=XSD.float) + b09 = Literal("-1.1", datatype=XSD.float) + b10 = Literal("1.1", datatype=XSD.decimal) + b11 = Literal("-1.1", datatype=XSD.decimal) + b12 = Literal("1.1", datatype=XSD.double) + b13 = Literal("-1.1", datatype=XSD.double) + b14 = Literal(u'1', datatype=XSD.integer) + b15 = Literal(u'-1', datatype=XSD.integer) + b16 = 1 + b17 = 1.1 + b18 = "1" + b19 = "1.1" + b20 = Literal("1", datatype=XSD.string) + b21 = Literal("1.1", datatype=XSD.string) + b22 = Literal(u'1', datatype=XSD.string) + b23 = None + + self.assertEqual(a + b01, Literal(2)) + self.assertEqual(a + b02, Literal(0)) + self.assertEqual(a + b03, Literal(2.1)) + # self.assertEqual(a + b04, Literal(-0.1)) # -0.10000000000000009 + self.assertEqual(a + b05, Literal(2)) + self.assertEqual(a + b06, Literal(0)) + self.assertEqual(a + b07, Literal(2)) + self.assertEqual(a + b08, Literal(2.1)) + # self.assertEqual(a + b09, Literal(-0.1)) + # self.assertEqual(a + b10, Literal(2.1)) # 2.1000000000 + # self.assertEqual(a + b11, Literal(-0.1)) + self.assertEqual(a + b12, Literal(2.1)) + # self.assertEqual(a + b13, Literal(-0.1)) + self.assertEqual(a + b14, Literal(2)) + self.assertEqual(a + b15, Literal(0)) + self.assertEqual(a + b16, Literal(2)) + self.assertEqual(a + b17, Literal(2.1)) + # self.assertEqual(a + b18, Literal(11)) + # self.assertEqual(a + b19, Literal(11.1)) + # self.assertEqual(a + b20, Literal(11)) + # self.assertEqual(a + b21, Literal(11.1)) + # self.assertEqual(a + b22, Literal(11)) + # self.assertEqual(a + b23, Literal(1)) + class TestValidityFunctions(unittest.TestCase): -- cgit v1.2.1 From 634b624cd6baf15fd7b01eb3b13842179cc2b7e1 Mon Sep 17 00:00:00 2001 From: Nicholas Car Date: Thu, 19 Mar 2020 13:51:48 +1000 Subject: Revert "adds cbd() function to Graph() and tests and dependency on black" This reverts commit a7863d25 --- test/test_graph_cbd.py | 124 ------------------------------------------------- 1 file changed, 124 deletions(-) delete mode 100644 test/test_graph_cbd.py (limited to 'test') diff --git a/test/test_graph_cbd.py b/test/test_graph_cbd.py deleted file mode 100644 index aedc9dd0..00000000 --- a/test/test_graph_cbd.py +++ /dev/null @@ -1,124 +0,0 @@ -import unittest -from rdflib import Graph, Namespace - - -class CbdTestCase(unittest.TestCase): - """Tests the Graph class' cbd() function""" - - def setUp(self): - self.g = Graph() - # adding example data for testing - self.g.parse( - data=""" - PREFIX ex: - PREFIX rdf: - - ex:R1 - a rdf:Resource ; - ex:hasChild ex:R2 , ex:R3 . - - ex:R2 - ex:propOne ex:P1 ; - ex:propTwo ex:P2 . - - ex:R3 - ex:propOne ex:P3 ; - ex:propTwo ex:P4 ; - ex:propThree [ - a rdf:Resource ; - ex:propFour "Some Literal" ; - ex:propFive ex:P5 ; - ex:propSix [ - ex:propSeven ex:P7 ; - ] ; - ] . - """, - format="turtle", - ) - - self.EX = Namespace("http://ex/") - self.g.bind("ex", self.EX) - - def testCbd(self): - self.assertEqual( - len(self.g.cbd(self.EX.R1)), 3, "cbd() for R1 should return 3 triples" - ) - - self.assertEqual( - len(self.g.cbd(self.EX.R2)), 2, "cbd() for R3 should return 2 triples" - ) - - self.assertEqual( - len(self.g.cbd(self.EX.R3)), 8, "cbd() for R3 should return 8 triples" - ) - - self.assertEqual( - len(self.g.cbd(self.EX.R4)), 0, "cbd() for R4 should return 0 triples" - ) - - def testCbdReified(self): - # add some reified triples to the testing graph - self.g.parse( - data=""" - PREFIX ex: - PREFIX rdf: - - ex:R5 - ex:propOne ex:P1 ; - ex:propTwo ex:P2 ; - ex:propRei ex:Pre1 . - - ex:S - a rdf:Statement ; - rdf:subject ex:R5 ; - rdf:predicate ex:propRei ; - rdf:object ex:Pre1 ; - ex:otherReiProp ex:Pre2 . - """, - format="turtle", - ) - - # this cbd() call should get the 3 basic triples with ex:R5 as subject as well as 5 more from the reified - # statement - self.assertEqual( - len(self.g.cbd(self.EX.R5)), (3 + 5), "cbd() for R5 should return 8 triples" - ) - - # add crazy reified triples to the testing graph - self.g.parse( - data=""" - PREFIX ex: - PREFIX rdf: - - ex:R6 - ex:propOne ex:P1 ; - ex:propTwo ex:P2 ; - ex:propRei ex:Pre1 . - - ex:S1 - a rdf:Statement ; - rdf:subject ex:R6 ; - rdf:predicate ex:propRei ; - rdf:object ex:Pre1 ; - ex:otherReiProp ex:Pre3 . - - ex:S2 - rdf:subject ex:R6 ; - rdf:predicate ex:propRei2 ; - rdf:object ex:Pre2 ; - ex:otherReiProp ex:Pre4 ; - ex:otherReiProp ex:Pre5 . - """, - format="turtle", - ) - - self.assertEqual( - len(self.g.cbd(self.EX.R6)), (3 + 5 + 5), "cbd() for R6 should return 12 triples" - ) - - def tearDown(self): - self.g.close() - - -if __name__ == "__main__": - unittest.main() -- cgit v1.2.1 From 01ec9c908e8c60d6aebf808ebad6b5e3cb8ec867 Mon Sep 17 00:00:00 2001 From: Nicholas Car Date: Thu, 19 Mar 2020 17:25:28 +1000 Subject: fix for Py3 --- test/test_hex_binary.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/test_hex_binary.py b/test/test_hex_binary.py index 711ff0bd..419a47e2 100644 --- a/test/test_hex_binary.py +++ b/test/test_hex_binary.py @@ -26,7 +26,7 @@ class HexBinaryTestCase(unittest.TestCase): if six.PY2: self.assertEquals(unicode(l), hex_i) else: - self.assertEquals(l, hex_i) + self.assertEquals(str(l), hex_i) self.assertEquals(int(hex_i, 16), i) if six.PY2: self.assertEquals(int(unicode(l), 16), i) @@ -44,7 +44,7 @@ class HexBinaryTestCase(unittest.TestCase): if six.PY2: self.assertEquals(unicode(l1), hex_str1) else: - self.assertEquals(l1, hex_str1) + self.assertEquals(str(l1), hex_str1) # b hexstring hex_str1b = binascii.hexlify(str1.encode('utf-8')) @@ -55,7 +55,7 @@ class HexBinaryTestCase(unittest.TestCase): if six.PY2: self.assertEquals(unicode(l1b), hex_str1) else: - self.assertEquals(l1b, hex_str1) + self.assertEquals(str(l1b), hex_str1) if __name__ == '__main__': -- cgit v1.2.1 From e196816c8670171d06a971ee645c11a20711961b Mon Sep 17 00:00:00 2001 From: Nicholas Car Date: Fri, 20 Mar 2020 21:41:05 +1000 Subject: passing all tests using round() --- test/test_term.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'test') diff --git a/test/test_term.py b/test/test_term.py index fcf9f460..774108cd 100644 --- a/test/test_term.py +++ b/test/test_term.py @@ -124,6 +124,8 @@ class TestLiteral(unittest.TestCase): self.assertListEqual(l1, sorted(l2)) def test_literal_add(self): + from decimal import Decimal + a = Literal(1) b01 = Literal(1) b02 = Literal(-1) @@ -151,27 +153,27 @@ class TestLiteral(unittest.TestCase): self.assertEqual(a + b01, Literal(2)) self.assertEqual(a + b02, Literal(0)) - self.assertEqual(a + b03, Literal(2.1)) - # self.assertEqual(a + b04, Literal(-0.1)) # -0.10000000000000009 + self.assertEqual(a + b03, Literal(round(Decimal(2.1), 15))) + self.assertEqual(a + b04, Literal(round(Decimal(-0.1), 15))) self.assertEqual(a + b05, Literal(2)) self.assertEqual(a + b06, Literal(0)) self.assertEqual(a + b07, Literal(2)) - self.assertEqual(a + b08, Literal(2.1)) - # self.assertEqual(a + b09, Literal(-0.1)) - # self.assertEqual(a + b10, Literal(2.1)) # 2.1000000000 - # self.assertEqual(a + b11, Literal(-0.1)) - self.assertEqual(a + b12, Literal(2.1)) - # self.assertEqual(a + b13, Literal(-0.1)) + self.assertEqual(a + b08, Literal(round(Decimal(2.1), 15))) + self.assertEqual(a + b09, Literal(round(Decimal(-0.1), 15))) + self.assertEqual(a + b10, Literal(round(Decimal(2.1), 15))) + self.assertEqual(a + b11, Literal(round(Decimal(-0.1), 15))) + self.assertEqual(a + b12, Literal(round(Decimal(2.1), 15))) + self.assertEqual(a + b13, Literal(round(Decimal(-0.1), 15))) self.assertEqual(a + b14, Literal(2)) self.assertEqual(a + b15, Literal(0)) self.assertEqual(a + b16, Literal(2)) - self.assertEqual(a + b17, Literal(2.1)) - # self.assertEqual(a + b18, Literal(11)) - # self.assertEqual(a + b19, Literal(11.1)) - # self.assertEqual(a + b20, Literal(11)) - # self.assertEqual(a + b21, Literal(11.1)) - # self.assertEqual(a + b22, Literal(11)) - # self.assertEqual(a + b23, Literal(1)) + self.assertEqual(a + b17, Literal(round(Decimal(2.1), 15))) + self.assertEqual(a + b18, Literal(11, datatype=XSD.string)) + self.assertEqual(a + b19, Literal(11.1, datatype=XSD.string)) + self.assertEqual(a + b20, Literal(11, datatype=XSD.string)) + self.assertEqual(a + b21, Literal(11.1, datatype=XSD.string)) + self.assertEqual(a + b22, Literal(11, datatype=XSD.string)) + self.assertEqual(a + b23, Literal(1)) class TestValidityFunctions(unittest.TestCase): -- cgit v1.2.1 From 2236eb1ee8298dcdfe4ae8696d508853f8125ff7 Mon Sep 17 00:00:00 2001 From: Nicholas Car Date: Sun, 22 Mar 2020 11:48:02 +1000 Subject: passing all add() specific tests --- test/test_term.py | 119 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 71 insertions(+), 48 deletions(-) (limited to 'test') diff --git a/test/test_term.py b/test/test_term.py index 774108cd..6b60a09f 100644 --- a/test/test_term.py +++ b/test/test_term.py @@ -126,54 +126,77 @@ class TestLiteral(unittest.TestCase): def test_literal_add(self): from decimal import Decimal - a = Literal(1) - b01 = Literal(1) - b02 = Literal(-1) - b03 = Literal(1.1) - b04 = Literal(-1.1) - b05 = Literal("1", datatype=XSD.integer) - b06 = Literal("-1", datatype=XSD.integer) - b07 = Literal("+1", datatype=XSD.integer) - b08 = Literal("1.1", datatype=XSD.float) - b09 = Literal("-1.1", datatype=XSD.float) - b10 = Literal("1.1", datatype=XSD.decimal) - b11 = Literal("-1.1", datatype=XSD.decimal) - b12 = Literal("1.1", datatype=XSD.double) - b13 = Literal("-1.1", datatype=XSD.double) - b14 = Literal(u'1', datatype=XSD.integer) - b15 = Literal(u'-1', datatype=XSD.integer) - b16 = 1 - b17 = 1.1 - b18 = "1" - b19 = "1.1" - b20 = Literal("1", datatype=XSD.string) - b21 = Literal("1.1", datatype=XSD.string) - b22 = Literal(u'1', datatype=XSD.string) - b23 = None - - self.assertEqual(a + b01, Literal(2)) - self.assertEqual(a + b02, Literal(0)) - self.assertEqual(a + b03, Literal(round(Decimal(2.1), 15))) - self.assertEqual(a + b04, Literal(round(Decimal(-0.1), 15))) - self.assertEqual(a + b05, Literal(2)) - self.assertEqual(a + b06, Literal(0)) - self.assertEqual(a + b07, Literal(2)) - self.assertEqual(a + b08, Literal(round(Decimal(2.1), 15))) - self.assertEqual(a + b09, Literal(round(Decimal(-0.1), 15))) - self.assertEqual(a + b10, Literal(round(Decimal(2.1), 15))) - self.assertEqual(a + b11, Literal(round(Decimal(-0.1), 15))) - self.assertEqual(a + b12, Literal(round(Decimal(2.1), 15))) - self.assertEqual(a + b13, Literal(round(Decimal(-0.1), 15))) - self.assertEqual(a + b14, Literal(2)) - self.assertEqual(a + b15, Literal(0)) - self.assertEqual(a + b16, Literal(2)) - self.assertEqual(a + b17, Literal(round(Decimal(2.1), 15))) - self.assertEqual(a + b18, Literal(11, datatype=XSD.string)) - self.assertEqual(a + b19, Literal(11.1, datatype=XSD.string)) - self.assertEqual(a + b20, Literal(11, datatype=XSD.string)) - self.assertEqual(a + b21, Literal(11.1, datatype=XSD.string)) - self.assertEqual(a + b22, Literal(11, datatype=XSD.string)) - self.assertEqual(a + b23, Literal(1)) + # compares Python decimals + def isclose(a, b, rel_tol=1e-09, abs_tol=0.0): + a = float(a) + b = float(b) + return abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol) + + cases = [ + (1, Literal(1), Literal(1), Literal(2)), + (2, Literal(Decimal(1)), Literal(Decimal(1)), Literal(Decimal(2))), + (3, Literal(float(1)), Literal(float(1)), Literal(float(2))), + (4, Literal(1), Literal(1.1), Literal(2.1, datatype=XSD.decimal)), + (5, Literal(1.1), Literal(1.1), Literal(2.2)), + (6, Literal(Decimal(1)), Literal(Decimal(1.1)), Literal(Decimal(2.1), datatype=XSD.decimal)), + (7, Literal(Decimal(1.1)), Literal(Decimal(1.1)), Literal(Decimal(2.2))), + (8, Literal(float(1)), Literal(float(1.1)), Literal(float(2.1))), + (9, Literal(float(1.1)), Literal(float(1.1)), Literal(float(2.2))), + (10, Literal(-1), Literal(-1), Literal(-2)), + (12, Literal(Decimal(-1)), Literal(Decimal(-1)), Literal(Decimal(-2))), + (13, Literal(float(-1)), Literal(float(-1)), Literal(float(-2))), + (14, Literal(-1), Literal(-1.1), Literal(-2.1)), + (15, Literal(-1.1), Literal(-1.1), Literal(-2.2)), + (16, Literal(Decimal(-1)), Literal(Decimal(-1.1)), Literal(Decimal(-2.1))), + (17, Literal(Decimal(-1.1)), Literal(Decimal(-1.1)), Literal(Decimal(-2.2))), + (18, Literal(float(-1)), Literal(float(-1.1)), Literal(float(-2.1))), + (19, Literal(float(-1.1)), Literal(float(-1.1)), Literal(float(-2.2))), + + (20, Literal(1), Literal(1.0), Literal(2.0)), + (21, Literal(1.0), Literal(1.0), Literal(2.0)), + (22, Literal(Decimal(1)), Literal(Decimal(1.0)), Literal(Decimal(2.0))), + (23, Literal(Decimal(1.0)), Literal(Decimal(1.0)), Literal(Decimal(2.0))), + (24, Literal(float(1)), Literal(float(1.0)), Literal(float(2.0))), + (25, Literal(float(1.0)), Literal(float(1.0)), Literal(float(2.0))), + + (26, Literal(1, datatype=XSD.integer), Literal(1, datatype=XSD.integer), Literal(2, datatype=XSD.integer)), + (27, Literal(1, datatype=XSD.integer), Literal("1", datatype=XSD.integer), Literal("2", datatype=XSD.integer)), + (28, Literal("1", datatype=XSD.integer), Literal("1", datatype=XSD.integer), Literal("2", datatype=XSD.integer)), + (29, Literal("1"), Literal("1", datatype=XSD.integer), Literal("11", datatype=XSD.string)), + (30, Literal(1), Literal("1", datatype=XSD.integer), Literal("2", datatype=XSD.integer)), + (31, Literal(Decimal(1), datatype=XSD.decimal), Literal(Decimal(1), datatype=XSD.decimal), Literal(Decimal(2), datatype=XSD.decimal)), + (32, Literal(Decimal(1)), Literal(Decimal(1), datatype=XSD.decimal), Literal(Decimal(2), datatype=XSD.decimal)), + (33, Literal(float(1)), Literal(float(1), datatype=XSD.float), Literal(float(2), datatype=XSD.float)), + (34, Literal(float(1), datatype=XSD.float), Literal(float(1), datatype=XSD.float), Literal(float(2), datatype=XSD.float)), + + (35, Literal(1), 1, Literal(2)), + (36, Literal(1), 1.0, Literal(2, datatype=XSD.decimal)), + (37, Literal(1.0), 1, Literal(2, datatype=XSD.decimal)), + (38, Literal(1.0), 1.0, Literal(2.0)), + (39, Literal(Decimal(1.0)), Decimal(1), Literal(Decimal(2.0))), + (40, Literal(Decimal(1.0)), Decimal(1.0), Literal(Decimal(2.0))), + (41, Literal(float(1.0)), float(1), Literal(float(2.0))), + (42, Literal(float(1.0)), float(1.0), Literal(float(2.0))), + + (43, Literal(1, datatype=XSD.integer), Literal("+1.1", datatype=XSD.string), Literal("1+1.1", datatype=XSD.string)), + (44, Literal(1, datatype=XSD.integer), Literal(u"1", datatype=XSD.string), Literal("11", datatype=XSD.string)), + (45, Literal(1.1, datatype=XSD.integer), Literal("1", datatype=XSD.string), Literal("1.11", datatype=XSD.string)), + + (46, Literal(1, datatype=XSD.integer), None, Literal(1, datatype=XSD.integer)), + (47, Literal("1", datatype=XSD.string), None, Literal("1", datatype=XSD.string)), + ] + + for case in cases: + # see if the addition exactly matches the expected output + case_passed = (case[1] + case[2]) == (case[3]) + # see if the addition almost matches the expected output, for decimal precision errors + if not case_passed: + try: + case_passed = isclose((case[1] + case[2].value), case[3].value) + except: + pass + + self.assertTrue(case_passed, "Case " + str(case[0]) + " failed") class TestValidityFunctions(unittest.TestCase): -- cgit v1.2.1 From b53dec4bf68df6f0f75d0dfe116fa21fb593b252 Mon Sep 17 00:00:00 2001 From: Nicholas Car Date: Mon, 23 Mar 2020 12:55:17 +1000 Subject: added collection of string types for string concat, added more tests --- test/test_term.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'test') diff --git a/test/test_term.py b/test/test_term.py index 6b60a09f..4dea9c3c 100644 --- a/test/test_term.py +++ b/test/test_term.py @@ -178,12 +178,13 @@ class TestLiteral(unittest.TestCase): (41, Literal(float(1.0)), float(1), Literal(float(2.0))), (42, Literal(float(1.0)), float(1.0), Literal(float(2.0))), - (43, Literal(1, datatype=XSD.integer), Literal("+1.1", datatype=XSD.string), Literal("1+1.1", datatype=XSD.string)), - (44, Literal(1, datatype=XSD.integer), Literal(u"1", datatype=XSD.string), Literal("11", datatype=XSD.string)), - (45, Literal(1.1, datatype=XSD.integer), Literal("1", datatype=XSD.string), Literal("1.11", datatype=XSD.string)), + (43, Literal(1, datatype=XSD.integer), "+1.1", Literal("1+1.1", datatype=XSD.string)), + (44, Literal(1, datatype=XSD.integer), Literal("+1.1", datatype=XSD.string), Literal("1+1.1", datatype=XSD.string)), + (45, Literal(Decimal(1.0), datatype=XSD.integer), Literal(u"1", datatype=XSD.string), Literal("11", datatype=XSD.string)), + (46, Literal(1.1, datatype=XSD.integer), Literal("1", datatype=XSD.string), Literal("1.11", datatype=XSD.string)), - (46, Literal(1, datatype=XSD.integer), None, Literal(1, datatype=XSD.integer)), - (47, Literal("1", datatype=XSD.string), None, Literal("1", datatype=XSD.string)), + (47, Literal(1, datatype=XSD.integer), None, Literal(1, datatype=XSD.integer)), + (48, Literal("1", datatype=XSD.string), None, Literal("1", datatype=XSD.string)), ] for case in cases: @@ -196,6 +197,11 @@ class TestLiteral(unittest.TestCase): except: pass + if not case_passed: + print(case[1], case[2]) + print("expected: " + case[3] + ", " + case[3].datatype) + print("actual: " + (case[1] + case[2]) + ", " + (case[1] + case[2]).datatype) + self.assertTrue(case_passed, "Case " + str(case[0]) + " failed") -- cgit v1.2.1 From c90c0071b89cbf130bbd4fc17851ff96e3237bd5 Mon Sep 17 00:00:00 2001 From: arushi019 Date: Mon, 6 Apr 2020 20:33:49 +0530 Subject: Added tests for _parseBoolean function --- test/test_literal.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/test_literal.py b/test/test_literal.py index dae2d187..a5fcab23 100644 --- a/test/test_literal.py +++ b/test/test_literal.py @@ -1,7 +1,7 @@ import unittest import rdflib # needed for eval(repr(...)) below -from rdflib.term import Literal, URIRef, _XSD_DOUBLE, bind +from rdflib.term import Literal, URIRef, _XSD_DOUBLE, bind, XSD, _XSD_BOOLEAN from six import integer_types, PY3, string_types @@ -100,6 +100,29 @@ class TestDoubleOutput(unittest.TestCase): out = vv._literal_n3(use_plain=True) self.assertTrue(out in ["8.8e-01", "0.88"], out) +class TestParseBoolean(unittest.TestCase): + """confirms the fix for https://github.com/RDFLib/rdflib/issues/913""" + def testTrueBoolean(self): + test_value = Literal("tRue", datatype = _XSD_BOOLEAN) + self.assertTrue(test_value.value) + test_value = Literal("1",datatype = _XSD_BOOLEAN) + self.assertTrue(test_value.value) + + def testFalseBoolean(self): + test_value = Literal("falsE", datatype = _XSD_BOOLEAN) + self.assertFalse(test_value.value) + test_value = Literal("0",datatype = _XSD_BOOLEAN) + self.assertFalse(test_value.value) + + def testNonFalseBoolean(self): + test_value = Literal("abcd", datatype = _XSD_BOOLEAN) + self.assertWarns(DeprecationWarning) + self.assertFalse(test_value.value) + test_value = Literal("10",datatype = _XSD_BOOLEAN) + self.assertWarns(DeprecationWarning) + self.assertFalse(test_value.value) + + class TestBindings(unittest.TestCase): @@ -163,6 +186,7 @@ class TestBindings(unittest.TestCase): self.assertEqual(specific_l.toPython(), s) self.assertEqual(specific_l.datatype, datatype) + if __name__ == "__main__": unittest.main() -- cgit v1.2.1 From d18cf4eb31c32086ed7c25703b58335146d072e3 Mon Sep 17 00:00:00 2001 From: arushi019 Date: Mon, 6 Apr 2020 20:47:18 +0530 Subject: Rectified import error in last commit --- test/test_literal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/test_literal.py b/test/test_literal.py index a5fcab23..7b2a292a 100644 --- a/test/test_literal.py +++ b/test/test_literal.py @@ -1,7 +1,7 @@ import unittest import rdflib # needed for eval(repr(...)) below -from rdflib.term import Literal, URIRef, _XSD_DOUBLE, bind, XSD, _XSD_BOOLEAN +from rdflib.term import Literal, URIRef, _XSD_DOUBLE, bind, _XSD_BOOLEAN from six import integer_types, PY3, string_types -- cgit v1.2.1 From 7bc05b3da8c52a1e11b21a2ab2c12aab0c4e4eaa Mon Sep 17 00:00:00 2001 From: arushi019 Date: Mon, 6 Apr 2020 21:46:57 +0530 Subject: Change to allow for Python 2.7 in _parseBoolean test --- test/test_literal.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/test_literal.py b/test/test_literal.py index 7b2a292a..6862b8b4 100644 --- a/test/test_literal.py +++ b/test/test_literal.py @@ -116,10 +116,10 @@ class TestParseBoolean(unittest.TestCase): def testNonFalseBoolean(self): test_value = Literal("abcd", datatype = _XSD_BOOLEAN) - self.assertWarns(DeprecationWarning) + self.assertRaises(DeprecationWarning) self.assertFalse(test_value.value) test_value = Literal("10",datatype = _XSD_BOOLEAN) - self.assertWarns(DeprecationWarning) + self.assertRaises(DeprecationWarning) self.assertFalse(test_value.value) -- cgit v1.2.1 From f561d9d8048886571412d64f67d984739b11affb Mon Sep 17 00:00:00 2001 From: arushi019 Date: Mon, 6 Apr 2020 21:55:14 +0530 Subject: Removed extra white space lines --- test/test_literal.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'test') diff --git a/test/test_literal.py b/test/test_literal.py index 6862b8b4..39d40b12 100644 --- a/test/test_literal.py +++ b/test/test_literal.py @@ -186,7 +186,5 @@ class TestBindings(unittest.TestCase): self.assertEqual(specific_l.toPython(), s) self.assertEqual(specific_l.datatype, datatype) - - if __name__ == "__main__": unittest.main() -- cgit v1.2.1 From 2d4dfceff68878283ce667e433703f2f076ab4a1 Mon Sep 17 00:00:00 2001 From: Natanael Arndt Date: Mon, 6 Apr 2020 18:59:27 +0200 Subject: Readd one of the blank lines --- test/test_literal.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/test_literal.py b/test/test_literal.py index 39d40b12..3ed62d11 100644 --- a/test/test_literal.py +++ b/test/test_literal.py @@ -107,13 +107,13 @@ class TestParseBoolean(unittest.TestCase): self.assertTrue(test_value.value) test_value = Literal("1",datatype = _XSD_BOOLEAN) self.assertTrue(test_value.value) - + def testFalseBoolean(self): test_value = Literal("falsE", datatype = _XSD_BOOLEAN) self.assertFalse(test_value.value) test_value = Literal("0",datatype = _XSD_BOOLEAN) self.assertFalse(test_value.value) - + def testNonFalseBoolean(self): test_value = Literal("abcd", datatype = _XSD_BOOLEAN) self.assertRaises(DeprecationWarning) @@ -121,8 +121,8 @@ class TestParseBoolean(unittest.TestCase): test_value = Literal("10",datatype = _XSD_BOOLEAN) self.assertRaises(DeprecationWarning) self.assertFalse(test_value.value) - - + + class TestBindings(unittest.TestCase): @@ -186,5 +186,6 @@ class TestBindings(unittest.TestCase): self.assertEqual(specific_l.toPython(), s) self.assertEqual(specific_l.datatype, datatype) + if __name__ == "__main__": unittest.main() -- cgit v1.2.1 From d183e3bb6900f41c2591724c22bc09821d5c85c4 Mon Sep 17 00:00:00 2001 From: Boris Pelakh Date: Tue, 14 Apr 2020 07:42:16 -0400 Subject: Unit test. --- test/test_sparql_construct_bindings.py | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 test/test_sparql_construct_bindings.py (limited to 'test') diff --git a/test/test_sparql_construct_bindings.py b/test/test_sparql_construct_bindings.py new file mode 100644 index 00000000..5a102fd8 --- /dev/null +++ b/test/test_sparql_construct_bindings.py @@ -0,0 +1,39 @@ +from rdflib import Graph, URIRef, Literal, BNode +from rdflib.plugins.sparql import prepareQuery +from rdflib.compare import isomorphic + +import unittest + +class TestConstructInitBindings(unittest.TestCase): + + def test_construct_init_bindings(self): + """ + This is issue https://github.com/RDFLib/rdflib/issues/1001 + """ + + g1 = Graph() + + q_str = (""" + PREFIX : + CONSTRUCT { + ?uri :prop1 ?val1; + :prop2 ?c . + } + WHERE { + bind(uri(concat("urn:ns1:", ?a)) as ?uri) + bind(?b as ?val1) + } + """) + q_prepared = prepareQuery(q_str) + + expected = [ + (URIRef('urn:ns1:A'),URIRef('urn:ns1:prop1'), Literal('B')), + (URIRef('urn:ns1:A'),URIRef('urn:ns1:prop2'), Literal('C')) + ] + results = g1.query(q_prepared, initBindings={ + 'a': Literal('A'), + 'b': Literal('B'), + 'c': Literal('C') + }) + + self.assertCountEqual(list(results), expected) -- cgit v1.2.1 From 1dbe7acc945a545d3b18ec5025c19b26d1ed110f Mon Sep 17 00:00:00 2001 From: Boris Pelakh Date: Tue, 14 Apr 2020 10:16:54 -0400 Subject: Fix unit tests for python2 --- test/test_sparql_construct_bindings.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/test_sparql_construct_bindings.py b/test/test_sparql_construct_bindings.py index 5a102fd8..d5a68b94 100644 --- a/test/test_sparql_construct_bindings.py +++ b/test/test_sparql_construct_bindings.py @@ -3,6 +3,7 @@ from rdflib.plugins.sparql import prepareQuery from rdflib.compare import isomorphic import unittest +from nose.tools import eq_ class TestConstructInitBindings(unittest.TestCase): @@ -36,4 +37,4 @@ class TestConstructInitBindings(unittest.TestCase): 'c': Literal('C') }) - self.assertCountEqual(list(results), expected) + eq_(sorted(results, key=lambda x: str(x[1])), expected) -- cgit v1.2.1 From 528a75febfed19e23731a306d57aa97dce0fc3a8 Mon Sep 17 00:00:00 2001 From: Nicholas Car Date: Thu, 16 Apr 2020 14:57:47 +1000 Subject: improved handling of base, including at Graph() init --- test/test_issue1003.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 test/test_issue1003.py (limited to 'test') diff --git a/test/test_issue1003.py b/test/test_issue1003.py new file mode 100644 index 00000000..99953ba9 --- /dev/null +++ b/test/test_issue1003.py @@ -0,0 +1,58 @@ +from rdflib import Graph, Literal, Namespace, RDF, URIRef +from rdflib.namespace import SKOS, DCTERMS + +""" +Testing scenarios: + 1. no base set + 2. base set at graph creation + 3. base set at serialization + 4. base set at both graph creation & serialization, serialization overrides +""" + +# variables +base = Namespace("http://example.org/") +title = Literal("Title", lang="en") +description = Literal("Test Description", lang="en") +creator = URIRef("https://creator.com") +cs = URIRef("") + +# starting graph +g = Graph() +g.add((cs, RDF.type, SKOS.ConceptScheme)) +g.add((cs, DCTERMS.creator, creator)) +g.add((cs, DCTERMS.source, URIRef("nick"))) +g.bind("dct", DCTERMS) +g.bind("skos", SKOS) + + +# 1. no base set +g1 = Graph() +g1 += g +# @base should not be in output +assert "@base" not in g.serialize(format='turtle').decode("utf-8") + + +# 2. base set at graph creation +g2 = Graph(base=base) +g2 += g +# @base should be in output +assert "@base" in g2.serialize(format='turtle').decode("utf-8") + + +# 3. base set at serialization +g3 = Graph() +g3 += g +# @base should be in output +assert "@base" in g3.serialize(format='turtle', base=base).decode("utf-8") + + +# 4. base set at both graph creation & serialization, serialization overrides +g4 = Graph(base=Namespace("http://nothing.com/")) +g4 += g + +# @base should be in output and it should be http://example.org/ (graph init copy) +assert "@base " in g4.serialize(format='turtle').decode("utf-8") +# @base should be in output and it should be http://example.org/, not http://nothing.com/ (serialization overwritten) +assert "@base " in g4.serialize(format='turtle', base=base).decode("utf-8") +# just checking that the graph init base isn't sneakily in output +assert "@base " not in g4.serialize(format='turtle', base=base).decode("utf-8") -- cgit v1.2.1 From 56d0d74c6becdfa22a193c98293314bd6ee9dde1 Mon Sep 17 00:00:00 2001 From: Nicholas Car Date: Thu, 16 Apr 2020 23:19:22 +1000 Subject: inverted precedence of base: graph wins over adding base to serialize(). Added RDF/XML, TriX & 1/2 TriG (incomplete) --- test/test_issue1003.py | 95 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 77 insertions(+), 18 deletions(-) (limited to 'test') diff --git a/test/test_issue1003.py b/test/test_issue1003.py index 99953ba9..883022a7 100644 --- a/test/test_issue1003.py +++ b/test/test_issue1003.py @@ -1,4 +1,4 @@ -from rdflib import Graph, Literal, Namespace, RDF, URIRef +from rdflib import Graph, Dataset, Literal, Namespace, RDF, URIRef from rdflib.namespace import SKOS, DCTERMS """ @@ -7,10 +7,15 @@ Testing scenarios: 2. base set at graph creation 3. base set at serialization 4. base set at both graph creation & serialization, serialization overrides + 5. multiple serialization side effect checking + 6. checking results for RDF/XML + 7. checking results for N3 + 8. checking results for TriX & TriG """ # variables -base = Namespace("http://example.org/") +base_one = Namespace("http://one.org/") +base_two = Namespace("http://two.org/") title = Literal("Title", lang="en") description = Literal("Test Description", lang="en") creator = URIRef("https://creator.com") @@ -25,34 +30,88 @@ g.bind("dct", DCTERMS) g.bind("skos", SKOS) -# 1. no base set +# 1. no base set for graph, no base set for serialization g1 = Graph() g1 += g # @base should not be in output assert "@base" not in g.serialize(format='turtle').decode("utf-8") -# 2. base set at graph creation -g2 = Graph(base=base) +# 2. base one set for graph, no base set for serialization +g2 = Graph(base=base_one) g2 += g -# @base should be in output -assert "@base" in g2.serialize(format='turtle').decode("utf-8") +# @base should be in output, from Graph (one) +assert "@base ." in g2.serialize(format='turtle').decode("utf-8") -# 3. base set at serialization +# 3. no base set for graph, base two set for serialization g3 = Graph() g3 += g -# @base should be in output -assert "@base" in g3.serialize(format='turtle', base=base).decode("utf-8") +# @base should be in output, from serialization (two) +assert "@base ." in g3.serialize(format='turtle', base=base_two).decode("utf-8") -# 4. base set at both graph creation & serialization, serialization overrides -g4 = Graph(base=Namespace("http://nothing.com/")) +# 4. base one set for graph, base two set for serialization, Graph one overrides +g4 = Graph(base=base_one) g4 += g +# @base should be in output, from graph (one) +assert "@base ." in g4.serialize(format='turtle', base=base_two).decode("utf-8") +# just checking that the serialization setting (two) hasn't snuck through +assert "@base ." not in g4.serialize(format='turtle', base=base_two).decode("utf-8") -# @base should be in output and it should be http://example.org/ (graph init copy) -assert "@base " in g4.serialize(format='turtle').decode("utf-8") -# @base should be in output and it should be http://example.org/, not http://nothing.com/ (serialization overwritten) -assert "@base " in g4.serialize(format='turtle', base=base).decode("utf-8") -# just checking that the graph init base isn't sneakily in output -assert "@base " not in g4.serialize(format='turtle', base=base).decode("utf-8") + +# 5. multiple serialization side effect checking +g5 = Graph() +g5 += g +# @base should be in output, from serialization (two) +assert "@base ." in g5.serialize(format='turtle', base=base_two).decode("utf-8") + +# checking for side affects - no base now set for this serialization +# @base should not be in output +assert "@base" not in g5.serialize(format='turtle').decode("utf-8") + + +# 6. checking results for RDF/XML +g6 = Graph() +g6 += g +g6.bind("dct", DCTERMS) +g6.bind("skos", SKOS) +assert "@xml:base" not in g6.serialize(format='xml').decode("utf-8") +assert 'xml:base="http://one.org/"' in g6.serialize(format='xml', base=base_one).decode("utf-8") +g6.base = base_two +assert 'xml:base="http://two.org/"' in g6.serialize(format='xml').decode("utf-8") +assert 'xml:base="http://two.org/"' in g6.serialize(format='xml', base=base_one).decode("utf-8") + +# 7. checking results for N3 +g7 = Graph() +g7 += g +g7.bind("dct", DCTERMS) +g7.bind("skos", SKOS) +assert "@xml:base" not in g7.serialize(format='xml').decode("utf-8") +assert "@base ." in g7.serialize(format='n3', base=base_one).decode("utf-8") +g7.base = base_two +assert "@base ." in g7.serialize(format='n3').decode("utf-8") +assert "@base ." in g7.serialize(format='n3', base=base_one).decode("utf-8") + +# 8. checking results for TriX & TriG +# TriX can specify a base per graph but setting a base for the whole +base_three = Namespace("http://three.org/") +ds1 = Dataset() +ds1.bind("dct", DCTERMS) +ds1.bind("skos", SKOS) +g8 = ds1.graph(URIRef('http://g8.com/'), base=base_one) +g9 = ds1.graph(URIRef('http://g9.com/')) +g8 += g +g9 += g +g9.base = base_two +ds1.base = base_three + +trix = ds1.serialize(format='trix', base=Namespace("http://two.org/")).decode("utf-8") +assert '' in trix +assert '' in trix +assert ' .' not in trig +assert '@base .' not in trig +assert '@base .' in trig -- cgit v1.2.1 From 308abf72f7fb6e8b198c8be8d2683806f5f7a6cd Mon Sep 17 00:00:00 2001 From: Natanael Arndt Date: Fri, 17 Apr 2020 13:38:38 +0200 Subject: Let argument of serialize method overwrite graph settings --- test/test_issue1003.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'test') diff --git a/test/test_issue1003.py b/test/test_issue1003.py index 883022a7..fdc56c82 100644 --- a/test/test_issue1003.py +++ b/test/test_issue1003.py @@ -55,9 +55,9 @@ assert "@base ." in g3.serialize(format='turtle', base=base_tw g4 = Graph(base=base_one) g4 += g # @base should be in output, from graph (one) -assert "@base ." in g4.serialize(format='turtle', base=base_two).decode("utf-8") +assert "@base ." in g4.serialize(format='turtle', base=base_two).decode("utf-8") # just checking that the serialization setting (two) hasn't snuck through -assert "@base ." not in g4.serialize(format='turtle', base=base_two).decode("utf-8") +assert "@base ." not in g4.serialize(format='turtle', base=base_two).decode("utf-8") # 5. multiple serialization side effect checking @@ -80,7 +80,7 @@ assert "@xml:base" not in g6.serialize(format='xml').decode("utf-8") assert 'xml:base="http://one.org/"' in g6.serialize(format='xml', base=base_one).decode("utf-8") g6.base = base_two assert 'xml:base="http://two.org/"' in g6.serialize(format='xml').decode("utf-8") -assert 'xml:base="http://two.org/"' in g6.serialize(format='xml', base=base_one).decode("utf-8") +assert 'xml:base="http://one.org/"' in g6.serialize(format='xml', base=base_one).decode("utf-8") # 7. checking results for N3 g7 = Graph() @@ -91,7 +91,7 @@ assert "@xml:base" not in g7.serialize(format='xml').decode("utf-8") assert "@base ." in g7.serialize(format='n3', base=base_one).decode("utf-8") g7.base = base_two assert "@base ." in g7.serialize(format='n3').decode("utf-8") -assert "@base ." in g7.serialize(format='n3', base=base_one).decode("utf-8") +assert "@base ." in g7.serialize(format='n3', base=base_one).decode("utf-8") # 8. checking results for TriX & TriG # TriX can specify a base per graph but setting a base for the whole @@ -109,9 +109,9 @@ ds1.base = base_three trix = ds1.serialize(format='trix', base=Namespace("http://two.org/")).decode("utf-8") assert '' in trix assert '' in trix -assert ' .' not in trig -assert '@base .' not in trig -assert '@base .' in trig +assert '@base .' not in trig +assert '@base .' in trig -- cgit v1.2.1 From 2d0c8055a3888a6ecadbac7276e1564562b813c1 Mon Sep 17 00:00:00 2001 From: Natanael Arndt Date: Fri, 17 Apr 2020 19:16:45 +0200 Subject: Add test for exponent floats with leading dot --- test/test_n3.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'test') diff --git a/test/test_n3.py b/test/test_n3.py index 5d447732..250ef086 100644 --- a/test/test_n3.py +++ b/test/test_n3.py @@ -164,6 +164,31 @@ foo-bar:Ex foo-bar:name "Test" . """ g = Graph() g.parse("test/n3/issue156.n3", format="n3") + def testIssue999(self): + """ + Make sure the n3 parser does recognize exponent and leading dot in ".171e-11" + """ + data = """ +@prefix rdfs: . + + + a ; + .171e-11 ; + 0e+00 ; + "0.001-fold of the SI base unit metre divided by the unit year" ; + ; + "0112/2///62720#UAA868" ; + "H66" ; + rdfs:isDefinedBy ; + rdfs:isDefinedBy ; + rdfs:label "MilliM PER YR" ; + "millimetre per year" ; +. + """ + g = Graph() + g.parse(data=data, format="n3") + g.parse(data=data, format="turtle") + def testDotInPrefix(self): g = Graph() g.parse( -- cgit v1.2.1 From 545c9da3834dadaa2e1580bf8fdb5494436e25f2 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Fri, 17 Apr 2020 15:56:20 -0400 Subject: TEST: Stress test exponent regex --- test/test_n3.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/test_n3.py b/test/test_n3.py index 250ef086..354bb073 100644 --- a/test/test_n3.py +++ b/test/test_n3.py @@ -1,7 +1,8 @@ from rdflib.graph import Graph, ConjunctiveGraph import unittest from rdflib.term import Literal, URIRef -from rdflib.plugins.parsers.notation3 import BadSyntax +from rdflib.plugins.parsers.notation3 import BadSyntax, exponent_syntax +import itertools from six import b from six.moves.urllib.error import URLError @@ -251,5 +252,24 @@ foo-bar:Ex foo-bar:name "Test" . """ g2), 'Document with declared empty prefix must match default #' +class TestRegularExpressions(unittest.TestCase): + def testExponents(self): + signs = ("", "+", "-") + mantissas = ("1", "1.", ".1", + "12", "12.", "1.2", ".12", + "123", "123.", "12.3", "1.23", ".123") + es = "eE" + exps = ("1", "12", "+1", "-1", "+12", "-12") + for parts in itertools.product(signs, mantissas, es, exps): + expstring = "".join(parts) + self.assertRegex(expstring, exponent_syntax) + + def testInvalidExponents(self): + # Add test cases as needed + invalid = (".e1",) + for expstring in invalid: + self.assertNotRegex(expstring, exponent_syntax) + + if __name__ == '__main__': unittest.main() -- cgit v1.2.1 From 5b1a9d09f866ead87073deb4c8d0ba9f3aaccf8e Mon Sep 17 00:00:00 2001 From: Natanael Arndt Date: Fri, 17 Apr 2020 23:10:56 +0200 Subject: Fix for python 2.7 --- test/test_n3.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/test_n3.py b/test/test_n3.py index 354bb073..5cdc74b5 100644 --- a/test/test_n3.py +++ b/test/test_n3.py @@ -262,13 +262,13 @@ class TestRegularExpressions(unittest.TestCase): exps = ("1", "12", "+1", "-1", "+12", "-12") for parts in itertools.product(signs, mantissas, es, exps): expstring = "".join(parts) - self.assertRegex(expstring, exponent_syntax) + self.assertTrue(exponent_syntax.match(expstring)) def testInvalidExponents(self): # Add test cases as needed invalid = (".e1",) for expstring in invalid: - self.assertNotRegex(expstring, exponent_syntax) + self.assertFalse(exponent_syntax.match(expstring)) if __name__ == '__main__': -- cgit v1.2.1 From 0986737fc8170d3cd3a7c95d078fa8f689267355 Mon Sep 17 00:00:00 2001 From: Natanael Arndt Date: Sat, 2 May 2020 00:02:34 +0200 Subject: Add test for Content-Type: application/sparql-update --- test/test_sparqlstore.py | 78 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/test_sparqlstore.py b/test/test_sparqlstore.py index 26a69460..a0a93b57 100644 --- a/test/test_sparqlstore.py +++ b/test/test_sparqlstore.py @@ -4,7 +4,10 @@ import os import unittest from nose import SkipTest from requests import HTTPError - +from http.server import BaseHTTPRequestHandler, HTTPServer +import socket +from threading import Thread +import requests try: assert len(urlopen("http://dbpedia.org/sparql").read()) > 0 @@ -67,5 +70,78 @@ class SPARQLStoreDBPediaTestCase(unittest.TestCase): assert type(i[0]) == Literal, i[0].n3() +class SPARQLStoreUpdateTestCase(unittest.TestCase): + def setUp(self): + port = self.setup_mocked_endpoint() + self.graph = Graph(store="SPARQLUpdateStore", identifier=URIRef("urn:ex")) + self.graph.open(("http://localhost:{port}/query".format(port=port), + "http://localhost:{port}/update".format(port=port)), create=False) + ns = list(self.graph.namespaces()) + assert len(ns) > 0, ns + + def setup_mocked_endpoint(self): + # Configure mock server. + s = socket.socket(socket.AF_INET, type=socket.SOCK_STREAM) + s.bind(('localhost', 0)) + address, port = s.getsockname() + s.close() + mock_server = HTTPServer(('localhost', port), SPARQL11ProtocolStoreMock) + + # Start running mock server in a separate thread. + # Daemon threads automatically shut down when the main process exits. + mock_server_thread = Thread(target=mock_server.serve_forever) + mock_server_thread.setDaemon(True) + mock_server_thread.start() + print("Started mocked sparql endpoint on http://localhost:{port}/".format(port=port)) + return port + + def tearDown(self): + self.graph.close() + + def test_Query(self): + query = "insert data { }" + res = self.graph.update(query) + print(res) + + +class SPARQL11ProtocolStoreMock(BaseHTTPRequestHandler): + def do_POST(self): + """ + If the body should be analysed as well, just use: + ``` + body = self.rfile.read(int(self.headers['Content-Length'])).decode() + print(body) + ``` + """ + contenttype = self.headers.get("Content-Type") + if self.path == "/query": + if self.headers.get("Content-Type") == "application/sparql-query": + pass + elif self.headers.get("Content-Type") == "application/x-www-form-urlencoded": + pass + else: + self.send_response(requests.codes.not_acceptable) + self.end_headers() + elif self.path == "/update": + if self.headers.get("Content-Type") == "application/sparql-update": + pass + elif self.headers.get("Content-Type") == "application/x-www-form-urlencoded": + pass + else: + self.send_response(requests.codes.not_acceptable) + self.end_headers() + else: + self.send_response(requests.codes.not_found) + self.end_headers() + self.send_response(requests.codes.ok) + self.end_headers() + return + + def do_GET(self): + # Process an HTTP GET request and return a response with an HTTP 200 status. + self.send_response(requests.codes.ok) + self.end_headers() + return + if __name__ == '__main__': unittest.main() -- cgit v1.2.1