Example #1
0
def test_type():
    query = q({'a': _type(2)})
    assert query == {'a': {'$type': 2}}

    query = q({'a': _type('String')})
    assert query == {'a': {'$type': 2}}

    query = q({'a': _type(BSON_STRING)})
    assert query == {'a': {'$type': 2}}
Example #2
0
def test_query_illegal_value():
    with pytest.raises(TypeError):
        q([1, 2])
    with pytest.raises(ValueError):
        q('abc')
    with pytest.raises(TypeError):
        q(0)
    with pytest.raises(TypeError):
        q(1)
Example #3
0
def test_query_from_query_arguments():
    query = q(q({'a': 1}), q({'b': 2}))
    assert query == {'a': 1, 'b': 2}
Example #4
0
def test_query_or_query():
    q1 = q({'a': 1, 'b': 2})
    q2 = q({'a': 1, 'c': 3})
    assert q1 | q2 == _or(q1, q2)
Example #5
0
def test_query_and_query():
    q1 = q({'a': 1, 'b': 2})
    q2 = q({'a': 1, 'c': 3})
    assert q1 & q2 == _and(q1, q2)
Example #6
0
def test_and():
    query = q(_and({'a': 1}, {'b': 2}))
    assert query == {'$and': [{'a': 1}, {'b': 2}]}
Example #7
0
def and_(*queries):
    return q({'$and': [q(query) for query in queries]})
Example #8
0
def test_gte():
    query = q(a=_gte(1))
    assert query == {'a': {'$gte': 1}}
Example #9
0
def test_ne():
    query = q(a=_ne(1))
    assert query == {'a': {'$ne': 1}}
Example #10
0
def test_exists():
    query = q({'a': _exists(1)})
    assert query == {'a': {'$exists': True}}
Example #11
0
from mnj.operators.logical import _and
from mnj.query import q, Doc


db = MongoClient()['test']
db.docs.drop()
db.docs.insert(Doc([('a', 1), ('b', 1)]))
db.docs.insert(Doc([('a', 2), ('b', 2)]))
db.docs.insert(Doc([('a', 3), ('b', 3)]))
db.docs.insert(Doc([('a', 1), ('b', 4)]))
db.docs.insert(Doc([('a', 2), ('b', 5)]))
db.docs.insert(Doc([('a', 3), ('b', 6)]))

# {'a': 1, 'b': 1}
# {'a': 1, 'b': 4}
for doc in db.docs.find(q(a=1)):
    del doc['_id']
    print(doc)
print()

# {'a': 1, 'b': 1}
# {'a': 3, 'b': 3}
# {'a': 1, 'b': 4}
# {'a': 3, 'b': 6}
for doc in db.docs.find(q(a=_ne(2))):
    del doc['_id']
    print(doc)
print()

# {'a': 1, 'b': 4}
# {'a': 2, 'b': 5}
Example #12
0
def exists_(value):
    return q({'$exists': bool(value)})
Example #13
0
def type_(value):
    if isinstance(value, BSONType):
        value = value.value
    elif isinstance(value, string_types):
        value = BSONType[value].value
    return q({'$type': int(value)})
Example #14
0
def test_nor():
    query = q(_nor({'a': 1}, {'b': 2}))
    assert query == {'$nor': [{'a': 1}, {'b': 2}]}
Example #15
0
def test_query_empty():
    query = q()
    assert query == {}
    query = q({})
    assert query == {}
Example #16
0
def test_lte():
    query = q(a=_lte(1))
    assert query == {'a': {'$lte': 1}}
Example #17
0
def test_query_dict():
    query = q({'a': 1, 'b': 2})
    assert query == {'a': 1, 'b': 2}
    query = q({'a': 1, 'b': {'$gt': 1}})
    assert query == {'a': 1, 'b': {'$gt': 1}}
Example #18
0
def test_nin():
    query = q(a=_nin(1, 2, 3))
    assert query == {'a': {'$nin': (1, 2, 3)}}
Example #19
0
def test_query_kwargs():
    query = q(a=1, b=2)
    assert query == {'a': 1, 'b': 2}
    query = q(a=1, b={'$gt': 1})
    assert query == {'a': 1, 'b': {'$gt': 1}}
Example #20
0
def not_(query):
    """TODO: check for supported $not arguments
    """
    return q({'$not': q(query)})
Example #21
0
def test_query_from_query():
    query = q(q())
    assert query == {}
    query = q(q({'a': 1, 'b': 2}))
    assert query == {'a': 1, 'b': 2}
Example #22
0
def nor_(*queries):
    return q({'$nor': [q(query) for query in queries]})
Example #23
0
def test_not():
    query = q(_not({'a': 1}))
    assert query == {'$not': {'a': 1}}