예제 #1
0
def test_or():
    q = Query(T, None)

    want = { '$or' : [{'i' : 3}, {'i' : 4}, {'i' : 5}] }
    assert q.filter((T.i == 3) | (T.i == 4) | (T.i == 5)).query == want

    assert Query(T, None).or_(T.i == 3, T.i == 4, T.i == 5).query == want
예제 #2
0
def computed_field_wrap_value_func_test():
    class TestDoc2(Document):
        @computed_field(StringField())
        def c(obj):
            return 'foo'

    Query(TestDoc2, None).in_(TestDoc2.c, 'bar').query == {'c': {'$in': 'bar'}}
예제 #3
0
    def query(self, type, exclude_subclasses=False):
        ''' Begin a query on the database's collection for `type`.  If `type`
			is an instance of basesting, the query will be in raw query mode
			which will not check field values or transform returned results
			into python objects.

		 .. seealso:: :class:`~ommongo.query.Query` class'''
        # This really should be adding a query operation to the
        # queue which is then forced to execute when the results are being
        # read
        if isinstance(type, basestring):
            type = FreeFormDoc(type)
        return Query(type, self, exclude_subclasses=exclude_subclasses)
예제 #4
0
    def __init__(self,
                 trans_id,
                 session,
                 document,
                 safe,
                 id_expression=None,
                 upsert=False,
                 update_ops={},
                 **kwargs):
        from ommongo.query import Query
        self.session = session
        self.trans_id = trans_id
        self.type = type(document)
        self.safe = safe
        self.upsert = upsert

        if id_expression:
            self.db_key = Query(self.type, session).filter(id_expression).query
        elif document.has_id():
            self.db_key = {'_id': document.mongo_id}
        else:
            raise InvalidUpdateException(
                'To upsert the document must have a '
                ' mongo_id OR id_expression must be specified')

        self.dirty_ops = document.get_dirty_ops(with_required=upsert)
        for key, op in chain(update_ops.items(), kwargs.items()):
            key = str(key)
            for current_op, keys in list(self.dirty_ops.items()):
                if key not in keys:
                    continue
                self.dirty_ops.setdefault(op, {})[key] = keys[key]
                del self.dirty_ops[current_op][key]
                if len(self.dirty_ops[current_op]) == 0:
                    del self.dirty_ops[current_op]
        document._mark_clean()
예제 #5
0
def test_exists():
    q = Query(T, None)
    assert q.filter(T.i.exists(False)).query == {'i': {'$exists': False}}
    assert q.filter(T.i.exists(True)).query == {'i': {'$exists': True}}
예제 #6
0
def test_nin():
    q = Query(T, None)
    assert q.nin(T.i, 1, 2, 3).query == {'i' : {'$nin' : [1,2,3]}}, q.nin(T.i, 1, 2, 3).query
    assert q.filter(T.i.nin(1, 2, 3)).query == {'i' : {'$nin' : [1,2,3]}}
예제 #7
0
def test_in():
    q = Query(T, None)
    assert q.in_(T.i, 1, 2, 3).query == {'i' : {'$in' : [1,2,3]}}, q.in_(T.i, 1, 2, 3).query
    assert q.filter(T.i.in_(1, 2, 3)).query == {'i' : {'$in' : [1,2,3]}}
예제 #8
0
def test_not_assign_dict_malformed_field():
    class Any(Document):
        i = AnythingField()
    not_q = Query(Any, None).not_(Any.i == { 'a' : 4, 'b' : 5}).query
    assert not_q == { 'i' : { '$ne' : { 'a' : 4, 'b' : 5 } } }, not_q
예제 #9
0
def test_not_with_malformed_field():
    class Any(Document):
        i = AnythingField()
    not_q = Query(Any, None).not_(Any.i == { '$gt' : 4, 'garbage' : 5})
예제 #10
0
def test_not():
    not_q = Query(T, None).filter( ~(T.i == 3) ).query
    assert not_q == { 'i' : {'$ne' : 3} }, not_q

    not_q = Query(T, None).not_(T.i > 4).query
    assert not_q == { 'i' : {'$not': { '$gt': 4}} }, not_q