Skip to content

Commit

Permalink
Wrap the arguments to in, nin, and __eq__ query expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Jenkins committed Nov 9, 2010
1 parent 66b1f0b commit 77f3cef
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions mongoalchemy/query.py
Expand Up @@ -277,7 +277,7 @@ def in_(self, qfield, *values):
understands
'''
# TODO: make sure that this field represents a list
self.filter(QueryExpression({ str(qfield) : { '$in' : values}}))
self.filter(QueryExpression({ str(qfield) : { '$in' : [qfield.get_type().wrap(value) for value in values]}}))
return self

def nin(self, qfield, *values):
Expand All @@ -288,7 +288,7 @@ def nin(self, qfield, *values):
understands
'''
# TODO: make sure that this field represents a list
self.filter(QueryExpression({ str(qfield) : { '$nin' : values}}))
self.filter(QueryExpression({ str(qfield) : { '$nin' : [qfield.get_type().wrap(value) for value in values]}}))
return self


Expand Down
6 changes: 3 additions & 3 deletions mongoalchemy/query_expression.py
Expand Up @@ -90,15 +90,15 @@ def in_(self, *values):
in ``values``. Produces a MongoDB ``$in`` expression.
'''
return QueryExpression({
str(self) : { '$in' : values }
str(self) : { '$in' : [self.__type.wrap(value) for value in values] }
})

def nin(self, *values):
''' A query to check if this query field is not one of the values
in ``values``. Produces a MongoDB ``$nin`` expression.
'''
return QueryExpression({
str(self) : { '$nin' : values }
str(self) : { '$nin' : [self.__type.wrap(value) for value in values] }
})

def __str__(self):
Expand All @@ -113,7 +113,7 @@ def eq_(self, value):
'''
if not self.__type.is_valid_wrap(value):
raise BadQueryException('Invalid "value" for comparison against %s: %s' % (str(self), value))
return QueryExpression({ self.__absolute_name() : value })
return QueryExpression({ self.__absolute_name() : self.__type.wrap(value) })

def __lt__(self, value):
return self.lt_(value)
Expand Down
11 changes: 7 additions & 4 deletions test/test_query_expressions.py
Expand Up @@ -5,6 +5,9 @@
from mongoalchemy.query import BadQueryException, Query, BadResultException
from test.util import known_failure


# TODO: Test al operators to make sure wrap is called on their values

class T(Document):
i = IntField()
j = IntField(required=False)
Expand Down Expand Up @@ -83,11 +86,11 @@ def test_or():

def test_in():
q = Query(T, None)
assert q.in_(T.f.i, 1, 2, 3).query == {'i' : {'$in' : (1,2,3)}}, q.in_(T.f.i, 1, 2, 3).query
assert q.filter(T.f.i.in_(1, 2, 3)).query == {'i' : {'$in' : (1,2,3)}}
assert q.in_(T.f.i, 1, 2, 3).query == {'i' : {'$in' : [1,2,3]}}, q.in_(T.f.i, 1, 2, 3).query
assert q.filter(T.f.i.in_(1, 2, 3)).query == {'i' : {'$in' : [1,2,3]}}

def test_nin():
q = Query(T, None)
assert q.nin(T.f.i, 1, 2, 3).query == {'i' : {'$nin' : (1,2,3)}}, q.nin(T.f.i, 1, 2, 3).query
assert q.filter(T.f.i.nin(1, 2, 3)).query == {'i' : {'$nin' : (1,2,3)}}
assert q.nin(T.f.i, 1, 2, 3).query == {'i' : {'$nin' : [1,2,3]}}, q.nin(T.f.i, 1, 2, 3).query
assert q.filter(T.f.i.nin(1, 2, 3)).query == {'i' : {'$nin' : [1,2,3]}}

0 comments on commit 77f3cef

Please sign in to comment.