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}}
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)
def test_query_from_query_arguments(): query = q(q({'a': 1}), q({'b': 2})) assert query == {'a': 1, 'b': 2}
def test_query_or_query(): q1 = q({'a': 1, 'b': 2}) q2 = q({'a': 1, 'c': 3}) assert q1 | q2 == _or(q1, q2)
def test_query_and_query(): q1 = q({'a': 1, 'b': 2}) q2 = q({'a': 1, 'c': 3}) assert q1 & q2 == _and(q1, q2)
def test_and(): query = q(_and({'a': 1}, {'b': 2})) assert query == {'$and': [{'a': 1}, {'b': 2}]}
def and_(*queries): return q({'$and': [q(query) for query in queries]})
def test_gte(): query = q(a=_gte(1)) assert query == {'a': {'$gte': 1}}
def test_ne(): query = q(a=_ne(1)) assert query == {'a': {'$ne': 1}}
def test_exists(): query = q({'a': _exists(1)}) assert query == {'a': {'$exists': True}}
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}
def exists_(value): return q({'$exists': bool(value)})
def type_(value): if isinstance(value, BSONType): value = value.value elif isinstance(value, string_types): value = BSONType[value].value return q({'$type': int(value)})
def test_nor(): query = q(_nor({'a': 1}, {'b': 2})) assert query == {'$nor': [{'a': 1}, {'b': 2}]}
def test_query_empty(): query = q() assert query == {} query = q({}) assert query == {}
def test_lte(): query = q(a=_lte(1)) assert query == {'a': {'$lte': 1}}
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}}
def test_nin(): query = q(a=_nin(1, 2, 3)) assert query == {'a': {'$nin': (1, 2, 3)}}
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}}
def not_(query): """TODO: check for supported $not arguments """ return q({'$not': q(query)})
def test_query_from_query(): query = q(q()) assert query == {} query = q(q({'a': 1, 'b': 2})) assert query == {'a': 1, 'b': 2}
def nor_(*queries): return q({'$nor': [q(query) for query in queries]})
def test_not(): query = q(_not({'a': 1})) assert query == {'$not': {'a': 1}}