From 1d7c86e4420576a076f5bbd6800883c544db3b09 Mon Sep 17 00:00:00 2001 From: Nicolas Drebenstedt Date: Thu, 19 Dec 2024 17:18:59 +0100 Subject: [PATCH] fix tests --- mex/editor/edit/transform.py | 4 +++- tests/conftest.py | 22 +++++++++++++++++++++- tests/edit/test_main.py | 11 ++++++----- tests/edit/test_transform.py | 18 ++---------------- tests/search/test_main.py | 8 +++----- 5 files changed, 35 insertions(+), 28 deletions(-) diff --git a/mex/editor/edit/transform.py b/mex/editor/edit/transform.py index 909fdae..e15ffc8 100644 --- a/mex/editor/edit/transform.py +++ b/mex/editor/edit/transform.py @@ -113,7 +113,9 @@ def transform_models_to_fields( """ fields_by_name = { field_name: EditorField(name=field_name, primary_sources=[]) - for field_name in {f for m in models for f in m.get_all_fields()} + for field_name in { + f for m in models for f in MERGEABLE_FIELDS_BY_CLASS_NAME[m.entityType] + } } for model in models: _transform_model_to_editor_primary_source( diff --git a/tests/conftest.py b/tests/conftest.py index 51a0f08..52f010a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -13,8 +13,10 @@ ExtractedOrganizationalUnit, ExtractedPrimarySource, ) +from mex.common.settings import BaseSettings from mex.common.types import ( Email, + IdentityProvider, Link, Text, TextLanguage, @@ -22,7 +24,11 @@ YearMonthDay, ) from mex.editor.settings import EditorSettings -from mex.editor.types import EditorUserDatabase, EditorUserPassword +from mex.editor.types import ( + EditorIdentityProvider, + EditorUserDatabase, + EditorUserPassword, +) from mex.mex import app pytest_plugins = ("mex.common.testing.plugin",) @@ -41,6 +47,20 @@ def settings() -> EditorSettings: return EditorSettings.get() +@pytest.fixture(autouse=True) +def set_identity_provider(is_integration_test: bool, monkeypatch: MonkeyPatch) -> None: + """Ensure the identifier provider is set correctly for unit and int tests.""" + # TODO(ND): clean this up after MX-1708 + for settings in (BaseSettings.get(), EditorSettings.get()): + if is_integration_test: + monkeypatch.setitem(settings.model_config, "validate_assignment", False) + monkeypatch.setattr( + settings, "identity_provider", EditorIdentityProvider.BACKEND + ) + else: + monkeypatch.setattr(settings, "identity_provider", IdentityProvider.MEMORY) + + @pytest.fixture def frontend_url() -> str: """Return the URL of the current local frontend server for testing.""" diff --git a/tests/edit/test_main.py b/tests/edit/test_main.py index 2c24abe..6050c98 100644 --- a/tests/edit/test_main.py +++ b/tests/edit/test_main.py @@ -4,6 +4,7 @@ import pytest from playwright.sync_api import Page, expect +from mex.common.fields import MERGEABLE_FIELDS_BY_CLASS_NAME from mex.common.models import AnyExtractedModel, ExtractedActivity @@ -45,13 +46,13 @@ def test_edit_heading(edit_page: Page) -> None: @pytest.mark.integration def test_edit_fields(edit_page: Page, extracted_activity: ExtractedActivity) -> None: page = edit_page - identifier_in_primary_source = page.get_by_test_id( - "field-identifierInPrimarySource-name" - ) + funding_program = page.get_by_test_id("field-fundingProgram-name") page.screenshot(path="tests_edit_test_main-test_edit_fields.png") - expect(identifier_in_primary_source).to_be_visible() + expect(funding_program).to_be_visible() all_fields = page.get_by_role("row").all() - assert len(all_fields) == len(extracted_activity.model_fields) + assert len(all_fields) == len( + MERGEABLE_FIELDS_BY_CLASS_NAME[extracted_activity.entityType] + ) @pytest.mark.integration diff --git a/tests/edit/test_transform.py b/tests/edit/test_transform.py index 533ff7f..ff307da 100644 --- a/tests/edit/test_transform.py +++ b/tests/edit/test_transform.py @@ -1,5 +1,6 @@ import pytest +from mex.common.fields import MERGEABLE_FIELDS_BY_CLASS_NAME from mex.common.models import ( MEX_PRIMARY_SOURCE_STABLE_TARGET_ID, AdditiveActivity, @@ -280,23 +281,8 @@ def test_transform_models_to_fields() -> None: preventive=PreventivePerson(memberOf=[MEX_PRIMARY_SOURCE_STABLE_TARGET_ID]), ) - assert len(editor_fields) == len(MergedPerson.get_all_fields()) + assert len(editor_fields) == len(MERGEABLE_FIELDS_BY_CLASS_NAME["MergedPerson"]) fields_by_name = {f.name: f for f in editor_fields} - assert fields_by_name["entityType"] == EditorField( - name="entityType", - primary_sources=[ - EditorPrimarySource( - name=EditorValue(text="00000000000000", href="/item/00000000000000"), - identifier=MergedPrimarySourceIdentifier("00000000000000"), - editor_values=[EditorValue(text="MergedPerson")], - ), - EditorPrimarySource( - name=EditorValue(text="00000000000000", href="/item/00000000000000"), - identifier=MergedPrimarySourceIdentifier("00000000000000"), - editor_values=[EditorValue(text="AdditivePerson")], - ), - ], - ) assert fields_by_name["givenName"] == EditorField( name="givenName", primary_sources=[ diff --git a/tests/search/test_main.py b/tests/search/test_main.py index 54173ee..6c434ef 100644 --- a/tests/search/test_main.py +++ b/tests/search/test_main.py @@ -19,14 +19,12 @@ def test_index(frontend_url: str, writer_user_page: Page) -> None: expect(page.get_by_text("showing 7 of total 7 items found")).to_be_visible() # check mex primary source is showing - primary_source = page.get_by_text( - re.compile(r"^00000000000000\s*MergedPrimarySource$") - ) - expect(primary_source).to_be_visible() + primary_source = page.get_by_text(re.compile(r"^PrimarySource$")) + expect(primary_source.first).to_be_visible() # check activity is showing activity = page.get_by_text( - re.compile(r"Aktivität 1\s*de\s*A1.*24\. Dezember 1999\s*1\. Januar 2023") + re.compile(r"Aktivität 1\s*de\s*A1.*24\. Dezember 1999\s*day\s*1\. Januar 2023") ) activity.scroll_into_view_if_needed() expect(activity).to_be_visible()