Skip to content

Commit

Permalink
pytest: Allow default test file naming patterns inside tests folders (#…
Browse files Browse the repository at this point in the history
…4784)

* pytest: Allow default test file naming patterns inside tests folders

* tests: Patch environment variables in xy_session fixture for grassTask test

* Update test_script_task.py

Co-authored-by: Markus Neteler <[email protected]>

* Extract expected value to variable

---------

Co-authored-by: Markus Neteler <[email protected]>
  • Loading branch information
echoix and neteler authored Dec 15, 2024
1 parent ea0185f commit 5e27d1a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ ignore = [

[tool.pytest.ini_options]
minversion = "6.0"
python_files = "*/tests/*_test.py"
python_files = "*/tests/*_test.py */tests/test_*.py"
addopts = """
--ignore-glob='dist.*'
--ignore-glob='bin.*'
Expand Down
47 changes: 45 additions & 2 deletions python/grass/script/tests/test_script_task.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,54 @@
import os

import pytest

import grass.script as gs
from grass.script.task import grassTask


def test_mapcalc_simple_e_name():
@pytest.fixture
def xy_session_patched_env(tmp_path, monkeypatch):
"""Active session in an XY location (scope: function), patching env vars directly.
This allows functions not accepting an env dictionary argument to work in tests"""
location = "xy_test"
gs.core._create_location_xy(tmp_path, location) # pylint: disable=protected-access
with gs.setup.init(tmp_path / location, env=os.environ.copy()) as session:
for key, value in session.env.items():
monkeypatch.setenv(key, value)
yield session


def test_mapcalc_simple_e_name(xy_session_patched_env):
gt = grassTask("r.mapcalc.simple")
assert gt.get_param("e")["name"] == "e"


def test_mapcalc_simple_expession_name():
def test_mapcalc_simple_expression_name(xy_session_patched_env):
gt = grassTask("r.mapcalc.simple")
assert gt.get_param("expression")["name"] == "expression"


def test_d_vect_from_bin(xy_session_patched_env):
"""Tests that a module installed in "$GISBASE/bin can be used with grassTask"""
task = grassTask("d.vect")
task.get_param("map")["value"] = "map_name"
task.get_flag("i")["value"] = True
task.get_param("layer")["value"] = 1
task.get_param("label_bcolor")["value"] = "red"
# the default parameter display is added automatically
actual = " ".join(task.get_cmd())
expected = "d.vect -i map=map_name layer=1 display=shape label_bcolor=red"
assert actual == expected


def test_v_clip_from_scripts(xy_session_patched_env):
"""Tests that a module installed in "$GISBASE/scripts can be used with grassTask"""
task = grassTask("v.clip")
task.get_param("input")["value"] = "map_name"
task.get_flag("r")["value"] = True
task.get_param("clip")["value"] = "clip_map_name"
task.get_param("output")["value"] = "output_map_name"
actual = " ".join(task.get_cmd())
expected = "v.clip -r input=map_name clip=clip_map_name output=output_map_name"
assert actual == expected

0 comments on commit 5e27d1a

Please sign in to comment.