Skip to content

Commit b31135d

Browse files
authored
Merge pull request #179 from bobleesj/pytest-load
Use pytest for `test_loaddata.py`
2 parents 9789eac + 4b05518 commit b31135d

File tree

3 files changed

+100
-63
lines changed

3 files changed

+100
-63
lines changed

news/test-refactor.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* <news item>
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* Unittest to Pytest migration for test_loaddata.py
20+
21+
**Security:**
22+
23+
* <news item>

src/diffpy/utils/parsers/loaddata.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#
1414
##############################################################################
1515

16+
import os
17+
1618
import numpy
1719

1820

@@ -99,6 +101,10 @@ def countcolumnsvalues(line):
99101
nc = nv = 0
100102
return nc, nv
101103

104+
# Check if file exists before trying to open
105+
if not os.path.exists(filename):
106+
raise IOError(f"File {filename} cannot be found. Please rerun the program specifying a valid filename.")
107+
102108
# make sure fid gets cleaned up
103109
with open(filename, "rb") as fid:
104110
# search for the start of datablock

tests/diffpy/utils/parsers/test_loaddata.py

Lines changed: 71 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,79 @@
33
"""Unit tests for diffpy.utils.parsers.loaddata
44
"""
55

6-
import unittest
7-
8-
import numpy
6+
import numpy as np
97
import pytest
108

119
from diffpy.utils.parsers.loaddata import loadData
1210

1311

14-
##############################################################################
15-
class TestLoadData(unittest.TestCase):
16-
@pytest.fixture(autouse=True)
17-
def prepare_fixture(self, datafile):
18-
self.datafile = datafile
19-
20-
def test_loadData_default(self):
21-
"""check loadData() with default options"""
22-
loaddata01 = self.datafile("loaddata01.txt")
23-
d2c = numpy.array([[3, 31], [4, 32], [5, 33]])
24-
self.assertRaises(IOError, loadData, "doesnotexist")
25-
# the default minrows=10 makes it read from the third line
26-
d = loadData(loaddata01)
27-
self.assertTrue(numpy.array_equal(d2c, d))
28-
# the usecols=(0, 1) would make it read from the third line
29-
d = loadData(loaddata01, minrows=1, usecols=(0, 1))
30-
self.assertTrue(numpy.array_equal(d2c, d))
31-
# check the effect of usecols effect
32-
d = loadData(loaddata01, usecols=(0,))
33-
self.assertTrue(numpy.array_equal(d2c[:, 0], d))
34-
d = loadData(loaddata01, usecols=(1,))
35-
self.assertTrue(numpy.array_equal(d2c[:, 1], d))
36-
return
37-
38-
def test_loadData_1column(self):
39-
"""check loading of one-column data."""
40-
loaddata01 = self.datafile("loaddata01.txt")
41-
d1c = numpy.arange(1, 6)
42-
d = loadData(loaddata01, usecols=[0], minrows=1)
43-
self.assertTrue(numpy.array_equal(d1c, d))
44-
d = loadData(loaddata01, usecols=[0], minrows=2)
45-
self.assertTrue(numpy.array_equal(d1c, d))
46-
d = loadData(loaddata01, usecols=[0], minrows=3)
47-
self.assertFalse(numpy.array_equal(d1c, d))
48-
return
49-
50-
def test_loadData_headers(self):
51-
"""check loadData() with headers options enabled"""
52-
loaddatawithheaders = self.datafile("loaddatawithheaders.txt")
53-
hignore = ["# ", "// ", "["] # ignore lines beginning with these strings
54-
delimiter = ": " # what our data should be separated by
55-
hdata = loadData(loaddatawithheaders, headers=True, hdel=delimiter, hignore=hignore)
56-
# only fourteen lines of data are formatted properly
57-
assert len(hdata) == 14
58-
# check the following are floats
59-
vfloats = ["wavelength", "qmaxinst", "qmin", "qmax", "bgscale"]
60-
for name in vfloats:
61-
assert isinstance(hdata.get(name), float)
62-
# check the following are NOT floats
63-
vnfloats = ["composition", "rmax", "rmin", "rstep", "rpoly"]
64-
for name in vnfloats:
65-
assert not isinstance(hdata.get(name), float)
66-
67-
68-
# End of class TestRoutines
69-
70-
if __name__ == "__main__":
71-
unittest.main()
72-
73-
# End of file
12+
def test_loadData_default(datafile):
13+
"""check loadData() with default options"""
14+
loaddata01 = datafile("loaddata01.txt")
15+
d2c = np.array([[3, 31], [4, 32], [5, 33]])
16+
17+
with pytest.raises(IOError) as err:
18+
loadData("doesnotexist.txt")
19+
assert (
20+
str(err.value)
21+
== "File doesnotexist.txt cannot be found. Please rerun the program specifying a valid filename."
22+
)
23+
24+
# The default minrows=10 makes it read from the third line
25+
d = loadData(loaddata01)
26+
assert np.array_equal(d2c, d)
27+
28+
# The usecols=(0, 1) would make it read from the third line
29+
d = loadData(loaddata01, minrows=1, usecols=(0, 1))
30+
assert np.array_equal(d2c, d)
31+
32+
# Check the effect of usecols effect
33+
d = loadData(loaddata01, usecols=(0,))
34+
assert np.array_equal(d2c[:, 0], d)
35+
36+
d = loadData(loaddata01, usecols=(1,))
37+
assert np.array_equal(d2c[:, 1], d)
38+
39+
40+
def test_loadData_1column(datafile):
41+
"""check loading of one-column data."""
42+
loaddata01 = datafile("loaddata01.txt")
43+
d1c = np.arange(1, 6)
44+
45+
# Assertions using pytest's assert
46+
d = loadData(loaddata01, usecols=[0], minrows=1)
47+
assert np.array_equal(d1c, d)
48+
49+
d = loadData(loaddata01, usecols=[0], minrows=2)
50+
assert np.array_equal(d1c, d)
51+
52+
d = loadData(loaddata01, usecols=[0], minrows=3)
53+
assert not np.array_equal(d1c, d)
54+
55+
56+
def test_loadData_headers(datafile):
57+
"""check loadData() with headers options enabled"""
58+
expected = {
59+
"wavelength": 0.1,
60+
"dataformat": "Qnm",
61+
"inputfile": "darkSub_rh20_C_01.chi",
62+
"mode": "xray",
63+
"bgscale": 1.2998929285,
64+
"composition": "0.800.20",
65+
"outputtype": "gr",
66+
"qmaxinst": 25.0,
67+
"qmin": 0.1,
68+
"qmax": 25.0,
69+
"rmax": "100.0r",
70+
"rmin": "0.0r",
71+
"rstep": "0.01r",
72+
"rpoly": "0.9r",
73+
}
74+
75+
loaddatawithheaders = datafile("loaddatawithheaders.txt")
76+
hignore = ["# ", "// ", "["] # ignore lines beginning with these strings
77+
delimiter = ": " # what our data should be separated by
78+
79+
# Load data with headers
80+
hdata = loadData(loaddatawithheaders, headers=True, hdel=delimiter, hignore=hignore)
81+
assert hdata == expected

0 commit comments

Comments
 (0)