Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run one warning for variables #375

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/datadoc/backend/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from datadoc.backend.utils import derive_assessment_from_state
from datadoc.backend.utils import normalize_path
from datadoc.backend.utils import num_obligatory_dataset_fields_completed
from datadoc.backend.utils import num_obligatory_variable_fields_completed
from datadoc.backend.utils import num_obligatory_variables_fields_completed
from datadoc.backend.utils import set_default_values_dataset
from datadoc.backend.utils import set_default_values_variables
from datadoc.enums import DataSetStatus
Expand Down Expand Up @@ -526,11 +526,10 @@ def percent_complete(self) -> int:
assigned. Used for a live progress bar in the UI, as well as being
saved in the datadoc as a simple quality indicator.
"""
num_all_fields = NUM_OBLIGATORY_DATASET_FIELDS
num_all_fields = NUM_OBLIGATORY_DATASET_FIELDS + (
NUM_OBLIGATORY_VARIABLES_FIELDS * len(self.variables)
)
num_set_fields = num_obligatory_dataset_fields_completed(
self.dataset,
)
for v in self.variables:
num_all_fields += NUM_OBLIGATORY_VARIABLES_FIELDS
num_set_fields += num_obligatory_variable_fields_completed(v)
) + num_obligatory_variables_fields_completed(self.variables)
return calculate_percentage(num_set_fields, num_all_fields)
23 changes: 9 additions & 14 deletions src/datadoc/backend/model_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from datadoc.backend.utils import get_missing_obligatory_variables_fields
from datadoc.backend.utils import incorrect_date_order
from datadoc.backend.utils import num_obligatory_dataset_fields_completed
from datadoc.backend.utils import num_obligatory_variable_fields_completed
from datadoc.backend.utils import num_obligatory_variables_fields_completed
from datadoc.backend.utils import set_variables_inherit_from_dataset
from datadoc.utils import get_timestamp_now

Expand Down Expand Up @@ -140,19 +140,14 @@ def check_obligatory_variables_metadata(self) -> Self:
ObligatoryVariableWarning: If not all obligatory variable metadata fields
are filled in.
"""
if self.variables is not None:
for v in self.variables:
if (
num_obligatory_variable_fields_completed(
v,
)
!= NUM_OBLIGATORY_VARIABLES_FIELDS
):
warnings.warn(
f"{OBLIGATORY_METADATA_WARNING} {get_missing_obligatory_variables_fields(self.variables)}",
ObligatoryVariableWarning,
stacklevel=2,
)
if self.variables is not None and num_obligatory_variables_fields_completed(
self.variables,
) != (NUM_OBLIGATORY_VARIABLES_FIELDS * len(self.variables)):
warnings.warn(
f"{OBLIGATORY_METADATA_WARNING} {get_missing_obligatory_variables_fields(self.variables)}",
ObligatoryVariableWarning,
stacklevel=2,
)
logger.warning(
"Type warning: %s.%s %s",
ObligatoryVariableWarning,
Expand Down
35 changes: 25 additions & 10 deletions src/datadoc/backend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def _is_missing_multilanguage_value(
Returns:
True if no value in any of languages for one field, False otherwise.
"""
if (
return bool(
field_name in obligatory_list
and field_value
and (
Expand All @@ -224,10 +224,8 @@ def _is_missing_multilanguage_value(
len(field_value) <= 2 # noqa: PLR2004 approve magic value
or not field_value[2]["languageText"]
)
)
):
return True
return False
),
)


def _is_missing_metadata(
Expand All @@ -253,17 +251,15 @@ def _is_missing_metadata(
Returns:
True if the field doesn't have a value, False otherwise.
"""
if (
return bool(
field_name in obligatory_list
and field_value is None
or _is_missing_multilanguage_value(
field_name,
field_value,
obligatory_multi_language_list,
)
):
return True
return False
),
)


def num_obligatory_dataset_fields_completed(dataset: model.Dataset) -> int:
Expand All @@ -283,6 +279,25 @@ def num_obligatory_dataset_fields_completed(dataset: model.Dataset) -> int:
)


def num_obligatory_variables_fields_completed(variables: list) -> int:
"""Count the number of obligatory fields completed for all variables.

This function calculates the total number of obligatory fields that have
values (are not None) for one variable in the list.

Args:
variables: A list with variable objects.

Returns:
The total number of obligatory variable fields that have been completed
(not None) for all variables.
"""
num_completed = 0
for v in variables:
num_completed += num_obligatory_variable_fields_completed(v)
return num_completed


def num_obligatory_variable_fields_completed(variable: model.Variable) -> int:
"""Count the number of obligatory fields completed for one variable.

Expand Down
2 changes: 1 addition & 1 deletion tests/backend/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def test_obligatory_metadata_dataset_warning(metadata: Datadoc):
) as record:
metadata.write_metadata_document()
all_obligatory_completed = 100
num_warnings = 9
num_warnings = 2
if metadata.percent_complete != all_obligatory_completed:
assert len(record) == num_warnings
assert issubclass(record[0].category, ObligatoryDatasetWarning)
Expand Down