Skip to content

Commit

Permalink
removed unnecessary try except statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Tjalling-dejong committed Nov 13, 2024
1 parent 3cc1c86 commit 503f432
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 378 deletions.
3 changes: 0 additions & 3 deletions tests/TestUtils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import contextlib
import os
import shutil
import sys
from pathlib import Path
from typing import List

import pytest

Expand Down
40 changes: 11 additions & 29 deletions tests/test_Import.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import numbers
import os
import shutil
import sys
import unittest
from pathlib import Path

import pytest
Expand Down Expand Up @@ -89,11 +84,9 @@ def test_when_given_expected_arguments_then_object_is_created(self):
css_data,
]
return_fm_model_data = None
# 2. Run test
try:
return_fm_model_data = FmModelData(arg_list)
except:
pytest.fail("No exception expected but was thrown")

# 2. Run test
return_fm_model_data = FmModelData(arg_list)

# 4. Verify final expectations
assert return_fm_model_data is not None
Expand Down Expand Up @@ -127,12 +120,9 @@ def test_when_given_data_dictionary_then_css_data_list_is_set(self):
# 2. Set expectations
expected_css_data_list = [{dummy_key: 0}, {dummy_key: 1}]

# 3. Run test
try:
return_fm_model_data = FmModelData(arg_list)
except:
pytest.fail("No exception expected but was thrown")

# 3. Run test
return_fm_model_data = FmModelData(arg_list)

# 4. Verify final expectations
assert return_fm_model_data is not None
assert return_fm_model_data.css_data_list != css_data_dict
Expand All @@ -153,11 +143,8 @@ def test_when_given_dictionary_then_returns_list(self):
expected_list = [{dummy_key: 0}, {dummy_key: 1}]

# 3. Run test
try:
return_list = FmModelData.get_ordered_css_list(test_dict)
except:
pytest.fail("No exception expected but was thrown")

return_list = FmModelData.get_ordered_css_list(test_dict)

# 4. Verify final expectations
assert return_list is not None
assert return_list == expected_list, (
Expand All @@ -170,18 +157,13 @@ def test_when_given_dictionary_then_returns_list(self):
def test_when_given_unexpected_value_then_returns_empty_list(self, test_dict):
# 1. Set up test_data
return_list = None
dummy_key = "dummyKey"
dummy_values = [0, 1]

# 2. Run test
expected_list = []

# 3. Run test
try:
return_list = FmModelData.get_ordered_css_list(test_dict)
except:
pytest.fail("No exception expected but was thrown")

# 3. Run test
return_list = FmModelData.get_ordered_css_list(test_dict)

# 4. Verify final expectations
assert return_list is not None
assert return_list == expected_list, (
Expand Down
118 changes: 42 additions & 76 deletions tests/test_acceptance.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import io
import os
import shutil
import sys
import unittest
from pathlib import Path
from typing import Optional

import matplotlib.pyplot as plt
import pytest

from fm2prof.Fm2ProfRunner import Fm2ProfRunner
Expand Down Expand Up @@ -374,21 +371,17 @@ def test_when_fm2prof_output_then_use_it_for_sobek_model_input(self):
fm_dir = str(waal_test_folder / "Model_FM")
fm2prof_dir = _get_test_case_output_dir(_waal_case)

result_figures = []

# 2. Try to compare.
try:
waal_comparer = CompareWaalModel()
output_1d, _ = waal_comparer._run_waal_1d_model(
case_name=_waal_case,
results_dir=fm2prof_dir,
sobek_dir=sobek_dir,
fm_dir=fm_dir,
)
except Exception as e_info:
pytest.fail(
"No exception expected but was thrown " + "{}.".format(str(e_info))
)

waal_comparer = CompareWaalModel()
output_1d, _ = waal_comparer._run_waal_1d_model(
case_name=_waal_case,
results_dir=fm2prof_dir,
sobek_dir=sobek_dir,
fm_dir=fm_dir,
)


# 3. Verify final expectations
assert output_1d
Expand All @@ -406,18 +399,14 @@ def test_when_sobek_output_exist_then_create_figures(self):
result_figures = []

# 2. Try to compare.
try:
waal_comparer = CompareWaalModel()
result_figures = waal_comparer._compare_waal(
case_name=_waal_case,
results_dir=fm2prof_dir,
sobek_dir=sobek_dir,
fm_dir=fm_dir,
)
except Exception as e_info:
pytest.fail(
"No exception expected but was thrown " + "{}.".format(str(e_info))
)

waal_comparer = CompareWaalModel()
result_figures = waal_comparer._compare_waal(
case_name=_waal_case,
results_dir=fm2prof_dir,
sobek_dir=sobek_dir,
fm_dir=fm_dir,
)

# 3. Verify final expectations
assert result_figures
Expand Down Expand Up @@ -451,14 +440,10 @@ def test_when_output_exists_then_compare_waal_model_volume(self, case_name: str)
if not os.path.exists(fm2prof_fig_dir):
os.makedirs(fm2prof_fig_dir)

# 3. Run
try:
waal_comparer = CompareWaalModel()
waal_comparer._compare_volume(case_name, input_volume_file, fm2prof_fig_dir)
except Exception as e_info:
pytest.fail(
"No exception expected but was thrown " + "{}.".format(str(e_info))
)
# 3. Run
waal_comparer = CompareWaalModel()
waal_comparer._compare_volume(case_name, input_volume_file, fm2prof_fig_dir)


# 4. Final expectation
assert os.listdir(
Expand Down Expand Up @@ -570,16 +555,12 @@ def ARCHIVED_test_compare_generic_model_geometry(self, case_name: str):
if not tzw_values or tzw_values is None:
pytest.fail("Test failed, no values retrieved for {}".format(case_name))

try:
generic_comparer = CompareIdealizedModel()
generic_comparer._compare_css(
case_name, tzw_values, input_geometry_file, fm2prof_fig_dir
)
except Exception as e_info:
pytest.fail(
"No exception expected but was thrown " + "{}.".format(str(e_info))
)


generic_comparer = CompareIdealizedModel()
generic_comparer._compare_css(
case_name, tzw_values, input_geometry_file, fm2prof_fig_dir
)

# 4. Final expectation
assert os.listdir(
fm2prof_fig_dir
Expand Down Expand Up @@ -616,15 +597,11 @@ def ARCHIVED_test_when_output_exists_then_compare_generic_model_roughness(
if not tzw_values or tzw_values is None:
pytest.fail("Test failed, no values retrieved for {}".format(case_name))

try:
generic_comparer = CompareIdealizedModel()
generic_comparer._compare_roughness(
case_name, tzw_values, input_roughness_file, fm2prof_fig_dir
)
except Exception as e_info:
pytest.fail(
"No exception expected but was thrown " + "{}.".format(str(e_info))
)
generic_comparer = CompareIdealizedModel()
generic_comparer._compare_roughness(
case_name, tzw_values, input_roughness_file, fm2prof_fig_dir
)


assert os.listdir(
fm2prof_fig_dir
Expand Down Expand Up @@ -658,16 +635,12 @@ def ARCHIVED_test_when_output_exists_then_compare_generic_model_volume(
os.makedirs(fm2prof_fig_dir)

# 3. Run
try:
generic_comparer = CompareIdealizedModel()
generic_comparer._compare_volume(
case_name, input_volume_file, fm2prof_fig_dir
)
except Exception as e_info:
pytest.fail(
"No exception expected but was thrown " + "{}.".format(str(e_info))
)


generic_comparer = CompareIdealizedModel()
generic_comparer._compare_volume(
case_name, input_volume_file, fm2prof_fig_dir
)

# 4. Final expectation
assert os.listdir(
fm2prof_fig_dir
Expand All @@ -685,10 +658,6 @@ def test_when_output_exists_then_compare_with_reference(self, case_name: str):
"""
# 1. Get all necessary output / input directories
reference_geometry = self.__case_tzw_dict.get(case_name)
reference_roughness = (
None # CompareHelper.get_analytical_roughness_for_case(case_name)
)
reference__volume = []
fm2prof_dir = _get_test_case_output_dir(case_name)

# 2. Verify / create necessary folders and directories
Expand Down Expand Up @@ -756,13 +725,10 @@ def test_when_waal_case_then_performance_is_slow(self):
assert os.path.exists(css_file), "" + "CrossSection (test) file was not found"

# 3. Run test.
try:
runner = Fm2ProfRunner(iniFilePath=None)
runner.run_inifile(iniFile=test_ini_file)
except Exception as e_error:
# if os.path.exists(root_output_dir):
# shutil.rmtree(root_output_dir)
pytest.fail("No exception expected but was thrown {}.".format(str(e_error)))

runner = Fm2ProfRunner(iniFilePath=None)
runner.run_inifile(iniFile=test_ini_file)


# 4. Verify final expectations.
pass
Expand Down
63 changes: 19 additions & 44 deletions tests/test_crosssection.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import datetime
import numpy as np
import pytest


from fm2prof.CrossSection import CrossSection
from fm2prof.IniFile import IniFile
import pickle

from tests.TestUtils import TestUtils
Expand Down Expand Up @@ -69,16 +67,10 @@ def test_when_correct_input_dict_is_given_CrossSection_initialises(self):
with open(tdir.joinpath(f"{test_cases[0].get('name')}.pickle"), 'rb') as f:
css_data = pickle.load(f)

# 2. Set expectations


# 3. Run test
try:
css = CrossSection(data=css_data)
except Exception as e_info:
pytest.fail(
f"No expected exception but was risen:" + " {e_info}")

# 2. Set expectations
# 3. Run test
css = CrossSection(data=css_data)

# 4. Verify final expectations
assert css.length == css_data.get('length')

Expand All @@ -96,13 +88,9 @@ def test_build_geometry(self):
css_z: np.array = test_case.get('css_z')
css_total_volume: np.array = test_case.get('css_total_volume')

# 3. Run test
try:
css = CrossSection(data=css_data)
css.build_geometry()
except Exception as e_info:
pytest.fail(
f"No expected exception but was risen:" + " {e_info}")
# 3. Run test
css = CrossSection(data=css_data)
css.build_geometry()

# 4. Verify final expectations
assert len(css_z) == len(css._css_z)
Expand All @@ -124,14 +112,11 @@ def test_calculate_correction(self):
crest_level:float = test_case.get('crest_level') # type: ignore
extra_total_volume: np.ndarray = test_case.get('extra_total_volume') # type: ignore

# 3. Run test
try:
css = CrossSection(data=css_data)
css.build_geometry()
css.calculate_correction()
except Exception as e_info:
pytest.fail(
f"No expected exception but was risen:" + " {e_info}")
# 3. Run test
css = CrossSection(data=css_data)
css.build_geometry()
css.calculate_correction()


# 4. Verify final expectations
assert abs(crest_level - css.crest_level) < tol
Expand All @@ -144,21 +129,11 @@ def test_reduce_points(self):
with open(tdir.joinpath(f"{test_case.get('name')}.pickle"), 'rb') as f:
css_data = pickle.load(f)

tol = 1e-6

# 2. Set expectations for
crest_level:float = test_case.get('crestlevel')
extra_total_volume: np.ndarray = test_case.get('extra_total_volume')

# 3. Run test
try:
css = CrossSection(data=css_data)
css.build_geometry()
css.calculate_correction()
css.reduce_points(count_after=20)
except Exception as e_info:
pytest.fail(
f"No expected exception but was risen:" + " {e_info}")

# 4. Verify final expectations
# 2. Run test
css = CrossSection(data=css_data)
css.build_geometry()
css.calculate_correction()
css.reduce_points(count_after=20)

# 3. Verify final expectations
assert len(css.z) == 20
Loading

0 comments on commit 503f432

Please sign in to comment.