Skip to content

Commit d968aeb

Browse files
authored
Override data type when copying variable (#392)
* Failing test * Override the data type when copying a variable * Fix mypy
1 parent 66ddefb commit d968aeb

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

src/dapla_metadata/datasets/core.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -561,22 +561,21 @@ def copy_variable(
561561

562562
source_short_name = source_short_name or target_short_name
563563

564-
metadata_document_variables = read_variables_from_metadata_document(
565-
metadata_document_path
566-
)
564+
source_variables = read_variables_from_metadata_document(metadata_document_path)
567565

568-
variables_by_short_name = {
569-
v.short_name: v
570-
for v in metadata_document_variables
571-
if v.short_name is not None
566+
source_variables_lookup = {
567+
v.short_name: v for v in source_variables if v.short_name is not None
572568
}
573569

574-
if source_short_name not in variables_by_short_name:
570+
if source_short_name not in source_variables_lookup:
575571
msg = f"{source_short_name} does not exist!"
576572
raise ValueError(msg)
577573

578-
source_variable = variables_by_short_name[source_short_name]
579-
574+
source_variable = source_variables_lookup[source_short_name]
575+
# Always override the data type to ensure it matches the physical dataset.
576+
source_variable.data_type = self.variables_lookup[ # type: ignore[assignment]
577+
target_short_name
578+
].data_type
580579
self.variables_lookup[target_short_name] = source_variable
581580

582581
source_variable = all_optional_model.Variable.model_validate(source_variable)

tests/datasets/resources/metadata_documents/copy_variables.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
"languageText": "Ny persid"
154154
}
155155
],
156-
"data_type": "STRING",
156+
"data_type": "ARRAY",
157157
"variable_role": "IDENTIFIER",
158158
"definition_uri": "https://www.ssb.no/a/metadata/conceptvariable/vardok/26/nb",
159159
"is_personal_data": true,

tests/datasets/test_copy_variables.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ def test_copy_variable(
2525

2626
with pathlib.Path.open(written_document) as f:
2727
written_metadata = json.loads(f.read())
28-
datadoc_metadata = written_metadata["datadoc"]["variables"]
2928

30-
assert datadoc_metadata[0]["short_name"] == target_short_name
31-
correct_variable = datadoc_metadata[0]
32-
assert correct_variable["name"] is not None
33-
assert correct_variable["name"][2]["languageCode"] == "nb"
34-
assert correct_variable["name"][2]["languageText"] == "Ny persid"
29+
variables = written_metadata["datadoc"]["variables"]
30+
assert variables[0]["short_name"] == target_short_name
31+
correct_variable = variables[0]
32+
assert correct_variable["name"] is not None
33+
assert correct_variable["name"][2]["languageCode"] == "nb"
34+
assert correct_variable["name"][2]["languageText"] == "Ny persid"
35+
# Data type must match that from the dataset.
36+
assert correct_variable["data_type"] == "STRING"
3537

3638

3739
def test_variable_not_in_target_dataset(

0 commit comments

Comments
 (0)