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
def test_auth_lookup(db): from dlx.marc import Bib, Auth bib = Bib.find_one({'_id': 1}) assert bib.get_xref('650', 'a') == 1 assert bib.get_value('650', 'a') == 'Header' auth = Auth.find_one({'_id': 1}) auth.set('150', 'a', 'Changed').commit() assert bib.get_value('650', 'a') == 'Changed'
def test_find_one(db, bibs, auths): from dlx.marc import Bib, Auth bib = Bib.find_one({'_id': 1}) assert bib.id == 1 assert isinstance(bib, Bib) auth = Auth.find_one({'_id': 1}) assert auth.id == 1 assert isinstance(auth, Auth)
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_to_mrc(db): from dlx.marc import Bib, Auth control = '00224r|||a2200097|||4500008001300000245002400013520001600037520004300053650001100096710001900107controlfield aThisbis thectitle aDescription aAnother descriptionaRepeated subfield aHeader aAnother header' bib = Bib.find_one({'_id': 1}) assert bib.to_mrc() == control control = '00049||||a2200037|||4500150001100000 aHeader' auth = Auth.find_one({'_id': 1}) assert auth.to_mrc() == control auth.set('994', 'a', 'Titulo').commit() assert bib.to_mrc(language='es') == '00224r|||a2200097|||4500008001300000245002400013520001600037520004300053650001100096710001900107controlfield aThisbis thectitle aDescription aAnother descriptionaRepeated subfield aTitulo aAnother header'