def test_conferences_create_with_invalid_data(inspire_app): data = faker.record("con", with_control_number=True) data["invalid_key"] = "should throw an error" record_control_number = str(data["control_number"]) with pytest.raises(ValidationError): ConferencesRecord.create(data) record_pid = PersistentIdentifier.query.filter_by( pid_value=record_control_number).one_or_none() assert record_pid is None
def test_conferences_create_with_existing_control_number(inspire_app): data = faker.record("con", with_control_number=True) existing_object_uuid = uuid.uuid4() create_pidstore( object_uuid=existing_object_uuid, pid_type="con", pid_value=data["control_number"], ) with pytest.raises(PIDAlreadyExists): ConferencesRecord.create(data)
def test_conference_paper_get_updated_reference_conference_when_updates_one_conference( inspire_app, clean_celery_session): conference_1 = ConferencesRecord.create(faker.record("con")) conference_1_control_number = conference_1["control_number"] ref_1 = f"http://localhost:8000/api/conferences/{conference_1_control_number}" conference_2 = ConferencesRecord.create(faker.record("con")) conference_2_control_number = conference_2["control_number"] ref_2 = f"http://localhost:8000/api/conferences/{conference_2_control_number}" expected_result = [conference_2.id] data = { "publication_info": [{ "conference_record": { "$ref": ref_1 } }], "document_type": ["conference paper"], } record = LiteratureRecord.create(faker.record("lit", data)) record_control_number = record["control_number"] db.session.commit() record = LiteratureRecord.get_record_by_pid_value(record_control_number) expected_versions_len = 1 results = record.model.versions.all() result_latest_version = results[-1].json assert expected_versions_len == len(results) assert result_latest_version == record data = copy.deepcopy(dict(record)) data["publication_info"].append({"conference_record": {"$ref": ref_2}}) record.update(data) db.session.commit() record = LiteratureRecord.get_record_by_pid_value(record_control_number) assert expected_result == sorted( record.get_newest_linked_conferences_uuid()) expected_versions_len = 2 results = record.model.versions.all() result_latest_version = results[-1].json assert expected_versions_len == len(results) assert result_latest_version == record
def test_minter_undelete_conference_record_without_cnum( inspire_app, clean_celery_session): rec = ConferencesRecord.create(faker.record("con")) db.session.commit() pid = (PersistentIdentifier.query.filter( PersistentIdentifier.object_uuid == rec.id).filter( PersistentIdentifier.pid_type == "cnum").one_or_none()) assert not pid rec.delete() db.session.commit() pid = (PersistentIdentifier.query.filter( PersistentIdentifier.object_uuid == rec.id).filter( PersistentIdentifier.pid_type == "cnum").one_or_none()) assert not pid rec["deleted"] = False rec.update(dict(rec), force_undelete=True) db.session.commit() pid = (PersistentIdentifier.query.filter( PersistentIdentifier.object_uuid == rec.id).filter( PersistentIdentifier.pid_type == "cnum").one_or_none()) assert not pid
def test_minter_undelete_conference_record_registers_deleted_pid( inspire_app, clean_celery_session): data = {"opening_date": "1994-12-10"} rec = ConferencesRecord.create(faker.record("con", data=data)) db.session.commit() pid = (PersistentIdentifier.query.filter( PersistentIdentifier.pid_type == "cnum").filter( PersistentIdentifier.pid_value == rec["cnum"]).one()) assert pid.status == PIDStatus.REGISTERED rec.delete() db.session.commit() pid = (PersistentIdentifier.query.filter( PersistentIdentifier.pid_type == "cnum").filter( PersistentIdentifier.pid_value == rec["cnum"]).one()) assert pid.status == PIDStatus.DELETED rec["deleted"] = False rec.update(dict(rec), force_undelete=True) db.session.commit() pid = (PersistentIdentifier.query.filter( PersistentIdentifier.pid_type == "cnum").filter( PersistentIdentifier.pid_value == rec["cnum"]).one()) assert pid.status == PIDStatus.REGISTERED
def test_aut_citation_count_property_blows_up_on_wrong_pid_type( base_app, db, es): data = faker.record("con") record = ConferencesRecord.create(data) with pytest.raises(AttributeError): record.citation_count
def test_minter_update_conference_record_with_different_cnum_raises_error( inspire_app, clean_celery_session): data = {"opening_date": "1994-12-10"} rec = ConferencesRecord.create(faker.record("con", data=data)) db.session.commit() rec["cnum"] = "C95-01-01" with pytest.raises(CNUMChanged): rec.update(dict(rec))
def test_create_record_from_db_depending_on_its_pid_type(base_app, db, es): data = faker.record("con") record = InspireRecord.create(data) assert type(record) == ConferencesRecord assert record.pid_type == "con" record = ConferencesRecord.create(data) assert type(record) == ConferencesRecord assert record.pid_type == "con"
def test_conference_record_updates_in_es_when_lit_rec_refers_to_it( inspire_app, clean_celery_session): conference_1 = ConferencesRecord.create(faker.record("con")) conference_1_control_number = conference_1["control_number"] ref_1 = f"http://localhost:8000/api/conferences/{conference_1_control_number}" db.session.commit() expected_contributions_count = 0 def assert_record(): current_search.flush_and_refresh("records-conferences") record_from_es = ConferencesSearch().get_record_data_from_es( conference_1) assert expected_contributions_count == record_from_es[ "number_of_contributions"] retry_until_pass(assert_record) data = { "publication_info": [{ "conference_record": { "$ref": ref_1 } }], "document_type": ["conference paper"], } LiteratureRecord.create(faker.record("lit", data)) data = { "publication_info": [{ "conference_record": { "$ref": ref_1 }, "journal_title": "nice title" }], "document_type": ["proceedings"], } record2 = LiteratureRecord.create(faker.record("lit", data)) db.session.commit() def assert_record(): current_search.flush_and_refresh("records-conferences") record_from_es = ConferencesSearch().get_record_data_from_es( conference_1) assert expected_contributions_count == record_from_es[ "number_of_contributions"] retry_until_pass(assert_record) expected_proceedings = [ProceedingInfoItemSchemaV1().dump(record2).data] def assert_record(): current_search.flush_and_refresh("records-conferences") record_from_es = ConferencesSearch().get_record_data_from_es( conference_1) assert expected_proceedings == record_from_es["proceedings"] retry_until_pass(assert_record)
def test_create_record_from_db_depending_on_its_pid_type(inspire_app): data = faker.record("con") record = InspireRecord.create(data) assert isinstance(record, ConferencesRecord) assert record.pid_type == "con" record = ConferencesRecord.create(data) assert isinstance(record, ConferencesRecord) assert record.pid_type == "con"
def test_conferences_create(inspire_app): data = faker.record("con") record = ConferencesRecord.create(data) control_number = str(record["control_number"]) record_db = RecordMetadata.query.filter_by(id=record.id).one() assert record == record_db.json record_pid = PersistentIdentifier.query.filter_by( pid_type="con", pid_value=str(control_number)).one() assert record.model.id == record_pid.object_uuid assert control_number == record_pid.pid_value
def test_indexer_updates_conference_papers_when_name_changes( inspire_app, clean_celery_session): conference_data = faker.record( "con", data={"titles": [{ "title": "Initial Title" }]}) conference = ConferencesRecord.create(conference_data) db.session.commit() current_search.flush_and_refresh("records-conferences") conference_id = conference["control_number"] conference_paper_data = faker.record( "lit", data={ "document_type": ["conference paper"], "publication_info": [{ "conference_record": { "$ref": f"https://labs.inspirehep.net/api/conferences/{conference_id}" } }], }, ) LiteratureRecord.create(conference_paper_data) db.session.commit() def assert_literature_has_correct_conference_title(): current_search.flush_and_refresh("*") result = es_search("records-hep") total = get_value(result, "hits.total.value") assert total == 1 literature = get_value(result, "hits.hits[0]._source") ui_display = orjson.loads(literature["_ui_display"]) assert conference["titles"] == get_value(ui_display, "conference_info[0].titles") retry_until_pass(assert_literature_has_correct_conference_title, timeout=45) data = dict(conference) data["titles"] = [{"title": "Updated Title"}] conference.update(data) db.session.commit() retry_until_pass(assert_literature_has_correct_conference_title, timeout=45)
def test_indexer_deletes_record_from_es(inspire_app, datadir): def assert_record_is_deleted_from_es(): current_search.flush_and_refresh("records-conferences") expected_records_count = 0 record_lit_es = ConferencesSearch().get_record(str( record.id)).execute().hits assert expected_records_count == len(record_lit_es) record = ConferencesRecord.create(faker.record("con")) db.session.commit() record.delete() db.session.commit() retry_until_pass(assert_record_is_deleted_from_es)
def post(self): """Adds new conference record""" data = self.load_data_from_request() record = ConferencesRecord.create(data) db.session.commit() if not is_superuser_or_cataloger_logged_in(): self.create_ticket(record, "rt/new_conference.html") send_conference_confirmation_email(current_user.email, record) return ( jsonify( {"pid_value": record["control_number"], "cnum": record.get("cnum")} ), 201, )
def test_minter_deleting_record_removes_cnum_pid(inspire_app): data = { "$schema": "https://labs.inspirehep.net/schemas/records/conferences.json", "_collections": ["Conferences"], "opening_date": "2005-09-16", } record = ConferencesRecord.create(data) expected_cnum = "C05-09-16" assert "cnum" in record assert record["cnum"] == expected_cnum record.hard_delete() pids_count = (PersistentIdentifier.query.filter_by( object_uuid=record.id).filter_by(pid_type="cnum").count()) assert pids_count == 0
def test_conferences_update(inspire_app): data = faker.record("con", with_control_number=True) record = ConferencesRecord.create(data) assert data["control_number"] == record["control_number"] data_update = {"public_notes": [{"value": "UPDATED"}]} data.update(data_update) record.update(data) control_number = str(record["control_number"]) record_updated_db = RecordMetadata.query.filter_by(id=record.id).one() assert data == record_updated_db.json record_updated_pid = PersistentIdentifier.query.filter_by( pid_type="con", pid_value=str(control_number)).one() assert record.model.id == record_updated_pid.object_uuid assert control_number == record_updated_pid.pid_value
def test_minter_change_opening_date_doesnt_change_cnum(inspire_app): data = { "$schema": "https://labs.inspirehep.net/schemas/records/conferences.json", "_collections": ["Conferences"], "opening_date": "2005-09-16", } record = ConferencesRecord.create(data) expected_cnum = "C05-09-16" assert "cnum" in record assert record["cnum"] == expected_cnum record["opening_date"] = "2020-09-16" record.update(dict(record)) updated_rec = ConferencesRecord.get_record_by_pid_value(record["control_number"]) assert updated_rec["opening_date"] == "2020-09-16" assert updated_rec["cnum"] == expected_cnum
def test_process_references_in_records_process_conference_records( mock_batch_index, inspire_app, clean_celery_session): conf_record = ConferencesRecord.create( faker.record("con", data={"titles": [{ "title": "Test conference" }]})) lit_data = { "publication_info": [{ "conference_record": { "$ref": conf_record["self"]["$ref"] } }], "document_type": ["conference paper"], } lit_record = LiteratureRecord.create(faker.record("lit", data=lit_data)) lit_record_2 = LiteratureRecord.create(faker.record("lit", data=lit_data)) db.session.commit() def assert_records_in_es(): lit_record_from_es = InspireSearch.get_record_data_from_es(lit_record) lit_record_from_es_2 = InspireSearch.get_record_data_from_es( lit_record_2) aut_record_from_es = InspireSearch.get_record_data_from_es(conf_record) assert lit_record_from_es and aut_record_from_es and lit_record_from_es_2 retry_until_pass(assert_records_in_es, retry_interval=5) models_committed.disconnect(index_after_commit) conf_record["titles"] = [{"title": "Southern California Strings Seminar "}] conf_record.update(dict(conf_record)) db.session.commit() # reconnect signal before we call process_references_in_records models_committed.connect(index_after_commit) task = process_references_in_records.delay([conf_record.id]) task.get(timeout=5) assert sorted(mock_batch_index.mock_calls[0][1][0]) == sorted( [lit_record.id, lit_record_2.id])
def test_conference_record_updates_in_es_when_lit_rec_reffers_to_it( inspire_app, celery_app_with_context, celery_session_worker): conference_1 = ConferencesRecord.create(faker.record("con")) conference_1_control_number = conference_1["control_number"] ref_1 = f"http://localhost:8000/api/conferences/{conference_1_control_number}" db.session.commit() expected_contributions_count = 0 steps = [ { "step": current_search.flush_and_refresh, "args": ["records-conferences"] }, { "step": es_search, "args": ["records-conferences"], "expected_result": { "expected_key": "hits.total.value", "expected_result": 1, }, }, { "step": es_search, "args": ["records-conferences"], "expected_result": { "expected_key": "hits.hits[0]._source.number_of_contributions", "expected_result": expected_contributions_count, }, }, ] retry_until_matched(steps) data = { "publication_info": [{ "conference_record": { "$ref": ref_1 } }], "document_type": ["conference paper"], } LiteratureRecord.create(faker.record("lit", data)) data = { "publication_info": [{ "conference_record": { "$ref": ref_1 }, "journal_title": "nice title" }], "document_type": ["proceedings"], } record2 = LiteratureRecord.create(faker.record("lit", data)) db.session.commit() expected_contributions_count = 1 steps = [ { "step": current_search.flush_and_refresh, "args": ["records-conferences"] }, { "step": es_search, "args": ["records-conferences"], "expected_result": { "expected_key": "hits.hits[0]._source.number_of_contributions", "expected_result": expected_contributions_count, }, }, ] retry_until_matched(steps) expected_proceedings = [ProceedingInfoItemSchemaV1().dump(record2).data] steps = [ { "step": current_search.flush_and_refresh, "args": ["records-conferences"] }, { "step": es_search, "args": ["records-conferences"], "expected_result": { "expected_key": "hits.hits[0]._source.proceedings", "expected_result": expected_proceedings, }, }, ] retry_until_matched(steps)