Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wenzeslaus committed Jul 10, 2023
1 parent 7b92a6d commit 0382686
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/vector/v.rast.move/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""Setup dataset for v.rast.move test"""

from types import SimpleNamespace

import pytest

import grass.script as gs

LINES = """\
VERTI:
L 9 1
0.0984456 0.61658031
0.15025907 0.68264249
0.2642487 0.76943005
0.39119171 0.79792746
0.52202073 0.80181347
0.62953368 0.78367876
0.68134715 0.73056995
0.71243523 0.64637306
0.73445596 0.54663212
1 1
L 8 1
0.09455959 0.40544041
0.31217617 0.40673575
0.61917098 0.40932642
0.75518135 0.41580311
0.86658031 0.41580311
0.86528497 0.35621762
0.87305699 0.20595855
0.87823834 0.08290155
1 2
"""


@pytest.fixture(scope="module")
def line_dataset(tmp_path_factory):
"""Create a session and fill mapset with data"""
tmp_path = tmp_path_factory.mktemp("line_dataset")
location = "test"
lines_name = "lines"
gs.core._create_location_xy(tmp_path, location) # pylint: disable=protected-access
with gs.setup.init(tmp_path / location):
gs.write_command(
"v.in.ascii", input="-", output=lines_name, stdin=LINES, format="standard"
)
gs.run_command("g.region", vector=lines_name, grow=0.1, res=0.01, flags="a")

x_name = "x"
y_name = "y"
x_value = 2.0
y_value = 5.3
nulls_name = "null_only"
gs.mapcalc(f"{x_name} = {x_value}")
gs.mapcalc(f"{y_name} = {y_value}")
gs.mapcalc(f"{nulls_name} = 0.0 + null()")

yield SimpleNamespace(
name=lines_name,
x=x_name,
y=y_name,
x_value=x_value,
y_value=y_value,
nulls=nulls_name,
)
72 changes: 72 additions & 0 deletions src/vector/v.rast.move/tests/test_v_rast_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""Test v.rast.move"""

import pytest

import grass.script as gs


def test_displacement_result(line_dataset):
"""Check geometry of the result"""
result = "result"
gs.run_command(
"v.rast.move",
input=line_dataset.name,
x_raster=line_dataset.x,
y_raster=line_dataset.y,
output=result,
)
old_metadata = gs.vector_info(line_dataset.name)
metadata = gs.vector_info(result)

for item in ["lines", "nodes"]:
assert old_metadata[item] == metadata[item]
assert metadata["north"] < line_dataset.y_value + 1
assert metadata["south"] > line_dataset.y_value
assert metadata["west"] > line_dataset.x_value
assert metadata["east"] < line_dataset.x_value + 1


@pytest.mark.parametrize("nulls", ["zeros", "warning", "error"])
def test_null_options_accepted(line_dataset, nulls):
"""Check that all values for the nulls option are accepted"""
result = f"result_nulls_{nulls}"
gs.run_command(
"v.rast.move",
input=line_dataset.name,
x_raster=line_dataset.x,
y_raster=line_dataset.y,
output=result,
nulls=nulls,
)


def test_nulls_as_zeros(line_dataset):
"""Check that transforming nulls to zeros works"""
result = "result_zeros"
gs.run_command(
"v.rast.move",
input=line_dataset.name,
x_raster=line_dataset.nulls,
y_raster=line_dataset.nulls,
output=result,
nulls="zeros",
)
old_metadata = gs.vector_info(line_dataset.name)
metadata = gs.vector_info(result)

for item in ["lines", "nodes", "north", "south", "east", "west"]:
assert old_metadata[item] == metadata[item], item


def test_nulls_fail(line_dataset):
"""Check that an error is generated for nulls"""
result = "result_fails"
with pytest.raises(gs.CalledModuleError):
gs.run_command(
"v.rast.move",
input=line_dataset.name,
x_raster=line_dataset.nulls,
y_raster=line_dataset.nulls,
output=result,
nulls="error",
)

0 comments on commit 0382686

Please sign in to comment.