Skip to content

Commit

Permalink
tests: cleanup and typo
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Apr 8, 2021
1 parent 1350447 commit dd6f96f
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 172 deletions.
22 changes: 22 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import shutil
import tempfile
import time

TMPDIR = tempfile.mkdtemp(prefix=time.strftime(
"qpformat_test_%H.%M_"))


def pytest_configure(config):
"""
Allows plugins and conftest files to perform initial configuration.
This hook is called for every plugin and initial conftest
file after command line options have been parsed.
"""
tempfile.tempdir = TMPDIR


def pytest_unconfigure(config):
"""
called before test process is exited.
"""
shutil.rmtree(TMPDIR, ignore_errors=True)
11 changes: 0 additions & 11 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ def test_wrong_file_format():
pass
else:
assert False, "Unknown file format was loaded!"
# cleanup
try:
os.remove(tf)
except OSError:
pass


def test_load_with_bg():
Expand All @@ -38,12 +33,6 @@ def test_load_with_bg():
as_type="float64")
qpi = ds.get_qpimage()
assert np.allclose(qpi.pha, data - bg_data, atol=1e-15, rtol=0)
# cleanup
try:
os.remove(f_data)
os.remove(f_bg_data)
except OSError:
pass


if __name__ == "__main__":
Expand Down
50 changes: 1 addition & 49 deletions tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ def test_change_wavelength():

assert ds1.identifier != ds2.identifier, "should be different identifiers"

try:
os.remove(path)
except OSError:
pass
shutil.rmtree(dout, ignore_errors=True)


def test_identifier():
path = datapath / "series_phasics.zip"
Expand All @@ -69,17 +63,11 @@ def test_meta():
ds = qpformat.load_data(path=tf, meta_data={"time": 47})
assert ds.get_time() == 47
for pp in ds.get_name():
if pp.same_file(tf):
if pp.samefile(tf):
break
else:
assert False, "{} not in {}".format(tf, ds.get_name())

# cleanup
try:
os.remove(tf)
except OSError:
pass


def test_meta_None():
data = np.ones((20, 20), dtype=float)
Expand All @@ -94,12 +82,6 @@ def test_meta_None():
assert "medium index" not in ds.meta_data
assert "wavelength" not in ds.meta_data

# cleanup
try:
os.remove(tf)
except OSError:
pass


def test_repr():
data = np.ones((20, 20), dtype=float)
Expand All @@ -115,11 +97,6 @@ def test_repr():
"pixel size": 15})
assert "λ=1" in ds2.__repr__()
assert "1px=15" in ds2.__repr__()
# cleanup
try:
os.remove(tf)
except OSError:
pass


def test_save():
Expand All @@ -146,9 +123,6 @@ def test_save():
assert len(ds) == len(ds2)
assert np.all(ds.get_qpimage(0).pha == ds2.get_qpimage(0).pha)

shutil.rmtree(data_dir, ignore_errors=True)
shutil.rmtree(save_dir, ignore_errors=True)


def test_save_one_bg():
"""Test that if there is a single bg, it is stored somehow
Expand Down Expand Up @@ -176,9 +150,6 @@ def test_save_one_bg():
assert np.all(ds2.get_qpimage(0).pha == 0)
assert not np.all(ds2.get_qpimage(1).pha == 0)

shutil.rmtree(data_dir, ignore_errors=True)
shutil.rmtree(save_dir, ignore_errors=True)


def test_save_identifier():
"""Test that identifier is saved only when full series was saved"""
Expand Down Expand Up @@ -207,9 +178,6 @@ def test_save_identifier():
with qpimage.QPSeries(h5file=save_path2) as qps2:
assert qps2.identifier is None

shutil.rmtree(data_dir, ignore_errors=True)
shutil.rmtree(save_dir, ignore_errors=True)


def test_set_bg():
data = np.ones((20, 20), dtype=float)
Expand Down Expand Up @@ -249,13 +217,6 @@ def test_set_bg():
else:
assert False, "ndarray cannot be used to set bg data"

# cleanup
try:
os.remove(f_data)
os.remove(f_bg_data)
except OSError:
pass


def test_set_bg_series():
data_dir = tempfile.mkdtemp(prefix="qpformat_test_data_")
Expand Down Expand Up @@ -288,9 +249,6 @@ def test_set_bg_series():
assert np.allclose(ds1.get_qpimage(1).pha, data2 - bg_data2)
assert not np.allclose(ds1.get_qpimage(0).pha, data2 - bg_data2)

shutil.rmtree(data_dir, ignore_errors=True)
shutil.rmtree(bg_data_dir, ignore_errors=True)


def test_set_bg_qpimage():
data = np.ones((20, 20), dtype=float)
Expand All @@ -306,12 +264,6 @@ def test_set_bg_qpimage():
ds1 = qpformat.core.load_data(path=f_data, bg_data=qpi)
assert np.allclose(ds1.get_qpimage().pha, data - bg_data)

# cleanup
try:
os.remove(f_data)
except OSError:
pass


if __name__ == "__main__":
# Run all tests
Expand Down
8 changes: 1 addition & 7 deletions tests/test_series_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

def assert_path_in_list(path, path_list):
for ff in path_list:
if path.same_file(ff):
if path.samefile(ff):
break
else:
assert False, "{} not in {}".format(path, path_list)
Expand Down Expand Up @@ -78,7 +78,6 @@ def test_load_data():
# format should be right
assert ds.verify(ds.path)
assert ds.__class__.__name__ == "SeriesFolder"
shutil.rmtree(path, ignore_errors=True)


def test_multiple_formats_phasics_tif():
Expand All @@ -92,7 +91,6 @@ def test_multiple_formats_phasics_tif():
assert_path_in_list(ff, files)
assert ds.verify(ds.path)
assert ds.__class__.__name__ == "SeriesFolder"
shutil.rmtree(path, ignore_errors=True)


def test_multiple_formats_phasics_tif_ignore_h5():
Expand All @@ -105,7 +103,6 @@ def test_multiple_formats_phasics_tif_ignore_h5():
ds = qpformat.load_data(path)
for ff in ds.files:
assert_path_in_list(ff, files1)
shutil.rmtree(path, ignore_errors=True)


def test_multiple_formats_error():
Expand All @@ -124,7 +121,6 @@ def test_multiple_formats_error():
pass
else:
assert False, "multiple formats should not be supported"
shutil.rmtree(path, ignore_errors=True)


def test_series_format_holo_kw():
Expand All @@ -138,7 +134,6 @@ def test_series_format_holo_kw():
p1 = ds1.get_qpimage(0)
p2 = ds2.get_qpimage(0)
assert np.all(p1.pha + p2.pha == 0)
shutil.rmtree(path, ignore_errors=True)


def test_series_format_ignored():
Expand All @@ -150,7 +145,6 @@ def test_series_format_ignored():
ds = qpformat.load_data(path)
assert len(ds) == 2
assert bad_path not in ds.files
shutil.rmtree(path, ignore_errors=True)


if __name__ == "__main__":
Expand Down
5 changes: 0 additions & 5 deletions tests/test_series_hdf5_hyperspy.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ def test_basic():
assert len(ds) == 1
assert qpi["pixel size"] == 0.107e-6

shutil.rmtree(path=tdir, ignore_errors=True)


def test_returned_identifier():
tdir, hspyf = make_hyperspy()
Expand All @@ -57,7 +55,6 @@ def test_returned_identifier():
assert "identifier" in qpi
qpiraw = ds.get_qpimage_raw(0)
assert "identifier" in qpiraw
shutil.rmtree(path=tdir, ignore_errors=True)


def test_wrong_format():
Expand Down Expand Up @@ -90,8 +87,6 @@ def test_wrong_signal_type():
assert issubclass(w[-1].category, WrongSignalTypeWarnging)
assert "unknown" in str(w[-1].message)

shutil.rmtree(path=tdir, ignore_errors=True)


if __name__ == "__main__":
# Run all tests
Expand Down
46 changes: 2 additions & 44 deletions tests/test_series_hdf5_qpimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ def test_identifier():
assert ds.get_identifier(1) != "an important string"
assert ds.identifier in ds.get_identifier(0)

# cleanup
try:
os.remove(tf)
except OSError:
pass


def test_load_data():
path = datapath / "single_qpimage.h5"
Expand All @@ -53,7 +47,7 @@ def test_load_data():

ds = qpformat.load_data(tf)
assert len(ds) == 2
assert ds.path.same_file(pathlib.Path(tf))
assert ds.path.samefile(pathlib.Path(tf))
assert np.isnan(ds.get_time(1))
assert "SeriesHdf5Qpimage" in ds.__repr__()
qpd = ds.get_qpimage(1)
Expand All @@ -63,12 +57,6 @@ def test_load_data():
assert np.allclose(qpd.amp, qpi1.amp)
assert np.allclose(qpd.pha, qpi1.pha)

# cleanup
try:
os.remove(tf)
except OSError:
pass


def test_meta_extraction():
path = datapath / "single_qpimage.h5"
Expand All @@ -87,12 +75,6 @@ def test_meta_extraction():
assert ds.meta_data["wavelength"] == 111e-9
assert ds.meta_data["pixel size"] == .12

# cleanup
try:
os.remove(tf)
except OSError:
pass


def test_meta_override():
path = datapath / "single_qpimage.h5"
Expand Down Expand Up @@ -122,12 +104,6 @@ def test_meta_override():
assert qpi_ds["wavelength"] == wl
assert qpi_ds["pixel size"] == px

# cleanup
try:
os.remove(tf)
except OSError:
pass


def test_returned_identifier():
path = datapath / "single_qpimage.h5"
Expand All @@ -146,12 +122,6 @@ def test_returned_identifier():
qpiraw = ds.get_qpimage_raw(0)
assert "identifier" in qpiraw

# cleanup
try:
os.remove(tf)
except OSError:
pass


def test_subjoined_load_data():
path = datapath / "single_qpimage.h5"
Expand All @@ -168,7 +138,7 @@ def test_subjoined_load_data():

ds = qpformat.load_data(tf)
assert len(ds) == 2
assert ds.path.same_file(pathlib.Path(tf))
assert ds.path.samefile(pathlib.Path(tf))
assert np.isnan(ds.get_time(1))
assert "SeriesHdf5QpimageSubjoined" in ds.__repr__()
qpd = ds.get_qpimage(1)
Expand All @@ -178,12 +148,6 @@ def test_subjoined_load_data():
assert np.allclose(qpd.amp, qpi1.amp)
assert np.allclose(qpd.pha, qpi1.pha)

# cleanup
try:
os.remove(tf)
except OSError:
pass


def test_subjoined_meta_extraction():
path = datapath / "single_qpimage.h5"
Expand All @@ -203,12 +167,6 @@ def test_subjoined_meta_extraction():
assert ds.meta_data["wavelength"] == 111e-9
assert ds.meta_data["pixel size"] == .12

# cleanup
try:
os.remove(tf)
except OSError:
pass


if __name__ == "__main__":
# Run all tests
Expand Down

0 comments on commit dd6f96f

Please sign in to comment.