Skip to content

Commit

Permalink
Add pytest for testing python API (#139)
Browse files Browse the repository at this point in the history
* add manifest

* update manifest

* exclude __pycache__ from sdist

* add loader path

* fix termination of cmake it

* fix description type

* remove stuff from install_requires

* start of tests

* add first idx test

* add pytest

* add pytest to requirements.txt

* add python testing stage

* move python testing to pip install

* rename workflow to be build_and_test

* remove cmake.yml and add pytest install

* add proper name

* change name again

* make Python_test lowercase
  • Loading branch information
hmacdope authored Nov 7, 2022
1 parent f549aab commit 7f99b07
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CMake
name: build_and_test

on:
push:
Expand Down Expand Up @@ -79,7 +79,7 @@ jobs:
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest --test-dir _skbuild/*/cmake-build/libdistopia

pip-install:
# A pure Python install, which relies purely on pyproject.toml contents
runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -109,3 +109,10 @@ jobs:

- name: test
run: ctest --test-dir _skbuild/*/cmake-build/libdistopia

- name: install_pytest
run: pip install pytest

- name: python_test
# run python API tests
run: pytest distopia/tests
94 changes: 94 additions & 0 deletions distopia/tests/test_distopia.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import pytest
import distopia
import numpy as np
from numpy.testing import assert_allclose


"""
Majority of detailed testing is done at the C++ level.
This is primarily to make sure that the Python API works as expected.
"""


class TestDistances:

def arange_input(self, N, dtype):
return np.arange(3*N, dtype=dtype).reshape(N,3)

def idx_input(self, N):
idxs = np.zeros((N, 2), dtype=np.ulonglong)
for i in range(N):
idxs[i,0]=i
idxs[i,1]=i+N
return np.ravel(idxs)

@pytest.mark.parametrize('box', ([10, 10, 10], [100, 20, 10]))
@pytest.mark.parametrize('N', (0, 10, 1000, 10000))
def test_calc_bonds_ortho_float_all_zero(self, N, box):
c0 = self.arange_input(N, np.float32)
c1 = self.arange_input(N, np.float32)
result = distopia.calc_bonds_ortho_float(
c0, c1, np.asarray(box, dtype=np.float32))
assert_allclose(result, np.zeros(N))

@pytest.mark.parametrize('box', ([10, 10, 10], [100, 20, 10]))
@pytest.mark.parametrize('N', (0, 10, 1000, 10000))
def test_calc_bonds_ortho_double_all_zero(self, N, box):
c0 = self.arange_input(N, np.float64)
c1 = self.arange_input(N, np.float64)
result = distopia.calc_bonds_ortho_double(
c0, c1, np.asarray(box, dtype=np.float64))
assert_allclose(result, np.zeros(N))

@pytest.mark.parametrize('N', (0, 10, 1000, 10000))
def test_calc_bonds_nobox_float_all_zero(self, N):
c0 = self.arange_input(N, np.float32)
c1 = self.arange_input(N, np.float32)
result = distopia.calc_bonds_no_box_float(c0, c1)
assert_allclose(result, np.zeros(N))

@pytest.mark.parametrize('N', (0, 10, 1000, 10000))
def test_calc_bonds_nobox_double_all_zero(self, N):
c0 = self.arange_input(N, np.float64)
c1 = self.arange_input(N, np.float64)
result = distopia.calc_bonds_no_box_double(c0, c1)
assert_allclose(result, np.zeros(N))

@pytest.mark.parametrize('box', ([10, 10, 10], [100, 20, 10]))
@pytest.mark.parametrize('N', (0, 10, 1000, 10000))
def test_calc_bonds_idx_ortho_float_all_zero(self, N, box):
c0 = self.arange_input(N, np.float32)
c1 = self.arange_input(N, np.float32)
idx = self.idx_input(N)
coords = np.vstack((c0, c1))
result = distopia.calc_bonds_idx_ortho_float(coords, idx, np.asarray(box).astype(np.float32))
assert_allclose(result, np.zeros(N))

@pytest.mark.parametrize('box', ([10, 10, 10], [100, 20, 10]))
@pytest.mark.parametrize('N', (0, 10, 1000, 10000))
def test_calc_bonds_idx_ortho_double_all_zero(self, N, box):
c0 = self.arange_input(N, np.float64)
c1 = self.arange_input(N, np.float64)
idx = self.idx_input(N)
coords = np.vstack((c0, c1))
result = distopia.calc_bonds_idx_ortho_double(coords, idx, np.asarray(box).astype(np.float64))
assert_allclose(result, np.zeros(N))


@pytest.mark.parametrize('N', (0, 10, 1000, 10000))
def test_calc_bonds_idx_no_box_float_all_zero(self, N):
c0 = self.arange_input(N, np.float32)
c1 = self.arange_input(N, np.float32)
idx = self.idx_input(N)
coords = np.vstack((c0, c1))
result = distopia.calc_bonds_idx_no_box_float(coords, idx)
assert_allclose(result, np.zeros(N))

@pytest.mark.parametrize('N', (0, 10, 1000, 10000))
def test_calc_bonds_idx_no_box_double_all_zero(self, N):
c0 = self.arange_input(N, np.float64)
c1 = self.arange_input(N, np.float64)
idx = self.idx_input(N)
coords = np.vstack((c0, c1))
result = distopia.calc_bonds_idx_no_box_double(coords, idx)
assert_allclose(result, np.zeros(N))
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ cmake
cython>=0.28.0,<3.0.0
numpy>=1.20.0
ninja
pytest
scikit-build

0 comments on commit 7f99b07

Please sign in to comment.