Skip to content

Commit 1c4d1fc

Browse files
committed
Use pytest for test_loaddata
1 parent 9789eac commit 1c4d1fc

File tree

1 file changed

+65
-63
lines changed

1 file changed

+65
-63
lines changed

tests/diffpy/utils/parsers/test_loaddata.py

Lines changed: 65 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,73 @@
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):
18+
loadData("doesnotexist")
19+
20+
# The default minrows=10 makes it read from the third line
21+
d = loadData(loaddata01)
22+
assert np.array_equal(d2c, d)
23+
24+
# The usecols=(0, 1) would make it read from the third line
25+
d = loadData(loaddata01, minrows=1, usecols=(0, 1))
26+
assert np.array_equal(d2c, d)
27+
28+
# Check the effect of usecols effect
29+
d = loadData(loaddata01, usecols=(0,))
30+
assert np.array_equal(d2c[:, 0], d)
31+
32+
d = loadData(loaddata01, usecols=(1,))
33+
assert np.array_equal(d2c[:, 1], d)
34+
35+
36+
def test_loadData_1column(datafile):
37+
"""check loading of one-column data."""
38+
loaddata01 = datafile("loaddata01.txt")
39+
d1c = np.arange(1, 6)
40+
41+
# Assertions using pytest's assert
42+
d = loadData(loaddata01, usecols=[0], minrows=1)
43+
assert np.array_equal(d1c, d)
44+
45+
d = loadData(loaddata01, usecols=[0], minrows=2)
46+
assert np.array_equal(d1c, d)
47+
48+
d = loadData(loaddata01, usecols=[0], minrows=3)
49+
assert not np.array_equal(d1c, d)
50+
51+
return
52+
53+
54+
def test_loadData_headers(datafile):
55+
"""check loadData() with headers options enabled"""
56+
loaddatawithheaders = datafile("loaddatawithheaders.txt")
57+
hignore = ["# ", "// ", "["] # ignore lines beginning with these strings
58+
delimiter = ": " # what our data should be separated by
59+
60+
# Load data with headers
61+
hdata = loadData(loaddatawithheaders, headers=True, hdel=delimiter, hignore=hignore)
62+
63+
# Assertions using pytest
64+
# Only fourteen lines of data are formatted properly
65+
assert len(hdata) == 14
66+
67+
# Check the following are floats
68+
vfloats = ["wavelength", "qmaxinst", "qmin", "qmax", "bgscale"]
69+
for name in vfloats:
70+
assert isinstance(hdata.get(name), float)
71+
72+
# Check the following are NOT floats
73+
vnfloats = ["composition", "rmax", "rmin", "rstep", "rpoly"]
74+
for name in vnfloats:
75+
assert not isinstance(hdata.get(name), float)

0 commit comments

Comments
 (0)