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_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 db(): from dlx import DB from dlx.marc import Bib, Auth from dlx.file import S3, File, Identifier from tempfile import TemporaryFile DB.connect( 'mongomock://localhost') # ? does mock connection create a fresh db ? DB.bibs.drop() DB.auths.drop() DB.files.drop() DB.handle['dlx_dl_log'].drop() Auth().set('100', 'a', 'name_1').commit() Auth().set('100', 'a', 'name_2').commit() Bib().set('191', 'a', 'TEST/1').set('245', 'a', 'title_1').set('700', 'a', 1).commit() Bib().set('245', 'a', 'title_2').set('700', 'a', 2).commit() S3.connect(access_key='key', access_key_id='key_id', bucket='mock_bucket') S3.client.create_bucket(Bucket=S3.bucket) handle = TemporaryFile() handle.write(b'some data') handle.seek(0) File.import_from_handle(handle, filename='', identifiers=[Identifier('symbol', 'TEST/1')], languages=['EN'], mimetype='text/plain', source='test') return DB.client
def test_commit(db, bibs, auths): from dlx import DB from dlx.marc import Bib, Auth from datetime import datetime from bson import ObjectId from jsonschema.exceptions import ValidationError with pytest.raises(Exception): Bib({'_id': 'I am invalid'}).commit() for bib in [Bib(x) for x in bibs]: assert bib.commit().acknowledged for auth in [Auth(x) for x in auths]: assert auth.commit().acknowledged bib = Bib({'_id': 3}) assert bib.commit().acknowledged assert isinstance(bib.updated, datetime) assert bib.user == 'admin' assert bib.history()[0].to_dict() == bib.to_dict() assert bib.history()[0].user == 'admin' assert Bib.max_id() == 3 Bib().commit() Bib().commit() assert Bib.max_id() == 5
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_init(bibs, auths): from dlx.marc import BibSet, Bib, AuthSet, Auth records = [Bib(x) for x in bibs] bibset = BibSet(records) assert isinstance(bibset, BibSet) assert len(bibset.records) == 2 assert bibset.count == 2 records = [Auth(x) for x in auths] authset = AuthSet(records) assert isinstance(authset, AuthSet) assert len(authset.records) == 2 assert authset.count == 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'
def test_init_auth(db, auths): from dlx.marc import Marc, Auth, Controlfield, Datafield, Subfield, Literal, Linked auth = Auth(auths[0]) assert isinstance(auth, Marc) assert isinstance(auth, Auth) assert len(auth.controlfields) == 0 assert len(auth.datafields) == 1 assert len(auth.fields) == 1 for field in auth.controlfields: assert isinstance(field, Controlfield) for field in auth.datafields: assert isinstance(field, Datafield) assert field.record_type == 'auth' assert field.tag and field.ind1 and field.ind2 for subfield in field.subfields: assert isinstance(subfield, Subfield) assert isinstance(subfield, (Literal, Linked)) assert subfield.code and subfield.value
def main(): args = parser.parse_args() DB.connect(args.connect) for tag, itype in Config.bib_index_fields.items(): if itype == 'literal': Bib.literal_index(tag) elif itype == 'linked': Bib.linked_index(tag) elif itype == 'hybrid': Bib.hybrid_index(tag) else: raise Exception('Invalid index type') for tag, itype in Config.auth_index_fields.items(): if itype == 'literal': Auth.literal_index(tag) elif itype == 'linked': Auth.linked_index(tag) elif itype == 'hybrid': Auth.hybrid_index(tag) else: raise Exception('Invalid index type')
def setUp(self): # runs before every test method DB.connect('mongomock://localhost') # an abundance of caution #self.assertEqual(get_config().connect_string,'mongomock://localhost') #self.assertIsInstance(DB.client, MockDB) # clear the db DB.bibs.delete_many({}) DB.auths.delete_many({}) #print(Config.auth_authority_controlled) # mock auths # all xrefs in the mock bibs must point to an auth in the mock db to work properly Auth({ '_id': 1 }).set('190', 'b', 'A/').set('190', 'c', 'SESSION_1').commit() Auth({ '_id': 2 }).set('190', 'b', 'A/').set('190', 'c', 'SESSION_2').commit() Auth({ '_id': 3 }).set('191', 'a', 'A/SESSION_1/x').set('191', 'b', 'AGENDA ITEM').set( '191', 'd', 'AGENDA SUBJECT').commit() Auth({ '_id': 4 }).set('191', 'a', 'E/SESSION_1/x').set('191', 'b', 'AGENDA ITEM').set( '191', 'd', 'AGENDA SUBJECT').commit() Auth({ '_id': 5 }).set('191', 'a', 'S/73/x').set('191', 'b', 'AGENDA ITEM').set('191', 'd', 'AGENDA SUBJECT').commit() Auth({'_id': 6}).set('100', 'a', 'Person, A.').commit()
def test_language(db): from dlx.marc import Bib, Auth Auth({'_id': 3}).set('150', 'a', 'Text').set('994', 'a', 'Texto').commit() bib = Bib({'_id': 3}).set('650', 'a', 3) assert bib.get('650', 'a', language='es') == 'Texto'