Skip to content

Commit

Permalink
Add utils tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JB Lovland committed Jan 10, 2024
1 parent 2c13a20 commit 8e6a2b8
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 13 deletions.
12 changes: 3 additions & 9 deletions src/fmu/dataio/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,15 @@ def detect_inside_rms() -> bool:
when using the Roxar API python, so that unit test outside of RMS behaves
properly
"""
inside_rms = False
try:
with contextlib.suppress(ModuleNotFoundError):
import roxar

inside_rms = True
logger.info("Roxar version is %s", roxar.__version__)
except ModuleNotFoundError:
pass
return True

# a special solution for testing mostly
if os.environ.get("INSIDE_RMS", 1) == "0":
inside_rms = False

inside_rms = os.environ.get("INSIDE_RMS", "0") != "0"
logger.info("Running truly in RMS GUI status: %s", inside_rms)

return inside_rms


Expand Down
34 changes: 30 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""The conftest.py, providing magical fixtures to tests."""
import contextlib
import datetime
import inspect
import json
Expand Down Expand Up @@ -45,15 +46,40 @@ def pytest_configure():
cprint(80 * "=", "red", attrs=["blink"])


@contextlib.contextmanager
def set_export_data_inside_rms_flag():
old = ExportData._inside_rms
ExportData._inside_rms = True
try:
yield
finally:
ExportData._inside_rms = old


@contextlib.contextmanager
def set_environ_inside_rms_flag():
unset = object()
old = os.environ.get("INSIDE_RMS", unset)
os.environ["INSIDE_RMS"] = "1"
try:
yield
finally:
if old is unset:
del os.environ["INSIDE_RMS"]
else:
os.environ["INSIDE_RMS"] = old


def inside_rms(func):
"""Decorator for being inside RMS"""

@wraps(func)
def wrapper(*args, **kwargs):
ExportData._inside_rms = True
retval = func(*args, **kwargs)
ExportData._inside_rms = False
return retval
with (
set_export_data_inside_rms_flag(),
set_environ_inside_rms_flag(),
):
return func(*args, **kwargs)

return wrapper

Expand Down
93 changes: 93 additions & 0 deletions tests/test_units/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"""Test the utils module"""

import numpy as np
import pytest
from conftest import set_environ_inside_rms_flag
from fmu.dataio import _utils as utils
from xtgeo import Grid, Polygons, RegularSurface


@pytest.mark.parametrize(
Expand All @@ -24,3 +27,93 @@
)
def test_check_if_number(value, result):
assert utils.check_if_number(value) == result


@pytest.mark.parametrize(
"given, expected, isoformat",
(
(
{},
(None, None),
True,
),
(
{"time": {"t0": {"value": "2022-08-02T00:00:00", "label": "base"}}},
("2022-08-02T00:00:00", None),
True,
),
(
{
"time": [
{"value": "2030-01-01T00:00:00", "label": "moni"},
{"value": "2010-02-03T00:00:00", "label": "base"},
]
},
("2030-01-01T00:00:00", "2010-02-03T00:00:00"),
True,
),
(
{},
(None, None),
False,
),
(
{"time": {"t0": {"value": "2022-08-02T00:00:00", "label": "base"}}},
("20220802", None),
False,
),
(
{
"time": [
{"value": "2030-01-01T00:00:00", "label": "moni"},
{"value": "2010-02-03T00:00:00", "label": "base"},
]
},
("20300101", "20100203"),
False,
),
),
)
def test_parse_timedata(given: dict, expected: tuple, isoformat: bool):
assert utils.parse_timedata(given, isoformat) == expected


def test_get_object_name():
assert utils.get_object_name(object()) is None

assert utils.get_object_name(RegularSurface(0, 0, 0, 0)) is None
assert (
utils.get_object_name(RegularSurface(0, 0, 0, 0, name="Not ukn")) == "Not ukn"
)

assert utils.get_object_name(Polygons()) is None
assert utils.get_object_name(Polygons(name="Not poly")) == "Not poly"

assert (
utils.get_object_name(
Grid(
np.random.randn(2, 2, 6).astype(np.float64),
np.random.randn(2, 2, 2, 4).astype(np.float32),
np.random.randn(1, 1, 1).astype(np.int32),
name="noname",
)
)
is None
)
assert (
utils.get_object_name(
Grid(
np.random.randn(2, 2, 6).astype(np.float64),
np.random.randn(2, 2, 2, 4).astype(np.float32),
np.random.randn(1, 1, 1).astype(np.int32),
name="Not noname",
)
)
== "Not noname"
)


def test_detect_inside_rms():
assert not utils.detect_inside_rms()
with set_environ_inside_rms_flag():
assert utils.detect_inside_rms()

0 comments on commit 8e6a2b8

Please sign in to comment.