Skip to content

Commit

Permalink
Added and refactored some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nina.Hakansson committed Apr 8, 2024
1 parent 5354d27 commit 2d2bb56
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 76 deletions.
2 changes: 1 addition & 1 deletion bin/pps_runner.py
Expand Up @@ -288,7 +288,7 @@ def pps(options):
from logging import handlers

CONFIG_PATH = os.environ.get('PPSRUNNER_CONFIG_DIR', './')
CONFIG_FILE = os.environ.get('PPSRUNNER_CONFIG_FILE', 'pps2018_config.yaml')
CONFIG_FILE = os.environ.get('PPSRUNNER_CONFIG_FILE', 'pps_config.yaml')

OPTIONS = get_config(os.path.join(CONFIG_PATH, CONFIG_FILE), add_defaults=True)

Expand Down
2 changes: 1 addition & 1 deletion nwcsafpps_runner/prepare_nwp.py
Expand Up @@ -215,7 +215,7 @@ def create_nwp_file(file_obj):
file_obj.result_file))
else:
LOG.warning("Missing important fields. No nwp file ({:s}) created".format(
result_file.result_file))
file_obj.result_file))
return None
return file_obj.result_file

Expand Down
171 changes: 97 additions & 74 deletions nwcsafpps_runner/tests/test_nwp_prepare.py
Expand Up @@ -21,13 +21,16 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Unit testing the nwp_prepare runner code."""

"""Test the nwp_prepare runner code."""
import pytest
import unittest
from datetime import datetime
import os
import logging
from datetime import timedelta
from nwcsafpps_runner.message_utils import prepare_nwp_message
import nwcsafpps_runner.prepare_nwp as nwc_prep

LOG = logging.getLogger(__name__)
logging.basicConfig(
format='%(levelname)s |%(asctime)s|: %(message)s',
Expand All @@ -36,91 +39,111 @@
datefmt='%H:%M:%S')


class NWPprepareRunner(unittest.TestCase):
@pytest.fixture
def fake_file_dir(tmp_path):
"""Create directory with test files."""
my_temp_dir = tmp_path / "temp_test_dir"
my_temp_dir.mkdir()
my_temp_dir = my_temp_dir

requirement_name = my_temp_dir / 'pps_nwp_req.txt'
req_file = open(requirement_name, 'w')
req_file.write("M 235 Skin temperature 0 surface\n" +
"O 129 Geopotential 350 isobaricInhPa\n")
req_file.close()
requirement_name_m = my_temp_dir / 'pps_nwp_req_mandatory.txt'
req_file = open(requirement_name_m, 'w')
req_file.write("M 235 Skin temperature 0 surface\n" +
"M 129 Geopotential 350 isobaricInhPa\n")
req_file.close()
static_surface = my_temp_dir / 'static_surface'
req_file = open(static_surface, 'a')
req_file.close()

cfg_file = my_temp_dir / 'pps_config.yaml'
req_file = open(cfg_file, 'w')
req_file.write("pps_nwp_requirements: " + str(requirement_name) + "\n"
"nwp_outdir: " + str(my_temp_dir) + "\n"
"nhsp_path: " + "nwcsafpps_runner/tests/files/" + "\n"
"nhsf_path: " + "nwcsafpps_runner/tests/files/" + "\n"
"nhsp_prefix: " + "LL02_NHSP_" + "\n"
"nhsf_prefix: " + "LL02_NHSF_" + "\n"
"nwp_static_surface: " + str(my_temp_dir) + "/static_surface"+ "\n"
"ecmwf_prefix: " + "LL02_NHSF" + "\n"
"nwp_output_prefix: " + "PPS_ECMWF_" + "\n"
"nhsf_file_name_sift: " + "'" + '{ecmwf_prefix:9s}_{analysis_time:%Y%m%d%H%M}+{forecast_step:d}H00M' + "'" + "\n")

cfg_file = my_temp_dir / 'pps_config_missing_fields.yaml'
req_file = open(cfg_file, 'w')
req_file.write("pps_nwp_requirements: " + str(requirement_name_m) + "\n"
"nwp_outdir: " + str(my_temp_dir) + "\n"
"nhsp_path: " + "nwcsafpps_runner/tests/files/" + "\n"
"nhsf_path: " + "nwcsafpps_runner/tests/files/" + "\n"
"nhsp_prefix: " + "LL02_NHSP_" + "\n"
"nhsf_prefix: " + "LL02_NHSF_" + "\n"
"nwp_static_surface: " + str(my_temp_dir) + "/static_surface"+ "\n"
"ecmwf_prefix: " + "LL02_NHSF" + "\n"
"nwp_output_prefix: " + "PPS_ECMWF_MANDATORY" + "\n"
"nhsf_file_name_sift: " + "'" + '{ecmwf_prefix:9s}_{analysis_time:%Y%m%d%H%M}+{forecast_step:d}H00M' + "'" + "\n")

return str(my_temp_dir)


class TestNwpMessage:
"""Test the nwp message."""

def test_nwp_message(self):
"""Test the nwp message."""
filename = "dummy_dir/PPS_ECMWF_202205100000+009H00M"
publish_msg = prepare_nwp_message(filename, "dummy_topic")
expected_uri = '"uri": "dummy_dir/PPS_ECMWF_202205100000+009H00M"'
assert expected_uri in publish_msg

Check notice on line 100 in nwcsafpps_runner/tests/test_nwp_prepare.py

View check run for this annotation

codefactor.io / CodeFactor

nwcsafpps_runner/tests/test_nwp_prepare.py#L100

multiple spaces after keyword (E271)


class TestNWPprepareRunner:
"""Test the nwp prepare runer."""

def setUp(self):
"""Create config options and file."""
my_tmp_dir = "temp_test_dir"
if os.path.exists(my_tmp_dir):
pass
else:
os.mkdir(my_tmp_dir)
self.requirement_name = my_tmp_dir + '/pps_nwp_req.txt'
req_file = open(self.requirement_name, 'w')
req_file.write("M 235 Skin temperature 0 surface\n" +
"O 129 Geopotential 350 isobaricInhPa\n")
req_file.close()
self.requirement_name_m = my_tmp_dir + '/pps_nwp_req_mandatory.txt'
req_file = open(self.requirement_name_m, 'w')
req_file.write("M 235 Skin temperature 0 surface\n" +
"M 129 Geopotential 350 isobaricInhPa\n")
req_file.close()
self.OPTIONS = {
"pps_nwp_requirements": self.requirement_name,
"nwp_outdir": my_tmp_dir,
"nhsp_path": "nwcsafpps_runner/tests/files/",
"nhsf_path": "nwcsafpps_runner/tests/files/",
"nhsp_prefix": "LL02_NHSP_",
"nhsf_prefix": "LL02_NHSF_",
"nwp_static_surface": my_tmp_dir + '/empty_file',
"ecmwf_prefix": "LL02_NHSF",
"nwp_output_prefix": "PPS_ECMWF_",
"nhsf_file_name_sift": '{ecmwf_prefix:9s}_{analysis_time:%Y%m%d%H%M}+{forecast_step:d}H00M'
}
self.OPTIONS_M = dict(self.OPTIONS)
self.OPTIONS_M["pps_nwp_requirements"] = self.requirement_name_m
self.OPTIONS_M["nwp_output_prefix"] = "PPS_ECMWF_MANDATORY_"
self.outfile = my_tmp_dir + "/PPS_ECMWF_202205100000+009H00M"
fhand = open(self.OPTIONS["nwp_static_surface"], 'a')
fhand.close()

def test_update_nwp_inner(self):
"""Create file."""
import nwcsafpps_runner.prepare_nwp as nwc_prep
def test_update_nwp(self, fake_file_dir):
"""Test create file."""
my_temp_dir = fake_file_dir
outfile = os.path.join(str(my_temp_dir), "PPS_ECMWF_202205100000+009H00M")
cfg_file = my_temp_dir + '/pps_config.yaml'
date = datetime(year=2022, month=5, day=10, hour=0)
nwc_prep.update_nwp_inner(date - timedelta(days=2), [9], self.OPTIONS)
nwc_prep.update_nwp(date - timedelta(days=2), [9], cfg_file)
# Run again when file is already created
nwc_prep.update_nwp_inner(date - timedelta(days=2), [9], self.OPTIONS)
self.assertTrue(os.path.exists(self.outfile))
nwc_prep.update_nwp(date - timedelta(days=2), [9], cfg_file)
assert os.path.exists(outfile)

def test_update_nwp_inner_no_requirement_file(self):
def test_update_nwp_no_requirement_file(self, fake_file_dir):
"""Create file no requirement file."""
os.remove(self.requirement_name)
import nwcsafpps_runner.prepare_nwp as nwc_prep
my_temp_dir = fake_file_dir
cfg_file = my_temp_dir + '/pps_config.yaml'
requirement_name = str(my_temp_dir) + '/pps_nwp_req.txt'
outfile = os.path.join(str(my_temp_dir), "PPS_ECMWF_202205100000+009H00M")
os.remove(requirement_name)
date = datetime(year=2022, month=5, day=10, hour=0)
nwc_prep.update_nwp_inner(date - timedelta(days=2), [9], self.OPTIONS)
self.assertTrue(os.path.exists(self.outfile))
nwc_prep.update_nwp(date - timedelta(days=2), [9], cfg_file)
assert os.path.exists(outfile)

def test_update_nwp_inner_missing_fields(self):
def test_update_nwp_missing_fields(self, fake_file_dir):
"""Test that no file without mandatory data is created."""
import nwcsafpps_runner.prepare_nwp as nwc_prep
my_temp_dir = fake_file_dir
outfile = os.path.join(str(my_temp_dir), "PPS_ECMWF_MANDATORY_202205100000+009H00M")
cfg_file = my_temp_dir + '/pps_config_missing_fields.yaml'
date = datetime(year=2022, month=5, day=10, hour=0)
nwc_prep.update_nwp(date - timedelta(days=2), [9], cfg_file)
date = datetime(year=2022, month=5, day=10, hour=0)
nwc_prep.update_nwp_inner(date - timedelta(days=2), [9], self.OPTIONS)
self.assertFalse(os.path.exists("temp_test_dir/PPS_ECMWF_MANDATORY_202205100000+009H00M"))
os.remove(self.OPTIONS["nwp_static_surface"])
os.remove(self.requirement_name_m)
nwc_prep.update_nwp(date - timedelta(days=2), [9], cfg_file)
assert not (os.path.exists(outfile))

def test_remove_filename(self):
def test_remove_filename(self, fake_file_dir):
"""Test the function for removing files."""
from nwcsafpps_runner.prepare_nwp import remove_file
remove_file(self.OPTIONS["nwp_static_surface"])
self.assertFalse(os.path.exists(self.OPTIONS["nwp_static_surface"]))
my_temp_dir = fake_file_dir
nwp_surface_file = str(my_temp_dir) + '/static_surface'
remove_file(nwp_surface_file)
assert not os.path.exists(nwp_surface_file)
# Should be able to run on already removed file without raising exception
remove_file(self.OPTIONS["nwp_static_surface"])

def tearDown(self):
"""Remove files after testing."""
for temp_file in [self.OPTIONS["nwp_static_surface"], self.requirement_name_m,
self.requirement_name, self.outfile]:
if os.path.exists(temp_file):
os.remove(temp_file)

remove_file(nwp_surface_file)

def suite():
"""Create the test suite for test_nwp_prepare."""
loader = unittest.TestLoader()
mysuite = unittest.TestSuite()
mysuite.addTest(loader.loadTestsFromTestCase(NWPprepareRunner))

return mysuite

0 comments on commit 2d2bb56

Please sign in to comment.