Skip to content

Use pytest for test_loaddata.py #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions news/test-refactor.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* <news item>

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* Unittest to Pytest migration for test_loaddata.py

**Security:**

* <news item>
6 changes: 6 additions & 0 deletions src/diffpy/utils/parsers/loaddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#
##############################################################################

import os

import numpy


Expand Down Expand Up @@ -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
Expand Down
134 changes: 71 additions & 63 deletions tests/diffpy/utils/parsers/test_loaddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,79 @@
"""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) as err:
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)
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)


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)
assert hdata == expected
Loading