mirror of
https://github.com/django/django.git
synced 2025-07-04 17:59:13 +00:00
[soc2010/query-refactor] Clean up the implementation of lookup_type, added more tests, and killed code that wasn't tested.
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/query-refactor@13369 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
706b18966f
commit
bf071f7c0c
@ -3,6 +3,12 @@ from django.db.models.sql.datastructures import FullResultSet
|
||||
|
||||
# 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
|
||||
@ -15,8 +21,7 @@ class SQLCompiler(object):
|
||||
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:
|
||||
@ -30,7 +35,7 @@ class SQLCompiler(object):
|
||||
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:
|
||||
@ -42,25 +47,10 @@ class SQLCompiler(object):
|
||||
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}}
|
||||
|
||||
|
@ -58,13 +58,20 @@ class MongoTestCase(TestCase):
|
||||
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,
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user