Skip to content

pkg resources #116

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 21 commits into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
c12a58a
fix: version.py now working again
sbillinge Jun 15, 2025
93ff368
[pre-commit.ci] auto fixes from pre-commit hooks
pre-commit-ci[bot] Jun 15, 2025
a23f8de
fix: test_sas working with skipping features
sbillinge Jun 19, 2025
b1f6369
merging init file
sbillinge Jun 19, 2025
e54ef40
[pre-commit.ci] auto fixes from pre-commit hooks
pre-commit-ci[bot] Jun 19, 2025
2338ba4
tests: test_builder.py passing all tests
sbillinge Jun 19, 2025
685df37
merging pre-commit changes. test_builder.py now passing tests
sbillinge Jun 19, 2025
4ddf81e
[pre-commit.ci] auto fixes from pre-commit hooks
pre-commit-ci[bot] Jun 19, 2025
9c825ec
tests: characteristicfunctions and contribution
sbillinge Jun 19, 2025
b791c52
tests: diffpyparset
sbillinge Jun 19, 2025
f51ee43
Merge branch 'pkg_resources' of github.com:sbillinge/diffpy.srfit int…
sbillinge Jun 19, 2025
c2dc20e
[pre-commit.ci] auto fixes from pre-commit hooks
pre-commit-ci[bot] Jun 19, 2025
be7eeb6
tests: equation.py
sbillinge Jun 19, 2025
f27ab7a
tests: fitrecipe
sbillinge Jun 19, 2025
8e0376a
tests: fitresults
sbillinge Jun 19, 2025
760bc0d
Merge branch 'pkg_resources' of github.com:sbillinge/diffpy.srfit int…
sbillinge Jun 19, 2025
632798e
tests pdf
sbillinge Jun 19, 2025
fb5001a
tests: profile
sbillinge Jun 19, 2025
59a56c7
tests: recipeorganizer
sbillinge Jun 19, 2025
f144bae
tests: objcrystparset and sgconstraints passing locally
sbillinge Jun 25, 2025
cb65f54
tests: visitors and speed. added sympy requirement to test
sbillinge Jun 25, 2025
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
1 change: 1 addition & 0 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ codecov
coverage
pytest-cov
pytest-env
sympy
85 changes: 0 additions & 85 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,85 +0,0 @@
#!/usr/bin/env python
##############################################################################
#
# diffpy.srfit by DANSE Diffraction group
# Simon J. L. Billinge
# (c) 2010 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.srfit."""

import logging
import unittest

# create logger instance for the tests subpackage
logging.basicConfig()
logger = logging.getLogger(__name__)
del logging


def testsuite(pattern=""):
"""Create a unit tests suite for diffpy.srfit 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 itertools import chain
from os.path import dirname

from pkg_resources import resource_filename

loader = unittest.defaultTestLoader
thisdir = resource_filename(__name__, "")
depth = __name__.count(".") + 1
topdir = thisdir
for i in range(depth):
topdir = dirname(topdir)
suite_all = loader.discover(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.srfit package.

Returns
-------
result : `unittest.TestResult`
"""
suite = testsuite()
runner = unittest.TextTestRunner()
result = runner.run(suite)
return result


# End of file
142 changes: 141 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,92 @@
import importlib.resources
import json
import logging
import sys
from functools import lru_cache
from pathlib import Path

import pytest
import six

import diffpy.srfit.equation.literals as literals
from diffpy.srfit.sas.sasimport import sasimport

@pytest.fixture
logger = logging.getLogger(__name__)


@lru_cache()
def has_sas():
try:
sasimport("sas.pr.invertor")
sasimport("sas.models")
return True

Check warning on line 22 in tests/conftest.py

View check run for this annotation

Codecov / codecov/patch

tests/conftest.py#L21-L22

Added lines #L21 - L22 were not covered by tests
except ImportError:
return False


# diffpy.structure
@lru_cache()
def has_diffpy_structure():
try:
import diffpy.structure as m

del m
return True

Check warning on line 34 in tests/conftest.py

View check run for this annotation

Codecov / codecov/patch

tests/conftest.py#L33-L34

Added lines #L33 - L34 were not covered by tests
except ImportError:
return False
logger.warning(
"Cannot import diffpy.structure, Structure tests skipped."
)


@lru_cache()
def has_pyobjcryst():
try:
import pyobjcryst as m

del m
return True

Check warning on line 48 in tests/conftest.py

View check run for this annotation

Codecov / codecov/patch

tests/conftest.py#L47-L48

Added lines #L47 - L48 were not covered by tests
except ImportError:
return False
logger.warning("Cannot import pyobjcryst, pyobjcryst tests skipped.")


# diffpy.srreal


@lru_cache()
def has_diffpy_srreal():
try:
import diffpy.srreal.pdfcalculator as m

del m
return True

Check warning on line 63 in tests/conftest.py

View check run for this annotation

Codecov / codecov/patch

tests/conftest.py#L62-L63

Added lines #L62 - L63 were not covered by tests
except ImportError:
return False
logger.warning("Cannot import diffpy.srreal, PDF tests skipped.")


@pytest.fixture(scope="session")
def sas_available():
return has_sas()


@pytest.fixture(scope="session")
def diffpy_structure_available():
return has_diffpy_structure()


@pytest.fixture(scope="session")
def diffpy_srreal_available():
return has_diffpy_srreal()


@pytest.fixture(scope="session")
def pyobjcryst_available():
return has_pyobjcryst()


@pytest.fixture(scope="session")
def user_filesystem(tmp_path):
base_dir = Path(tmp_path)
home_dir = base_dir / "home_dir"
Expand All @@ -17,3 +99,61 @@
json.dump(home_config_data, f)

yield tmp_path


@pytest.fixture(scope="session")
def datafile():
"""Fixture to load a test data file from the testdata package directory."""

def _datafile(filename):
return importlib.resources.files("tests.testdata").joinpath(filename)

return _datafile


@pytest.fixture(scope="session")
def make_args():
def _makeArgs(num):
args = []
for i in range(num):
j = i + 1
args.append(literals.Argument(name="v%i" % j, value=j))
return args

return _makeArgs


@pytest.fixture(scope="session")
def noObserversInGlobalBuilders():
def _noObserversInGlobalBuilders():
"""True if no observer function leaks to global builder objects.

Ensure objects are not immortal due to a reference from static
value.
"""
from diffpy.srfit.equation.builder import _builders

rv = True
for n, b in _builders.items():
if b.literal and b.literal._observers:
rv = False
break

Check warning on line 140 in tests/conftest.py

View check run for this annotation

Codecov / codecov/patch

tests/conftest.py#L139-L140

Added lines #L139 - L140 were not covered by tests
return rv

return _noObserversInGlobalBuilders()


@pytest.fixture(scope="session")
def capturestdout():
def _capturestdout(f, *args, **kwargs):
"""Capture the standard output from a call of function f."""
savestdout = sys.stdout
fp = six.StringIO()
try:
sys.stdout = fp
f(*args, **kwargs)
finally:
sys.stdout = savestdout
return fp.getvalue()

return _capturestdout
Loading
Loading