def test_find(db): from dlx.marc import Bib, Auth bibs = Bib.find({}) assert inspect.isgenerator(bibs) assert len(list(bibs)) == 2 for bib in Bib.find({}): assert isinstance(bib, Bib) auths = Auth.find({}) assert inspect.isgenerator(auths) assert len(list(auths)) == 2 for auth in Auth.find({}): assert isinstance(auth, Auth) auths = Auth.find({'_id': 1}) assert len(list(auths)) == 1 auths = Auth.find({}, limit=1) assert len(list(auths)) == 1 auths = Auth.find({}, limit=0, skip=1, projection={}) assert len(list(auths)) == 1
def test_querystring(db): from dlx.marc import Bib, Auth, QueryDocument query = QueryDocument.from_string('{"245": {"a": "/^(This|Another)/", "b": "is the", "c": "title"}}') assert len(list(Bib.find(query.compile()))) == 2 query = QueryDocument.from_string('{"OR": {"650": 0, "710": 0}}') assert len(list(Bib.find(query.compile()))) == 1 query = QueryDocument.from_string('{"110": {"a": "Another header"}}') assert Auth.find_one(query.compile()).id == 2
def test_querydocument(db): from dlx.marc import Bib, Auth, QueryDocument, Condition, Or from bson import SON from json import loads import re query = QueryDocument(Condition(tag='245', subfields={'a': 'This'})) assert isinstance(query.compile(), SON) qjson = query.to_json() qdict = loads(qjson) assert qdict['245']['$elemMatch']['subfields']['$elemMatch']['code'] == 'a' assert qdict['245']['$elemMatch']['subfields']['$elemMatch']['value'] == 'This' query = QueryDocument( Condition(tag='245', subfields={'a': re.compile(r'(This|Another)'), 'b': 'is the', 'c': 'title'}), Condition(tag='650', modifier='exists'), Or( Condition(tag='710', modifier='exists'), Condition(tag='520', modifier='not_exists') ) ) assert len(list(Bib.find(query.compile()))) == 2 query = QueryDocument( Condition(tag='110', subfields={'a': 'Another header'}), ) assert len(list(Auth.find(query.compile()))) == 1 assert Auth.find_one(query.compile()).id == 2