Skip to content

Commit

Permalink
Added tests to improve code coverage
Browse files Browse the repository at this point in the history
Also reformatted with black. Tests were added for the get_method and diagnostics_info functions in the postprocessing interface. Tests for the discover_diagnostics function will be written once these changes have been merged as then the cookiecutter plugins can then be properly tested.
  • Loading branch information
joeycasey87 committed Aug 13, 2024
1 parent 429641d commit 6452ef2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 50 deletions.
55 changes: 7 additions & 48 deletions pysteps/postprocessing/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
pysteps.postprocessing.diagnostics
====================
Methods for applying postprocessing.
Methods for diagnostic postprocessing.
The methods in this module implement the following interface::
postprocess_xxx(optional arguments)
diagnostic_xyz(optional arguments)
where **xxx** is the name of the postprocess to be applied.
where **xyz** is the name of the diagnostic postprocessing to be applied.
Postprocessor standardizations can be specified here if there is a desired input and output format that all should
adhere to.
Expand All @@ -21,51 +21,10 @@
"""

try:
from osgeo import gdal, gdalconst, osr

GDAL_IMPORTED = True
except ImportError:
GDAL_IMPORTED = False
def diagnostics_example1(filename, **kwargs):
return "Hello, I am an example postprocessor."

try:
import h5py

H5PY_IMPORTED = True
except ImportError:
H5PY_IMPORTED = False

try:
import metranet

METRANET_IMPORTED = True
except ImportError:
METRANET_IMPORTED = False

try:
import netCDF4

NETCDF4_IMPORTED = True
except ImportError:
NETCDF4_IMPORTED = False

try:
from PIL import Image

PIL_IMPORTED = True
except ImportError:
PIL_IMPORTED = False

try:
import pyproj

PYPROJ_IMPORTED = True
except ImportError:
PYPROJ_IMPORTED = False

try:
import pygrib

PYGRIB_IMPORTED = True
except ImportError:
PYGRIB_IMPORTED = False
def diagnostics_example2(filename, **kwargs):
return 42
8 changes: 6 additions & 2 deletions pysteps/postprocessing/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
from pysteps.postprocessing import diagnostics
from pprint import pprint

_diagnostics_methods = dict()
_diagnostics_methods = dict(
example1=diagnostics.diagnostics_example1, example3=lambda x: [x, x]
)


def discover_diagnostics():
Expand Down Expand Up @@ -97,6 +99,8 @@ def diagnostics_info():
available_diagnostics = set(available_diagnostics)
diagnostics_in_the_interface = set(diagnostics_in_the_interface)

available_diagnostics = {s.split("_")[1] for s in available_diagnostics}

difference = available_diagnostics ^ diagnostics_in_the_interface
if len(difference) > 0:
print("\nIMPORTANT:")
Expand Down Expand Up @@ -145,7 +149,7 @@ def get_method(name, method_type):
| Name | Description |
+=============+=======================================================+
method_type: {'diagnostics', diagnostic_diagnostics}
method_type: {'diagnostics', diagnostics_name}
Type of the method (see tables above).
"""
Expand Down
48 changes: 48 additions & 0 deletions pysteps/tests/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,51 @@ def test_tracking_interface():

invalid_names = ["lucas-kanade", "dating"]
_generic_interface_test(method_getter, valid_names_func_pair, invalid_names)


def test_postprocessing_interface():
"""Test the postprocessing module interface."""

import pysteps.postprocessing as postprocessing
from pysteps.postprocessing import diagnostics_example1
from pysteps.postprocessing import diagnostics_example2

valid_names_func_pair = [("example1", diagnostics_example1)]

def method_getter(name):
return pysteps.postprocessing.interface.get_method(name, "diagnostics")

invalid_names = ["bom", "fmi", "knmi", "mch", "mrms", "opera", "saf"]
_generic_interface_test(method_getter, valid_names_func_pair, invalid_names)

# Test for invalid argument type
with pytest.raises(TypeError):
pysteps.postprocessing.interface.get_method("example1", None)
pysteps.postprocessing.interface.get_method(None, "diagnostics")

# Test for invalid method types
with pytest.raises(ValueError):
pysteps.postprocessing.interface.get_method("example1", "io")

with pytest.raises(TypeError):
pysteps.postprocessing.interface.get_method(24, "diagnostics")

assert isinstance(
pysteps.postprocessing.interface.diagnostics_info()[0],
set,
)

assert isinstance(
pysteps.postprocessing.interface.diagnostics_info()[1],
set,
)

assert isinstance(
diagnostics_example1(filename="example_filename"),
str,
)

assert isinstance(
diagnostics_example2(filename="example_filename"),
int,
)

0 comments on commit 6452ef2

Please sign in to comment.