From 5afcc96304231f0b21b9187399fb4a130efee308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20C=2E=20Riven=C3=A6s?= Date: Tue, 22 Nov 2022 12:38:12 +0100 Subject: [PATCH] Improve testing wrt add2docs. The tests now to generate the added YAML files can be generated with: pytest -k add2docs --- .projectile | 0 docs/examples.rst | 10 ++-- .../test_scripts/test_grid3d_aggregate_map.py | 53 +++++++++---------- .../test_grid3d_average_map_dataio1a.py | 36 +++++++++---- .../test_grid3d_hc_thickness_dataio1a.py | 28 +++++++--- .../test_grid3d_hc_thickness_dataio1b.py | 27 +++++++--- .../test_grid3d_hc_thickness_dataio1c.py | 27 +++++++--- 7 files changed, 115 insertions(+), 66 deletions(-) delete mode 100644 .projectile diff --git a/.projectile b/.projectile deleted file mode 100644 index e69de29..0000000 diff --git a/docs/examples.rst b/docs/examples.rst index fc59464..69a99c5 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -275,7 +275,7 @@ Aggregate example 1 Minimal aggregation example, doing a "maximum" aggregation across all layers -.. literalinclude:: ../docs/test_to_docs/aggregate1.yml +.. literalinclude:: ../tests/yaml/aggregate1.yml :language: yaml Aggregate example 2 @@ -284,7 +284,7 @@ Aggregate example 2 Example using "minimum" aggregation. Zonation is provided in a property file, and ``eclroot:`` is used as an alias -.. literalinclude:: ../docs/test_to_docs/aggregate2.yml +.. literalinclude:: ../tests/yaml/aggregate2.yml :language: yaml Aggregate example 3 @@ -292,7 +292,7 @@ Aggregate example 3 Example using "mean" aggregation and explicit map settings -.. literalinclude:: ../docs/test_to_docs/aggregate3.yml +.. literalinclude:: ../tests/yaml/aggregate3.yml :language: yaml Aggregate example 4 @@ -300,7 +300,7 @@ Aggregate example 4 Example using an interval-based zonation -.. literalinclude:: ../docs/test_to_docs/aggregate4.yml +.. literalinclude:: ../tests/yaml/aggregate4.yml :language: yaml Aggregate example 5 @@ -310,5 +310,5 @@ Example using "mean" aggregation, with weights calculated from cell thickness. " is used as an alias. The map settings are calculated automatically, with resolution determined by "pixel_to_cell_ratio". -.. literalinclude:: ../docs/test_to_docs/aggregate5.yml +.. literalinclude:: ../tests/yaml/aggregate5.yml :language: yaml diff --git a/tests/test_scripts/test_grid3d_aggregate_map.py b/tests/test_scripts/test_grid3d_aggregate_map.py index 4d84f84..267a0c6 100644 --- a/tests/test_scripts/test_grid3d_aggregate_map.py +++ b/tests/test_scripts/test_grid3d_aggregate_map.py @@ -3,20 +3,14 @@ import pytest import xtgeo - from xtgeoapp_grd3dmaps.aggregate import grid3d_aggregate_map -def _copy2docs(file): - root = Path(__file__).absolute().parent.parent.parent - dst = root / "docs" / "test_to_docs" - shutil.copy2(file, dst) - - -def test_aggregated_map1_add2docs(datatree): +def test_aggregated_map1(datatree): result = datatree / "aggregate1_folder" result.mkdir(parents=True) cfg = "tests/yaml/aggregate1.yml" + grid3d_aggregate_map.main( [ "--config", @@ -29,13 +23,13 @@ def test_aggregated_map1_add2docs(datatree): ) swat = xtgeo.surface_from_file(result / "all--max_swat--20030101.gri") assert swat.values.min() == pytest.approx(0.14292679727077484, abs=1e-8) - _copy2docs(cfg) -def test_aggregated_map2_add2docs(datatree): +def test_aggregated_map2(datatree): result = datatree / "aggregate2_folder" result.mkdir(parents=True) cfg = "tests/yaml/aggregate2.yml" + grid3d_aggregate_map.main( [ "--config", @@ -48,13 +42,13 @@ def test_aggregated_map2_add2docs(datatree): ) swat = xtgeo.surface_from_file(result / "all--min_swat--20030101.gri") assert swat.values.mean() == pytest.approx(0.7908786104444353, abs=1e-8) - _copy2docs(cfg) -def test_aggregated_map3_add2docs(datatree): +def test_aggregated_map3(datatree): result = datatree / "aggregate3_folder" result.mkdir(parents=True) cfg = "tests/yaml/aggregate3.yml" + grid3d_aggregate_map.main( [ "--config", @@ -67,13 +61,13 @@ def test_aggregated_map3_add2docs(datatree): ) poro = xtgeo.surface_from_file(result / "all--mean_poro.gri") assert poro.values.mean() == pytest.approx(0.1677586422488292, abs=1e-8) - _copy2docs(cfg) -def test_aggregated_map4_add2docs(datatree): +def test_aggregated_map4(datatree): result = datatree / "aggregate4_folder" result.mkdir(parents=True) yml = "tests/yaml/aggregate4.yml" + grid3d_aggregate_map.main( [ "--config", @@ -89,13 +83,13 @@ def test_aggregated_map4_add2docs(datatree): assert (result / "all--max_swat--20030101.gri").is_file() assert (result / "zone2--max_swat--20030101.gri").is_file() assert (result / "zone3--max_swat--20030101.gri").is_file() - _copy2docs(yml) -def test_aggregated_map5_add2docs(datatree): +def test_aggregated_map5(datatree): result = datatree / "aggregate5_folder" result.mkdir(parents=True) cfg = "tests/yaml/aggregate5.yml" + grid3d_aggregate_map.main( [ "--config", @@ -108,13 +102,13 @@ def test_aggregated_map5_add2docs(datatree): ) poro = xtgeo.surface_from_file(result / "all--mean_poro.gri") assert poro.values.mean() == pytest.approx(0.1648792893163274, abs=1e-8) - _copy2docs(cfg) -def test_aggregated_map6_add2docs(datatree): +def test_aggregated_map6(datatree): result = datatree / "aggregate6_folder" result.mkdir(parents=True) cfg = "tests/yaml/aggregate6.yml" + grid3d_aggregate_map.main( [ "--config", @@ -126,14 +120,15 @@ def test_aggregated_map6_add2docs(datatree): ] ) gri_files = [p.stem for p in Path(result).glob("*.gri")] - assert sorted(gri_files) == sorted([ - "all--max_swat--19991201", - "all--max_swat--20030101", - "firstzone--max_swat--19991201", - "firstzone--max_swat--20030101", - "secondzone--max_swat--19991201", - "secondzone--max_swat--20030101", - "thirdzone--max_swat--19991201", - "thirdzone--max_swat--20030101", - ]) - _copy2docs(cfg) + assert sorted(gri_files) == sorted( + [ + "all--max_swat--19991201", + "all--max_swat--20030101", + "firstzone--max_swat--19991201", + "firstzone--max_swat--20030101", + "secondzone--max_swat--19991201", + "secondzone--max_swat--20030101", + "thirdzone--max_swat--19991201", + "thirdzone--max_swat--20030101", + ] + ) diff --git a/tests/test_scripts/test_grid3d_average_map_dataio1a.py b/tests/test_scripts/test_grid3d_average_map_dataio1a.py index 4c902b1..8dc0f38 100644 --- a/tests/test_scripts/test_grid3d_average_map_dataio1a.py +++ b/tests/test_scripts/test_grid3d_average_map_dataio1a.py @@ -74,7 +74,29 @@ SOURCEPATH = Path(__file__).absolute().parent.parent.parent -def test_average_map_dataio1a_legacy(datatree): +@pytest.fixture(name="avgdataio1aconfig", scope="module") +def fixture_avgdataio1aconfig(datatree): + """Fixture to make config.""" + name = "avgdataio1a.yml" + cfg = datatree / name + + content = YAMLCONTENT.replace("MODE", "fmu-dataio") + + cfg.write_text(content) + + # for auto documentation + shutil.copy2(cfg, SOURCEPATH / "docs" / "test_to_docs") + + return name + + +def test_avgmap_dataio1a_add2docs(avgdataio1aconfig): + """For using pytest -k add2docs to be ran prior to docs building.""" + print("Export config to docs folder") + assert avgdataio1aconfig is not None + + +def test_average_map_1a_legacy(datatree): """Test AVG with YAML config example 3a as legacy example""" tmp = datatree / "tmp_legacy" @@ -92,19 +114,14 @@ def test_average_map_dataio1a_legacy(datatree): assert out.is_file() -def test_average_map_dataio1a_add2docs(datatree): +def test_average_map_dataio1a(datatree, avgdataio1aconfig): """Test AVG with YAML config example 3a piped through dataio""" - content = YAMLCONTENT.replace("MODE", "fmu-dataio") - - cfg = datatree / "avgdataio1a.yml" - cfg.write_text(content) - os.environ["FMU_GLOBAL_CONFIG"] = str( datatree / "tests" / "data" / "reek" / "global_variables.yml" ) grid3d_average_map.main( - ["--config", "avgdataio1a.yml", "--dump", "dump_config.yml"] + ["--config", avgdataio1aconfig, "--dump", "dump_config.yml"] ) # read result file @@ -135,6 +152,3 @@ def test_average_map_dataio1a_add2docs(datatree): if legacy.is_file(): surf2 = xtgeo.surface_from_file(legacy) assert surf2.generate_hash() == surf.generate_hash() - - # for auto documentation - shutil.copy2(cfg, SOURCEPATH / "docs" / "test_to_docs") diff --git a/tests/test_scripts/test_grid3d_hc_thickness_dataio1a.py b/tests/test_scripts/test_grid3d_hc_thickness_dataio1a.py index fb28ea3..4a82b08 100644 --- a/tests/test_scripts/test_grid3d_hc_thickness_dataio1a.py +++ b/tests/test_scripts/test_grid3d_hc_thickness_dataio1a.py @@ -47,16 +47,33 @@ SOURCEPATH = Path(__file__).absolute().parent.parent.parent -def test_hc_thickness_1a_add2docs(datatree): - """Test HC thickness map piped through dataio""" - cfg = datatree / "hcdataio1a.yml" +@pytest.fixture(name="hcdataio1aconfig", scope="module") +def fixture_hcdataio1a(datatree): + """Fixture to make config.""" + name = "hcdataio1a.yml" + cfg = datatree / name + cfg.write_text(YAMLCONTENT) + # for auto documentation + shutil.copy2(cfg, SOURCEPATH / "docs" / "test_to_docs") + return name + + +def test_hc_thickness_1a_add2docs(hcdataio1aconfig): + """For using pytest -k add2docs to be ran prior to docs building.""" + print("Export config to docs folder") + assert hcdataio1aconfig is not None + + +def test_hc_thickness_1a(datatree, hcdataio1aconfig): + """Test HC thickness map piped through dataio""" + os.environ["FMU_GLOBAL_CONFIG"] = str( datatree / "tests" / "data" / "reek" / "global_variables.yml" ) grid3d_hc_thickness.main( - ["--config", "hcdataio1a.yml", "--dump", "dump_config.yml"] + ["--config", hcdataio1aconfig, "--dump", "dump_config.yml"] ) # read result file @@ -75,6 +92,3 @@ def test_hc_thickness_1a_add2docs(datatree): assert metadata["data"]["spec"]["ncol"] == 146 assert metadata["data"]["property"]["attribute"] == "oilthickness" - - # for auto documentation - shutil.copy2(cfg, SOURCEPATH / "docs" / "test_to_docs") diff --git a/tests/test_scripts/test_grid3d_hc_thickness_dataio1b.py b/tests/test_scripts/test_grid3d_hc_thickness_dataio1b.py index 76ff8a0..70027cc 100644 --- a/tests/test_scripts/test_grid3d_hc_thickness_dataio1b.py +++ b/tests/test_scripts/test_grid3d_hc_thickness_dataio1b.py @@ -46,17 +46,33 @@ SOURCEPATH = Path(__file__).absolute().parent.parent.parent -def test_hc_thickness_1b_add2docs(datatree): - """Test HC thickness map piped through dataio, using 'both' mode""" +@pytest.fixture(name="hcdataio1bconfig", scope="module") +def fixture_hcdataio1b(datatree): + """Fixture to make config.""" + name = "hcdataio1b.yml" + cfg = datatree / name - cfg = datatree / "hcdataio1b.yml" cfg.write_text(YAMLCONTENT) + # for auto documentation + shutil.copy2(cfg, SOURCEPATH / "docs" / "test_to_docs") + return name + + +def test_hc_thickness_1b_add2docs(hcdataio1bconfig): + """For using pytest -k add2docs to be ran prior to docs building.""" + print("Export config to docs folder") + assert hcdataio1bconfig is not None + + +def test_hc_thickness_1b(datatree, hcdataio1bconfig): + """Test HC thickness map piped through dataio, using 'both' mode""" + os.environ["FMU_GLOBAL_CONFIG"] = str( datatree / "tests" / "data" / "reek" / "global_variables.yml" ) grid3d_hc_thickness.main( - ["--config", "hcdataio1b.yml", "--dump", "dump_config.yml"] + ["--config", hcdataio1bconfig, "--dump", "dump_config.yml"] ) # read result file @@ -76,6 +92,3 @@ def test_hc_thickness_1b_add2docs(datatree): assert metadata["data"]["spec"]["ncol"] == 161 assert metadata["data"]["content"] == "property" assert metadata["data"]["property"]["attribute"] == "oilthickness" - - # for auto documentation - shutil.copy2(cfg, SOURCEPATH / "docs" / "test_to_docs") diff --git a/tests/test_scripts/test_grid3d_hc_thickness_dataio1c.py b/tests/test_scripts/test_grid3d_hc_thickness_dataio1c.py index 6772681..912ce1f 100644 --- a/tests/test_scripts/test_grid3d_hc_thickness_dataio1c.py +++ b/tests/test_scripts/test_grid3d_hc_thickness_dataio1c.py @@ -45,17 +45,33 @@ SOURCEPATH = Path(__file__).absolute().parent.parent.parent -def test_hc_thickness_1c_add2docs(datatree): - """Test HC thickness map piped through dataio, see former yaml hc_thickness2a.""" +@pytest.fixture(name="hcdataio1cconfig", scope="module") +def fixture_hcdataio1c(datatree): + """Fixture to make config.""" + name = "hcdataio1c.yml" + cfg = datatree / name - cfg = datatree / "hcdataio1c.yml" cfg.write_text(YAMLCONTENT) + # for auto documentation + shutil.copy2(cfg, SOURCEPATH / "docs" / "test_to_docs") + return name + + +def test_hc_thickness_1c_add2docs(hcdataio1cconfig): + """For using pytest -k add2docs to be ran prior to docs building.""" + print("Export config to docs folder") + assert hcdataio1cconfig is not None + + +def test_hc_thickness_1c(datatree, hcdataio1cconfig): + """Test HC thickness map piped through dataio, see former yaml hc_thickness2a.""" + os.environ["FMU_GLOBAL_CONFIG"] = str( datatree / "tests" / "data" / "reek" / "global_variables.yml" ) grid3d_hc_thickness.main( - ["--config", "hcdataio1c.yml", "--dump", "dump_config.yml"] + ["--config", hcdataio1cconfig, "--dump", "dump_config.yml"] ) # read result file @@ -74,6 +90,3 @@ def test_hc_thickness_1c_add2docs(datatree): assert metadata["data"]["spec"]["ncol"] == 292 assert metadata["data"]["time"]["t0"]["value"] == "1990-01-01T00:00:00" - - # for auto documentation - shutil.copy2(cfg, SOURCEPATH / "docs" / "test_to_docs")