Skip to content

Commit

Permalink
Merge pull request #3 from mundialis/tests
Browse files Browse the repository at this point in the history
Tests
  • Loading branch information
anikaweinmann committed Jul 7, 2023
2 parents f930fdd + 02235c9 commit d23c1d0
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 93 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/grass-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Run tests for GRASS GIS addons
on:
push:
branches: [ main ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ main ]

jobs:
tests:
uses: mundialis/github-workflows/.github/workflows/grass-tests.yml@main
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*/test_keyvalue_result.txt
39 changes: 0 additions & 39 deletions .pylintrc

This file was deleted.

50 changes: 0 additions & 50 deletions .pylintrc_allowed_to_fail

This file was deleted.

1 change: 0 additions & 1 deletion README.md

This file was deleted.

2 changes: 1 addition & 1 deletion r.in.wcs.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ <h3>Import WCS of global worldpop map</h3>
Import WCS of global worldpop map in `nc_spm_08` location:

<div class="code"><pre>
g.region n=187000 s=185000 w=173000 e=173300 -p res=1
g.region n=186650 s=185425 w=172622 e=174012 res=1 -p

# get GetCapabilities
r.in.wcs -c url=https://geoserver.mundialis.de/geoserver/global/ows?
Expand Down
4 changes: 2 additions & 2 deletions r.in.wcs.worker/r.in.wcs.worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ def main():
RM_FILES.append(tif)
try:
urlretrieve(url, tif)
except URLError as url_e:
grass.fatal(_(f"Failed to reach the server.\nReason: {url_e.reason}"))
except URLError:
grass.fatal(_(f"Failed to reach the server.\nURL: {url}"))
grass.run_command("r.import", input=tif, output=options["output"])
grass.message(
_(f"WCS Coverage {coverageid} is impored as {options['output']}")
Expand Down
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
beautifulsoup4
grass-gis-helpers
lxml
xmltodict
191 changes: 191 additions & 0 deletions testsuite/test_r_in_wcs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
#!/usr/bin/env python3
#
############################################################################
#
# MODULE: r.in.wcs tests
# AUTHOR(S): Anika Weinmann
#
# PURPOSE: Tests r.in.wcs
# COPYRIGHT: (C) 2023 by mundialis GmbH & Co. KG and the GRASS Development
# Team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#############################################################################

import os

from grass.gunittest.case import TestCase
from grass.gunittest.main import test
from grass.gunittest.gmodules import SimpleModule
import grass.script as grass


class TestRInWcs(TestCase):
"""Test class for r.in.wcs"""

pid = os.getpid()
region = f"r_in_wcs_orig_region_{pid}"
out = f"r_in_wcs_test_output_{pid}"
# WCS and coverageId
url = "https://geoserver.mundialis.de/geoserver/global/ows?"
coverageid = "global__worldpop_2020_1km_aggregated_UNadj"
num_coverage_ids = 11
coverage_name = "Worldpop 2020 1km aggregated UNadj Pop. density"
north = 186650
south = 185425
west = 172622
east = 174012
num_data = None

@classmethod
# pylint: disable=invalid-name
def setUpClass(cls):
"""Ensures expected computational region"""
cls.num_data = len(
grass.parse_command("g.list", type="all", mapset=".")
)
# set region
cls.runModule("g.region", save=cls.region)
cls.runModule(
"g.region",
n=cls.north,
s=cls.south,
w=cls.west,
e=cls.east,
res=1,
)

@classmethod
# pylint: disable=invalid-name
def tearDownClass(cls):
"""Remove the temporary region and generated data"""
cls.runModule("g.region", region=cls.region)
cls.runModule("g.remove", type="region", name=cls.region, flags="f")
# check number of data in mapset
num_data = len(grass.parse_command("g.list", type="all", mapset="."))
if num_data != cls.num_data:
cls.fail(cls, "Test or addon does not cleaned up correctly.")

# pylint: disable=invalid-name
def tearDown(self):
"""Remove the outputs created
This is executed after each test run.
"""
if grass.find_file(name=self.out, element="raster")["file"]:
self.runModule(
"g.remove",
type="raster",
name=f"{self.out}",
flags="f",
)

def test_getcapabilities(self):
"""
Tests GetCapabilities
"""
print("\nTest GetCapabilities ...")
r_check = SimpleModule(
"r.in.wcs",
url=self.url,
flags="c",
)
self.assertModule(r_check, "GetCapabilities fails.")
stdout = r_check.outputs.stdout
self.assertIn(
self.coverageid,
stdout,
"Coverage not in xml GetCapabilities stdout",
)
print("Test GetCapabilities successfully finished.\n")

def test_list_coverageids(self):
"""
Tests list coverage ids
"""
print("\nTest list coverage ids ...")
r_check = SimpleModule(
"r.in.wcs",
url=self.url,
flags="l",
)
self.assertModule(r_check, "List coverage ids fails.")
stdout = r_check.outputs.stdout
self.assertIn(
self.coverageid,
stdout,
"Coverage not in list of coverage id",
)
self.assertEqual(
len(stdout.split("\n")),
self.num_coverage_ids,
f"Length of the coverage id list not {self.num_coverage_ids}.",
)
print("Test list coverage ids successfully finished.\n")

def test_describe_coverage(self):
"""
Tests DescribeCoverage
"""
print("\nTest DescribeCoverage ...")
r_check = SimpleModule(
"r.in.wcs",
url=self.url,
flags="d",
coverageid=self.coverageid,
)
self.assertModule(r_check, "DescribeCoverage fails.")
stdout = r_check.outputs.stdout
self.assertIn(
self.coverageid,
stdout,
"Coverage id not in xml DescribeCoverage stdout",
)
self.assertIn(
self.coverage_name,
stdout,
"Coverage name not in xml DescribeCoverage stdout",
)
print("Test DescribeCoverage successfully finished.\n")

def test_data_import(self):
"""
Tests data import
"""
print("\nTest data import ...")
r_check = SimpleModule(
"r.in.wcs",
url=self.url,
coverageid=self.coverageid,
output=self.out,
tile_size=10000,
)
self.assertModule(r_check, "data import fails.")
self.assertRasterExists(self.out)
self.assertRasterMinMax(
self.out, 32.67583, 184.805, "Raster range wrong"
)
# check extent
info = grass.parse_command("r.info", map=self.out, flags="g")
self.assertTrue(
(
self.north > float(info["north"])
and float(info["south"]) > self.south
and self.east > float(info["east"])
and float(info["west"]) > self.west
),
"Extent of output raster wrong",
)
print("Test data import successfully finished.\n")


if __name__ == "__main__":
test()

0 comments on commit d23c1d0

Please sign in to comment.