Skip to content

Commit

Permalink
Merge pull request #46 from Tieqiong/test
Browse files Browse the repository at this point in the history
refactor: move tests and fix pytest
  • Loading branch information
sbillinge authored Jan 11, 2025
2 parents 4bf46d2 + 0ce4864 commit 5f34e62
Show file tree
Hide file tree
Showing 38 changed files with 174 additions and 275 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests-on-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ jobs:
- name: Install diffpy.srreal and requirements
run: |
conda install --file requirements/test.txt
conda install --file requirements/conda.txt
conda install boost numpy libdiffpy setuptools diffpy.structure periodictable gsl
python -m pip install . --no-deps
- name: Validate diffpy.pdfgui
run: python -m diffpy.srreal.tests.run
run: pytest tests
85 changes: 0 additions & 85 deletions src/diffpy/srreal/tests/__init__.py

This file was deleted.

33 changes: 0 additions & 33 deletions src/diffpy/srreal/tests/debug.py

This file was deleted.

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

This file was deleted.

75 changes: 75 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import json
import logging
from pathlib import Path

import pytest

from diffpy.srreal.structureconverters import convertObjCrystCrystal


@pytest.fixture
def user_filesystem(tmp_path):
base_dir = Path(tmp_path)
home_dir = base_dir / "home_dir"
home_dir.mkdir(parents=True, exist_ok=True)
cwd_dir = base_dir / "cwd_dir"
cwd_dir.mkdir(parents=True, exist_ok=True)

home_config_data = {"username": "home_username", "email": "[email protected]"}
with open(home_dir / "diffpyconfig.json", "w") as f:
json.dump(home_config_data, f)

yield tmp_path


# Resolve availability of optional packages.

# pyobjcryst


@pytest.fixture(scope="session")
def _msg_nopyobjcryst():
return "No module named 'pyobjcryst'"


@pytest.fixture(scope="session")
def has_pyobjcryst():
try:
import pyobjcryst.crystal

convertObjCrystCrystal(pyobjcryst.crystal.Crystal())
has_pyobjcryst = True
except ImportError:
has_pyobjcryst = False
logging.warning("Cannot import pyobjcryst, pyobjcryst tests skipped.")
print("Cannot import pyobjcryst, pyobjcryst tests skipped.")
except TypeError:
has_pyobjcryst = False
logging.warning("Compiled without ObjCryst, pyobjcryst tests skipped.")
print("Compiled without ObjCryst, pyobjcryst tests skipped.")

return has_pyobjcryst


# periodictable


@pytest.fixture(scope="session")
def _msg_noperiodictable():
return "No module named 'periodictable'"


@pytest.fixture(scope="session")
def has_periodictable():
try:
import periodictable

has_periodictable = True

# silence the pyflakes syntax checker
del periodictable
except ImportError:
has_periodictable = False
logging.warning("Cannot import periodictable, periodictable tests skipped.")

return has_periodictable
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import pickle
import unittest

import pytest

from diffpy.srreal.atomradiitable import AtomRadiiTable, ConstantRadiiTable, CovalentRadiiTable
from diffpy.srreal.tests.testutils import _msg_noperiodictable, has_periodictable

# ----------------------------------------------------------------------------


class TestAtomRadiiTable(unittest.TestCase):

def setUp(self):
self.rtb = AtomRadiiTable()
self.ctb = ConstantRadiiTable()
Expand Down Expand Up @@ -103,9 +103,13 @@ def test_toString(self):
# ----------------------------------------------------------------------------


@unittest.skipUnless(has_periodictable, _msg_noperiodictable)
class TestCovalentRadiiTable(unittest.TestCase):

@pytest.fixture(autouse=True)
def _check_periodictable(self, has_periodictable, _msg_noperiodictable):
if not has_periodictable:
pytest.skip(_msg_noperiodictable)

def setUp(self):
self.rtb = CovalentRadiiTable()
return
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@
import unittest

import numpy
import pytest
from testutils import loadDiffPyStructure, loadObjCrystCrystal, pickle_with_attr

from diffpy.srreal.bondcalculator import BondCalculator
from diffpy.srreal.tests.testutils import (
_msg_nopyobjcryst,
has_pyobjcryst,
loadDiffPyStructure,
loadObjCrystCrystal,
pickle_with_attr,
)

# ----------------------------------------------------------------------------

Expand Down Expand Up @@ -81,7 +76,7 @@ def test_pickling(self):

def test_pickling_derived_structure(self):
"""Check pickling of BondCalculator with DerivedStructureAdapter."""
from diffpy.srreal.tests.testutils import DerivedStructureAdapter
from testutils import DerivedStructureAdapter

bdc = self.bdc
stru0 = DerivedStructureAdapter()
Expand Down Expand Up @@ -249,9 +244,13 @@ def test_setTypeMask(self):
# ----------------------------------------------------------------------------


@unittest.skipUnless(has_pyobjcryst, _msg_nopyobjcryst)
class TestBondCalculatorObjCryst(unittest.TestCase):

@pytest.fixture(autouse=True)
def _check_periodictable(self, has_pyobjcryst, _msg_nopyobjcryst):
if not has_pyobjcryst:
pytest.skip(_msg_nopyobjcryst)

def setUp(self):
self.bdc = BondCalculator()
if not hasattr(self, "rutile"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import pickle
import unittest

from testutils import loadDiffPyStructure, pickle_with_attr

from diffpy.srreal.bvscalculator import BVSCalculator
from diffpy.srreal.tests.testutils import loadDiffPyStructure, pickle_with_attr


##############################################################################
Expand Down Expand Up @@ -139,7 +140,7 @@ def test_table_pickling(self):

def test_pickling_derived_structure(self):
"""Check pickling of BVSCalculator with DerivedStructureAdapter."""
from diffpy.srreal.tests.testutils import DerivedStructureAdapter
from testutils import DerivedStructureAdapter

bvc = self.bvc
stru0 = DerivedStructureAdapter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
import unittest

import numpy
from testutils import _maxNormDiff, loadDiffPyStructure, pickle_with_attr

from diffpy.srreal.pdfcalculator import DebyePDFCalculator, PDFCalculator
from diffpy.srreal.tests.testpdfcalculator import _maxNormDiff
from diffpy.srreal.tests.testutils import loadDiffPyStructure, pickle_with_attr


##############################################################################
Expand Down Expand Up @@ -189,7 +188,7 @@ def test_mask_pickling(self):
def test_pickling_derived_structure(self):
"""Check pickling of DebyePDFCalculator with
DerivedStructureAdapter."""
from diffpy.srreal.tests.testutils import DerivedStructureAdapter
from testutils import DerivedStructureAdapter

dpdfc = self.dpdfc
stru0 = DerivedStructureAdapter()
Expand Down
Loading

0 comments on commit 5f34e62

Please sign in to comment.