Skip to content
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
2 changes: 0 additions & 2 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
[pytest]
testpaths= src/diffpy/utils/tests
python_files = test_*.py
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytest automatically detects files starting with test_.

81 changes: 0 additions & 81 deletions src/diffpy/utils/tests/__init__.py

This file was deleted.

35 changes: 0 additions & 35 deletions src/diffpy/utils/tests/debug.py

This file was deleted.

37 changes: 0 additions & 37 deletions src/diffpy/utils/tests/run.py

This file was deleted.

Empty file added tests/__init__.py
Empty file.
18 changes: 18 additions & 0 deletions src/diffpy/utils/tests/conftest.py → tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from importlib.resources import as_file, files
from pathlib import Path

import pytest
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@
import unittest

import numpy
import pytest

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)
Copy link
Contributor Author

@bobleesj bobleesj Sep 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trick to use @pytest.fixture within unittest class

Source: https://stackoverflow.com/a/66222108

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
Expand All @@ -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))
Expand All @@ -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)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading