Skip to content

Commit 30f43d7

Browse files
NO-SNOW: Refactor test_np_unique into a class (#3943)
1 parent 5ac5b71 commit 30f43d7

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed

tests/integ/modin/test_numpy.py

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -57,37 +57,44 @@ def test_np_may_share_memory():
5757
assert not np.may_share_memory(snow_df_A, native_df_A)
5858

5959

60-
@sql_count_checker(query_count=2)
61-
def test_np_unique():
62-
# tests np.unique usage as seen in
63-
# scikit-learn/sklearn/metrics/_ranking.py::average_precision_score
64-
# and other places in the scikit-learn library
65-
y_true_np = np.array([0, 0, 1, 1])
66-
y_true_snow = pd.Series([0, 0, 1, 1])
67-
res_np = np.unique(y_true_np)
68-
res_snow = np.unique(y_true_snow)
69-
assert (res_np == res_snow).all()
70-
71-
y_true_np_2d = np.array([[1, 2, 5, 6], [1, 2, 3, 4]])
72-
y_true_snow_2d = pd.DataFrame({"a": [1, 2, 5, 6], "b": [1, 2, 3, 4]})
73-
res_np = np.unique(y_true_np_2d)
74-
res_snow = np.unique(y_true_snow_2d)
75-
assert (res_np == res_snow).all()
76-
77-
# Verify that numpy throws type errors when we return NotImplemented
78-
# when using optional parameters
79-
with pytest.raises(TypeError):
80-
np.unique(y_true_snow_2d, return_index=True)
81-
with pytest.raises(TypeError):
82-
np.unique(y_true_snow_2d, return_inverse=True)
83-
with pytest.raises(TypeError):
84-
np.unique(y_true_snow_2d, return_counts=True)
85-
with pytest.raises(TypeError):
86-
np.unique(y_true_snow_2d, axis=1)
87-
with pytest.raises(TypeError):
88-
np.unique(y_true_snow_2d, equal_nan=False)
89-
with pytest.raises(TypeError):
90-
np.unique(y_true_snow_2d, sorted=False)
60+
class TestUnique:
61+
@sql_count_checker(query_count=1)
62+
def test_np_unique(self):
63+
# tests np.unique usage as seen in
64+
# scikit-learn/sklearn/metrics/_ranking.py::average_precision_score
65+
# and other places in the scikit-learn library
66+
numpy_ar = np.array([0, 0, 1, 1])
67+
snow_ar = pd.Series([0, 0, 1, 1])
68+
numpy_res = np.unique(numpy_ar)
69+
snow_res = np.unique(snow_ar)
70+
assert (numpy_res == snow_res).all()
71+
72+
@sql_count_checker(query_count=1)
73+
def test_np_unique_2d(self):
74+
numpy_ar_2d = np.array([[1, 2, 5, 6], [1, 2, 3, 4]])
75+
snow_ar_2d = pd.DataFrame({"a": [1, 2, 5, 6], "b": [1, 2, 3, 4]})
76+
numpy_res = np.unique(numpy_ar_2d)
77+
snow_res = np.unique(snow_ar_2d)
78+
assert (numpy_res == snow_res).all()
79+
80+
@pytest.mark.parametrize(
81+
"kwargs",
82+
[
83+
{"return_index": True},
84+
{"return_inverse": True},
85+
{"return_counts": True},
86+
{"axis": 1},
87+
{"equal_nan": False},
88+
{"sorted": False},
89+
],
90+
)
91+
@sql_count_checker(query_count=0)
92+
def test_np_unique_neg(self, kwargs):
93+
snow_ar_2d = pd.DataFrame({"a": [1, 2, 5, 6], "b": [1, 2, 3, 4]})
94+
# Verify that numpy throws type errors when we return NotImplemented
95+
# when using optional parameters
96+
with pytest.raises(TypeError):
97+
np.unique(snow_ar_2d, **kwargs)
9198

9299

93100
def test_full_like():

0 commit comments

Comments
 (0)