Skip to content

Commit

Permalink
Test clumerge() exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
nunofachada committed Jun 20, 2023
1 parent e763366 commit 3fc7fc4
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,3 +1000,60 @@ def test_clumerge_fields(
assert mds["directions"].shape == expect_shape
assert len(mds["angles"]) == tclu_i
assert len(mds["lengths"]) == tclu_i


def test_clumerge_exceptions(prng: Generator):
"""Test that clumerge() raises the expected exceptions."""
# Data item does not contain required field `unknown`
nd = 3
npts = prng.integers(10, high=101)
ds = {
"points": prng.random((npts, nd)),
"clusters": prng.integers(1, high=6, size=npts),
}
with pytest.raises(
ValueError,
match=re.escape("Data item does not contain required field `unknown`"),
):
clumerge(ds, fields=("clusters", "unknown"))

# "`{clusters_field}` must contain integer types
nd = 4
npts = prng.integers(10, high=101)
ds = {"points": prng.random((npts, nd)), "clusters": prng.random(npts)}
with pytest.raises(
ValueError,
match=re.escape("`clusters` must contain integer types"),
):
clumerge(ds)

# Data item contains fields with different sizes (npts != npts / 2)
nd = 2
npts = prng.integers(10, high=101)
ds = {
"points": prng.random((npts, nd)),
"clusters": prng.integers(1, high=6, size=npts // 2),
}
with pytest.raises(
ValueError,
match=r"Data item contains fields with different sizes \([0-9]+ != [0-9]+\)",
):
clumerge(ds)

# Dimension mismatch in field `points`
nd1 = 2
nd2 = 3
npts = prng.integers(10, high=101)
ds1 = {
"points": prng.random((npts, nd1)),
"clusters": prng.integers(1, high=6, size=npts),
}
ds2 = {
"points": prng.random((npts, nd2)),
"clusters": prng.integers(1, high=6, size=npts),
}
with pytest.raises(
ValueError,
match=re.escape("Dimension mismatch in field `points`"),
):
clumerge(ds1, ds2)

0 comments on commit 3fc7fc4

Please sign in to comment.