changeset 131:a47c8224e97f

raise if we see xsd:float or xsd:double, since i want to be using xsd:decimal everywhere. This revealed a bug where adds/dels iterables got consumed(!)
author drewp@bigasterisk.com
date Mon, 29 May 2023 16:15:46 -0700
parents d195a5f50137
children 453726e6f891
files rdfdb/patch.py rdfdb/patch_test.py
diffstat 2 files changed, 17 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/rdfdb/patch.py	Mon May 29 16:14:17 2023 -0700
+++ b/rdfdb/patch.py	Mon May 29 16:15:46 2023 -0700
@@ -1,5 +1,6 @@
+import itertools
 import json
-from typing import Iterable, Optional, Tuple
+from typing import Iterable, Optional, Tuple, cast
 
 from rdflib import ConjunctiveGraph, Graph, Literal, Namespace, URIRef
 
@@ -50,7 +51,8 @@
         """
         assert jsonRepr is None or isinstance(jsonRepr, str), repr(jsonRepr)
         self._jsonRepr = jsonRepr
-        self._addQuads, self._delQuads = addQuads, delQuads
+        self._addQuads = list(addQuads) if addQuads is not None else None
+        self._delQuads = list(delQuads) if delQuads is not None else None
         self._addGraph, self._delGraph = addGraph, delGraph
 
         if self._jsonRepr is not None:
@@ -60,6 +62,14 @@
             if 'senderUpdateUri' in body:
                 self.senderUpdateUri = URIRef(body['senderUpdateUri'])
 
+        self._validateDecimals()
+
+    def _validateDecimals(self):
+        for q in itertools.chain(self.addQuads, self.delQuads):
+            if isinstance(q[2], Literal):
+                if cast(Literal, q[2]).datatype in [XSD['double'], XSD['float']]:
+                    raise ValueError(f'fix creation of quad {q} to use xsd:decimal, or comparisons will fail')
+
     def __str__(self):
 
         def shorten(n):
--- a/rdfdb/patch_test.py	Mon May 29 16:14:17 2023 -0700
+++ b/rdfdb/patch_test.py	Mon May 29 16:15:46 2023 -0700
@@ -56,3 +56,8 @@
     def testMultiContextPatchFailsToReturnContext(self):
         p = Patch(addQuads=[stmt1[:3] + (U('http://ctx1'),), stmt1[:3] + (U('http://ctx2'),)])
         self.assertRaises(ValueError, p.getContext)
+
+class TestPatchDoesntConsumeIterators:
+    def test(self):
+        p = Patch(addQuads=iter(stmt1))
+        assert not p.isEmpty()
\ No newline at end of file