From 1c4d1fc9ef4d2b9d2ab0d2372aafdf2b3947cd17 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Fri, 15 Nov 2024 22:37:23 -0500 Subject: [PATCH 1/4] Use pytest for test_loaddata --- tests/diffpy/utils/parsers/test_loaddata.py | 128 ++++++++++---------- 1 file changed, 65 insertions(+), 63 deletions(-) diff --git a/tests/diffpy/utils/parsers/test_loaddata.py b/tests/diffpy/utils/parsers/test_loaddata.py index 3d775c74..45874fab 100644 --- a/tests/diffpy/utils/parsers/test_loaddata.py +++ b/tests/diffpy/utils/parsers/test_loaddata.py @@ -3,71 +3,73 @@ """Unit tests for diffpy.utils.parsers.loaddata """ -import unittest - -import numpy +import numpy as np import pytest from diffpy.utils.parsers.loaddata import loadData -############################################################################## -class TestLoadData(unittest.TestCase): - @pytest.fixture(autouse=True) - def prepare_fixture(self, datafile): - self.datafile = datafile - - def test_loadData_default(self): - """check loadData() with default options""" - loaddata01 = self.datafile("loaddata01.txt") - d2c = numpy.array([[3, 31], [4, 32], [5, 33]]) - self.assertRaises(IOError, loadData, "doesnotexist") - # the default minrows=10 makes it read from the third line - d = loadData(loaddata01) - self.assertTrue(numpy.array_equal(d2c, d)) - # the usecols=(0, 1) would make it read from the third line - d = loadData(loaddata01, minrows=1, usecols=(0, 1)) - self.assertTrue(numpy.array_equal(d2c, d)) - # check the effect of usecols effect - d = loadData(loaddata01, usecols=(0,)) - self.assertTrue(numpy.array_equal(d2c[:, 0], d)) - d = loadData(loaddata01, usecols=(1,)) - self.assertTrue(numpy.array_equal(d2c[:, 1], d)) - return - - def test_loadData_1column(self): - """check loading of one-column data.""" - loaddata01 = self.datafile("loaddata01.txt") - d1c = numpy.arange(1, 6) - d = loadData(loaddata01, usecols=[0], minrows=1) - self.assertTrue(numpy.array_equal(d1c, d)) - d = loadData(loaddata01, usecols=[0], minrows=2) - self.assertTrue(numpy.array_equal(d1c, d)) - d = loadData(loaddata01, usecols=[0], minrows=3) - self.assertFalse(numpy.array_equal(d1c, d)) - return - - def test_loadData_headers(self): - """check loadData() with headers options enabled""" - loaddatawithheaders = self.datafile("loaddatawithheaders.txt") - hignore = ["# ", "// ", "["] # ignore lines beginning with these strings - delimiter = ": " # what our data should be separated by - hdata = loadData(loaddatawithheaders, headers=True, hdel=delimiter, hignore=hignore) - # only fourteen lines of data are formatted properly - assert len(hdata) == 14 - # check the following are floats - vfloats = ["wavelength", "qmaxinst", "qmin", "qmax", "bgscale"] - for name in vfloats: - assert isinstance(hdata.get(name), float) - # check the following are NOT floats - vnfloats = ["composition", "rmax", "rmin", "rstep", "rpoly"] - for name in vnfloats: - assert not isinstance(hdata.get(name), float) - - -# End of class TestRoutines - -if __name__ == "__main__": - unittest.main() - -# End of file +def test_loadData_default(datafile): + """check loadData() with default options""" + loaddata01 = datafile("loaddata01.txt") + d2c = np.array([[3, 31], [4, 32], [5, 33]]) + + with pytest.raises(IOError): + loadData("doesnotexist") + + # The default minrows=10 makes it read from the third line + d = loadData(loaddata01) + assert np.array_equal(d2c, d) + + # The usecols=(0, 1) would make it read from the third line + d = loadData(loaddata01, minrows=1, usecols=(0, 1)) + assert np.array_equal(d2c, d) + + # Check the effect of usecols effect + d = loadData(loaddata01, usecols=(0,)) + assert np.array_equal(d2c[:, 0], d) + + d = loadData(loaddata01, usecols=(1,)) + assert np.array_equal(d2c[:, 1], d) + + +def test_loadData_1column(datafile): + """check loading of one-column data.""" + loaddata01 = datafile("loaddata01.txt") + d1c = np.arange(1, 6) + + # Assertions using pytest's assert + d = loadData(loaddata01, usecols=[0], minrows=1) + assert np.array_equal(d1c, d) + + d = loadData(loaddata01, usecols=[0], minrows=2) + assert np.array_equal(d1c, d) + + d = loadData(loaddata01, usecols=[0], minrows=3) + assert not np.array_equal(d1c, d) + + return + + +def test_loadData_headers(datafile): + """check loadData() with headers options enabled""" + loaddatawithheaders = datafile("loaddatawithheaders.txt") + hignore = ["# ", "// ", "["] # ignore lines beginning with these strings + delimiter = ": " # what our data should be separated by + + # Load data with headers + hdata = loadData(loaddatawithheaders, headers=True, hdel=delimiter, hignore=hignore) + + # Assertions using pytest + # Only fourteen lines of data are formatted properly + assert len(hdata) == 14 + + # Check the following are floats + vfloats = ["wavelength", "qmaxinst", "qmin", "qmax", "bgscale"] + for name in vfloats: + assert isinstance(hdata.get(name), float) + + # Check the following are NOT floats + vnfloats = ["composition", "rmax", "rmin", "rstep", "rpoly"] + for name in vnfloats: + assert not isinstance(hdata.get(name), float) From bf88396cf1a9f703c066dccafb1026003f886d2d Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Fri, 15 Nov 2024 22:40:13 -0500 Subject: [PATCH 2/4] Add news --- news/test-refactor.rst | 23 +++++++++++++++++++++ tests/diffpy/utils/parsers/test_loaddata.py | 3 --- 2 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 news/test-refactor.rst diff --git a/news/test-refactor.rst b/news/test-refactor.rst new file mode 100644 index 00000000..095df231 --- /dev/null +++ b/news/test-refactor.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* Unittest to Pytest migration for test_loaddata.py + +**Security:** + +* diff --git a/tests/diffpy/utils/parsers/test_loaddata.py b/tests/diffpy/utils/parsers/test_loaddata.py index 45874fab..8ee80bf5 100644 --- a/tests/diffpy/utils/parsers/test_loaddata.py +++ b/tests/diffpy/utils/parsers/test_loaddata.py @@ -48,8 +48,6 @@ def test_loadData_1column(datafile): d = loadData(loaddata01, usecols=[0], minrows=3) assert not np.array_equal(d1c, d) - return - def test_loadData_headers(datafile): """check loadData() with headers options enabled""" @@ -60,7 +58,6 @@ def test_loadData_headers(datafile): # Load data with headers hdata = loadData(loaddatawithheaders, headers=True, hdel=delimiter, hignore=hignore) - # Assertions using pytest # Only fourteen lines of data are formatted properly assert len(hdata) == 14 From 0f4c5400869c4daf3aad6e26fff57300ec69b427 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Sat, 16 Nov 2024 23:11:00 -0500 Subject: [PATCH 3/4] Use actual data to test headers and test error IO msg --- tests/diffpy/utils/parsers/test_loaddata.py | 34 ++++++++++++--------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/tests/diffpy/utils/parsers/test_loaddata.py b/tests/diffpy/utils/parsers/test_loaddata.py index 8ee80bf5..a747be8a 100644 --- a/tests/diffpy/utils/parsers/test_loaddata.py +++ b/tests/diffpy/utils/parsers/test_loaddata.py @@ -14,8 +14,9 @@ def test_loadData_default(datafile): loaddata01 = datafile("loaddata01.txt") d2c = np.array([[3, 31], [4, 32], [5, 33]]) - with pytest.raises(IOError): + with pytest.raises(IOError) as err: loadData("doesnotexist") + assert "No such file or directory" in str(err.value) # The default minrows=10 makes it read from the third line d = loadData(loaddata01) @@ -51,22 +52,27 @@ def test_loadData_1column(datafile): def test_loadData_headers(datafile): """check loadData() with headers options enabled""" + expected = { + "wavelength": 0.1, + "dataformat": "Qnm", + "inputfile": "darkSub_rh20_C_01.chi", + "mode": "xray", + "bgscale": 1.2998929285, + "composition": "0.800.20", + "outputtype": "gr", + "qmaxinst": 25.0, + "qmin": 0.1, + "qmax": 25.0, + "rmax": "100.0r", + "rmin": "0.0r", + "rstep": "0.01r", + "rpoly": "0.9r", + } + loaddatawithheaders = datafile("loaddatawithheaders.txt") hignore = ["# ", "// ", "["] # ignore lines beginning with these strings delimiter = ": " # what our data should be separated by # Load data with headers hdata = loadData(loaddatawithheaders, headers=True, hdel=delimiter, hignore=hignore) - - # Only fourteen lines of data are formatted properly - assert len(hdata) == 14 - - # Check the following are floats - vfloats = ["wavelength", "qmaxinst", "qmin", "qmax", "bgscale"] - for name in vfloats: - assert isinstance(hdata.get(name), float) - - # Check the following are NOT floats - vnfloats = ["composition", "rmax", "rmin", "rstep", "rpoly"] - for name in vnfloats: - assert not isinstance(hdata.get(name), float) + assert hdata == expected From 4b055189e8927793943ad10266086208ae37cafd Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Sun, 17 Nov 2024 11:37:31 -0500 Subject: [PATCH 4/4] Add custom IOError msg for file not found in loaddata --- src/diffpy/utils/parsers/loaddata.py | 6 ++++++ tests/diffpy/utils/parsers/test_loaddata.py | 7 +++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/diffpy/utils/parsers/loaddata.py b/src/diffpy/utils/parsers/loaddata.py index 18375d90..870e6016 100644 --- a/src/diffpy/utils/parsers/loaddata.py +++ b/src/diffpy/utils/parsers/loaddata.py @@ -13,6 +13,8 @@ # ############################################################################## +import os + import numpy @@ -99,6 +101,10 @@ def countcolumnsvalues(line): nc = nv = 0 return nc, nv + # Check if file exists before trying to open + if not os.path.exists(filename): + raise IOError(f"File {filename} cannot be found. Please rerun the program specifying a valid filename.") + # make sure fid gets cleaned up with open(filename, "rb") as fid: # search for the start of datablock diff --git a/tests/diffpy/utils/parsers/test_loaddata.py b/tests/diffpy/utils/parsers/test_loaddata.py index a747be8a..a7e273e7 100644 --- a/tests/diffpy/utils/parsers/test_loaddata.py +++ b/tests/diffpy/utils/parsers/test_loaddata.py @@ -15,8 +15,11 @@ def test_loadData_default(datafile): d2c = np.array([[3, 31], [4, 32], [5, 33]]) with pytest.raises(IOError) as err: - loadData("doesnotexist") - assert "No such file or directory" in str(err.value) + loadData("doesnotexist.txt") + assert ( + str(err.value) + == "File doesnotexist.txt cannot be found. Please rerun the program specifying a valid filename." + ) # The default minrows=10 makes it read from the third line d = loadData(loaddata01)