diff --git a/test/baseline/test_hazard_map_regression.png b/test/baseline/test_hazard_map_regression.png new file mode 100644 index 0000000..82da75f Binary files /dev/null and b/test/baseline/test_hazard_map_regression.png differ diff --git a/test/baseline/test_succeeds.png b/test/baseline/test_succeeds.png new file mode 100644 index 0000000..2247363 Binary files /dev/null and b/test/baseline/test_succeeds.png differ diff --git a/test/test.py b/test/test.py index 834d5e1..4599d54 100644 --- a/test/test.py +++ b/test/test.py @@ -1,42 +1,61 @@ import os import pytest +import logging + import numpy as np import matplotlib.pyplot as plt -from hazardmaps import hazardmap as hazmap +from hazardmaps import hazardmap as hazmap from hazardmaps.config import volcfile, volcnames, exposure_file, exposure_breakdown_file, floodratio, floodtypes, eearthquake_file +logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO) + @pytest.mark.mpl_image_compare def test_succeeds(): + """ + Very simple test to check that the matplotlib + image compare hook is set up correctly. + """ fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.plot([1,2,3], linestyle='-') return fig -# This is a long-running test as it essential runs the whole script against -# the full Tanzania dataset. -# Should rewrite or skip if nepal data because the logic is slightly different + @pytest.mark.skipif(os.getenv("GITLAB_CI") is not None, reason="cannot run this test on CI server due to data requirement") @pytest.mark.mpl_image_compare def test_hazard_map_regression(): - # Call the main function here to get the image gpd - print("Generating regression test data...this may take a while") + """Regression test to check that running a full hazard map analysis to completion + produces the same map by the way of an image comparison check. + + You must run this test locally. If the test case data is changed, you will need + to regenerate the test images, the "baseline" images. and replace + the ones in the baseline folder in the test directory. + + pytest --mpl-generate-path="test/baseline" test/test.py + + Then run the tests with the --mpl flag + + pytest --mpl test/test.py + """ + logging.info("Generating regression test data...this may take a while") + # The steps below are similar to the ones found in the main() function in the hazardmaps.py file volcano_lahar, volcano_pyro = hazmap.read_volcano_data(volcfile, volcnames) - tz, tz_withgeometry = hazmap.buildings(exposure_file, exposure_breakdown_file) # tz needs a rename... - tz_withgeometry_withflood = hazmap.flood_data(floodratio, floodtypes, tz, tz_withgeometry) # returns tz_withgeometry again! - tz_withgeometry_withflood_withvolcano = hazmap.combine_volcano_buildings(tz_withgeometry_withflood, volcano_lahar, volcano_pyro) # as above - flood_data is the tz_geometry - tz_earthquakes = hazmap.earthquake_data(eearthquake_file) # as above - but this one only generates earthquake data + tz, tz_withgeometry = hazmap.buildings(exposure_file, exposure_breakdown_file) + tz_withgeometry_withflood = hazmap.flood_data(floodratio, floodtypes, tz, tz_withgeometry) + tz_withgeometry_withflood_withvolcano = hazmap.combine_volcano_buildings(tz_withgeometry_withflood, volcano_lahar, volcano_pyro) + tz_earthquakes = hazmap.earthquake_data(eearthquake_file) combined_data = hazmap.hazards_combined(tz_earthquakes, tz_withgeometry_withflood_withvolcano) - np.sum(np.isnan(combined_data.volc)) - print("Printing single hmap plot:") + logging.info("Printing single hmap plot:") + # Now plot the test figures fig = plt.figure() ax = fig.add_subplot(1,1,1) - ax = combined_data.plot(ax=ax, column="hmap", markersize=0.01, legend=True) # Problemm calling from pandas? - plt.savefig("regression_test_hmap_step1.png") + ax = combined_data.plot(ax=ax, column="hmap", markersize=0.01, legend=True) + lims = plt.axis('equal') - plt.savefig("regression_test_hmap_step2.png") - return fig # Test function must return the figure to work wth decorator + # Test function must return the figure to work with the mpl image compare decorator + return fig