From 63f224eb91a11e7e8b475c68da8f00dc23dc369b Mon Sep 17 00:00:00 2001 From: mlee03 Date: Fri, 28 Mar 2025 10:33:32 -0400 Subject: [PATCH 1/5] create and destroy input.nml --- run_tests.sh | 75 ++++++++++---------- tests/horiz_interp/test_horiz_interp.py | 74 ------------------- tests/py_data_override/test_data_override.py | 14 ++++ tests/py_mpp/test_define_domains.py | 17 +++++ tests/py_mpp/test_getset_domains.py | 17 +++++ tests/test_pyfms.py | 16 +++++ 6 files changed, 102 insertions(+), 111 deletions(-) delete mode 100755 tests/horiz_interp/test_horiz_interp.py diff --git a/run_tests.sh b/run_tests.sh index 5497027..49aea54 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -1,39 +1,40 @@ #!/bin/bash -pytest tests/test_build.py -if [ $? -ne 0 ] ; then - echo "test_build error" ; - exit 1 -fi - -pytest tests/test_pyfms.py -if [ $? -ne 0 ] ; then - echo "test_pyfms error" ; - exit 1 -fi - -mpirun -n 8 python -m pytest tests/py_mpp/test_define_domains.py -if [ $? -ne 0 ] ; then - echo "test_pympp/test_define_domains error" ; - exit 1 -fi - -mpirun -n 4 python -m pytest tests/py_mpp/test_getset_domains.py -if [ $? -ne 0 ] ; then - echo "test_pympp/test_getset_domains error" ; - exit 1 -fi - -pytest tests/horiz_interp -if [ $? -ne 0 ] ; then - echo "test_horiz_interp error" ; - exit 1 -fi - -pytest tests/py_data_override/test_generate_files.py -mpirun -n 6 python -m pytest tests/py_data_override/test_data_override.py -if [ $? -ne 0 ] ; then - echo "test_data_override error" ; - exit 1 -fi -rm -rf INPUT input.nml data_table.yaml *logfile* *warnfile* +function create_input() { + pytest -m "create" $1 + if [ $? -ne 0 ] ; then exit 1 ; fi +} + +function remove_input() { + pytest -m "remove" tests/test_pyfms.py + if [ $? -ne 0 ] ; then exit 1 ; fi +} + +function run_test() { + eval $1 + if [ $? -ne 0 ] ; then exit 1 ; fi } + +run_test "pytest tests/test_build.py" + +test="tests/test_pyfms.py" +create_input $test +run_test "pytest -m parallel $test" +remove_input $test + +test="tests/py_mpp/test_define_domains.py" +create_input $test +run_test "mpirun -n 8 python -m pytest -m 'parallel' $test" +remove_input $test + +test="tests/py_mpp/test_getset_domains.py" +create_input $test +run_test "mpirun -n 4 python -m pytest -m 'parallel' tests/py_mpp/test_getset_domains.py" +remove_input $test + +run_test "pytest tests/py_horiz_interp" + +run_test "pytest tests/py_data_override/test_generate_files.py" +run_test "mpirun -n 6 python -m pytest -m 'parallel' tests/py_data_override/test_data_override.py" +remove_input "tests/py_data_override/test_data_override.py" + +rm -rf INPUT *logfile* *warnfile* diff --git a/tests/horiz_interp/test_horiz_interp.py b/tests/horiz_interp/test_horiz_interp.py deleted file mode 100755 index 20703a5..0000000 --- a/tests/horiz_interp/test_horiz_interp.py +++ /dev/null @@ -1,74 +0,0 @@ -import os - -import numpy as np - -import pyfms - - -cfms_path = os.path.dirname(__file__) + "/../../cFMS/cLIBFMS/lib/libcFMS.so" - - -def test_create_xgrid(): - - cfms = pyfms.pyFMS(cFMS_path=cfms_path).cFMS - create_xgrid = pyfms.HorizInterp(cfms=cfms).create_xgrid_2dx2d_order1 - - refine = 1 - lon_init = 0.0 - lat_init = -np.pi / 4.0 - nlon_src = 10 - nlat_src = 10 - nlon_tgt = nlon_src * refine - nlat_tgt = nlat_src * refine - dlon_src = np.pi / nlon_src - dlat_src = (np.pi / 2.0) * nlat_src - dlon_tgt = dlon_src / refine - dlat_tgt = dlat_src / refine - - lon_src = np.array( - [lon_init + (dlon_src * i) for i in range(nlon_src + 1)] * (nlat_src + 1), - dtype=np.float64, - ) - lat_src = np.array( - [ - lat_init + (dlat_src * i) - for i in range(nlat_src + 1) - for j in range(nlon_src + 1) - ], - dtype=np.float64, - ) - lon_tgt = np.array( - [lon_init + (dlon_tgt * i) for i in range(nlon_tgt + 1)] * (nlat_tgt + 1), - dtype=np.float64, - ) - lat_tgt = np.array( - [ - lat_init + (dlat_tgt * i) - for i in range(nlat_tgt + 1) - for j in range(nlon_tgt + 1) - ], - dtype=np.float64, - ) - mask_src = np.ones((nlon_src + 1) * (nlat_src + 1), dtype=np.float64) - - xgrid = create_xgrid( - nlon_src=nlon_src, - nlat_src=nlat_src, - nlon_tgt=nlon_tgt, - nlat_tgt=nlat_tgt, - lon_src=lon_src, - lat_src=lat_src, - lon_tgt=lon_tgt, - lat_tgt=lat_tgt, - mask_src=mask_src, - ) - - # answer checking - area = pyfms.GridUtils.get_grid_area( - cfms=cfms, nlon=nlon_src, nlat=nlat_src, lon=lon_src, lat=lat_src - ) - - assert xgrid["nxgrid"] == nlon_src * nlat_src - assert np.array_equal(xgrid["i_src"], xgrid["i_tgt"]) - assert np.array_equal(xgrid["j_src"], xgrid["j_tgt"]) - assert np.array_equal(xgrid["xarea"], area) diff --git a/tests/py_data_override/test_data_override.py b/tests/py_data_override/test_data_override.py index 74447c9..a966e9c 100644 --- a/tests/py_data_override/test_data_override.py +++ b/tests/py_data_override/test_data_override.py @@ -1,8 +1,12 @@ +import os + import numpy as np +import pytest from pyfms import pyDataOverride, pyFMS, pyFMS_mpp, pyFMS_mpp_domains +@pytest.mark.parallel def test_data_override(): pyfms = pyFMS() @@ -45,3 +49,13 @@ def test_data_override(): ) pyfms.pyfms_end() assert data == 1.0 + + +@pytest.mark.remove +def test_remove_files(): + os.removedir("INPUT") + os.remove("input.nml") + os.remove("data_table.yaml") + assert not os.path.exists("INPUT") + assert not os.isfile("input.nml") + assert not os.isfile("data_table.yaml") diff --git a/tests/py_mpp/test_define_domains.py b/tests/py_mpp/test_define_domains.py index 010babd..e1f87e7 100644 --- a/tests/py_mpp/test_define_domains.py +++ b/tests/py_mpp/test_define_domains.py @@ -1,8 +1,19 @@ +import os + import numpy as np +import pytest from pyfms import pyDomain, pyFMS, pyFMS_mpp, pyFMS_mpp_domains, pyNestDomain +@pytest.mark.create +def test_create_input_nml(): + inputnml = open("input.nml", "w") + inputnml.close() + os.path.isfile("input.nml") + + +@pytest.mark.parallel def test_define_domains(): NX = 96 @@ -191,5 +202,11 @@ def test_define_domains(): # mpp.pyfms_error(errortype=1) +@pytest.mark.remove +def test_remove_input_nml(): + os.remove("input.nml") + assert not os.path.isfile("input.nml") + + if __name__ == "__main__": test_define_domains() diff --git a/tests/py_mpp/test_getset_domains.py b/tests/py_mpp/test_getset_domains.py index 5d2e057..3954576 100644 --- a/tests/py_mpp/test_getset_domains.py +++ b/tests/py_mpp/test_getset_domains.py @@ -1,8 +1,19 @@ +import os + import numpy as np +import pytest from pyfms import pyDomain, pyFMS, pyFMS_mpp, pyFMS_mpp_domains +@pytest.mark.create +def test_create_input_nml(): + inputnml = open("input.nml", "w") + inputnml.close() + assert os.path.isfile("input.nml") + + +@pytest.mark.parallel def test_getset_domains(): """ global domain @@ -142,5 +153,11 @@ def test_getset_domains(): pyfms.pyfms_end() +@pytest.mark.remove +def test_remove_input_nml(): + os.remove("input.nml") + assert not os.path.isfile("input.nml") + + if __name__ == "__main__": test_getset_domains() diff --git a/tests/test_pyfms.py b/tests/test_pyfms.py index 20ae686..7906c6e 100644 --- a/tests/test_pyfms.py +++ b/tests/test_pyfms.py @@ -1,5 +1,6 @@ import os +import pytest from mpi4py import MPI from pyfms import pyFMS @@ -8,6 +9,15 @@ cfms_path = os.path.dirname(__file__) + "/../cFMS/cLIBFMS/lib/libcFMS.so" +@pytest.mark.create +def test_create_input_nml(): + inputnml = open("input.nml", "w") + inputnml.close() + + assert os.path.isfile("input.nml") + + +@pytest.mark.parallel def test_pyfms_init(): assert os.path.exists(cfms_path) @@ -22,3 +32,9 @@ def test_pyfms_init(): assert isinstance(pyfmsobj, pyFMS) pyfmsobj.pyfms_end() + + +@pytest.mark.remove +def test_remove_input_nml(): + os.remove("input.nml") + assert not os.path.isfile("input.nml") From b7be05df0ec992f96c4e288e0b22f8ca5620aa30 Mon Sep 17 00:00:00 2001 From: mlee03 Date: Fri, 28 Mar 2025 10:33:44 -0400 Subject: [PATCH 2/5] add pytest.ini --- pytest.ini | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 pytest.ini diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..5202f7b --- /dev/null +++ b/pytest.ini @@ -0,0 +1,5 @@ +[pytest] +markers = + create: mark a test as create. + remove: mark test as remove. + parallel: mark test as parallel. \ No newline at end of file From d1e55b07b99771bbc2846acae5705dd5b7854ecd Mon Sep 17 00:00:00 2001 From: mlee03 Date: Fri, 28 Mar 2025 10:44:38 -0400 Subject: [PATCH 3/5] add untracked dir --- tests/py_horiz_interp/test_horiz_interp.py | 80 ++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100755 tests/py_horiz_interp/test_horiz_interp.py diff --git a/tests/py_horiz_interp/test_horiz_interp.py b/tests/py_horiz_interp/test_horiz_interp.py new file mode 100755 index 0000000..6757bd6 --- /dev/null +++ b/tests/py_horiz_interp/test_horiz_interp.py @@ -0,0 +1,80 @@ +import os +import numpy as np +import pytest +import pyfms + +cfms_path = os.path.dirname(__file__) + "/../../cFMS/cLIBFMS/lib/libcFMS.so" + +def test_create_input_nml(): + inputnml = open("input.nml","w") + inputnml.close() + assert os.path.isfile("input.nml") + +def test_create_xgrid(): + + cfms = pyfms.pyFMS(cFMS_path=cfms_path).cFMS + create_xgrid = pyfms.HorizInterp(cfms=cfms).create_xgrid_2dx2d_order1 + + refine = 1 + lon_init = 0.0 + lat_init = -np.pi / 4.0 + nlon_src = 10 + nlat_src = 10 + nlon_tgt = nlon_src * refine + nlat_tgt = nlat_src * refine + dlon_src = np.pi / nlon_src + dlat_src = (np.pi / 2.0) * nlat_src + dlon_tgt = dlon_src / refine + dlat_tgt = dlat_src / refine + + lon_src = np.array( + [lon_init + (dlon_src * i) for i in range(nlon_src + 1)] * (nlat_src + 1), + dtype=np.float64, + ) + lat_src = np.array( + [ + lat_init + (dlat_src * i) + for i in range(nlat_src + 1) + for j in range(nlon_src + 1) + ], + dtype=np.float64, + ) + lon_tgt = np.array( + [lon_init + (dlon_tgt * i) for i in range(nlon_tgt + 1)] * (nlat_tgt + 1), + dtype=np.float64, + ) + lat_tgt = np.array( + [ + lat_init + (dlat_tgt * i) + for i in range(nlat_tgt + 1) + for j in range(nlon_tgt + 1) + ], + dtype=np.float64, + ) + mask_src = np.ones((nlon_src + 1) * (nlat_src + 1), dtype=np.float64) + + xgrid = create_xgrid( + nlon_src=nlon_src, + nlat_src=nlat_src, + nlon_tgt=nlon_tgt, + nlat_tgt=nlat_tgt, + lon_src=lon_src, + lat_src=lat_src, + lon_tgt=lon_tgt, + lat_tgt=lat_tgt, + mask_src=mask_src, + ) + + # answer checking + area = pyfms.GridUtils.get_grid_area( + cfms=cfms, nlon=nlon_src, nlat=nlat_src, lon=lon_src, lat=lat_src + ) + + assert xgrid["nxgrid"] == nlon_src * nlat_src + assert np.array_equal(xgrid["i_src"], xgrid["i_tgt"]) + assert np.array_equal(xgrid["j_src"], xgrid["j_tgt"]) + assert np.array_equal(xgrid["xarea"], area) + +def test_remove_input_nml(): + os.remove("input.nml") + assert not os.path.isfile("input.nml") From ecce4107c28b62b75b0093136c88b7917f95e8b2 Mon Sep 17 00:00:00 2001 From: mlee03 Date: Fri, 28 Mar 2025 10:52:18 -0400 Subject: [PATCH 4/5] fix error in script --- run_tests.sh | 2 +- tests/py_data_override/test_data_override.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/run_tests.sh b/run_tests.sh index 49aea54..53aa06e 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -6,7 +6,7 @@ function create_input() { } function remove_input() { - pytest -m "remove" tests/test_pyfms.py + pytest -m "remove" $1 if [ $? -ne 0 ] ; then exit 1 ; fi } diff --git a/tests/py_data_override/test_data_override.py b/tests/py_data_override/test_data_override.py index a966e9c..acf10fe 100644 --- a/tests/py_data_override/test_data_override.py +++ b/tests/py_data_override/test_data_override.py @@ -1,7 +1,7 @@ import os - import numpy as np import pytest +import shutil from pyfms import pyDataOverride, pyFMS, pyFMS_mpp, pyFMS_mpp_domains @@ -53,9 +53,9 @@ def test_data_override(): @pytest.mark.remove def test_remove_files(): - os.removedir("INPUT") + shutil.rmtree("INPUT") os.remove("input.nml") os.remove("data_table.yaml") assert not os.path.exists("INPUT") - assert not os.isfile("input.nml") - assert not os.isfile("data_table.yaml") + assert not os.path.isfile("input.nml") + assert not os.path.isfile("data_table.yaml") From 0e768942e1a91ec158451f610a2aeaf5adfa9ea2 Mon Sep 17 00:00:00 2001 From: mlee03 Date: Fri, 28 Mar 2025 10:52:59 -0400 Subject: [PATCH 5/5] fix test script --- pytest.ini | 2 +- tests/py_data_override/test_data_override.py | 3 ++- tests/py_horiz_interp/test_horiz_interp.py | 9 +++++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pytest.ini b/pytest.ini index 5202f7b..551ab7d 100644 --- a/pytest.ini +++ b/pytest.ini @@ -2,4 +2,4 @@ markers = create: mark a test as create. remove: mark test as remove. - parallel: mark test as parallel. \ No newline at end of file + parallel: mark test as parallel. diff --git a/tests/py_data_override/test_data_override.py b/tests/py_data_override/test_data_override.py index acf10fe..6b7c3ef 100644 --- a/tests/py_data_override/test_data_override.py +++ b/tests/py_data_override/test_data_override.py @@ -1,7 +1,8 @@ import os +import shutil + import numpy as np import pytest -import shutil from pyfms import pyDataOverride, pyFMS, pyFMS_mpp, pyFMS_mpp_domains diff --git a/tests/py_horiz_interp/test_horiz_interp.py b/tests/py_horiz_interp/test_horiz_interp.py index 6757bd6..67cbecf 100755 --- a/tests/py_horiz_interp/test_horiz_interp.py +++ b/tests/py_horiz_interp/test_horiz_interp.py @@ -1,15 +1,19 @@ import os + import numpy as np -import pytest + import pyfms + cfms_path = os.path.dirname(__file__) + "/../../cFMS/cLIBFMS/lib/libcFMS.so" + def test_create_input_nml(): - inputnml = open("input.nml","w") + inputnml = open("input.nml", "w") inputnml.close() assert os.path.isfile("input.nml") + def test_create_xgrid(): cfms = pyfms.pyFMS(cFMS_path=cfms_path).cFMS @@ -75,6 +79,7 @@ def test_create_xgrid(): assert np.array_equal(xgrid["j_src"], xgrid["j_tgt"]) assert np.array_equal(xgrid["xarea"], area) + def test_remove_input_nml(): os.remove("input.nml") assert not os.path.isfile("input.nml")