Skip to content

Commit

Permalink
Fix spelling and do less patching in unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Adam.Dybbroe <a000680@c21856.ad.smhi.se>
  • Loading branch information
Adam.Dybbroe committed Oct 24, 2022
1 parent 582d012 commit 970e8f6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 35 deletions.
20 changes: 10 additions & 10 deletions activefires_pp/post_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def _apply_timezone_offset(self, obstime):
len(self._afdata)).astype(np.datetime64)

def fires_filtering(self, shapefile, start_geometries_index=1, inside=True):
"""Remove fires outside National boarders or filter out potential false detections.
"""Remove fires outside National borders or filter out potential false detections.
If *inside* is True the filtering will keep those detections that are inside the polygon.
If *inside* is False the filtering will disregard the detections that are inside the polygon.
Expand Down Expand Up @@ -339,10 +339,10 @@ def get_global_mask_from_shapefile(shapefile, lonlats, start_geom_index=0):
class ActiveFiresPostprocessing(Thread):
"""The active fires post processor."""

def __init__(self, configfile, shp_boarders, shp_mask, regional_filtermask=None):
def __init__(self, configfile, shp_borders, shp_mask, regional_filtermask=None):
"""Initialize the active fires post processor class."""
super().__init__()
self.shp_boarders = shp_boarders
self.shp_borders = shp_borders
self.shp_filtermask = shp_mask

self.regional_filtermask = regional_filtermask
Expand Down Expand Up @@ -377,7 +377,7 @@ def _setup_and_start_communication(self):
now = datetime_utc2local(datetime.now(), self.timezone)
logger.debug("Output times for timezone: {zone} Now = {time}".format(zone=str(self.timezone), time=now))

self._check_boarders_shapes_exists()
self._check_borders_shapes_exists()

self.listener = ListenerContainer(topics=[self.input_topic])
self.publisher = NoisyPublisher("active_fires_postprocessing")
Expand Down Expand Up @@ -527,7 +527,7 @@ def fires_filtering(self, msg, af_shapeff):
logger.debug("Output file path = %s", out_filepath)

# National filtering:
af_shapeff.fires_filtering(self.shp_boarders)
af_shapeff.fires_filtering(self.shp_borders)

# Metadata should be transfered here!
afdata_ff = af_shapeff.get_af_data()
Expand All @@ -551,7 +551,7 @@ def get_output_messages(self, filepath, msg, number_of_data):
else:
logger.info("No geojson file created, number of fires after filtering = %d", number_of_data)
return self._generate_no_fires_messages(msg,
'No true fire detections inside National boarders')
'No true fire detections inside National borders')

def _generate_output_message(self, filepath, input_msg, region=None):
"""Create the output message to publish."""
Expand All @@ -576,10 +576,10 @@ def _generate_no_fires_messages(self, input_msg, msg_string):

return publish_messages

def _check_boarders_shapes_exists(self):
"""Check that the national boarders shapefile exists on disk."""
if not os.path.exists(self.shp_boarders):
raise OSError("Shape file does not exist! Filename = %s" % self.shp_boarders)
def _check_borders_shapes_exists(self):
"""Check that the national borders shapefile exists on disk."""
if not os.path.exists(self.shp_borders):
raise OSError("Shape file does not exist! Filename = %s" % self.shp_borders)

def close(self):
"""Shutdown the Active Fires postprocessing."""
Expand Down
32 changes: 29 additions & 3 deletions activefires_pp/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@
restapi_url: "https://xxx.smhi.se:xxxx"
"""

TEST_POST_PROCESSING_YAML_CONFIG_CONTENT = """# Publish/subscribe
publish_topic: /VIIRS/L2/Fires/PP
subscribe_topics: VIIRS/L2/AFI
af_pattern_ibands: AFIMG_{platform:s}_d{start_time:%Y%m%d_t%H%M%S%f}_e{end_hour:%H%M%S%f}_b{orbit:s}_c{processing_time:%Y%m%d%H%M%S%f}_cspp_dev.txt
geojson_file_pattern_national: AFIMG_{platform:s}_d{start_time:%Y%m%d_t%H%M%S}.geojson
geojson_file_pattern_regional: AFIMG_{platform:s}_d{start_time:%Y%m%d_t%H%M%S}_{region_name:s}.geojson
regional_shapefiles_format: omr_{region_code:s}_Buffer.{ext:s}
output_dir: /path/where/the/filtered/results/will/be/stored
timezone: Europe/Stockholm
""" # noqa

TEST_YAML_TOKENS = """xauth_tokens:
x-auth-satellite-alarm : 'my-token'
"""
Expand Down Expand Up @@ -133,6 +149,16 @@ def fake_yamlconfig_file(tmp_path):
yield file_path


@pytest.fixture
def fake_yamlconfig_file_post_processing(tmp_path):
"""Write fake yaml config file."""
file_path = tmp_path / 'test_af_post_processing_config.yaml'
with open(file_path, 'w') as fpt:
fpt.write(TEST_POST_PROCESSING_YAML_CONFIG_CONTENT)

yield file_path


@pytest.fixture
def fake_geojson_file_many_detections(tmp_path):
"""Write fake geojson file with many close detections."""
Expand Down Expand Up @@ -174,9 +200,9 @@ def fake_past_detections_dir(tmp_path):


@pytest.fixture
def fake_national_boarders_shapefile(tmp_path):
"""Write fake national boarders shape file."""
file_path = tmp_path / 'some_national_boarders_shape.yaml'
def fake_national_borders_shapefile(tmp_path):
"""Write fake national borders shape file."""
file_path = tmp_path / 'some_national_borders_shape.yaml'
with open(file_path, 'w') as fpt:
fpt.write('')

Expand Down
43 changes: 21 additions & 22 deletions activefires_pp/tests/test_fires_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ def test_regional_fires_filtering(setup_comm, get_config, gethostname):
gethostname.return_value = "my.host.name"

myconfigfile = "/my/config/file/path"
myboarders_file = "/my/shape/file/with/country/boarders"
myborders_file = "/my/shape/file/with/country/borders"
mymask_file = "/my/shape/file/with/polygons/to/filter/out"

afpp = ActiveFiresPostprocessing(myconfigfile, myboarders_file, mymask_file)
afpp = ActiveFiresPostprocessing(myconfigfile, myborders_file, mymask_file)

fstream = io.StringIO(TEST_ACTIVE_FIRES_FILE_DATA)
afdata = pd.read_csv(fstream, index_col=None, header=None, comment='#', names=COL_NAMES)
Expand Down Expand Up @@ -251,10 +251,10 @@ def test_general_national_fires_filtering(get_global_mask, setup_comm, get_confi
gethostname.return_value = "my.host.name"

myconfigfile = "/my/config/file/path"
myboarders_file = "/my/shape/file/with/country/boarders"
myborders_file = "/my/shape/file/with/country/borders"
mymask_file = "/my/shape/file/with/polygons/to/filter/out"

afpp = ActiveFiresPostprocessing(myconfigfile, myboarders_file, mymask_file)
afpp = ActiveFiresPostprocessing(myconfigfile, myborders_file, mymask_file)

fstream = io.StringIO(TEST_ACTIVE_FIRES_FILE_DATA)
afdata = pd.read_csv(fstream, index_col=None, header=None, comment='#', names=COL_NAMES)
Expand Down Expand Up @@ -290,41 +290,40 @@ def test_general_national_fires_filtering(get_global_mask, setup_comm, get_confi
assert outmsg == ["my fake output message"]


@pytest.mark.usefixtures("fake_national_boarders_shapefile")
@pytest.mark.usefixtures("fake_national_borders_shapefile")
@pytest.mark.usefixtures("fake_yamlconfig_file_post_processing")
@patch('socket.gethostname')
@patch('activefires_pp.post_processing.read_config')
@patch('activefires_pp.post_processing.ActiveFiresPostprocessing._setup_and_start_communication')
def test_checking_national_boarders_shapefile_file_exists(setup_comm, get_config,
gethostname, fake_national_boarders_shapefile):
"""Test the checking of the national boarders shapefile - boarders shapefile exists."""
get_config.return_value = CONFIG_EXAMPLE
def test_checking_national_borders_shapefile_file_exists(setup_comm, gethostname,
fake_yamlconfig_file_post_processing,
fake_national_borders_shapefile):
"""Test the checking of the national borders shapefile - borders shapefile exists."""
gethostname.return_value = "my.host.name"

myconfigfile = "/my/config/file/path"
mymask_file = "/my/shape/file/with/polygons/to/filter/out"

afpp = ActiveFiresPostprocessing(myconfigfile, fake_national_boarders_shapefile, mymask_file)
afpp._check_boarders_shapes_exists()
afpp = ActiveFiresPostprocessing(str(fake_yamlconfig_file_post_processing),
fake_national_borders_shapefile, mymask_file)
afpp._check_borders_shapes_exists()

assert afpp.shp_boarders.name == 'some_national_boarders_shape.yaml'
assert afpp.shp_boarders.is_file()
assert afpp.shp_borders.name == 'some_national_borders_shape.yaml'
assert afpp.shp_borders.is_file()


@patch('socket.gethostname')
@patch('activefires_pp.post_processing.read_config')
@patch('activefires_pp.post_processing.ActiveFiresPostprocessing._setup_and_start_communication')
def test_checking_national_boarders_shapefile_file_nonexisting(setup_comm, get_config, gethostname):
"""Test the checking of the national boarders shapefile - boarders shapefile does not exist."""
def test_checking_national_borders_shapefile_file_nonexisting(setup_comm, get_config, gethostname):
"""Test the checking of the national borders shapefile - borders shapefile does not exist."""
get_config.return_value = CONFIG_EXAMPLE
gethostname.return_value = "my.host.name"

myconfigfile = "/my/config/file/path"
myboarders_file = "/my/shape/file/with/country/boarders"
myborders_file = "/my/shape/file/with/country/borders"
mymask_file = "/my/shape/file/with/polygons/to/filter/out"

afpp = ActiveFiresPostprocessing(myconfigfile, myboarders_file, mymask_file)
afpp = ActiveFiresPostprocessing(myconfigfile, myborders_file, mymask_file)
with pytest.raises(OSError) as exec_info:
afpp._check_boarders_shapes_exists()
afpp._check_borders_shapes_exists()

expected = "Shape file does not exist! Filename = /my/shape/file/with/country/boarders"
expected = "Shape file does not exist! Filename = /my/shape/file/with/country/borders"
assert str(exec_info.value) == expected

0 comments on commit 970e8f6

Please sign in to comment.