Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

The statement generated by utils.dictToWhere is not suitable for None value #49

Closed
g1itch opened this issue Aug 22, 2014 · 2 comments
Closed

Comments

@g1itch
Copy link

g1itch commented Aug 22, 2014

The statement should be IS NULL for None value, not = NULL. As a result BDObject.findBy() returns [] in that case. The following test succeeds if name is a string:

diff --git a/twistar/tests/test_dbobject.py b/twistar/tests/test_dbobject.py
index 6611e90..d7001d9 100644
--- a/twistar/tests/test_dbobject.py
+++ b/twistar/tests/test_dbobject.py
@@ -84,6 +84,12 @@ class DBObjectTest(unittest.TestCase):
         resultids = [result.id for result in results]
         self.assertEqual(ids, resultids)

+    @inlineCallbacks
+    def test_null(self):
+        name = None
+        user = yield User(first_name=name).save()
+        users = yield User.findBy(first_name=name)
+        self.assertEqual(user, users[0])

     @inlineCallbacks
     def test_count(self):
@g1itch
Copy link
Author

g1itch commented Aug 22, 2014

I think a solution may be like this

diff --git a/twistar/utils.py b/twistar/utils.py
index 91e7ee1..8b5c1f1 100644
--- a/twistar/utils.py
+++ b/twistar/utils.py
@@ -67,7 +67,8 @@ def dictToWhere(attrs, joiner="AND"):
     if len(attrs) == 0:
         return None

-    wheres = map(lambda name: "(%s = ?)" % name, attrs.keys())
+    wheres = ["(%s = ?)" % key if val else "(%s IS ?)" % key
+              for key, val in attrs.iteritems()]
     return [(" %s " % joiner).join(wheres)] + attrs.values()

@bmuller
Copy link
Owner

bmuller commented Aug 22, 2014

Great catch! Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants