From 37b32dbabdf7d6b1aa932d5ea703390e42dfecf1 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Sat, 7 Sep 2024 13:02:48 -0400 Subject: [PATCH 1/7] Move tests dir to top level --- {src/diffpy/utils/tests => tests}/__init__.py | 0 {src/diffpy/utils/tests => tests}/conftest.py | 0 {src/diffpy/utils/tests => tests}/debug.py | 0 {src/diffpy/utils/tests => tests}/run.py | 0 {src/diffpy/utils/tests => tests}/test_diffraction_objects.py | 0 {src/diffpy/utils/tests => tests}/test_loaddata.py | 0 {src/diffpy/utils/tests => tests}/test_resample.py | 0 {src/diffpy/utils/tests => tests}/test_serialization.py | 0 {src/diffpy/utils/tests => tests}/test_tools.py | 0 {src/diffpy/utils/tests => tests}/testdata/dbload/e1.gr | 0 {src/diffpy/utils/tests => tests}/testdata/dbload/e2.gr | 0 {src/diffpy/utils/tests => tests}/testdata/dbload/e3.gr | 0 {src/diffpy/utils/tests => tests}/testdata/generated_db.json | 0 {src/diffpy/utils/tests => tests}/testdata/generatewarnings.txt | 0 {src/diffpy/utils/tests => tests}/testdata/loaddata01.txt | 0 .../diffpy/utils/tests => tests}/testdata/loaddatawithheaders.txt | 0 {src/diffpy/utils/tests => tests}/testdata/loadfile.txt | 0 {src/diffpy/utils/tests => tests}/testdata/strumining.json | 0 {src/diffpy/utils/tests => tests}/testdata/targetjson.json | 0 {src/diffpy/utils/tests => tests}/testdata/wrong.type | 0 {src/diffpy/utils/tests => tests}/testhelpers.py | 0 21 files changed, 0 insertions(+), 0 deletions(-) rename {src/diffpy/utils/tests => tests}/__init__.py (100%) rename {src/diffpy/utils/tests => tests}/conftest.py (100%) rename {src/diffpy/utils/tests => tests}/debug.py (100%) rename {src/diffpy/utils/tests => tests}/run.py (100%) rename {src/diffpy/utils/tests => tests}/test_diffraction_objects.py (100%) rename {src/diffpy/utils/tests => tests}/test_loaddata.py (100%) rename {src/diffpy/utils/tests => tests}/test_resample.py (100%) rename {src/diffpy/utils/tests => tests}/test_serialization.py (100%) rename {src/diffpy/utils/tests => tests}/test_tools.py (100%) rename {src/diffpy/utils/tests => tests}/testdata/dbload/e1.gr (100%) rename {src/diffpy/utils/tests => tests}/testdata/dbload/e2.gr (100%) rename {src/diffpy/utils/tests => tests}/testdata/dbload/e3.gr (100%) rename {src/diffpy/utils/tests => tests}/testdata/generated_db.json (100%) rename {src/diffpy/utils/tests => tests}/testdata/generatewarnings.txt (100%) rename {src/diffpy/utils/tests => tests}/testdata/loaddata01.txt (100%) rename {src/diffpy/utils/tests => tests}/testdata/loaddatawithheaders.txt (100%) rename {src/diffpy/utils/tests => tests}/testdata/loadfile.txt (100%) rename {src/diffpy/utils/tests => tests}/testdata/strumining.json (100%) rename {src/diffpy/utils/tests => tests}/testdata/targetjson.json (100%) rename {src/diffpy/utils/tests => tests}/testdata/wrong.type (100%) rename {src/diffpy/utils/tests => tests}/testhelpers.py (100%) diff --git a/src/diffpy/utils/tests/__init__.py b/tests/__init__.py similarity index 100% rename from src/diffpy/utils/tests/__init__.py rename to tests/__init__.py diff --git a/src/diffpy/utils/tests/conftest.py b/tests/conftest.py similarity index 100% rename from src/diffpy/utils/tests/conftest.py rename to tests/conftest.py diff --git a/src/diffpy/utils/tests/debug.py b/tests/debug.py similarity index 100% rename from src/diffpy/utils/tests/debug.py rename to tests/debug.py diff --git a/src/diffpy/utils/tests/run.py b/tests/run.py similarity index 100% rename from src/diffpy/utils/tests/run.py rename to tests/run.py diff --git a/src/diffpy/utils/tests/test_diffraction_objects.py b/tests/test_diffraction_objects.py similarity index 100% rename from src/diffpy/utils/tests/test_diffraction_objects.py rename to tests/test_diffraction_objects.py diff --git a/src/diffpy/utils/tests/test_loaddata.py b/tests/test_loaddata.py similarity index 100% rename from src/diffpy/utils/tests/test_loaddata.py rename to tests/test_loaddata.py diff --git a/src/diffpy/utils/tests/test_resample.py b/tests/test_resample.py similarity index 100% rename from src/diffpy/utils/tests/test_resample.py rename to tests/test_resample.py diff --git a/src/diffpy/utils/tests/test_serialization.py b/tests/test_serialization.py similarity index 100% rename from src/diffpy/utils/tests/test_serialization.py rename to tests/test_serialization.py diff --git a/src/diffpy/utils/tests/test_tools.py b/tests/test_tools.py similarity index 100% rename from src/diffpy/utils/tests/test_tools.py rename to tests/test_tools.py diff --git a/src/diffpy/utils/tests/testdata/dbload/e1.gr b/tests/testdata/dbload/e1.gr similarity index 100% rename from src/diffpy/utils/tests/testdata/dbload/e1.gr rename to tests/testdata/dbload/e1.gr diff --git a/src/diffpy/utils/tests/testdata/dbload/e2.gr b/tests/testdata/dbload/e2.gr similarity index 100% rename from src/diffpy/utils/tests/testdata/dbload/e2.gr rename to tests/testdata/dbload/e2.gr diff --git a/src/diffpy/utils/tests/testdata/dbload/e3.gr b/tests/testdata/dbload/e3.gr similarity index 100% rename from src/diffpy/utils/tests/testdata/dbload/e3.gr rename to tests/testdata/dbload/e3.gr diff --git a/src/diffpy/utils/tests/testdata/generated_db.json b/tests/testdata/generated_db.json similarity index 100% rename from src/diffpy/utils/tests/testdata/generated_db.json rename to tests/testdata/generated_db.json diff --git a/src/diffpy/utils/tests/testdata/generatewarnings.txt b/tests/testdata/generatewarnings.txt similarity index 100% rename from src/diffpy/utils/tests/testdata/generatewarnings.txt rename to tests/testdata/generatewarnings.txt diff --git a/src/diffpy/utils/tests/testdata/loaddata01.txt b/tests/testdata/loaddata01.txt similarity index 100% rename from src/diffpy/utils/tests/testdata/loaddata01.txt rename to tests/testdata/loaddata01.txt diff --git a/src/diffpy/utils/tests/testdata/loaddatawithheaders.txt b/tests/testdata/loaddatawithheaders.txt similarity index 100% rename from src/diffpy/utils/tests/testdata/loaddatawithheaders.txt rename to tests/testdata/loaddatawithheaders.txt diff --git a/src/diffpy/utils/tests/testdata/loadfile.txt b/tests/testdata/loadfile.txt similarity index 100% rename from src/diffpy/utils/tests/testdata/loadfile.txt rename to tests/testdata/loadfile.txt diff --git a/src/diffpy/utils/tests/testdata/strumining.json b/tests/testdata/strumining.json similarity index 100% rename from src/diffpy/utils/tests/testdata/strumining.json rename to tests/testdata/strumining.json diff --git a/src/diffpy/utils/tests/testdata/targetjson.json b/tests/testdata/targetjson.json similarity index 100% rename from src/diffpy/utils/tests/testdata/targetjson.json rename to tests/testdata/targetjson.json diff --git a/src/diffpy/utils/tests/testdata/wrong.type b/tests/testdata/wrong.type similarity index 100% rename from src/diffpy/utils/tests/testdata/wrong.type rename to tests/testdata/wrong.type diff --git a/src/diffpy/utils/tests/testhelpers.py b/tests/testhelpers.py similarity index 100% rename from src/diffpy/utils/tests/testhelpers.py rename to tests/testhelpers.py From fa20e3d7eabc3d3ca1b9f4d786e3d8663ded2d01 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Sat, 7 Sep 2024 13:27:00 -0400 Subject: [PATCH 2/7] Implement test helper as fixture --- tests/conftest.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index e3b63139..dd493352 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,5 @@ import json +from importlib.resources import as_file, files from pathlib import Path import pytest @@ -17,3 +18,20 @@ def user_filesystem(tmp_path): json.dump(home_config_data, f) yield tmp_path + + +def get_datafile(filename): + """Helper function to retrieve the file path for test data.""" + ref = files(__package__) / f"testdata/{filename}" + with as_file(ref) as rv: + return rv + + +@pytest.fixture +def datafile(): + """Fixture to dynamically load any test file.""" + + def _load(filename): + return get_datafile(filename) + + return _load \ No newline at end of file From a36af71eb77f046c8bd692a6ec79f9b5439db94f Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Sat, 7 Sep 2024 13:27:23 -0400 Subject: [PATCH 3/7] Remove unittest run files --- tests/debug.py | 35 ----------------------------------- tests/run.py | 37 ------------------------------------- 2 files changed, 72 deletions(-) delete mode 100644 tests/debug.py delete mode 100644 tests/run.py diff --git a/tests/debug.py b/tests/debug.py deleted file mode 100644 index fed20421..00000000 --- a/tests/debug.py +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env python -############################################################################## -# -# diffpy.utils Complex Modeling Initiative -# (c) 2016 Brookhaven Science Associates, -# Brookhaven National Laboratory. -# All rights reserved. -# -# File coded by: Pavol Juhas -# -# See AUTHORS.txt for a list of people who contributed. -# See LICENSE.txt for license information. -# -############################################################################## - -""" -Convenience module for debugging the unit tests using - -python -m diffpy.utils.tests.debug - -Exceptions raised by failed tests or other errors are not caught. -""" - - -if __name__ == "__main__": - import sys - - from diffpy.utils.tests import testsuite - - pattern = sys.argv[1] if len(sys.argv) > 1 else "" - suite = testsuite(pattern) - suite.debug() - - -# End of file diff --git a/tests/run.py b/tests/run.py deleted file mode 100644 index 241caf11..00000000 --- a/tests/run.py +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env python -############################################################################## -# -# diffpy.utils by DANSE Diffraction group -# Simon J. L. Billinge -# (c) 2012 The Trustees of Columbia University -# in the City of New York. All rights reserved. -# -# File coded by: Pavol Juhas -# -# See AUTHORS.txt for a list of people who contributed. -# See LICENSE_DANSE.txt for license information. -# -############################################################################## - -"""Convenience module for executing all unit tests with - -python -m diffpy.utils.tests.run -""" - -if __name__ == "__main__": - import sys - - # show warnings by default - if not sys.warnoptions: - import os - import warnings - - warnings.simplefilter("default") - # also affect subprocesses - os.environ["PYTHONWARNINGS"] = "default" - from diffpy.utils.tests import test - - # produce zero exit code for a successful test - sys.exit(not test().wasSuccessful()) - -# End of file From ccce7f2d153e5b779578d7105492e57e63f211be Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Sat, 7 Sep 2024 14:48:58 -0400 Subject: [PATCH 4/7] Remove content of pytest.ini --- pytest.ini | 2 -- 1 file changed, 2 deletions(-) diff --git a/pytest.ini b/pytest.ini index 21473ae6..eea2c180 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,3 +1 @@ [pytest] -testpaths= src/diffpy/utils/tests -python_files = test_*.py From cc5305dcc1327c89ee459a35edfda7ec27dbd97c Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Sat, 7 Sep 2024 14:49:15 -0400 Subject: [PATCH 5/7] Run init files for unittest --- tests/__init__.py | 81 ----------------------------------------------- 1 file changed, 81 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index 42a123f3..e69de29b 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,81 +0,0 @@ -#!/usr/bin/env python -############################################################################## -# -# diffpy.utils by DANSE Diffraction group -# Simon J. L. Billinge -# (c) 2011 The Trustees of Columbia University -# in the City of New York. All rights reserved. -# -# File coded by: Pavol Juhas -# -# See AUTHORS.txt for a list of people who contributed. -# See LICENSE_DANSE.txt for license information. -# -############################################################################## - -"""Unit tests for diffpy.utils. -""" - -import unittest - - -def testsuite(pattern=""): - """Create a unit tests suite for diffpy.utils package. - - Parameters - ---------- - pattern : str, optional - Regular expression pattern for selecting test cases. - Select all tests when empty. Ignore the pattern when - any of unit test modules fails to import. - - Returns - ------- - suite : `unittest.TestSuite` - The TestSuite object containing the matching tests. - """ - import re - from importlib.resources import as_file, files - from itertools import chain - from os.path import dirname - - loader = unittest.defaultTestLoader - ref = files(__package__) - with as_file(ref) as thisdir: - depth = __name__.count(".") + 1 - topdir = thisdir - for i in range(depth): - topdir = dirname(topdir) - suite_all = loader.discover(str(thisdir), top_level_dir=topdir) - # always filter the suite by pattern to test-cover the selection code. - suite = unittest.TestSuite() - rx = re.compile(pattern) - tsuites = list(chain.from_iterable(suite_all)) - tsok = all(isinstance(ts, unittest.TestSuite) for ts in tsuites) - if not tsok: # pragma: no cover - return suite_all - tcases = chain.from_iterable(tsuites) - for tc in tcases: - tcwords = tc.id().split(".") - shortname = ".".join(tcwords[-3:]) - if rx.search(shortname): - suite.addTest(tc) - # verify all tests are found for an empty pattern. - assert pattern or suite_all.countTestCases() == suite.countTestCases() - return suite - - -def test(): - """Execute all unit tests for the diffpy.utils package. - - Returns - ------- - result : `unittest.TestResult` - """ - suite = testsuite() - runner = unittest.TextTestRunner() - result = runner.run(suite) - return result - - -# End of file From 2d5e5716cfea89a066323d13c4d1c3a100bb6d72 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Sat, 7 Sep 2024 14:49:26 -0400 Subject: [PATCH 6/7] Use conftest within unittesT --- tests/test_loaddata.py | 12 ++++++++---- tests/test_serialization.py | 23 +++++++++++------------ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/tests/test_loaddata.py b/tests/test_loaddata.py index 9399e71d..a7221d0e 100644 --- a/tests/test_loaddata.py +++ b/tests/test_loaddata.py @@ -4,20 +4,22 @@ """ import unittest +import pytest import numpy from diffpy.utils.parsers import loadData -from diffpy.utils.tests.testhelpers import datafile - -loaddata01 = datafile("loaddata01.txt") -loaddatawithheaders = datafile("loaddatawithheaders.txt") ############################################################################## 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 @@ -35,6 +37,7 @@ def test_loadData_default(self): 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)) @@ -46,6 +49,7 @@ def test_loadData_1column(self): 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) diff --git a/tests/test_serialization.py b/tests/test_serialization.py index bbe6be24..986892e7 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -5,21 +5,15 @@ from diffpy.utils.parsers import deserialize_data, loadData, serialize_data from diffpy.utils.parsers.custom_exceptions import ImproperSizeError, UnsupportedTypeError -from diffpy.utils.tests.testhelpers import datafile tests_dir = os.path.dirname(os.path.abspath(locals().get("__file__", "file.py"))) -targetjson = datafile("targetjson.json") -schemaname = datafile("strumining.json") -wrongtype = datafile("wrong.type") -loadfile = datafile("loadfile.txt") -warningfile = datafile("generatewarnings.txt") -nodt = datafile("loaddatawithheaders.txt") - -def test_load_multiple(tmp_path): - # generate json and apply schema +def test_load_multiple(tmp_path, datafile): + # Load test data + targetjson = datafile("targetjson.json") generatedjson = tmp_path / "generated_serialization.json" + tlm_list = os.listdir(os.path.join(tests_dir, "testdata", "dbload")) tlm_list.sort() generated_data = None @@ -50,7 +44,12 @@ def test_load_multiple(tmp_path): assert target_data == deserialize_data(generatedjson, filetype=".json") -def test_exceptions(): +def test_exceptions(datafile): + # Load test data + wrongtype = datafile("wrong.type") + loadfile = datafile("loadfile.txt") + warningfile = datafile("generatewarnings.txt") + nodt = datafile("loaddatawithheaders.txt") hdata = loadData(loadfile, headers=True) data_table = loadData(loadfile) @@ -107,4 +106,4 @@ def test_exceptions(): ) assert len(record) == 4 for msg in record: - assert "overwritten" in msg.message.args[0] + assert "overwritten" in msg.message.args[0] \ No newline at end of file From 19a7da822a7d92a9c9f7eba7413fc15fbe054843 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Sep 2024 18:52:14 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit hooks --- tests/conftest.py | 2 +- tests/test_loaddata.py | 4 ++-- tests/test_serialization.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index dd493352..09551da5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -34,4 +34,4 @@ def datafile(): def _load(filename): return get_datafile(filename) - return _load \ No newline at end of file + return _load diff --git a/tests/test_loaddata.py b/tests/test_loaddata.py index a7221d0e..2ca2fa07 100644 --- a/tests/test_loaddata.py +++ b/tests/test_loaddata.py @@ -4,9 +4,9 @@ """ import unittest -import pytest import numpy +import pytest from diffpy.utils.parsers import loadData @@ -16,7 +16,7 @@ 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") diff --git a/tests/test_serialization.py b/tests/test_serialization.py index 986892e7..51921e01 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -106,4 +106,4 @@ def test_exceptions(datafile): ) assert len(record) == 4 for msg in record: - assert "overwritten" in msg.message.args[0] \ No newline at end of file + assert "overwritten" in msg.message.args[0]