Skip to content

Commit

Permalink
Inherit dataset value - multilanguage (#326)
Browse files Browse the repository at this point in the history
* add population description - issue inherits both ways

* Method for multilanguage updates only one value in fieldset

* add TODO test

* Test multilanguage can inherit and can be changed for each variable

* update test multilanguage

* replace method with too many branches with dict

* Update test names
  • Loading branch information
tilen1976 authored Apr 29, 2024
1 parent b6ad1d3 commit fa66260
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/datadoc/frontend/callbacks/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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_value_multilanguage
from datadoc.frontend.callbacks.variables import (
set_variables_values_inherited_from_dataset,
)
Expand Down Expand Up @@ -117,6 +118,7 @@ def process_keyword(value: str) -> list[str]:
return [item.strip() for item in value.split(",")]


# legge inn population update her?
def process_special_cases(
value: MetadataInputTypes | LanguageStringType,
metadata_identifier: str,
Expand Down Expand Up @@ -147,6 +149,7 @@ def process_special_cases(
metadata_identifier,
language,
)
set_variables_value_multilanguage(value, metadata_identifier, language)
elif metadata_identifier in DROPDOWN_DATASET_METADATA_IDENTIFIERS and value == "":
updated_value = None
else:
Expand Down
32 changes: 32 additions & 0 deletions src/datadoc/frontend/callbacks/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,38 @@ def variable_identifier(
return metadata_identifiers.get(dataset_identifier)


def variable_identifier_multilanguage(
dataset_identifier: str,
) -> str | None:
"""Pair corresponding identifiers for multilanguage fields."""
metadata_identifiers = {
"population_description": VariableIdentifiers.POPULATION_DESCRIPTION,
}
return metadata_identifiers.get(dataset_identifier)


def set_variables_value_multilanguage(
value: MetadataInputTypes | LanguageStringType,
metadata_identifier: str,
language: str,
) -> None:
"""Set variable multilanguage value based on dataset value."""
variable = variable_identifier_multilanguage(metadata_identifier)
if value is not None and variable is not None:
for val in state.metadata.variables:
update_value = handle_multi_language_metadata(
variable,
value,
val.short_name,
language,
)
setattr(
state.metadata.variables_lookup[val.short_name],
variable,
update_value,
)


def set_variables_values_inherited_from_dataset(
value: MetadataInputTypes | LanguageStringType,
metadata_identifier: str,
Expand Down
79 changes: 78 additions & 1 deletion tests/frontend/callbacks/test_variables_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@

import arrow
import pytest
from datadoc_model.model import LanguageStringTypeItem
from pydantic_core import Url

from datadoc import enums
from datadoc import state
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_value_multilanguage
from datadoc.frontend.callbacks.variables import (
set_variables_values_inherited_from_dataset,
)
from datadoc.frontend.fields.display_base import get_metadata_and_stringify
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
Expand Down Expand Up @@ -396,7 +399,7 @@ def test_variables_values_inherit_dataset_values(
),
],
)
def test_variables_values_can_be_changed_after_update_from_dataset_value(
def test_variables_values_can_be_changed_after_inherit_dataset_value(
dataset_value,
dataset_identifier,
variable_identifier,
Expand Down Expand Up @@ -438,3 +441,77 @@ def test_variables_values_can_be_changed_after_update_from_dataset_value(
)
== update_value
)


def test_variables_values_multilanguage_inherit_dataset_values(
metadata: DataDocMetadata,
):
state.metadata = metadata
dataset_population_description = "Personer bosatt i Norge"
dataset_population_description_language_item = [
LanguageStringTypeItem(
languageCode="nb",
languageText="Personer bosatt i Norge",
),
]
metadata_identifier = DatasetIdentifiers.POPULATION_DESCRIPTION
language = "nb"
setattr(
state.metadata.dataset,
metadata_identifier,
dataset_population_description_language_item,
)
set_variables_value_multilanguage(
dataset_population_description,
metadata_identifier,
language,
)
for val in state.metadata.variables:
assert metadata.dataset.population_description == get_standard_metadata(
metadata.variables_lookup[val.short_name],
VariableIdentifiers.POPULATION_DESCRIPTION.value,
)


def test_variables_values_multilanguage_can_be_changed_after_inherit_dataset_value(
metadata: DataDocMetadata,
):
state.metadata = metadata
dataset_population_description = "Persons in Norway"
dataset_population_description_language_item = [
LanguageStringTypeItem(languageCode="en", languageText="Persons in Norway"),
]
dataset_identifier = DatasetIdentifiers.POPULATION_DESCRIPTION
variables_identifier = VariableIdentifiers.POPULATION_DESCRIPTION
language = "en"
setattr(
state.metadata.dataset,
dataset_identifier,
dataset_population_description_language_item,
)
set_variables_value_multilanguage(
dataset_population_description,
dataset_identifier,
language,
)
for val in state.metadata.variables:
assert metadata.dataset.population_description == get_standard_metadata(
metadata.variables_lookup[val.short_name],
variables_identifier,
)
variables_language_item = [
LanguageStringTypeItem(languageCode="en", languageText="Persons in Sweden"),
]
setattr(
state.metadata.variables_lookup["pers_id"],
variables_identifier,
variables_language_item,
)
assert metadata.dataset.population_description != get_standard_metadata(
metadata.variables_lookup["pers_id"],
variables_identifier,
)
assert metadata.dataset.population_description == get_standard_metadata(
metadata.variables_lookup["sivilstand"],
variables_identifier,
)

0 comments on commit fa66260

Please sign in to comment.