Skip to content

Commit

Permalink
Allow workflow argument to be a dict (#245)
Browse files Browse the repository at this point in the history
* Allow for workflow arg as a dict and typecheck it
  • Loading branch information
perolavsvendsen committed Sep 14, 2022
1 parent 2301440 commit 889853d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
19 changes: 18 additions & 1 deletion src/fmu/dataio/_fmu_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,24 @@ def generate_ert2_metadata(self):
meta["context"] = {"stage": self.dataio._usecontext}

if self.dataio.workflow:
meta["workflow"] = {"reference": self.dataio.workflow}
if isinstance(self.dataio.workflow, str):
meta["workflow"] = {"reference": self.dataio.workflow}
elif isinstance(self.dataio.workflow, dict):
if "reference" not in self.dataio.workflow:
raise ValueError(
"When workflow is given as a dict, the 'reference' "
"key must be included and be a string"
)
warn(
"The 'workflow' argument should be given as a string. "
"Support for dictionary input is scheduled for deprecation.",
PendingDeprecationWarning,
)

meta["workflow"] = {"reference": self.dataio.workflow["reference"]}

else:
raise TypeError("'workflow' should be string.")

case_uuid = "not_present" # TODO! not allow missing case metadata?
if self.case_metadata and "fmu" in self.case_metadata:
Expand Down
2 changes: 0 additions & 2 deletions src/fmu/dataio/dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,6 @@ class ExportData:
grid_model: Currently allowed but planned for deprecation
grid_model: Currently allowed but planned for deprecation
include_index: This applies to Pandas (table) data only, and if True then the
index column will be exported. Deprecated, use class variable
``table_include_index`` instead
Expand Down
47 changes: 47 additions & 0 deletions tests/test_units/test_fmuprovider_class.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Test the _MetaData class from the _metadata.py module"""
import os

import pytest

import fmu.dataio as dio
from fmu.dataio._fmu_provider import _FmuProvider, _get_folderlist

Expand Down Expand Up @@ -73,3 +75,48 @@ def test_fmuprovider_detect_case_has_metadata(fmurun_w_casemetadata, edataobj1):
myfmu.case_metadata["fmu"]["case"]["uuid"]
== "a40b05e8-e47f-47b1-8fee-f52a5116bd37"
)


def test_fmuprovider_workflow_reference(fmurun_w_casemetadata, edataobj1):
"""Testing the handling of workflow reference input.
Metadata definitions of fmu.workflow is that it is a dictionary with 'reference'
as a mandatory key. In early versions, the 'workflow' argument was to be given as
a dictionary and directly inserted. However, during development, this has changed
to a string which is inserted into the 'workflow' element in the outgoing metadata.
Some users still have legacy workflows that give this as a dictionary, so we will
continue to allow it, but with a warning.
This test is asserting that when 'workflow' is given in various shapes and forms,
it shall always produce valid metadata.
"""
edataobj1._rootpath = fmurun_w_casemetadata
os.chdir(fmurun_w_casemetadata)

# workflow input is a string
edataobj1.workflow = "my workflow"
myfmu = _FmuProvider(edataobj1)
myfmu.detect_provider()
assert "workflow" in myfmu.metadata
assert myfmu.metadata["workflow"] == {"reference": "my workflow"}

# workflow input is a correct dict
edataobj1.workflow = {"reference": "my workflow"}
myfmu = _FmuProvider(edataobj1)
with pytest.warns(PendingDeprecationWarning, match="The 'workflow' argument"):
myfmu.detect_provider()
assert "workflow" in myfmu.metadata
assert myfmu.metadata["workflow"] == {"reference": "my workflow"}

# workflow input is non-correct dict
edataobj1.workflow = {"something": "something"}
myfmu = _FmuProvider(edataobj1)
with pytest.raises(ValueError):
myfmu.detect_provider()

# workflow input is other types - shall fail
edataobj1.workflow = 123.4
myfmu = _FmuProvider(edataobj1)
with pytest.raises(TypeError):
myfmu.detect_provider()

0 comments on commit 889853d

Please sign in to comment.