def oaiset_update_records(minimal_record, db, es): """Fixture with records for query-based OAISet updating tests.""" rec_ok = { 'title': 'extra', '_oai': { 'id': '12345', 'sets': ['extra', 'user-foobar'], 'updated': datetime(1970, 1, 1).isoformat(), } } # Record which needs removal of 'extra' from oai sets rec_remove = deepcopy(rec_ok) rec_remove['title'] = 'other' # Record which needs addition of 'extra' to oai sets rec_add = deepcopy(rec_ok) rec_add['_oai']['sets'] = [ 'user-foobar', ] records = [ rec_ok, rec_remove, rec_add, ] rec_uuids = [] for record_meta in records: rec = RecordMetadata() rec.json = deepcopy(record_meta) db.session.add(rec) db.session.commit() RecordIndexer().index_by_id(rec.id) rec_uuids.append(rec.id) current_search.flush_and_refresh('records') return rec_uuids
def minimal_record_model(minimal_record): """Minimal record.""" model = RecordMetadata() model.created = datetime.utcnow() - timedelta(days=1) model.updated = model.created + timedelta(days=1) model.version_id = 0 return Record(minimal_record, model=model)
def audit_records(minimal_record, db): """Audit test records.""" records = {} for i in (1, 2, 3, 4): record = RecordMetadata() record.json = deepcopy(minimal_record) record.json['recid'] = i record.json['_oai'] = { 'id': 'oai:{}'.format(i), 'sets': [], 'updated': datetime.utcnow().date().isoformat(), } db.session.add(record) db.session.commit() records[i] = ZenodoRecord(data=record.json, model=record) recid = PersistentIdentifier(pid_type='recid', pid_value=str(i), status='R', object_type='rec', object_uuid=record.id) db.session.add(recid) db.session.commit() return records
def test_record_audit(record_audit, full_record, db, communities, users, oaiid_pid): # Add the "ecfunded" community since it's usually being added automatically # after processing a deposit if the record has an EC grant. oaiid_pid.pid_value = full_record['communities'].append('ecfunded') # Mint the OAI identifier oaiid_pid.pid_value = full_record['_oai']['id'] db.session.add(oaiid_pid) # Create the record metadata, to store the record_model = RecordMetadata() record_model.json = full_record db.session.add(record_model) db.session.commit() record = ZenodoRecord(data=full_record, model=record_model) check = RecordCheck(record_audit, record) check.perform() assert check.issues == {} assert check.is_ok is True assert check.dump() == { 'record': { 'recid': record['recid'], 'object_uuid': str(record.id), }, 'issues': {}, }
def can_edit_accessright(record): """Test if access right is valid.""" r = RecordMetadata() setattr(r, 'id', record) permission_edit_record = update_permission_factory(r) if permission_edit_record.can(): return True return False
def minimal_record_model(db, minimal_record, sip_metadata_types, recid_pid): """Minimal record.""" model = RecordMetadata(id=str(recid_pid.object_uuid)) model.created = datetime.utcnow() - timedelta(days=1) model.updated = model.created + timedelta(days=1) model.version_id = 0 rec = ZenodoRecord(minimal_record, model=model) db.session.commit() return rec
def test_bucket_link_factory_has_bucket(app, db, location, bucket): """Test bucket link factory retrieval of a bucket.""" with app.test_request_context(): with db.session.begin_nested(): record = RecordMetadata() RecordsBuckets.create(record, bucket) db.session.add(record) pid = mock.Mock() pid.get_assigned_object.return_value = record.id assert default_bucket_link_factory(pid) == url_for( 'invenio_files_rest.bucket_api', bucket_id=bucket.id, _external=True)
def is_record_public(record): """Test if access right is valid.""" r = RecordMetadata() setattr(r, 'id', record) is_public = ActionUsers.query.filter( ActionUsers.action == 'records-read', ActionUsers.argument == str(r.id), ActionUsers.user_id.is_(None)).first() if is_public: return True else: return False
def minimal_record_model(db, minimal_record, sip_metadata_types): """Minimal record.""" model = RecordMetadata() model.created = datetime.utcnow() - timedelta(days=1) model.updated = model.created + timedelta(days=1) model.version_id = 0 rec = ZenodoRecord(minimal_record, model=model) PersistentIdentifier.create( 'recid', '123', status=PIDStatus.REGISTERED, object_type='rec', object_uuid=rec.id) db.session.commit() return rec
def test_record_files_link_factory(app, db, location, bucket): """Test record files link factory.""" with app.test_request_context(): with db.session.begin_nested(): record = RecordMetadata() RecordsBuckets.create(record, bucket) db.session.add(record) pid = mock.Mock() pid.pid_value = 1 pid.pid_type = "recid" pid.get_assigned_object.return_value = record.id links = default_record_files_links_factory(pid) assert links == { "files": "http://localhost/records/1/files", "self": "http://localhost/records/1", }
def _create(pid_type, filename): data = json.loads(_get_fixture(filename)) record = RecordMetadata(json=data) db.session.add(record) db.session.commit() pid = PersistentIdentifier( pid_type=pid_type, pid_value=data['control_number'], status='R', object_type='rec', object_uuid=record.id ) db.session.add(pid) db.session.commit() return record
def test_record_indexing(app, queue): """Run record autoindexer.""" @before_record_index.connect_via(app) def remove_schema(sender, json=None, record=None): if '$schema' in json: del json['$schema'] models_committed.connect(process_models_committed_signal, sender=app) with app.app_context(): current_search_client.indices.delete_alias('_all', '_all', ignore=[400, 404]) current_search_client.indices.delete('*') aliases = current_search_client.indices.get_aliases() assert 0 == len(aliases) runner = CliRunner() script_info = ScriptInfo(create_app=lambda info: app) with runner.isolated_filesystem(): result = runner.invoke(cmd, ['destroy', '--yes-i-know'], obj=script_info) result = runner.invoke(cmd, ['init'], obj=script_info) assert 0 == result.exit_code with app.app_context(): from invenio_records.models import RecordMetadata with db.session.begin_nested(): record1 = RecordMetadata( json={ '$schema': ( 'http://example.com/schemas/' # external site 'records/default-v1.0.0.json'), 'title': 'Test1', }) db.session.add(record1) record2 = RecordMetadata( json={ '$schema': { '$ref': ( 'http://example.com/schemas/' # external site 'records/default-v1.0.0.json') }, 'title': 'Test2', }) db.session.add(record2) db.session.commit() record_indexer = RecordIndexer(queue=queue) result = record_indexer.process_bulk_queue() assert 2 == len(list(result)) response = current_search_client.get( index='records-default-v1.0.0', id=record1.id, ) assert str(record1.id) == response['_id'] response = current_search_client.get( index='records-default-v1.0.0', id=record2.id, ) assert str(record2.id) == response['_id'] db.session.delete(record1) db.session.commit() record_indexer.process_bulk_queue() response = current_search_client.get( index='records-default-v1.0.0', id=record1.id, ignore=404, ) assert not response['found'] # Clean-up: with app.app_context(): result = runner.invoke(cmd, ['destroy', '--yes-i-know'], obj=script_info) assert 0 == result.exit_code