Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test output #119

Merged
merged 4 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
/build
/documentation
*.log
.coverage
.coverage
/tests/test_data/cases/case_02_compound/output/**
/tests/test_data/compare1d2d/cases/case1/output/**
/tests/test_data/compare1d2d/rijn-j22_6-v1a2/output/**
testIni.ini
10 changes: 5 additions & 5 deletions fm2prof/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ class GenerateCrossSectionLocationFile(FM2ProfBase):
def __init__(
self,
network_definition_file: str | Path,
crossection_location_file: str | Path,
branchrule_file: str | Path = "",
cross_section_location_file: str | Path,
branch_rule_file: str | Path = "",
) -> None:
"""Generate cross section location file object.

Expand All @@ -143,16 +143,16 @@ def __init__(
"""
super().__init__()

network_definition_file, crossection_location_file, branchrule_file = map(
network_definition_file, cross_section_location_file, branch_rule_file = map(
Path,
[network_definition_file, crossection_location_file, branchrule_file],
[network_definition_file, cross_section_location_file, branch_rule_file],
)

if not network_definition_file.exists():
err_msg = "Network difinition file not found"
raise FileNotFoundError(err_msg)

self._network_definition_file_to_input(network_definition_file, crossection_location_file, branchrule_file)
self._network_definition_file_to_input(network_definition_file, cross_section_location_file, branch_rule_file)

def _parse_network_definition_file(self, network_definition_file: Path, branchrules: dict | None = None) -> dict:
"""Parse network definition file.
Expand Down
51 changes: 29 additions & 22 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,63 @@
import os
import shutil
from datetime import datetime
from pathlib import Path

import pytest

from fm2prof import Project
from fm2prof.utils import GenerateCrossSectionLocationFile, Compare1D2D, VisualiseOutput
from fm2prof.utils import Compare1D2D, GenerateCrossSectionLocationFile, VisualiseOutput
from tests.TestUtils import TestUtils

from datetime import datetime

_root_output_dir = None


class Test_GenerateCrossSectionLocationFile:
def test_given_networkdefinitionfile_cssloc_file_is_generated(self):
def test_given_networkdefinitionfile_cssloc_file_is_generated(self, tmp_path: Path):
# 1. Set up initial test data
path_1d = TestUtils.get_local_test_file("cases/case_02_compound/Model_SOBEK/dimr/dflow1d/NetworkDefinition.ini")
output_file = TestUtils.get_local_test_file("cases/case_02_compound/Data/cross_section_locations.xyz")

output_file = tmp_path / "cross_section_locations.xyz"

# 2. Set Expectations

# 3. Run test
GenerateCrossSectionLocationFile(network_definition_file=path_1d, crossection_location_file=output_file)
GenerateCrossSectionLocationFile(network_definition_file=path_1d, cross_section_location_file=output_file)

# 4. verify
assert output_file.is_file()

def test_given_branchrulefile_output_is_generated(self):
def test_given_branchrulefile_output_is_generated(self, tmp_path: Path):
# 1. Set up initial test data
path_1d = TestUtils.get_local_test_file("cases/case_02_compound/Model_SOBEK/dimr/dflow1d/NetworkDefinition.ini")
output_file = TestUtils.get_local_test_file("cases/case_02_compound/Data/cross_section_locations_new.xyz")
if output_file.is_file():
os.remove(output_file)
output_file = tmp_path / "cross_section_locations_new.xyz"

branch_rule_file = TestUtils.get_local_test_file("cases/case_02_compound/Data/branchrules_onlyfirst.ini")
# 2. Set Expectations

# 3. Run test
GenerateCrossSectionLocationFile(
network_definition_file=path_1d, crossection_location_file=output_file, branchrule_file=branch_rule_file
network_definition_file=path_1d,
cross_section_location_file=output_file,
branch_rule_file=branch_rule_file,
)

# 4. verify
assert output_file.is_file()

def test_given_branchrule_exceptions_output_is_generated(self):
def test_given_branchrule_exceptions_output_is_generated(self, tmp_path: Path):
# 1. Set up initial test data
path_1d = TestUtils.get_local_test_file("cases/case_02_compound/Model_SOBEK/dimr/dflow1d/NetworkDefinition.ini")
output_file = TestUtils.get_local_test_file("cases/case_02_compound/Data/cross_section_locations_new.xyz")
if output_file.is_file():
os.remove(output_file)
output_file = tmp_path / "cross_section_locations_new.xyz"

branch_rule_file = TestUtils.get_local_test_file("cases/case_02_compound/Data/branchrules_exceptions.ini")
# 2. Set Expectations

# 3. Run test
GenerateCrossSectionLocationFile(
network_definition_file=path_1d, crossection_location_file=output_file, branchrule_file=branch_rule_file
network_definition_file=path_1d,
cross_section_location_file=output_file,
branch_rule_file=branch_rule_file,
)

# 4. verify
Expand Down Expand Up @@ -95,7 +98,7 @@ def test_when_branch_in_branches_produce_figure(self):
os.mkdir(output_dir)

output_file = TestUtils.get_local_test_file(
"cases/case_02_compound/output/figures/roughness/roughness_longitudinal_channel1.png"
"cases/case_02_compound/output/figures/roughness/roughness_longitudinal_channel1.png",
)

# 3. Run test
Expand All @@ -116,7 +119,7 @@ def test_when_given_output_css_figure_produced(self):
shutil.rmtree(output_dir)
os.mkdir(output_dir)
output_file = TestUtils.get_local_test_file(
"cases/case_02_compound/output/figures/cross_sections/channel1_125.000.png"
"cases/case_02_compound/output/figures/cross_sections/channel1_125.000.png",
)

# 3. Run test
Expand Down Expand Up @@ -176,7 +179,7 @@ def test_figure_longitudinal(self):
# 2. Set expectations
# this file should exist
output_file = TestUtils.get_local_test_file(
"compare1d2d/rijn-j22_6-v1a2/output/figures/longitudinal/BR-PK-IJ.png"
"compare1d2d/rijn-j22_6-v1a2/output/figures/longitudinal/BR-PK-IJ.png",
)

# 3. Run test
Expand All @@ -194,7 +197,7 @@ def test_figure_discharge(self):
# 2. Set expectations
# this file should exist
output_file = TestUtils.get_local_test_file(
"compare1d2d/rijn-j22_6-v1a2/output/figures/discharge/Pannerdensche Kop.png"
"compare1d2d/rijn-j22_6-v1a2/output/figures/discharge/Pannerdensche Kop.png",
)

# 3. Run test
Expand Down Expand Up @@ -229,12 +232,16 @@ def test_if_style_is_given_figure_produced(self):
# 2. Set expectations
# this file should exist
output_file = TestUtils.get_local_test_file(
"compare1d2d/rijn-j22_6-v1a2/output/figures/longitudinal/BR-PK-IJ.png"
"compare1d2d/rijn-j22_6-v1a2/output/figures/longitudinal/BR-PK-IJ.png",
)

# 3. Run test
for style in styles:
plotter = Compare1D2D(project=project, start_time=datetime(year=2000, month=1, day=5), style=style)
plotter = Compare1D2D(
project=project,
start_time=datetime(year=2000, month=1, day=5),
style=style,
)

plotter.figure_longitudinal(route=["BR", "PK", "IJ"], stat="last25")

Expand Down
Loading