Skip to content

Commit c30093f

Browse files
Add hypothesis tests for coordinate transformations (#22)
1 parent 94020cb commit c30093f

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

pyforc/core/coordinates.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ def from_str(name: str) -> "Coordinates":
3131
if subc.name == name:
3232
return subc()
3333

34-
raise ValueError(f"Invalid coordinate type {name}")
34+
names = [subc.name for name in Coordinates.__subclasses__()]
35+
raise ValueError(f"Invalid coordinate type {name}. Valid names: {names}")
3536

3637

3738
class CoordinatesHcHb(Coordinates):

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ profile = "black"
4242

4343
[project.optional-dependencies]
4444
dev = ["pre-commit>=3.6.0"]
45-
test = ["pytest", "pytest-cov"]
45+
test = ["pytest", "pytest-cov", "hypothesis"]
4646
doc = ["sphinx", "myst-parser"]
4747

4848
[tool.ruff]

tests/core/test_coordinates.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import numpy as np
2+
import pytest
3+
from hypothesis import given
4+
from hypothesis import strategies as st
5+
6+
from pyforc.core import Coordinates, CoordinatesHcHb, CoordinatesHHr
7+
8+
9+
@pytest.mark.parametrize(
10+
("coord_str", "expected"),
11+
[
12+
("hchb", CoordinatesHcHb),
13+
("hhr", CoordinatesHHr),
14+
],
15+
)
16+
def test_from_str(coord_str, expected):
17+
"""Test that the right Coordinates subclass is returned for each string."""
18+
assert isinstance(Coordinates.from_str(coord_str), expected)
19+
20+
21+
def test_from_str_invalid():
22+
"""Test that an invalid name raises an error for Coordinates.from_str()."""
23+
name = "foo"
24+
with pytest.raises(ValueError, match=f"Invalid coordinate type {name}"):
25+
Coordinates.from_str(name)
26+
27+
28+
@given(
29+
st.floats(min_value=-2 * 53 - 1, max_value=2 * 53 - 1),
30+
st.floats(min_value=-2 * 53 - 1, max_value=2 * 53 - 1),
31+
)
32+
def test_hchb_matrix(h, hr):
33+
"""Test that the hchb coordinate matrix transforms coordinates as expected."""
34+
hhr_vec = np.array([h, hr, 0])
35+
36+
coords = CoordinatesHcHb()
37+
assert coords.has_inverse
38+
39+
arr = np.array(coords.get_matrix())
40+
41+
[hc, hb, _] = arr @ hhr_vec
42+
43+
assert np.isclose(h, hb + hc)
44+
assert np.isclose(hr, hb - hc)
45+
assert np.isclose(hc, (h - hr) / 2)
46+
assert np.isclose(hb, (h + hr) / 2)
47+
48+
49+
@given(
50+
st.floats(min_value=-2 * 53 - 1, max_value=2 * 53 - 1),
51+
st.floats(min_value=-2 * 53 - 1, max_value=2 * 53 - 1),
52+
)
53+
def test_hhr_matrix(h, hr):
54+
"""Test that the hhr coordinate matrix transforms coordinates as expected."""
55+
hhr_vec = np.array([h, hr, 0])
56+
57+
coords = CoordinatesHHr()
58+
assert coords.has_inverse
59+
60+
arr = np.array(coords.get_matrix())
61+
62+
[h_calc, hr_calc, _] = arr @ hhr_vec
63+
64+
assert h_calc == h
65+
assert hr_calc == hr

0 commit comments

Comments
 (0)