From e97bb16381c5f87026de2d4341bf2c7af2212a64 Mon Sep 17 00:00:00 2001 From: Cecilie Seim <68303562+tilen1976@users.noreply.github.com> Date: Tue, 20 Aug 2024 20:19:33 +0200 Subject: [PATCH] refactor tests --- tests/conftest.py | 17 ----- .../callbacks/test_callbacks_utils.py | 29 +++++++++ .../test_callbacks_validation_utils.py | 64 ------------------- 3 files changed, 29 insertions(+), 81 deletions(-) delete mode 100644 tests/frontend/callbacks/test_callbacks_validation_utils.py diff --git a/tests/conftest.py b/tests/conftest.py index 00a7bd01..33464a5e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -24,9 +24,7 @@ from datadoc import state -from .utils import TEST_EKS_PARQUET from .utils import TEST_EXISTING_METADATA_DIRECTORY -from .utils import TEST_EXISTING_METADATA_NAMING_STANDARD_FILEPATH_NO_MISSING from .utils import TEST_PARQUET_FILE_NAME from .utils import TEST_PARQUET_FILEPATH from .utils import TEST_RESOURCES_DIRECTORY @@ -91,21 +89,6 @@ def metadata( ) -@pytest.fixture() -def metadata_3( - _mock_timestamp: None, - _mock_user_info: None, - subject_mapping_fake_statistical_structure: StatisticSubjectMapping, - tmp_path: Path, -) -> Datadoc: - shutil.copy(TEST_PARQUET_FILEPATH, tmp_path / TEST_EKS_PARQUET) - return Datadoc( - None, - str(TEST_EXISTING_METADATA_NAMING_STANDARD_FILEPATH_NO_MISSING), - statistic_subject_mapping=subject_mapping_fake_statistical_structure, - ) - - @pytest.fixture() def existing_metadata_path() -> Path: return TEST_EXISTING_METADATA_DIRECTORY diff --git a/tests/frontend/callbacks/test_callbacks_utils.py b/tests/frontend/callbacks/test_callbacks_utils.py index 36a00220..a2f804b8 100644 --- a/tests/frontend/callbacks/test_callbacks_utils.py +++ b/tests/frontend/callbacks/test_callbacks_utils.py @@ -1,9 +1,14 @@ +from unittest import mock + +import dash_bootstrap_components as dbc import pytest from dapla_metadata.datasets import model from dash import html +from datadoc import state from datadoc.frontend.callbacks.utils import find_existing_language_string from datadoc.frontend.callbacks.utils import render_tabs +from datadoc.frontend.callbacks.utils import save_metadata_and_generate_alerts from datadoc.frontend.components.identifiers import ACCORDION_WRAPPER_ID from datadoc.frontend.components.identifiers import SECTION_WRAPPER_ID @@ -67,3 +72,27 @@ def test_render_tabs(tab: str, identifier: str): result = render_tabs(tab) assert isinstance(result, html.Article) assert result.children[-1].id == identifier + + +# if none metadata missing: only save alert +# if dataset missing -> +# if variables missing -> +# if another warning -> +# if not n_clicks ? +# if n_clicks and n_clicks > 0 ? +def test_save_and_generate_alerts(): + mock_metadata = mock.Mock() + mock_metadata.variables = [ + "var1", + "var2", + ] + state.metadata = mock_metadata + + result = save_metadata_and_generate_alerts( + mock_metadata, + ) + + num_list_of_alerts = 3 + assert len(result) == num_list_of_alerts + assert result[2] is None + assert isinstance(result[0], dbc.Alert) diff --git a/tests/frontend/callbacks/test_callbacks_validation_utils.py b/tests/frontend/callbacks/test_callbacks_validation_utils.py deleted file mode 100644 index 74cffd60..00000000 --- a/tests/frontend/callbacks/test_callbacks_validation_utils.py +++ /dev/null @@ -1,64 +0,0 @@ -# def test_validation_utils() - - -from unittest import mock - -import dash_bootstrap_components as dbc -from dapla_metadata.datasets import Datadoc - -from datadoc import state -from datadoc.frontend.callbacks.utils import save_metadata_and_generate_alerts -from datadoc.frontend.components.builders import AlertTypes -from datadoc.frontend.components.builders import build_ssb_alert - - -# if none metadata missing: only save alert -# if dataset missing -> -# if variables missing -> -# if another warning -> -# if not n_clicks ? -def test_save_and_validate(metadata: Datadoc, mocker): - # if n_clicks and n_clicks > 0 ? - - success_alert = build_ssb_alert( - AlertTypes.SUCCESS, - "Lagret metadata", - ) - state.metadata = metadata - - mocker.patch( - "datadoc.frontend.callbacks.utils.save_metadata_and_generate_alerts", - return_value=success_alert, - ) - output = save_metadata_and_generate_alerts(metadata) - assert isinstance(output, list) - num_list_of_alerts = 3 - assert len(output) == num_list_of_alerts - assert output[1] is not None - - -def test_with_mock_patch(metadata_3): - state.metadata = metadata_3 - result = save_metadata_and_generate_alerts(metadata_3) - num_list_of_alerts = 3 - assert len(result) == num_list_of_alerts - assert result[1] is None - assert result[2] is None - - -def test_1(): - mock_metadata = mock.Mock() - mock_metadata.variables = [ - "var1", - "var2", - ] - state.metadata = mock_metadata - - result = save_metadata_and_generate_alerts( - mock_metadata, - ) - - num_list_of_alerts = 3 - assert len(result) == num_list_of_alerts - assert result[2] is None - assert isinstance(result[0], dbc.Alert)