-
Notifications
You must be signed in to change notification settings - Fork 0
TIMX 655 - handle deletes for researchdatabases (AZ) #277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...xtures/researchdatabases/researchdatabases-2026-08-11-full-extracted-records-to-index.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| <?xml version="1.0"?> | ||
| <OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"> | ||
| <responseDate>2026-08-11T09:30:27Z</responseDate> | ||
| <request verb="ListRecords" metadataPrefix="oai_dc" set="az"> | ||
| https://libguides.mit.edu/oai.php | ||
| </request> | ||
| <ListRecords> | ||
| <record> | ||
| <header> | ||
| <identifier>oai:libguides.com:az/65257807</identifier> | ||
| <datestamp>2025-12-01T14:59:03Z</datestamp> | ||
| <setSpec>az</setSpec> | ||
| </header> | ||
| <metadata> | ||
| <oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" | ||
| xmlns:dc="http://purl.org/dc/elements/1.1/" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd"> | ||
| <dc:title> | ||
| <![CDATA[Linguistics and Language Behavior Abstracts (LLBA)]]></dc:title> | ||
| <dc:subject><![CDATA[Humanities]]></dc:subject> | ||
| <dc:type><![CDATA[Articles]]></dc:type> | ||
| <dc:type><![CDATA[Dissertations & theses]]></dc:type> | ||
| <dc:type><![CDATA[eBooks]]></dc:type> | ||
| <dc:description> | ||
| <![CDATA[<p>The most comprehensive index to articles in Linguistics and Language Development and use.</p>]]></dc:description> | ||
| <dc:date>2022-01-28 22:15:37</dc:date> | ||
| <dc:identifier>https://libguides.mit.edu/llba</dc:identifier> | ||
| </oai_dc:dc> | ||
| </metadata> | ||
| </record> | ||
| <record> | ||
| <header> | ||
| <identifier>oai:libguides.com:az/65257808</identifier> | ||
| <datestamp>2025-12-01T14:59:03Z</datestamp> | ||
| <setSpec>az</setSpec> | ||
| </header> | ||
| <metadata> | ||
| <oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" | ||
| xmlns:dc="http://purl.org/dc/elements/1.1/" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd"> | ||
| <dc:title><![CDATA[ADS (NASA Astrophysics Data System)]]></dc:title> | ||
| <dc:subject><![CDATA[Engineering]]></dc:subject> | ||
| <dc:subject><![CDATA[Science]]></dc:subject> | ||
| <dc:type><![CDATA[Articles]]></dc:type> | ||
| <dc:type><![CDATA[Data & statistics]]></dc:type> | ||
| <dc:description> | ||
| <![CDATA[<p>Indexes major astronomy and astrophysics journals; includes abstracts for most entries and full text scans of tens of thousands of articles.</p>]]></dc:description> | ||
| <dc:date>2022-01-28 22:15:37</dc:date> | ||
| <dc:identifier>https://libguides.mit.edu/ads</dc:identifier> | ||
| </oai_dc:dc> | ||
| </metadata> | ||
| </record> | ||
| <record> | ||
| <header> | ||
| <identifier>oai:libguides.com:az/65257809</identifier> | ||
| <datestamp>2025-12-01T14:59:03Z</datestamp> | ||
| <setSpec>az</setSpec> | ||
| </header> | ||
| <metadata> | ||
| <oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" | ||
| xmlns:dc="http://purl.org/dc/elements/1.1/" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd"> | ||
| <dc:title><![CDATA[Ethnic NewsWatch]]></dc:title> | ||
| <dc:subject><![CDATA[Social sciences]]></dc:subject> | ||
| <dc:type><![CDATA[Newspapers & magazine articles]]></dc:type> | ||
| <dc:type><![CDATA[Primary sources]]></dc:type> | ||
| <dc:description> | ||
| <![CDATA[<p><strong>Coverage:</strong> 1990 - present<br>A full-text collection of newspapers, journals and magazines published by ethnic and minority presses.</p>]]></dc:description> | ||
| <dc:date>2022-01-28 22:15:37</dc:date> | ||
| <dc:identifier>https://libguides.mit.edu/ethnic</dc:identifier> | ||
| </oai_dc:dc> | ||
| </metadata> | ||
| </record> | ||
| </ListRecords> | ||
| </OAI-PMH> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| # ruff: noqa: PLR2004, SLF001 | ||
|
|
||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
| from bs4 import Tag | ||
|
|
||
| from transmogrifier.helpers import LibGuidesAPIClient | ||
| from transmogrifier.sources.xml.researchdatabases import ResearchDatabases | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def _test_env_libguides(): | ||
| with ( | ||
| patch("transmogrifier.config.LIBGUIDES_CLIENT_ID", "123"), | ||
| patch("transmogrifier.config.LIBGUIDES_API_TOKEN", "aaabbbdddccc"), | ||
| patch("transmogrifier.config.TIMDEX_DATASET_LOCATION", "s3://timdex/dataset"), | ||
| ): | ||
| yield | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mocked_current_az_identifiers(): | ||
| """Mock current AZ identifiers from LibGuides API, none matching previous ones.""" | ||
| with patch.object( | ||
| LibGuidesAPIClient, | ||
| "get_current_az_identifiers", | ||
| return_value=["7777", "8888", "9999"], | ||
| ): | ||
| yield | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mocked_timdex_dataset_az_identifiers(): | ||
| with patch.object( | ||
| ResearchDatabases, | ||
| "_get_current_dataset_az_identifiers", | ||
| return_value=[ | ||
| "1234", # mocked as present only in dataset, not current/public | ||
| "7777", | ||
| "8888", | ||
| "9999", | ||
| ], | ||
| ): | ||
| yield | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def researchdatabases_transformer(): | ||
|
|
||
| return ResearchDatabases.load( | ||
| "researchdatabases", | ||
| ( | ||
| "tests/fixtures/researchdatabases/researchdatabases-" | ||
| "2026-08-11-full-extracted-records-to-index.xml" | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| def test_researchdatabases_yields_oai_records_pass(researchdatabases_transformer): | ||
|
|
||
| oai_records = list( | ||
| researchdatabases_transformer._yield_oai_xml_records_for_indexing( | ||
| researchdatabases_transformer.source_file | ||
| ) | ||
| ) | ||
|
|
||
| assert len(oai_records) == 3 | ||
| assert isinstance(oai_records[0], Tag) | ||
|
|
||
|
|
||
| def test_researchdatabases_yields_deleted_records_success( | ||
| researchdatabases_transformer, | ||
| mocked_timdex_dataset_az_identifiers, | ||
| mocked_current_az_identifiers, | ||
| ): | ||
| deleted_records = list( | ||
| researchdatabases_transformer._yield_api_records_for_deleting() | ||
| ) | ||
| assert len(deleted_records) == 1 | ||
| assert isinstance(deleted_records[0], Tag) | ||
|
|
||
|
|
||
| def test_researchdatabases_synthetic_deleted_record( | ||
| researchdatabases_transformer, | ||
| mocked_timdex_dataset_az_identifiers, | ||
| mocked_current_az_identifiers, | ||
| ): | ||
| """Assert that Transformer injects synthetic OAI delete records. | ||
|
|
||
| Example record: | ||
|
|
||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <record> | ||
| <header status="deleted"> | ||
| <identifier>oai:libguides.com:az/1234</identifier> | ||
| <datestamp>2026-08-11T14:13:38Z</datestamp> | ||
| <setSpec>az</setSpec> | ||
| </header> | ||
| <note>This is a synthetic delete record created by Transmogrifier.</note> | ||
| <metadata/> | ||
| </record> | ||
| """ | ||
| deleted_record = next(researchdatabases_transformer._yield_api_records_for_deleting()) | ||
|
|
||
| header = deleted_record.find("record").find("header") | ||
|
|
||
| assert header.get("status") == "deleted" | ||
| assert header.find("identifier").string == "oai:libguides.com:az/1234" | ||
| assert ( | ||
| deleted_record.find("note").string | ||
| == "This is a synthetic delete record created by Transmogrifier." | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "env_var", | ||
| [ | ||
| "LIBGUIDES_API_TOKEN", | ||
| "LIBGUIDES_CLIENT_ID", | ||
| "TIMDEX_DATASET_LOCATION", | ||
| ], | ||
| ) | ||
| def test_researchdatabases_env_vars_not_set_skips_deletes( | ||
| env_var, | ||
| caplog, | ||
| researchdatabases_transformer, | ||
| mocked_current_az_identifiers, | ||
| ): | ||
| caplog.set_level("WARNING") | ||
| with patch(f"transmogrifier.config.{env_var}", None): | ||
| assert list(researchdatabases_transformer._yield_api_records_for_deleting()) == [] | ||
|
|
||
| assert ( | ||
| "Skipping deleting record detection, not all required env vars are set." | ||
| in caplog.text | ||
| ) | ||
|
|
||
|
|
||
| def test_research_databases_custom_parse_source_file_yields_oai_and_synthetic_records( | ||
| researchdatabases_transformer, | ||
| mocked_timdex_dataset_az_identifiers, | ||
| mocked_current_az_identifiers, | ||
| ): | ||
| records = list(researchdatabases_transformer.source_records) | ||
|
|
||
| assert len(records) == 4 # 3 OAI + 1 synthetic delete | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
test_researchdatabases_synthetic_deleted_record_createdto follow our usual test naming convention?