Skip to content

Commit

Permalink
CLN: Clean up pytest global configuration user warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
JB Lovland committed Feb 6, 2024
1 parent 8357fc7 commit 997032c
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 95 deletions.
114 changes: 55 additions & 59 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import yaml
from fmu.config import utilities as ut
from fmu.dataio.dataio import ExportData, read_metadata
from fmu.dataio.datastructure.configuration import global_configuration

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -165,8 +166,8 @@ def fixture_rmsglobalconfig(rmssetup):
return global_cfg


@pytest.fixture(name="globalvars_norw_letters", scope="module")
def fixture_globalvars_norw_letters(tmp_path_factory):
@pytest.fixture(name="globalvars_norwegian_letters", scope="module")
def fixture_globalvars_norwegian_letters(tmp_path_factory):
"""Read a global config with norwegian special letters w/ fmu.config utilities."""

tmppath = tmp_path_factory.mktemp("revisionxx")
Expand Down Expand Up @@ -200,70 +201,62 @@ def fixture_casesetup(tmp_path_factory):
return tmppath


@pytest.fixture(name="caseglobalconfig", scope="module")
def fixture_caseglobalconfig():
"""Create as global config for case testing."""
gconfig = {}
gconfig["model"] = {"name": "Test", "revision": "21.0.0"}
gconfig["masterdata"] = {
"smda": {
"country": [
{"identifier": "Norway", "uuid": "ad214d85-8a1d-19da-e053-c918a4889309"}
],
"discovery": [{"short_identifier": "abdcef", "uuid": "ghijk"}],
}
}
gconfig["stratigraphy"] = {"TopVolantis": {}}
gconfig["model"] = {"revision": "0.99.0"}
gconfig["access"] = {"asset": "Drogon", "ssdl": "internal"}
logger.info("Ran %s", inspect.currentframe().f_code.co_name)
return gconfig


@pytest.fixture(name="globalconfig1", scope="module")
def fixture_globalconfig1():
"""Minimalistic global config variables no. 1 in ExportData class."""

cfg = {}

cfg = {}
cfg["model"] = {"name": "Test", "revision": "AUTO"}
cfg["stratigraphy"] = {
"TopWhatever": {
"stratigraphic": True,
"name": "Whatever Top",
"alias": ["TopDindong", "TopWhatever"],
},
}
cfg["masterdata"] = {
"smda": {
"country": [
{"identifier": "Norway", "uuid": "ad214d85-8a1d-19da-e053-c918a4889309"}
],
"discovery": [{"short_identifier": "abdcef", "uuid": "ghijk"}],
}
}
cfg["access"] = {
"asset": {
"name": "Test",
},
"ssdl": {
"access_level": "internal",
"rep_include": False,
},
}
logger.info("Ran %s", inspect.currentframe().f_code.co_name)
return cfg
return global_configuration.GlobalConfiguration(
masterdata=global_configuration.meta.Masterdata(
smda=global_configuration.meta.Smda(
coordinate_system=global_configuration.meta.CoordinateSystem(
identifier="ST_WGS84_UTM37N_P32637",
uuid="15ce3b84-766f-4c93-9050-b154861f9100",
),
country=[
global_configuration.meta.CountryItem(
identifier="Norway",
uuid="ad214d85-8a1d-19da-e053-c918a4889309",
),
],
discovery=[
global_configuration.meta.DiscoveryItem(
short_identifier="abdcef",
uuid="56c92484-8798-4f1f-9f14-d237a3e1a4ff",
),
],
stratigraphic_column=global_configuration.meta.StratigraphicColumn(
identifier="TestStratigraphicColumn",
uuid="56c92484-8798-4f1f-9f14-d237a3e1a4ff",
),
field=[],
)
),
access=global_configuration.Access(
asset=global_configuration.Asset(name="Test"),
ssdl=global_configuration.Ssdl(
access_level=global_configuration.enums.AccessLevel.internal,
rep_include=False,
),
),
model=global_configuration.Model(
name="Test",
revision="AUTO",
),
stratigraphy=global_configuration.Stratigraphy(
root={
"TopWhatever": global_configuration.StratigraphyElement(
name="Whatever Top",
stratigraphic=True,
alias=["TopDindong", "TopWhatever"],
)
}
),
).model_dump()


@pytest.fixture(name="globalconfig_asfile", scope="module")
def fixture_globalconfig_asfile() -> str:
"""Global config as file for use with environment variable"""

globalconfigfile = ROOTPWD / "tests/data/drogon/global_config2/global_variables.yml"

logger.info("Ran %s", inspect.currentframe().f_code.co_name)
return str(globalconfigfile)
return ROOTPWD / "tests/data/drogon/global_config2/global_variables.yml"


@pytest.fixture(name="edataobj1", scope="module")
Expand Down Expand Up @@ -535,7 +528,10 @@ def fixture_edataobj3(globalconfig1):
# logger.info("Establish edataobj1")

return ExportData(
config=globalconfig1, name="summary", content="timeseries", tagname=""
config=globalconfig1,
name="summary",
content="timeseries",
tagname="",
)


Expand Down
73 changes: 47 additions & 26 deletions tests/test_units/test_dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,12 @@ def test_set_display_name(regsurf, globalconfig2):

def test_global_config_from_env(globalconfig_asfile, monkeypatch):
"""Testing getting global config from a file"""
monkeypatch.setenv("FMU_GLOBAL_CONFIG", globalconfig_asfile)
monkeypatch.setenv("FMU_GLOBAL_CONFIG", str(globalconfig_asfile))
edata = ExportData(content="depth") # the env variable will override this
assert "smda" in edata.config["masterdata"]


def test_settings_config_from_env(tmp_path, rmsglobalconfig, regsurf):
def test_settings_config_from_env(tmp_path, rmsglobalconfig, regsurf, monkeypatch):
"""Testing getting user settings config from a file via env variable."""

settings = {}
Expand All @@ -343,17 +343,20 @@ def test_settings_config_from_env(tmp_path, rmsglobalconfig, regsurf):
with open(tmp_path / "mysettings.yml", "w") as stream:
yaml.dump(settings, stream)

os.environ["FMU_DATAIO_CONFIG"] = str(tmp_path / "mysettings.yml")
monkeypatch.setenv("FMU_DATAIO_CONFIG", str(tmp_path / "mysettings.yml"))
edata = ExportData(content="depth") # the env variable will override this
assert edata.name == "MyFancyName"

meta = edata.generate_metadata(regsurf)
assert "myfancyname--myfancytag" in meta["file"]["relative_path"]

del os.environ["FMU_DATAIO_CONFIG"]


def test_settings_and_global_config_from_env(tmp_path, rmsglobalconfig, regsurf):
def test_settings_and_global_config_from_env(
tmp_path,
rmsglobalconfig,
regsurf,
monkeypatch,
):
"""Testing getting user settings config ands global from a env -> file."""

settings = {}
Expand All @@ -368,20 +371,21 @@ def test_settings_and_global_config_from_env(tmp_path, rmsglobalconfig, regsurf)
with open(tmp_path / "global_variables.yml", "w") as stream:
yaml.dump(rmsglobalconfig, stream)

os.environ["FMU_GLOBAL_CONFIG"] = str(tmp_path / "global_variables.yml")
os.environ["FMU_DATAIO_CONFIG"] = str(tmp_path / "mysettings.yml")
monkeypatch.setenv("FMU_GLOBAL_CONFIG", str(tmp_path / "global_variables.yml"))
monkeypatch.setenv("FMU_DATAIO_CONFIG", str(tmp_path / "mysettings.yml"))

edata = ExportData(content="depth") # the env variable will override this
assert edata.name == "MyFancyName"

meta = edata.generate_metadata(regsurf)
assert "myfancyname--myfancytag" in meta["file"]["relative_path"]

del os.environ["FMU_DATAIO_CONFIG"]
del os.environ["FMU_GLOBAL_CONFIG"]


def test_settings_config_from_env_invalid(tmp_path, rmsglobalconfig):
def test_settings_config_from_env_invalid(
tmp_path,
rmsglobalconfig,
monkeypatch,
):
"""Testing getting user settings config from a file but some invalid stuff."""

settings = {}
Expand All @@ -392,20 +396,23 @@ def test_settings_config_from_env_invalid(tmp_path, rmsglobalconfig):
with open(tmp_path / "mysettings.yml", "w") as stream:
yaml.dump(settings, stream)

os.environ["FMU_DATAIO_CONFIG"] = str(tmp_path / "mysettings.yml")
monkeypatch.setenv("FMU_DATAIO_CONFIG", str(tmp_path / "mysettings.yml"))
with pytest.raises(ValidationError):
_ = ExportData(content="depth")

del os.environ["FMU_DATAIO_CONFIG"]


def test_norwegian_letters_globalconfig(globalvars_norw_letters, regsurf):
def test_norwegian_letters_globalconfig(
globalvars_norwegian_letters,
regsurf,
monkeypatch,
):
"""Testing using norwegian letters in global config.
Note that fmu.config utilities yaml_load() is applied to read cfg (cf conftest.py)
"""

path, cfg, cfg_asfile = globalvars_norw_letters
path, cfg, cfg_asfile = globalvars_norwegian_letters

os.chdir(path)

edata = ExportData(content="depth", config=cfg, name="TopBlåbær")
Expand All @@ -418,25 +425,27 @@ def test_norwegian_letters_globalconfig(globalvars_norw_letters, regsurf):
result = pathlib.Path(edata.export(regsurf))
metafile = result.parent / ("." + str(result.stem) + ".gri.yml")
with open(metafile, encoding="utf-8") as stream:
stuff = stream.read()
assert "DRÅGØN" in stuff
assert "DRÅGØN" in stream.read()

# read file as global config

os.environ["FMU_GLOBAL_CONFIG"] = cfg_asfile
monkeypatch.setenv("FMU_GLOBAL_CONFIG", cfg_asfile)
edata2 = ExportData(content="depth") # the env variable will override this
return
meta2 = edata2.generate_metadata(regsurf, name="TopBlåbær")
logger.debug("\n %s", prettyprint_dict(meta2))
assert meta2["data"]["name"] == "TopBlåbær"
assert meta2["masterdata"]["smda"]["field"][0]["identifier"] == "DRÅGØN"

del os.environ["FMU_GLOBAL_CONFIG"]


def test_norwegian_letters_globalconfig_as_json(globalvars_norw_letters, regsurf):
def test_norwegian_letters_globalconfig_as_json(
globalvars_norwegian_letters,
regsurf,
):
return
"""Testing using norwegian letters in global config, with json output."""

path, cfg, _ = globalvars_norw_letters
path, cfg, _ = globalvars_norwegian_letters
os.chdir(path)

ExportData.meta_format = "json"
Expand All @@ -445,8 +454,7 @@ def test_norwegian_letters_globalconfig_as_json(globalvars_norw_letters, regsurf
result = pathlib.Path(edata.export(regsurf))
metafile = result.parent / ("." + str(result.stem) + ".gri.json")
with open(metafile, encoding="utf-8") as stream:
stuff = stream.read()
assert "DRÅGØN" in stuff
assert "DRÅGØN" in stream.read()

ExportData.meta_format = "yaml" # reset

Expand Down Expand Up @@ -513,3 +521,16 @@ def test_forcefolder_absolute_shall_raise_or_warn(tmp_path, globalconfig2, regsu
def test_deprecated_verbosity(globalconfig1):
with pytest.warns(UserWarning, match="Using the 'verbosity' key is now deprecated"):
ExportData(config=globalconfig1, verbosity="INFO")


@pytest.mark.parametrize("encoding", ("utf-8", "latin1"))
@pytest.mark.parametrize("mode", ("w", "w+"))
def test_norwegian_letters(encoding, mode, tmp_path):
with open(tmp_path / "no-letters.yml", encoding=encoding, mode=mode) as f:
f.write(
"""æøå:
æøå"""
)

with open(tmp_path / "no-letters.yml", encoding=encoding) as f:
assert yaml.safe_load(f) == {"æøå": "æøå"}
32 changes: 22 additions & 10 deletions tests/test_units/test_metadata_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ def test_metadata_populate_wrong_config(globalconfig1):
_config = deepcopy(globalconfig1)
_config["access"]["ssdl"]["access_level"] = "wrong"

edata = dio.ExportData(config=_config, content="depth")
with pytest.warns(UserWarning):
edata = dio.ExportData(config=_config, content="depth")

mymeta = _MetaData("dummy", edata)
with pytest.raises(ConfigurationError, match="Illegal value for access"):
mymeta._populate_meta_access()
Expand All @@ -230,9 +232,12 @@ def test_metadata_populate_wrong_config(globalconfig1):
def test_metadata_populate_wrong_argument(globalconfig1):
"""Test error in access_ssdl in arguments."""

edata = dio.ExportData(
config=globalconfig1, access_ssdl={"access_level": "wrong"}, content="depth"
)
with pytest.warns(UserWarning):
edata = dio.ExportData(
config=globalconfig1,
access_ssdl={"access_level": "wrong"},
content="depth",
)
mymeta = _MetaData("dummy", edata)
with pytest.raises(ConfigurationError, match="Illegal value for access"):
mymeta._populate_meta_access()
Expand Down Expand Up @@ -286,17 +291,24 @@ def test_metadata_access_illegal_input(globalconfig1):
"""Test giving illegal input."""

# Input is "secret". Not allowed, shall fail.
edata = dio.ExportData(
config=globalconfig1, access_ssdl={"access_level": "secret"}, content="depth"
)
with pytest.warns(UserWarning):
edata = dio.ExportData(
config=globalconfig1,
access_ssdl={"access_level": "secret"},
content="depth",
)

mymeta = _MetaData("dummy", edata)
with pytest.raises(ConfigurationError, match="Illegal value for access"):
mymeta._populate_meta_access()

# Input is "open". Not allowed, shall fail.
edata = dio.ExportData(
config=globalconfig1, access_ssdl={"access_level": "open"}, content="depth"
)
with pytest.warns(UserWarning):
edata = dio.ExportData(
config=globalconfig1,
access_ssdl={"access_level": "open"},
content="depth",
)
mymeta = _MetaData("dummy", edata)
with pytest.raises(ConfigurationError, match="Illegal value for access"):
mymeta._populate_meta_access()
Expand Down

0 comments on commit 997032c

Please sign in to comment.