Commits

Anonymous committed b61f2ed

[soc2010/query-refactor] Clean up the implementation of lookup_type, added more tests, and killed code that wasn't tested.

Comments (0)

Files changed (2)

django/contrib/mongodb/compiler.py

 
 # TODO: ...
 class SQLCompiler(object):
+    LOOKUP_TYPES = {
+        "exact": lambda params: params[0],
+        "lt": lambda params: {"$lt": params[0]},
+        "isnull": lambda params: params[0]
+    }
+    
     def __init__(self, query, connection, using):
         self.query = query
         self.connection = connection
             if isinstance(child, self.query.where_class):
                 child_filters = self.get_filters(child)
                 for k, v in child_filters.iteritems():
-                    if k in filters:
-                        v = {"$and": [filters[k], v]}
+                    assert k not in filters
                     if where.negated:
                         filters.update(self.negate(k, v))
                     else:
         return filters
     
     def make_atom(self, lhs, lookup_type, value_annotation, params_or_value, negated):
-        assert lookup_type in ["exact", "isnull", "lt"], lookup_type
+        assert lookup_type in self.LOOKUP_TYPES, lookup_type
         if hasattr(lhs, "process"):
             lhs, params = lhs.process(lookup_type, params_or_value, self.connection)
         else:
         if column == self.query.model._meta.pk.column:
             column = "_id"
         
-        if lookup_type == "exact":
-            val = params[0]
-            if negated:
-                val = {"$ne": val}
-            return column, val
-        elif lookup_type == "isnull":
-            val = None
-            if value_annotation == negated:
-                val = {"$not": val}
-            return column, val
-        elif lookup_type == "lt":
-            if negated:
-                return {"$gte": params[0]}
-            return column, {"$lt": params[0]}
+        return column, self.LOOKUP_TYPES[lookup_type](params)
     
     def negate(self, k, v):
         if isinstance(v, dict):
-            if v.keys() == ["$not"]:
-                return {k: v["$not"]}
             return {k: {"$not": v}}
         return {k: {"$ne": v}}
     

tests/regressiontests/mongodb/tests.py

         self.assertFalse(hasattr(b, "_current_group_cache"))
         self.assertEqual(b.current_group, e)
     
+    def test_exists(self):
+        self.assertFalse(Artist.objects.filter(name="Brian May").exists())
+        Artist.objects.create(name="Brian May")
+        self.assertTrue(Artist.objects.filter(name="Brian May").exists())
+    
     def test_not_equals(self):
         q = Group.objects.create(name="Queen", year_formed=1971)
         e = Group.objects.create(name="The E Street Band", year_formed=1972)
+        b = Group.objects.create(name="The Beetles")
         
         self.assertQuerysetEqual(
             Group.objects.exclude(year_formed=1972), [
                 "Queen",
+                "The Beetles",
             ],
             lambda g: g.name,
         )