Skip to content

Commit

Permalink
Inherit dataset value (#322)
Browse files Browse the repository at this point in the history
* use dcc tabs

* Target css selectors for Tabs

* override dash default styling selected tab @media

* remove bundle css - add ssb-styling tabs

* return tab content conditionally

* remove unnecessary input in callback

* remove buil ssb tab

* remove hover effect on selected tab

* update css

* Move method to utils - add test

* add test for build method

* correct merge error

* method for set temporality value on variables based on dataset value

* method for population - datasource and temporality

* remove deleted method

* update name

* Log period value

* Add test for set variables values inherited from dataset

* add test for possible to change variables value after inherit from dataset

* update tests and refactor code

* method for only updating the dataset value that is changed

* method ok for data source and temporality type - not ok population description

* update test

* remove multilanguage from test - method not ready for this case yet

* update docstring

* remove population description
  • Loading branch information
tilen1976 authored Apr 26, 2024
1 parent aaf795d commit cf8ad5c
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/datadoc/frontend/callbacks/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
from datadoc.frontend.callbacks.utils import find_existing_language_string
from datadoc.frontend.callbacks.utils import get_dataset_path
from datadoc.frontend.callbacks.utils import parse_and_validate_dates
from datadoc.frontend.callbacks.variables import (
set_variables_values_inherited_from_dataset,
)
from datadoc.frontend.components.builders import AlertTypes
from datadoc.frontend.components.builders import build_ssb_alert
from datadoc.frontend.fields.display_dataset import DISPLAY_DATASET
Expand Down Expand Up @@ -172,6 +175,7 @@ def accept_dataset_metadata_input(
metadata_identifier,
value,
)
set_variables_values_inherited_from_dataset(value, metadata_identifier)
except (ValidationError, ValueError):
show_error = True
error_explanation = INVALID_VALUE
Expand All @@ -195,7 +199,6 @@ def accept_dataset_metadata_date_input(
) -> tuple[bool, str, bool, str]:
"""Validate and save date range inputs."""
message = ""

try:
(
parsed_contains_data_from,
Expand All @@ -214,7 +217,6 @@ def accept_dataset_metadata_date_input(
state.metadata.dataset.contains_data_until = (
parsed_contains_data_until.isoformat()
)

except (ValidationError, ValueError) as e:
logger.exception(
"Validation failed for %s, %s, %s: %s, %s",
Expand Down
29 changes: 29 additions & 0 deletions src/datadoc/frontend/callbacks/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from datadoc.frontend.callbacks.utils import parse_and_validate_dates
from datadoc.frontend.components.builders import build_edit_section
from datadoc.frontend.components.builders import build_ssb_accordion
from datadoc.frontend.fields.display_dataset import DatasetIdentifiers
from datadoc.frontend.fields.display_variables import DISPLAY_VARIABLES
from datadoc.frontend.fields.display_variables import (
MULTIPLE_LANGUAGE_VARIABLES_METADATA,
Expand Down Expand Up @@ -232,3 +233,31 @@ def accept_variable_metadata_date_input(
if variable_identifier == VariableIdentifiers.CONTAINS_DATA_FROM
else no_error + error
)


def get_corresponding_identifier(
metadata_identifier: str,
) -> str | None:
"""Get the corresponding variables identifier for dataset identifier."""
match metadata_identifier:
case DatasetIdentifiers.TEMPORALITY_TYPE:
return VariableIdentifiers.TEMPORALITY_TYPE
case DatasetIdentifiers.DATA_SOURCE:
return VariableIdentifiers.DATA_SOURCE
case _:
return None


def set_variables_values_inherited_from_dataset(
value: MetadataInputTypes | LanguageStringType,
metadata_identifier: str,
) -> None:
"""Set variable value based on dataset value."""
variable = get_corresponding_identifier(metadata_identifier)
if value is not None and variable is not None:
for val in state.metadata.variables:
setattr(
state.metadata.variables_lookup[val.short_name],
variable,
value,
)
80 changes: 80 additions & 0 deletions tests/frontend/callbacks/test_variables_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
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
from datadoc.frontend.callbacks.variables import (
set_variables_values_inherited_from_dataset,
)
from datadoc.frontend.fields.display_base import get_standard_metadata
from datadoc.frontend.fields.display_dataset import DatasetIdentifiers
from datadoc.frontend.fields.display_variables import DISPLAY_VARIABLES
from datadoc.frontend.fields.display_variables import VariableIdentifiers
from datadoc.frontend.text import INVALID_DATE_ORDER
Expand Down Expand Up @@ -298,3 +303,78 @@ def test_populate_variables_workspace_filter_variables(
)
== expected_length
)


def test_update_variables_values_from_dataset_values(metadata: DataDocMetadata):
state.metadata = metadata
dataset_temporality_type = "FIXED"
dataset_data_source = None
setattr(
state.metadata.dataset,
DatasetIdentifiers.TEMPORALITY_TYPE,
dataset_temporality_type,
)
setattr(
state.metadata.dataset,
DatasetIdentifiers.DATA_SOURCE,
dataset_data_source,
)
set_variables_values_inherited_from_dataset(
dataset_temporality_type,
DatasetIdentifiers.TEMPORALITY_TYPE,
)
for val in state.metadata.variables:
assert metadata.dataset.temporality_type == get_standard_metadata(
metadata.variables_lookup[val.short_name],
VariableIdentifiers.TEMPORALITY_TYPE.value,
)
set_variables_values_inherited_from_dataset(
dataset_data_source,
DatasetIdentifiers.DATA_SOURCE,
)
for val in state.metadata.variables:
assert metadata.dataset.data_source == get_standard_metadata(
metadata.variables_lookup[val.short_name],
VariableIdentifiers.DATA_SOURCE.value,
)


def test_variables_value_can_be_changed_after_update_from_dataset_value(
metadata: DataDocMetadata,
):
state.metadata = metadata
dataset_temporality_type = "FIXED"
setattr(
state.metadata.dataset,
DatasetIdentifiers.TEMPORALITY_TYPE,
dataset_temporality_type,
)
set_variables_values_inherited_from_dataset(
dataset_temporality_type,
DatasetIdentifiers.TEMPORALITY_TYPE,
)
for val in state.metadata.variables:
assert metadata.dataset.temporality_type == get_standard_metadata(
metadata.variables_lookup[val.short_name],
VariableIdentifiers.TEMPORALITY_TYPE.value,
)
setattr(
state.metadata.variables_lookup["pers_id"],
VariableIdentifiers.TEMPORALITY_TYPE,
enums.TemporalityTypeType.ACCUMULATED,
)
assert dataset_temporality_type == get_standard_metadata(
metadata.variables_lookup["sivilstand"],
VariableIdentifiers.TEMPORALITY_TYPE.value,
)
assert dataset_temporality_type != get_standard_metadata(
metadata.variables_lookup["pers_id"],
VariableIdentifiers.TEMPORALITY_TYPE.value,
)
assert (
get_standard_metadata(
metadata.variables_lookup["pers_id"],
VariableIdentifiers.TEMPORALITY_TYPE.value,
)
== "ACCUMULATED"
)

0 comments on commit cf8ad5c

Please sign in to comment.