Skip to content

Commit 6b6373f

Browse files
committed
fix(tests): ensure mapping suite/package is loaded for normalisation
Tests were failing with ModelNotFoundError because: - Notice fixtures didn't set mapping_package_identifier - Mapping suite/package weren't loaded into test MongoDB instances - normalise_notice() calls didn't pass mongodb_client parameter Changes: - Add load_mapping_suite_and_package fixture to features/conftest.py - Update notice fixtures to set mapping_package_identifier - Pass mongodb_client to normalise_notice() in test steps - Add load_mapping_suite_and_package_fake for e2e tests using mongomock - Update e2e fixtures to link GitHub-loaded packages to local mapping suite Fixes 30+ e2e/feature tests that were failing after metadata resource refactoring with dynamic MS Config loading via MSSDK.
1 parent 024f86a commit 6b6373f

9 files changed

Lines changed: 81 additions & 22 deletions

File tree

test/e2e/conftest.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,13 @@ def load_mapping_suite_and_package(mongodb_client, mapping_suite, mapping_packag
8787
mapping_suite_repository = MappingSuiteRepositoryMongoDB(mongodb_client=mongodb_client)
8888
mapping_suite_repository.add(mapping_suite=mapping_suite)
8989
mapping_package_repository = MappingPackageRepositoryMongoDB(mongodb_client=mongodb_client)
90-
mapping_package_repository.add(mapping_package=mapping_package)
90+
mapping_package_repository.add(mapping_package=mapping_package)
91+
92+
93+
@pytest.fixture
94+
def load_mapping_suite_and_package_fake(fake_mongodb_client, mapping_suite, mapping_package):
95+
"""Load mapping suite and package into fake MongoDB (mongomock) for tests."""
96+
mapping_suite_repository = MappingSuiteRepositoryMongoDB(mongodb_client=fake_mongodb_client)
97+
mapping_suite_repository.add(mapping_suite=mapping_suite)
98+
mapping_package_repository = MappingPackageRepositoryMongoDB(mongodb_client=fake_mongodb_client)
99+
mapping_package_repository.add(mapping_package=mapping_package)

test/e2e/dags/pipelines/test_notice_processor_pipelines.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
notice_validation_pipeline, notice_package_pipeline, notice_publish_pipeline
44
from src.ted_sws.core.model.notice import NoticeStatus
55
from src.ted_sws.data_manager.adapters.notice_repository import NoticeRepository
6+
from src.ted_sws.data_manager.adapters.mapping_package_repository import MappingPackageRepositoryMongoDB
67
from src.ted_sws.mapping_suite_processor.services.mapping_package_processor import \
78
load_mapping_suite_and_packages_from_github_to_mongo_db
89

@@ -11,12 +12,20 @@
1112
NOTICE_ID = "057215-2021"
1213

1314

14-
def test_notice_processor_pipelines(fake_mongodb_client):
15+
def test_notice_processor_pipelines(fake_mongodb_client, load_mapping_suite_and_package_fake, mapping_suite):
1516
load_mapping_suite_and_packages_from_github_to_mongo_db(
1617
mapping_package_name=MAPPING_PACKAGE_NAME,
1718
mongodb_client=fake_mongodb_client,
1819
load_test_data=True
1920
)
21+
22+
# Update GitHub-loaded packages to use the local mapping suite
23+
mapping_package_repository = MappingPackageRepositoryMongoDB(mongodb_client=fake_mongodb_client)
24+
for pkg in mapping_package_repository.list():
25+
if not pkg.mapping_suite_identifier:
26+
pkg.mapping_suite_identifier = mapping_suite.id
27+
mapping_package_repository.update(pkg)
28+
2029
notice_id = NOTICE_ID
2130
notice_repository = NoticeRepository(mongodb_client=fake_mongodb_client)
2231
notice = notice_repository.get(reference=notice_id)

test/e2e/data_manager/test_mongodb_client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def test_mongo_db_query_2():
139139
])
140140

141141

142-
def test_create_matview_for_notices(fake_mongodb_client):
142+
def test_create_matview_for_notices(fake_mongodb_client, load_mapping_suite_and_package_fake, mapping_package):
143143
notice_id = "696661-2022"
144144
ted_api_query = {"query": f"ND={notice_id}"}
145145
mongodb_client = fake_mongodb_client
@@ -152,8 +152,9 @@ def test_create_matview_for_notices(fake_mongodb_client):
152152
notice_event.caller_name = "execute"
153153
notice_event.start_record()
154154
notice = notice_repository.get(reference=notice_id)
155+
notice.mapping_package_identifier = mapping_package.id
155156
indexed_notice = index_notice(notice=notice)
156-
normalised_notice = normalise_notice(notice=indexed_notice)
157+
normalised_notice = normalise_notice(notice=indexed_notice, mongodb_client=mongodb_client)
157158
notice = normalised_notice
158159
notice_repository.update(notice=normalised_notice)
159160
notice_event.end_record()

test/e2e/data_sampler/conftest.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from src.ted_sws import config
55
from src.ted_sws.data_manager.adapters.notice_repository import NoticeRepository
6+
from src.ted_sws.data_manager.adapters.mapping_package_repository import MappingPackageRepositoryMongoDB
67
from src.ted_sws.data_sampler.services.notice_xml_indexer import index_notice
78
from src.ted_sws.mapping_suite_processor.services.mapping_package_processor import \
89
load_mapping_suite_and_packages_from_github_to_mongo_db
@@ -21,13 +22,30 @@ def mongodb_client():
2122

2223

2324
@pytest.fixture
24-
def notice_repository_with_indexed_notices(mongodb_client, load_mapping_suite_and_package) -> NoticeRepository:
25+
def notice_repository_with_indexed_notices(mongodb_client, load_mapping_suite_and_package, mapping_suite, mapping_package) -> NoticeRepository:
26+
"""Load notices from GitHub and ensure they reference the local mapping suite/package.
2527
28+
This fixture:
29+
1. Uses load_mapping_suite_and_package to store local suite/package in MongoDB
30+
2. Downloads packages from GitHub (which loads test notices into the repository)
31+
3. Updates all packages loaded from GitHub to reference the local mapping suite
32+
4. Updates all notices to reference those packages
33+
5. Indexes and normalizes all notices
34+
"""
2635
load_mapping_suite_and_packages_from_github_to_mongo_db(
2736
mapping_package_name="package_F03_test",
2837
mongodb_client=mongodb_client,
2938
load_test_data=True
3039
)
40+
41+
# Update GitHub-loaded packages to use the local mapping suite
42+
mapping_package_repository = MappingPackageRepositoryMongoDB(mongodb_client=mongodb_client)
43+
for pkg in mapping_package_repository.list():
44+
if pkg.id != mapping_package.id and not pkg.mapping_suite_identifier:
45+
# This is a GitHub-loaded package without mapping_suite_identifier
46+
pkg.mapping_suite_identifier = mapping_suite.id
47+
mapping_package_repository.update(pkg)
48+
3149
notice_repository = NoticeRepository(mongodb_client=mongodb_client)
3250
for notice in notice_repository.list():
3351
indexed_notice = index_notice(notice=notice)

test/features/conftest.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from src.ted_sws.core.model.metadata import NormalisedMetadata, XMLMetadata
1111
from src.ted_sws.core.model.notice import NoticeStatus, Notice
1212
from src.ted_sws.data_manager.adapters.notice_repository import NoticeRepository
13+
from src.ted_sws.data_manager.adapters.mapping_package_repository import MappingPackageRepositoryMongoDB
14+
from src.ted_sws.data_manager.adapters.mapping_suite_repository import MappingSuiteRepositoryMongoDB
1315
from src.ted_sws.notice_fetcher.adapters.ted_api import TedAPIAdapter, TedRequestAPI
1416
from src.ted_sws.notice_fetcher.services.notice_fetcher import NoticeFetcher
1517

@@ -41,36 +43,48 @@ def notice_repository(mongodb_client):
4143

4244

4345
@pytest.fixture
44-
def f03_notice_2020(notice_repository, ted_api_end_point):
46+
def load_mapping_suite_and_package(mongodb_client, mapping_suite, mapping_package):
47+
"""Load mapping suite and package into MongoDB for tests that need mapping resources."""
48+
mapping_suite_repository = MappingSuiteRepositoryMongoDB(mongodb_client=mongodb_client)
49+
mapping_suite_repository.add(mapping_suite=mapping_suite)
50+
mapping_package_repository = MappingPackageRepositoryMongoDB(mongodb_client=mongodb_client)
51+
mapping_package_repository.add(mapping_package=mapping_package)
52+
53+
54+
@pytest.fixture
55+
def f03_notice_2020(notice_repository, ted_api_end_point, load_mapping_suite_and_package, mapping_package):
4556
notice_search_query = {"query": "ND=408313-2020"}
4657
NoticeFetcher(notice_repository=notice_repository,
4758
ted_api_adapter=TedAPIAdapter(request_api=TedRequestAPI(),
4859
ted_api_url=ted_api_end_point)).fetch_notices_by_query(
4960
query=notice_search_query)
5061
notice = notice_repository.get(reference="408313-2020")
5162
notice.set_xml_metadata(xml_metadata=XMLMetadata(unique_xpaths=["FAKE_INDEX_XPATHS"]))
63+
notice.mapping_package_identifier = mapping_package.id
5264
return notice
5365

5466
@pytest.fixture
55-
def eForm_notice_2023(notice_repository, ted_api_end_point):
67+
def eForm_notice_2023(notice_repository, ted_api_end_point, load_mapping_suite_and_package, mapping_package):
5668
notice_search_query = {"query": "ND=17554-2024"}
5769
NoticeFetcher(notice_repository=notice_repository,
5870
ted_api_adapter=TedAPIAdapter(request_api=TedRequestAPI(),
5971
ted_api_url=ted_api_end_point)).fetch_notices_by_query(
6072
query=notice_search_query)
6173
notice = notice_repository.get(reference="17554-2024")
6274
notice.set_xml_metadata(xml_metadata=XMLMetadata(unique_xpaths=["FAKE_INDEX_XPATHS"]))
75+
notice.mapping_package_identifier = mapping_package.id
6376
return notice
6477

6578
@pytest.fixture
66-
def f18_notice_2022(notice_repository, ted_api_end_point):
79+
def f18_notice_2022(notice_repository, ted_api_end_point, load_mapping_suite_and_package, mapping_package):
6780
notice_search_query = {"query": "ND=67623-2022"}
6881
NoticeFetcher(notice_repository=notice_repository,
6982
ted_api_adapter=TedAPIAdapter(request_api=TedRequestAPI(),
7083
ted_api_url=ted_api_end_point)).fetch_notices_by_query(
7184
query=notice_search_query)
7285
notice = notice_repository.get(reference="67623-2022")
7386
notice.set_xml_metadata(xml_metadata=XMLMetadata(unique_xpaths=["FAKE_INDEX_XPATHS"]))
87+
notice.mapping_package_identifier = mapping_package.id
7488
return notice
7589

7690

test/features/notice_metadata_processor/conftest.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,18 @@ def notice_eligibility_repository_path():
3333

3434

3535
@pytest.fixture
36-
def normalised_notice(notice_2020):
36+
def normalised_notice(notice_2020, load_mapping_suite_and_package, mapping_package, mongodb_client):
3737
notice = notice_2020.copy()
38-
normalise_notice(notice=notice)
38+
notice.mapping_package_identifier = mapping_package.id
39+
normalise_notice(notice=notice, mongodb_client=mongodb_client)
3940
return notice
4041

4142

4243
@pytest.fixture
43-
def normalised_eForm_notice(indexed_eform_notice_622690):
44+
def normalised_eForm_notice(indexed_eform_notice_622690, load_mapping_suite_and_package, mapping_package, mongodb_client):
4445
notice = indexed_eform_notice_622690.copy()
45-
normalise_notice(notice=notice)
46+
notice.mapping_package_identifier = mapping_package.id
47+
normalise_notice(notice=notice, mongodb_client=mongodb_client)
4648
return notice
4749

4850

@@ -65,10 +67,13 @@ def sample_ef_html_unsafe_notice_path() -> pathlib.Path:
6567

6668
@pytest.fixture
6769
def sample_indexed_ef_html_unsafe_notice(
68-
sample_ef_html_unsafe_notice_path: pathlib.Path) -> Notice:
70+
sample_ef_html_unsafe_notice_path: pathlib.Path,
71+
load_mapping_suite_and_package,
72+
mapping_package) -> Notice:
6973
notice: Notice = Notice(ted_id=sample_ef_html_unsafe_notice_path.name)
7074
notice.set_xml_manifestation(
7175
XMLManifestation(object_data=sample_ef_html_unsafe_notice_path.read_text()))
76+
notice.mapping_package_identifier = mapping_package.id
7277

7378
return index_notice(notice)
7479

@@ -80,10 +85,13 @@ def sample_sf_html_unsafe_notice_path() -> pathlib.Path:
8085

8186
@pytest.fixture
8287
def sample_indexed_sf_html_unsafe_notice(
83-
sample_sf_html_unsafe_notice_path: pathlib.Path) -> Notice:
88+
sample_sf_html_unsafe_notice_path: pathlib.Path,
89+
load_mapping_suite_and_package,
90+
mapping_package) -> Notice:
8491
notice: Notice = Notice(ted_id=sample_sf_html_unsafe_notice_path.name)
8592
notice.set_xml_manifestation(
8693
XMLManifestation(object_data=sample_sf_html_unsafe_notice_path.read_text()))
94+
notice.mapping_package_identifier = mapping_package.id
8795

8896
return index_notice(notice)
8997

test/features/notice_metadata_processor/test_metadata_normaliser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ def step_impl(f03_notice_2020):
1515

1616

1717
@when("the normalize process is executed")
18-
def step_impl(notice):
19-
normalise_notice(notice=notice)
18+
def step_impl(notice, mongodb_client):
19+
normalise_notice(notice=notice, mongodb_client=mongodb_client)
2020

2121

2222
@then(parsers.parse("a normalized notice {metadata} is {possibly} available"))

test/features/notice_metadata_processor/test_metadata_normaliser_eforms.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ def step_impl(eForm_notice_2023):
1515

1616

1717
@when("the normalise process is executed")
18-
def step_impl(notice):
19-
normalise_notice(notice=notice)
18+
def step_impl(notice, mongodb_client):
19+
normalise_notice(notice=notice, mongodb_client=mongodb_client)
2020

2121

2222
@then(parsers.parse("a normalised notice {metadata} is {possibly} available"))

test/features/notice_metadata_processor/test_notice_normalisation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ def an_sf_notice_with_spaces_in_notice_id(sample_indexed_sf_html_unsafe_notice:
4242

4343

4444
@when('the EF notice is normalised', target_fixture='normalised_ef_notice')
45-
def the_ef_notice_is_normalised(sample_indexed_ef_html_unsafe_notice: Notice) -> Notice:
45+
def the_ef_notice_is_normalised(sample_indexed_ef_html_unsafe_notice: Notice, mongodb_client) -> Notice:
4646
"""Normalize EF notice."""
47-
return normalise_notice(sample_indexed_ef_html_unsafe_notice)
47+
return normalise_notice(sample_indexed_ef_html_unsafe_notice, mongodb_client=mongodb_client)
4848

4949

5050
@when('the SF notice is normalised', target_fixture='normalised_sf_notice')
51-
def the_sf_notice_is_normalised(sample_indexed_sf_html_unsafe_notice: Notice) -> Notice:
51+
def the_sf_notice_is_normalised(sample_indexed_sf_html_unsafe_notice: Notice, mongodb_client) -> Notice:
5252
"""Normalize SF notice."""
53-
return normalise_notice(sample_indexed_sf_html_unsafe_notice)
53+
return normalise_notice(sample_indexed_sf_html_unsafe_notice, mongodb_client=mongodb_client)
5454

5555

5656
@then('the EF notice ID should not contain leading or trailing spaces')

0 commit comments

Comments
 (0)