Skip to content

Commit

Permalink
use local import to solve circular import - tests in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
tilen1976 committed Aug 20, 2024
1 parent 45bf94c commit f95dc2b
Show file tree
Hide file tree
Showing 11 changed files with 544 additions and 61 deletions.
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions src/datadoc/frontend/callbacks/register_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
from datadoc.frontend.callbacks.dataset import accept_dataset_metadata_input
from datadoc.frontend.callbacks.dataset import open_dataset_handling
from datadoc.frontend.callbacks.utils import render_tabs
from datadoc.frontend.callbacks.validation_utils import (
save_metadata_and_generate_alerts,
)
from datadoc.frontend.callbacks.utils import save_metadata_and_generate_alerts
from datadoc.frontend.callbacks.variables import accept_variable_metadata_date_input
from datadoc.frontend.callbacks.variables import accept_variable_metadata_input
from datadoc.frontend.callbacks.variables import populate_variables_workspace
Expand Down Expand Up @@ -73,7 +71,7 @@ def callback_save_metadata_file(
If none return no_update.
"""
if n_clicks and n_clicks > 0:
return save_metadata_and_generate_alerts()
return save_metadata_and_generate_alerts(state.metadata)

return no_update

Expand Down
45 changes: 45 additions & 0 deletions src/datadoc/frontend/callbacks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@

import datetime
import logging
import warnings
from typing import TYPE_CHECKING
from typing import TypeAlias

import arrow
import ssb_dash_components as ssb
from dapla_metadata.datasets import Datadoc
from dapla_metadata.datasets import ObligatoryDatasetWarning
from dapla_metadata.datasets import ObligatoryVariableWarning
from dapla_metadata.datasets import model
from dash import html

from datadoc import config
from datadoc import state
from datadoc.frontend.components.builders import AlertTypes
from datadoc.frontend.components.builders import build_ssb_alert
from datadoc.frontend.components.identifiers import ACCORDION_WRAPPER_ID
from datadoc.frontend.components.identifiers import SECTION_WRAPPER_ID
from datadoc.frontend.components.identifiers import VARIABLES_INFORMATION_ID
Expand Down Expand Up @@ -226,3 +232,42 @@ def render_tabs(tab: str) -> html.Article | None:
)

return None


def save_metadata_and_generate_alerts(metadata: Datadoc) -> list:
"""Save the metadata document to disk and check obligatory metadata.
Returns:
List of alerts including obligatory metadata warnings if missing,
and success alert if metadata is saved correctly.
"""
from datadoc.frontend.callbacks.dataset import dataset_control
from datadoc.frontend.callbacks.variables import variables_control

missing_obligatory_dataset = ""
missing_obligatory_variables = ""

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
metadata.write_metadata_document()
success_alert = build_ssb_alert(
AlertTypes.SUCCESS,
"Lagret metadata",
)

for warning in w:
if issubclass(warning.category, ObligatoryDatasetWarning):
missing_obligatory_dataset = str(warning.message)
elif issubclass(warning.category, ObligatoryVariableWarning):
missing_obligatory_variables = str(warning.message)
else:
logger.warning(
"An unexpected warning was caught: %s",
warning.message,
)

return [
success_alert,
dataset_control(missing_obligatory_dataset),
variables_control(missing_obligatory_variables, metadata.variables),
]
51 changes: 0 additions & 51 deletions src/datadoc/frontend/callbacks/validation_utils.py

This file was deleted.

6 changes: 4 additions & 2 deletions src/datadoc/frontend/callbacks/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def _get_dict_by_key(
return next((item for item in metadata_list if key in item), None)


def variables_control(error_message: str | None) -> dbc.Alert | None:
def variables_control(error_message: str | None, variables: list) -> dbc.Alert | None:
"""Check obligatory metadata for variables and return an alert if any metadata is missing.
This function parses an error message to identify missing obligatory metadata
Expand All @@ -379,13 +379,15 @@ def variables_control(error_message: str | None) -> dbc.Alert | None:
Args:
error_message: A message generated by ObligatoryVariableWarning
containing the variable short name and a list of field names with missing values.
variables: list of datadoc variables
Returns:
An alert object if there are missing metadata fields, otherwise None.
"""
missing_metadata: list = []
error_message_parsed = _parse_error_message(str(error_message))
for variable in state.metadata.variables:
# for variable in state.metadata.variables:
for variable in variables:
if error_message_parsed:
fields_by_variable = _get_dict_by_key(
error_message_parsed,
Expand Down
17 changes: 17 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@

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
Expand Down Expand Up @@ -89,6 +91,21 @@ 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
Expand Down
64 changes: 64 additions & 0 deletions tests/frontend/callbacks/test_callbacks_validation_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# 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)
1 change: 0 additions & 1 deletion tests/frontend/callbacks/test_validation_utils.py

This file was deleted.

4 changes: 2 additions & 2 deletions tests/frontend/callbacks/test_variables_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ def test_variables_metadata_control_return_alert(metadata: Datadoc):
state.metadata.write_metadata_document()
if issubclass(w[1].category, ObligatoryVariableWarning):
missing_metadata = str(w[1].message)
result = variables_control(missing_metadata)
result = variables_control(missing_metadata, metadata.variables)
assert isinstance(result, dbc.Alert)


Expand Down Expand Up @@ -632,5 +632,5 @@ def test_variables_metadata_control_dont_return_alert(metadata: Datadoc):
state.metadata.write_metadata_document()
if issubclass(w[0].category, ObligatoryVariableWarning):
missing_metadata = str(w[0].message)
result = variables_control(missing_metadata)
result = variables_control(missing_metadata, metadata.variables)
assert result is None
Loading

0 comments on commit f95dc2b

Please sign in to comment.