Skip to content

Commit

Permalink
SMRF test suite - Rework base class.
Browse files Browse the repository at this point in the history
Sweeping rework of the SMRFTestCase class, breaking the setUpClass into
chunks to enable overwriting of inheriting classes. Also adds a new
method to copy the SMRF wide base config and adapt for specific cases.
  • Loading branch information
jomey committed Jul 10, 2020
1 parent a68a422 commit 91225f0
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 208 deletions.
2 changes: 1 addition & 1 deletion smrf/tests/output/test_netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from smrf import __version__
from smrf.framework.model_framework import SMRF
from smrf.output.output_netcdf import OutputNetcdf
from smrf.tests.test_configurations import SMRFTestCase
from smrf.tests.smrf_test_case import SMRFTestCase


class TestOutputNetCDF(SMRFTestCase):
Expand Down
8 changes: 4 additions & 4 deletions smrf/tests/output/test_output_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from inicheck.tools import cast_all_variables

from smrf.framework.model_framework import run_smrf
from smrf.tests.test_configurations import SMRFTestCase
from smrf.tests.smrf_test_case import SMRFTestCase


class TestOutputThreadedVariables(SMRFTestCase):

def tearDown(self):
super().tearDownClass()
self.remove_output_dir()

def change_variables(self, new_variables):
config = copy(self.base_config)
Expand Down Expand Up @@ -60,9 +60,9 @@ def setUpClass(cls):
"""
super().setUpClass()

config = copy(cls.base_config)
config = cls.base_config_copy()
config.raw_cfg['system']['threading'] = False

config.apply_recipes()
config = cast_all_variables(config, config.mcfg)
cls.base_config = config
cls._base_config = config
2 changes: 1 addition & 1 deletion smrf/tests/recipes/test_wind_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TestInterpWindRecipes(SMRFTestCase, BaseRecipes):
def test_interp_wind_recipe(self):
"""Test the intper wind recipe
"""
config = deepcopy(self.base_config)
config = self.base_config_copy()

adj_config = {
'wind': {
Expand Down
96 changes: 57 additions & 39 deletions smrf/tests/smrf_test_case.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import shutil
import unittest
from copy import deepcopy
from pathlib import PurePath

import netCDF4 as nc
import numpy as np
Expand All @@ -15,6 +17,8 @@ class SMRFTestCase(unittest.TestCase):
The base test case for SMRF that will load in the configuration file
and store as the base config. Also will remove the output
directory upon tear down.
Runs the short simulation over reynolds mountain east
"""
DIST_VARIABLES = frozenset([
'air_temp',
Expand All @@ -25,13 +29,58 @@ class SMRFTestCase(unittest.TestCase):
'wind',
])

BASE_INI_FILE_NAME = 'test_base_config.ini'

test_dir = PurePath(smrf.__file__).parent.joinpath('tests')
config_file = os.path.join(test_dir, BASE_INI_FILE_NAME)

@property
def dist_variables(self):
if self._dist_variables is None:
self._dist_variables = list(self.DIST_VARIABLES)
return self._dist_variables

def can_i_run_smrf(self, config):
@property
def base_config(self):
return deepcopy(self._base_config)

@classmethod
def base_config_copy(cls):
return deepcopy(cls._base_config)

@classmethod
def load_base_config(cls):
cls._base_config = get_user_config(cls.config_file, modules='smrf')

@classmethod
def setUpClass(cls):
cls.load_base_config()
cls.create_output_dir()

@classmethod
def tearDownClass(cls):
cls.remove_output_dir()
delattr(cls, '_output_dir')

@classmethod
def create_output_dir(cls):
folder = os.path.join(cls._base_config.cfg['output']['out_location'])

# Remove any potential files to ensure fresh run
if os.path.isdir(folder):
shutil.rmtree(folder)

os.makedirs(folder)
cls._output_dir = folder

@classmethod
def remove_output_dir(cls):
if hasattr(cls, '_output_dir') and \
os.path.exists(cls._output_dir):
shutil.rmtree(cls._output_dir)

@staticmethod
def can_i_run_smrf(config):
"""
Test whether a config is possible to run
"""
Expand All @@ -43,7 +92,8 @@ def can_i_run_smrf(self, config):
print(e)
return False

def assertGoldEqual(self, gold, not_gold, error_msg):
@staticmethod
def assert_gold_equal(gold, not_gold, error_msg):
"""Compare two arrays
Arguments:
Expand All @@ -66,6 +116,9 @@ def assertGoldEqual(self, gold, not_gold, error_msg):
err_msg=error_msg
)

def setUp(self):
self._dist_variables = None

def compare_netcdf_files(self, gold_file, test_file):
"""
Compare two netcdf files to ensure that they are identical. The
Expand Down Expand Up @@ -96,48 +149,13 @@ def compare_netcdf_files(self, gold_file, test_file):

# only compare those that are floats
if gold.variables[var_name].datatype != np.dtype('S1'):
error_msg = "Variable: {0} did not match gold standard".\
error_msg = "Variable: {0} did not match gold standard". \
format(var_name)
self.assertGoldEqual(
self.assert_gold_equal(
gold.variables[var_name][:],
test.variables[var_name][:],
error_msg
)

gold.close()
test.close()

def setUp(self):
self._dist_variables = None

@classmethod
def setUpClass(cls):
"""
Runs the short simulation over reynolds mountain east
"""
base = os.path.dirname(smrf.__file__)
cls.test_dir = os.path.abspath(os.path.join(base, 'tests'))

config_file = 'test_base_config.ini'
config_file = os.path.join(cls.test_dir, config_file)

if not os.path.isfile(config_file):
raise Exception('Configuration file not found for testing')

cls.config_file = config_file

# read in the base configuration
cls.base_config = get_user_config(config_file, modules='smrf')

# create the output dir
folder = os.path.join(cls.base_config.cfg['output']['out_location'])
os.makedirs(folder, exist_ok=True)

@classmethod
def tearDownClass(cls):
"""
Clean up the output directory
"""
folder = os.path.join(cls.base_config.cfg['output']['out_location'])
if os.path.exists(folder):
shutil.rmtree(folder)
40 changes: 6 additions & 34 deletions smrf/tests/smrf_test_case_lakes.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,19 @@
import os
import shutil

from inicheck.tools import get_user_config

import smrf
from smrf.tests.smrf_test_case import SMRFTestCase


class SMRFTestCaseLakes(SMRFTestCase):
"""
The base test case for SMRF that will load in the configuration file
and store as the base config. Also will remove the output directory
upon tear down.
Runs the short simulation over reynolds mountain east
"""

test_dir = SMRFTestCase.test_dir.joinpath('Lakes')
config_file = os.path.join(test_dir, 'config.ini')

@classmethod
def setUpClass(cls):
"""
Runs the short simulation over reynolds mountain east
"""

base = os.path.dirname(smrf.__file__)
cls.test_dir = os.path.join(base, 'tests', 'Lakes')
cls.config_file = os.path.join(cls.test_dir, 'config.ini')

# read in the base configuration
cls.base_config = get_user_config(cls.config_file, modules='smrf')
super().setUpClass()

cls.gold = os.path.abspath(os.path.join(cls.test_dir, 'gold_hrrr'))
cls.gold = cls.test_dir.joinpath('gold_hrrr')
cls.output = os.path.join(cls.test_dir, 'output')

# Remove any potential files to ensure fresh run
if os.path.isdir(cls.output):
shutil.rmtree(cls.output)

# create the output dir
os.makedirs(cls.output, exist_ok=True)

@classmethod
def tearDownClass(cls):
"""
Clean up the output directory
"""
folder = os.path.join(cls.base_config.cfg['output']['out_location'])
if os.path.exists(folder):
shutil.rmtree(folder)
13 changes: 0 additions & 13 deletions smrf/tests/test_configurations.py

This file was deleted.

7 changes: 3 additions & 4 deletions smrf/tests/test_data_gridded.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
from copy import deepcopy
from glob import glob

from inicheck.tools import cast_all_variables
Expand Down Expand Up @@ -35,7 +34,7 @@ def compare_hrrr_gold(self, out_dir):
def test_grid_wrf(self):
""" WRF NetCDF loading """

config = deepcopy(self.base_config)
config = self.base_config_copy()
del config.raw_cfg['csv']

adj_config = {
Expand Down Expand Up @@ -85,7 +84,7 @@ def test_grid_wrf(self):
def test_grid_hrrr_local(self):
""" HRRR grib2 loading with local elevation gradient """

config = deepcopy(self.base_config)
config = self.base_config_copy()
del config.raw_cfg['csv']

adj_config = {
Expand Down Expand Up @@ -150,7 +149,7 @@ def test_grid_hrrr_local(self):
def test_grid_netcdf(self):
""" Generic NetCDF loading """

config = deepcopy(self.base_config)
config = self.base_config_copy()
del config.raw_cfg['csv']

generic_grid = {
Expand Down
Loading

0 comments on commit 91225f0

Please sign in to comment.