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

r.null: added test cases the module #5214

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
91 changes: 91 additions & 0 deletions raster/r.null/testsuite/test_r_null.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import grass.script as gs
from grass.gunittest.case import TestCase
from grass.gunittest.main import test
from grass.gunittest.gmodules import SimpleModule


class TestRNull(TestCase):
"""Test case for r.null module"""

@classmethod
def setUpClass(cls):
"""Set up a temporary region and create test raster maps"""
cls.use_temp_region()
cls.runModule("g.region", n=3, s=0, e=3, w=0, res=1)

# Create map1: categories 1, 2, 3 as rows
cls.runModule(
"r.mapcalc",
expression="map_basic = if(row() == 1, 1, if(row() == 2, 2, if(row() == 3, 3, null())))",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be simplified:

map_basic = row()

overwrite=True,
)

cls.runModule(
"r.mapcalc",
expression="map_fill_nulls = if(row() == 1, 1, if(row() == 2, 2, if(row() == 3, 3, null())))",
overwrite=True,
)

# Create map1: categories 1, 2.5, 3 as rows
cls.runModule(
"r.mapcalc",
expression="map2 = if(row() == 1, 1, if(row() == 2, 2.5, if(row() == 3, 3, null())))",
overwrite=True,
)

@classmethod
def tearDownClass(cls):
"""Remove temporary maps and region"""
cls.runModule(
"g.remove",
flags="f",
type="raster",
name=["map_basic", "map_fill_nulls", "map2"],
)
cls.del_temp_region()

def test_basic(self):
"""Verify module execute with -i flag"""
module = SimpleModule("r.null", map="map_basic", setnull="1", flags="i")
self.assertModule(module)

# Validate category mappings using r.category
category_output = gs.parse_command("r.describe", map="map_basic", format="json")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using "1" flag here to just list all the values seems better

expected_output = {
"has_nulls": True,
"ranges": [{"min": 2, "max": 2}, {"min": 3, "max": 3}],
}

self.assertEqual(category_output, expected_output)

def test_float_flag(self):
"""Verify module execute with -f flag"""
module = SimpleModule("r.null", map="map2", setnull="1", flags="f")
self.assertModule(module)

category_output = gs.parse_command("r.describe", map="map2", format="json")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here the range flag may be better for the floating point

expected_output = {
"has_nulls": True,
"ranges": [
{"min": 2.5, "max": 2.5019607843137255},
{"min": 2.9980392156862745, "max": 3},
],
}

self.assertEqual(category_output, expected_output)

def test_fill_nulls(self):
"""Verify module fills nulls"""
module = SimpleModule("r.null", map="map_fill_nulls", null="1")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not doing much, it would be better if the input had some nulls, no?

self.assertModule(module)

category_output = gs.parse_command(
"r.describe", map="map_fill_nulls", format="json"
)
expected_output = {"has_nulls": False, "ranges": [{"min": 1, "max": 3}]}

self.assertEqual(category_output, expected_output)


if __name__ == "__main__":
test()
Loading