Skip to content

Commit

Permalink
feat: add tests for Python package; bump min Python version; run in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
sd2k committed Nov 16, 2024
1 parent 125af81 commit 1a876bf
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 1 deletion.
33 changes: 33 additions & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Python tests

on:
push:
branches: ["main"]
pull_request:

jobs:
test:
name: Python tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v3
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version-file: crates/pyaugurs/pyproject.toml
- name: Setup virtualenv
run: uv venv --project crates/pyaugurs
- name: Build wheel
uses: PyO3/maturin-action@v1
with:
command: develop
target: x86_64
args: --uv
working-directory: crates/pyaugurs
sccache: 'true'
- name: Install deps
run: uv sync --project crates/pyaugurs --all-extras --dev
- name: Run tests
run: uv run --project crates/pyaugurs pytest crates/pyaugurs/tests
1 change: 1 addition & 0 deletions crates/pyaugurs/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.python-version
__pycache__
7 changes: 6 additions & 1 deletion crates/pyaugurs/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "maturin"
[project]
name = "augurs"
dependencies = ["numpy"]
requires-python = ">=3.7"
requires-python = ">=3.9"
classifiers = [
"Programming Language :: Rust",
"Programming Language :: Python :: Implementation :: CPython",
Expand All @@ -14,3 +14,8 @@ classifiers = [

[tool.maturin]
features = ["pyo3/extension-module"]

[dependency-groups]
dev = [
"pytest>=8.3.3",
]
48 changes: 48 additions & 0 deletions crates/pyaugurs/tests/test_dtw.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import numpy as np
import pytest

from augurs import Dtw


class TestDtw:
@pytest.mark.parametrize(
"opts, input, expected",
[
({}, [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]], 5.0990195135926845),
({"window": 2}, [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]], 5.0990195135926845),
({"distance_fn": "euclidean"}, [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]], 5.0990195135926845),
({"distance_fn": "manhattan"}, [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]], 9),
],
)
def test_distance(self, opts, input, expected):
d = Dtw(**opts)
arrays = [np.array(x) for x in input]
np.testing.assert_allclose(d.distance(*arrays), expected)

@pytest.mark.parametrize(
"opts, input, expected",
[
(
{},
[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]],
[
[0.0, 5.0990195135926845, 10.392304845413264],
[5.0990195135926845, 0.0, 5.0990195135926845],
[10.392304845413264, 5.0990195135926845, 0.0],
]
),
(
{"distance_fn": "manhattan"},
[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]],
[
[0.0, 9.0, 18.0],
[9.0, 0.0, 9.0],
[18.0, 9.0, 0.0],
]
),
],
)
def test_distance_matrix(self, opts, input, expected):
d = Dtw(**opts)
arrays = [np.array(x) for x in input]
np.testing.assert_allclose(d.distance_matrix(arrays).to_numpy(), expected)
Loading

0 comments on commit 1a876bf

Please sign in to comment.