Commits

Jean-Tiare Le Bigot  committed 99a9da0

add query comparison operators + regular tests

  • Participants
  • Parent commits 921dc87

Comments (0)

Files changed (3)

File ddbmock/database/comparison.py

+# -*- coding: utf-8 -*-
+
+from decimal import Decimal
+
+# Types and syntax are already checked
+# syntax is expected allright cf input validators
+
+def _coerce(typename, value):
+    if typename == 'S' or typename == 'B':
+        return value
+    if typename == 'SS' or typename == 'BS':
+        return set(value)
+    if typename == 'N':
+        return Decimal(value)
+    if typename == 'NS':
+        return set(map(value, Decimal))
+
+def _parse_elem(elem):
+    typename, value = elem.items()[0]
+    value = _coerce(typename, value)
+    return typename, value
+
+#Types restrictions are performed by the input validators
+
+TYPEMSG2 = "Types {} and {} are not copatible in test {}"
+TYPEMSG3 = "Types {}, {} and {} are not copatible in test {}"
+
+def eq(target, rule):
+    itype, ivalue = _parse_elem(target)
+    rtype, rvalue = _parse_elem(rule)
+    if itype != rtype: raise TypeError(TYPEMSG2.format(itype, rtype, __name__))
+    return ivalue == rvalue
+
+def le(target, rule):
+    itype, ivalue = _parse_elem(target)
+    rtype, rvalue = _parse_elem(rule)
+    if itype != rtype: raise TypeError(TYPEMSG2.format(itype, rtype, __name__))
+    return ivalue <= rvalue
+
+def lt(target, rule):
+    itype, ivalue = _parse_elem(target)
+    rtype, rvalue = _parse_elem(rule)
+    if itype != rtype: raise TypeError(TYPEMSG2.format(itype, rtype, __name__))
+    return ivalue < rvalue
+
+def ge(target, rule):
+    itype, ivalue = _parse_elem(target)
+    rtype, rvalue = _parse_elem(rule)
+    if itype != rtype: raise TypeError(TYPEMSG2.format(itype, rtype, __name__))
+    return ivalue >= rvalue
+
+def gt(target, rule):
+    itype, ivalue = _parse_elem(target)
+    rtype, rvalue = _parse_elem(rule)
+    if itype != rtype: raise TypeError(TYPEMSG2.format(itype, rtype, __name__))
+    return ivalue > rvalue
+
+def begins_with(target, rule):
+    itype, ivalue = _parse_elem(target)
+    rtype, rvalue = _parse_elem(rule)
+    if itype != rtype: raise TypeError(TYPEMSG2.format(itype, rtype, __name__))
+    return ivalue.startswith(rvalue)
+
+def between(target, rule1, rule2):
+    itype, ivalue = _parse_elem(target)
+    rtype1, rvalue1 = _parse_elem(rule1)
+    rtype2, rvalue2 = _parse_elem(rule2)
+    if not (itype == rtype2 == rtype2):
+        raise TypeError(TYPEMSG3.format(itype, rtype1, rtype2, __name__))
+    return ivalue >= rvalue1 and ivalue <= rvalue2

File ddbmock/tests/unit/__init__.py

+# -*- coding: utf-8 -*-

File ddbmock/tests/unit/test_item_field_comparison.py

+# -*- coding: utf-8 -*-
+
+import unittest
+
+
+class TestItemFieldComparison(unittest.TestCase):
+    def test_eq(self):
+        from ddbmock.database.comparison import eq
+
+        self.assertTrue(eq({u'S': u'waldo'}, {u'S': u'waldo'}))
+        self.assertFalse(eq({u'S': u'waldo'}, {u'S': u'on-time'}))
+
+    def test_le(self):
+        from ddbmock.database.comparison import le
+
+        self.assertTrue(le({u'S': u'waldo'}, {u'S': u'waldo'}))
+        self.assertTrue(le({u'S': u'waldo'}, {u'S': u'waldo1'}))
+        self.assertFalse(le({u'S': u'waldo'}, {u'S': u'wald'}))
+
+        self.assertTrue(le({u'N': u'42'}, {u'N': u'42'}))
+        self.assertTrue(le({u'N': u'42'}, {u'N': u'42.001'}))
+        self.assertFalse(le({u'N': u'42'}, {u'N': u'7'}))
+
+    def test_lt(self):
+        from ddbmock.database.comparison import lt
+
+        self.assertFalse(lt({u'S': u'waldo'}, {u'S': u'waldo'}))
+        self.assertTrue(lt({u'S': u'waldo'}, {u'S': u'waldo1'}))
+        self.assertFalse(lt({u'S': u'waldo'}, {u'S': u'wald'}))
+
+        self.assertFalse(lt({u'N': u'42'}, {u'N': u'42'}))
+        self.assertTrue(lt({u'N': u'42'}, {u'N': u'42.001'}))
+        self.assertFalse(lt({u'N': u'42'}, {u'N': u'7'}))
+
+    def test_ge(self):
+        from ddbmock.database.comparison import ge
+
+        self.assertTrue(ge({u'S': u'waldo'}, {u'S': u'waldo'}))
+        self.assertFalse(ge({u'S': u'waldo'}, {u'S': u'waldo1'}))
+        self.assertTrue(ge({u'S': u'waldo'}, {u'S': u'wald'}))
+
+        self.assertTrue(ge({u'N': u'42'}, {u'N': u'42'}))
+        self.assertFalse(ge({u'N': u'42'}, {u'N': u'42.001'}))
+        self.assertTrue(ge({u'N': u'42'}, {u'N': u'7'}))
+
+    def test_gt(self):
+        from ddbmock.database.comparison import gt
+
+        self.assertFalse(gt({u'S': u'waldo'}, {u'S': u'waldo'}))
+        self.assertFalse(gt({u'S': u'waldo'}, {u'S': u'waldo1'}))
+        self.assertTrue(gt({u'S': u'waldo'}, {u'S': u'wald'}))
+
+        self.assertFalse(gt({u'N': u'42'}, {u'N': u'42'}))
+        self.assertFalse(gt({u'N': u'42'}, {u'N': u'42.001'}))
+        self.assertTrue(gt({u'N': u'42'}, {u'N': u'7'}))
+
+    def test_begins_with(self):
+        from ddbmock.database.comparison import begins_with
+
+        self.assertTrue(begins_with({u'S': u'waldo'}, {u'S': u'waldo'}))
+        self.assertTrue(begins_with({u'S': u'waldo'}, {u'S': u'wal'}))
+        self.assertFalse(begins_with({u'S': u'waldo'}, {u'S': u'waldo-mario'}))
+
+    def test_between(self):
+        from ddbmock.database.comparison import between
+
+        self.assertTrue(between({u'S': u'waldo'}, {u'S': u'wal-mart'}, {u'S': u'walee'}))
+        self.assertFalse(between({u'S': u'waldo'}, {u'S': u'wale'}, {u'S': u'walee'}))
+        self.assertTrue(between({u'N': u'42'}, {u'N': u'7'}, {u'N': u'123'}))
+        self.assertTrue(between({u'N': u'42'}, {u'N': u'42'}, {u'N': u'42'}))
+        self.assertFalse(between({u'N': u'42'}, {u'N': u'43'}, {u'N': u'123'}))