Skip to content

Commit

Permalink
move the SourceSinkConverter tests to a separate test file
Browse files Browse the repository at this point in the history
  • Loading branch information
MAfarrag committed Dec 23, 2024
1 parent 5934485 commit 8847cc0
Show file tree
Hide file tree
Showing 2 changed files with 246 additions and 244 deletions.
244 changes: 0 additions & 244 deletions tests/tools/test_converters.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
from pathlib import Path
from unittest.mock import patch

import numpy as np
import pytest

from hydrolib.core.basemodel import DiskOnlyFileModel
from hydrolib.core.dflowfm.bc.models import ForcingModel
from hydrolib.core.dflowfm.ext.models import Boundary, Meteo
from hydrolib.core.dflowfm.extold.models import ExtOldForcing, ExtOldQuantity
from hydrolib.core.dflowfm.inifield.models import InitialField, ParameterField
from hydrolib.core.dflowfm.polyfile.models import PolyFile
from hydrolib.tools.ext_old_to_new.converters import (
BoundaryConditionConverter,
InitialConditionConverter,
MeteoConverter,
ParametersConverter,
SourceSinkConverter,
)


Expand Down Expand Up @@ -115,241 +109,3 @@ def test_default(self):
assert new_quantity_block.nodeid is None
assert new_quantity_block.bndwidth1d is None
assert new_quantity_block.bndbldepth is None


class TestParseTimFileForSourceSink:
def test_default(self):
"""
The test case is based on the following assumptions:
- The tim file has 4 columns (plus the time column), but the list of ext quantities has only 4 quantities.
"""
tim_file = Path("tests/data/input/source-sink/leftsor.tim")
ext_file_quantity_list = [
"discharge",
"temperature",
"salinity",
"initialtracer_anyname",
]
converter = SourceSinkConverter()

time_series_data = converter.parse_tim_model(tim_file, ext_file_quantity_list)
assert time_series_data["discharge"] == [1.0, 1.0, 1.0, 1.0, 1.0]
assert time_series_data["temperaturedelta"] == [2.0, 2.0, 2.0, 2.0, 2.0]
assert time_series_data["salinitydelta"] == [3.0, 3.0, 3.0, 3.0, 3.0]
assert time_series_data["initialtracer_anyname"] == [4.0, 4.0, 4.0, 4.0, 4.0]

def test_list_of_ext_quantities_tim_column_mismatch(self):
"""
The test case is based on the following assumptions:
- The tim file has 4 columns (plus the time column), but the list of ext quantities has only 3 quantities.
"""
tim_file = Path("tests/data/input/source-sink/leftsor.tim")
ext_file_quantity_list = ["discharge", "temperature", "salinity"]
converter = SourceSinkConverter()
with pytest.raises(ValueError):
converter.parse_tim_model(tim_file, ext_file_quantity_list)


class TestSourceSinkConverter:

def test_get_z_sources_sinks_single_value(self):
"""
The test case is based on the following assumptions:
- The polyline has only 3 columns, so the zsink and zsource will have only one value which is in the third column.
```
zsink = -4.2
zsource = -3
```
- The polyline file has the following structure:
```
L1
2 3
63.35 12.95 -4.20
45.20 6.35 -3.00
```
"""
polyfile = PolyFile("tests/data/input/source-sink/leftsor.pliz")

z_source, z_sink = SourceSinkConverter().get_z_sources_sinks(polyfile)
assert z_source == [-3]
assert z_sink == [-4.2]

def test_get_z_sources_sinks_multiple_values(self):
"""
The test case is based on the following assumptions:
- The polyline has only four or five columns, so the zsink and zsource will have two values which is in the
third and forth columns' values, and if there is a fifth column it will be ignored.
```
zsink = [-4.2, -5.35]
zsource = [-3, -2.90]
```
- The polyline file has the following structure:
```
L1
2 3
63.35 12.95 -4.20 -5.35
...
...
45.20 6.35 -3.00 -2.90
```
when there is a fifth column:
```
L1
2 3
63.35 12.95 -4.20 -5.35 0
...
...
45.20 6.35 -3.00 -2.90 0
```
"""
polyfile = PolyFile("tests/data/input/source-sink/leftsor-5-columns.pliz")

z_source, z_sink = SourceSinkConverter().get_z_sources_sinks(polyfile)
assert z_source == [-3, -2.90]
assert z_sink == [-4.2, -5.35]

def test_default(self):
"""
The test case is based on the following assumptions:
- temperature, salinity, and initialtracer_anyname are other quantities in the ext file.
- The ext file has the following structure:
```
QUANTITY=initialtemperature
FILENAME=right.pol
FILETYPE=10
METHOD=4
OPERAND=O
VALUE=11.
QUANTITY=initialsalinity
FILENAME=right.pol
FILETYPE=10
METHOD=4
OPERAND=O
VALUE=11.
QUANTITY=initialtracer_anyname
FILENAME=leftsor.pliz
FILETYPE=9
METHOD=1
OPERAND=O
QUANTITY=discharge_salinity_temperature_sorsin
FILENAME=leftsor.pliz
FILETYPE=9
METHOD=1
OPERAND=O
AREA=1.0
```
- The time file has the following structure:
```
0.0 1.0 2.0 3.0 4.0
100 1.0 2.0 3.0 4.0
200 1.0 2.0 3.0 4.0
300 1.0 2.0 3.0 4.0
400 1.0 2.0 3.0 4.0
```
- The polyline has only 3 columns, so the zsink and zsource will have only one value which is in the third column.
```
zsink = -4.2
zsource = -3
```
- The polyline file has the following structure:
```
L1
2 3
63.350456 12.950216 -4.200000
45.200344 6.350155 -3.000
```
"""
forcing = ExtOldForcing(
quantity=ExtOldQuantity.DischargeSalinityTemperatureSorSin,
filename="tests/data/input/source-sink/leftsor.pliz",
filetype=9,
method="1",
operand="O",
area=1.0,
)

ext_file_other_quantities = [
"salinity",
"temperature",
"initialtracer_anyname",
]

new_quantity_block = SourceSinkConverter().convert(
forcing, ext_file_other_quantities
)
assert new_quantity_block.initialtracer_anyname == [4.0, 4.0, 4.0, 4.0, 4.0]
assert new_quantity_block.salinitydelta == [3.0, 3.0, 3.0, 3.0, 3.0]
assert new_quantity_block.temperaturedelta == [2.0, 2.0, 2.0, 2.0, 2.0]
assert new_quantity_block.discharge == [1.0, 1.0, 1.0, 1.0, 1.0]
assert new_quantity_block.zsink == [-4.2]
assert new_quantity_block.zsource == [-3]

def test_4_5_columns_polyline(self):
"""
The test case is based on the assumptions of the default test plus the following changes:
- The polyline has only four or five columns, so the zsink and zsource will have two values which is in the
third and forth columns' values, and if there is a fifth column it will be ignored.
```
zsink = [-4.2, -5.35]
zsource = [-3, -2.90]
```
- The polyline file has the following structure:
```
L1
2 3
63.35 12.95 -4.20 -5.35
...
...
45.20 6.35 -3.00 -2.90
```
when there is a fifth column:
```
L1
2 3
63.35 12.95 -4.20 -5.35 0
...
...
45.20 6.35 -3.00 -2.90 0
```
"""
forcing = ExtOldForcing(
quantity=ExtOldQuantity.DischargeSalinityTemperatureSorSin,
filename="tests/data/input/source-sink/leftsor-5-columns.pliz",
filetype=9,
method="1",
operand="O",
area=1.0,
)

ext_file_other_quantities = [
"salinity",
"temperature",
"initialtracer_anyname",
]
tim_file = Path("tests/data/input/source-sink/leftsor.tim")
with patch("pathlib.Path.with_suffix", return_value=tim_file):
new_quantity_block = SourceSinkConverter().convert(
forcing, ext_file_other_quantities
)
assert new_quantity_block.initialtracer_anyname == [4.0, 4.0, 4.0, 4.0, 4.0]
assert new_quantity_block.salinitydelta == [3.0, 3.0, 3.0, 3.0, 3.0]
assert new_quantity_block.temperaturedelta == [2.0, 2.0, 2.0, 2.0, 2.0]
assert new_quantity_block.discharge == [1.0, 1.0, 1.0, 1.0, 1.0]
assert new_quantity_block.zsink == [-4.2, -5.35]
assert new_quantity_block.zsource == [-3, -2.90]
Loading

0 comments on commit 8847cc0

Please sign in to comment.