Skip to content

Commit

Permalink
CI: test the package as-installed
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentRDC committed Dec 15, 2020
1 parent 916ef63 commit 68728df
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 43 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ jobs:
pip install -r requirements.txt
# Note the use of the -Wa flag to show DeprecationWarnings
# We test the package as-installed. Pytest is better than unittest at finding
# unittest tests when the package is installed.
- name: Unit tests
env:
MATERIALS_PROJECT_API_KEY: ${{ secrets.MATERIALS_PROJECT_API_KEY }}
run: |
python -Wa -m unittest discover --verbose --buffer
python setup.py install
pip install pytest
python -Wa -m pytest --pyargs crystals --ignore=crystals --import-mode=importlib --verbose
- name: Build documentation
run: |
Expand Down
37 changes: 35 additions & 2 deletions crystals/tests/test_crystal.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
# -*- coding: utf-8 -*-
import pickle
import os
import pickle
import tempfile
import unittest
from copy import copy, deepcopy
from functools import wraps, lru_cache
from itertools import islice
from math import radians
from pathlib import Path
import socket
from contextlib import suppress

import numpy as np
from crystals import Atom, AtomicStructure, CenteringType, Crystal, Lattice
from crystals.affine import rotation_matrix, transform, translation_matrix
from crystals.crystal import symmetry_expansion, symmetry_reduction
from .utils import retry_test, connection_available

np.random.seed(23)


@lru_cache(maxsize=1)
def connection_available():
""" Returns whether or not an internet connection is available """
with suppress(OSError), socket.create_connection(("www.google.com", 80)):
return True
return False


class TestSpglibMethods(unittest.TestCase):
def test_symmetry_graphite(self):
""" Test that Crystal.symmetry() works correctly for graphite """
Expand Down Expand Up @@ -190,6 +200,29 @@ def test_trivial_reindexing(self):
self.assertEqual(c1, c2)


def retry_test(max_retries):
""" Retry the decorated test up to `max_retries` times if failure happens. """

def decorator(test_item):
@wraps(test_item)
def new_test_item(self, *args, **kwargs):
tries = 1
while tries < max_retries:
try:
r = test_item(self, *args, **kwargs)
except self.failureException:
tries += 1
else:
return r

# Final retry.
return test_item(self, *args, **kwargs)

return new_test_item

return decorator


# # The tests below are very flaky. Due to floating-point arithmetic
# # being non-deterministic, sometimes comparing atoms does not work
# # and the tests below fail.
Expand Down
15 changes: 12 additions & 3 deletions crystals/tests/test_parsers.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
# -*- coding: utf-8 -*-
import os
import socket
import tempfile
import unittest
from collections import Counter, namedtuple
from contextlib import suppress
from functools import lru_cache
from itertools import chain
from pathlib import Path
from tempfile import gettempdir
from warnings import catch_warnings, filterwarnings
from .utils import connection_available

import numpy as np
from spglib import get_symmetry_dataset

from crystals import CIFParser, Crystal, MPJParser, PDBParser, frac_coords, is_element
from crystals.affine import transform
from crystals.parsers import STRUCTURE_CACHE, PWSCFParser
from crystals.spg_data import Hall2Number
from spglib import get_symmetry_dataset

try:
import Bio.PDB as biopdb
Expand All @@ -33,6 +34,14 @@
filterwarnings("ignore", category=UserWarning)


@lru_cache(maxsize=1)
def connection_available():
""" Returns whether or not an internet connection is available """
with suppress(OSError), socket.create_connection(("www.google.com", 80)):
return True
return False


@unittest.skipUnless(connection_available(), "Internet connection is required.")
class TestPDBParser(unittest.TestCase):
def test_fractional_atoms(self):
Expand Down
37 changes: 0 additions & 37 deletions crystals/tests/utils.py

This file was deleted.

0 comments on commit 68728df

Please sign in to comment.