Skip to content

Commit

Permalink
add test cases to test files and make edition to make sure the behavi…
Browse files Browse the repository at this point in the history
…or of the test pass.
  • Loading branch information
stevenhua0320 committed Aug 12, 2024
1 parent 5be09d5 commit 95fc07e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/diffpy/srmise/dataclusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ def setdata(self, x, y, res):
self.x = x
self.y = y
self.res = res
if x.size > 0 and res == 0:
raise ValueError("Make trivial clustering, please make positive resolution.")
# If x sequence size is empty, set the object into Initialized state.
if x.size == 0 and res == 0:
self.data_order = np.array([])
Expand Down
85 changes: 84 additions & 1 deletion src/diffpy/srmise/tests/test_dataclusters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from copy import copy

import pytest
import numpy as np

from diffpy.srmise.dataclusters import DataClusters
Expand Down Expand Up @@ -32,3 +32,86 @@ def test___eq__():
print(f"not-equal test failed on {attr_key}")
assert not expected == actual
attributes.update({attr_key: reset})

# In the set data test, we test for these cases.
# (1) x and y are non-empty array values, and res is positive (the most generic case)
# (2) x and y are non-empty array values, and res is 0 (will produce a msg that makes trivial clustering)
# (3) x and y are non-empty array values, and res is negative (will produce a ValueError,
# msg = please enter a non-negative res value)
# (4, 5) One of x and y is empty array, and res is positive
# (produce ValueError & msg "Sequences x and y must have the same length.", something like that)
# (6) Both x and y are empty array, and res is zero.

@pytest.mark.parametrize(
"inputs, expected",
[
(
# case (1)
{
"input_x": np.array([1, 2, 3]),
"input_y": np.array([3, 2, 1]),
"input_res": 4,
},
DataClusters(np.array([1, 2, 3]), np.array([3, 2, 1]), 4),
),
(
# case (6)
{
"input_x": np.array([]),
"input_y": np.array([]),
"input_res": 0,
},
DataClusters(np.array([]), np.array([]), 0),
),
],
)
def test_set_data(inputs, expected):
actual = DataClusters(x=inputs["input_x"], y=inputs["input_y"], res=inputs["input_res"])
assert actual == expected


@pytest.mark.parametrize(
"inputs, msg",
[
(
# case (4)
{
"input_x": np.array([]),
"input_y": np.array([3, 2]),
"input_res": 4,
},
"Sequences x and y must have the same length.",
),
(
# case (5)
{
"input_x": np.array([1, 2]),
"input_y": np.array([]),
"input_res": 4,
},
"Sequences x and y must have the same length.",
),
(
# case (3)
{
"input_x": np.array([1]),
"input_y": np.array([3]),
"input_res": -1,
},
"Resolution res must be non-negative.",
),
(
# case (2)
{
"input_x": np.array([1, 2, 3]),
"input_y": np.array([3, 2, 1]),
"input_res": 0,
},
"Make trivial clustering, please make positive resolution.",
),
],
)
def test_set_data_order_bad(inputs, msg):
with pytest.raises(ValueError, match=msg):
DataClusters(x=inputs["input_x"], y=inputs["input_y"], res=inputs["input_res"])

0 comments on commit 95fc07e

Please sign in to comment.