Skip to content

Commit 32aeafd

Browse files
authored
TST: use one-class pattern in json extension tests (pandas-dev#56510)
1 parent 4763b91 commit 32aeafd

File tree

1 file changed

+134
-46
lines changed

1 file changed

+134
-46
lines changed

pandas/tests/extension/json/test_json.py

+134-46
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import operator
33
import sys
44

5+
import numpy as np
56
import pytest
67

78
import pandas as pd
@@ -13,6 +14,10 @@
1314
make_data,
1415
)
1516

17+
# We intentionally don't run base.BaseSetitemTests because pandas'
18+
# internals has trouble setting sequences of values into scalar positions.
19+
unhashable = pytest.mark.xfail(reason="Unhashable")
20+
1621

1722
@pytest.fixture
1823
def dtype():
@@ -73,24 +78,14 @@ def data_for_grouping():
7378
)
7479

7580

76-
class BaseJSON:
77-
pass
78-
79-
80-
class TestDtype(BaseJSON, base.BaseDtypeTests):
81-
pass
82-
83-
84-
class TestInterface(BaseJSON, base.BaseInterfaceTests):
81+
class TestJSONArray(base.ExtensionTests):
8582
@pytest.mark.xfail(
8683
reason="comparison method not implemented for JSONArray (GH-37867)"
8784
)
8885
def test_contains(self, data):
8986
# GH-37867
9087
super().test_contains(data)
9188

92-
93-
class TestConstructors(BaseJSON, base.BaseConstructorsTests):
9489
@pytest.mark.xfail(reason="not implemented constructor from dtype")
9590
def test_from_dtype(self, data):
9691
# construct from our dtype & string dtype
@@ -129,8 +124,6 @@ def test_series_constructor_scalar_with_index(self, data, dtype):
129124
finally:
130125
sys.setrecursionlimit(rec_limit)
131126

132-
133-
class TestReshaping(BaseJSON, base.BaseReshapingTests):
134127
@pytest.mark.xfail(reason="Different definitions of NA")
135128
def test_stack(self):
136129
"""
@@ -146,16 +139,6 @@ def test_unstack(self, data, index):
146139
# this matches otherwise
147140
return super().test_unstack(data, index)
148141

149-
150-
class TestGetitem(BaseJSON, base.BaseGetitemTests):
151-
pass
152-
153-
154-
class TestIndex(BaseJSON, base.BaseIndexTests):
155-
pass
156-
157-
158-
class TestMissing(BaseJSON, base.BaseMissingTests):
159142
@pytest.mark.xfail(reason="Setting a dict as a scalar")
160143
def test_fillna_series(self):
161144
"""We treat dictionaries as a mapping in fillna, not a scalar."""
@@ -166,15 +149,6 @@ def test_fillna_frame(self):
166149
"""We treat dictionaries as a mapping in fillna, not a scalar."""
167150
super().test_fillna_frame()
168151

169-
170-
unhashable = pytest.mark.xfail(reason="Unhashable")
171-
172-
173-
class TestReduce(base.BaseReduceTests):
174-
pass
175-
176-
177-
class TestMethods(BaseJSON, base.BaseMethodsTests):
178152
@unhashable
179153
def test_value_counts(self, all_data, dropna):
180154
super().test_value_counts(all_data, dropna)
@@ -240,8 +214,6 @@ def test_equals_same_data_different_object(
240214
request.applymarker(mark)
241215
super().test_equals_same_data_different_object(data)
242216

243-
244-
class TestCasting(BaseJSON, base.BaseCastingTests):
245217
@pytest.mark.xfail(reason="failing on np.array(self, dtype=str)")
246218
def test_astype_str(self):
247219
"""This currently fails in NumPy on np.array(self, dtype=str) with
@@ -250,12 +222,6 @@ def test_astype_str(self):
250222
"""
251223
super().test_astype_str()
252224

253-
254-
# We intentionally don't run base.BaseSetitemTests because pandas'
255-
# internals has trouble setting sequences of values into scalar positions.
256-
257-
258-
class TestGroupby(BaseJSON, base.BaseGroupbyTests):
259225
@unhashable
260226
def test_groupby_extension_transform(self):
261227
"""
@@ -295,25 +261,147 @@ def test_groupby_extension_no_sort(self):
295261
"""
296262
super().test_groupby_extension_no_sort()
297263

298-
299-
class TestArithmeticOps(BaseJSON, base.BaseArithmeticOpsTests):
300264
def test_arith_frame_with_scalar(self, data, all_arithmetic_operators, request):
301265
if len(data[0]) != 1:
302266
mark = pytest.mark.xfail(reason="raises in coercing to Series")
303267
request.applymarker(mark)
304268
super().test_arith_frame_with_scalar(data, all_arithmetic_operators)
305269

306-
307-
class TestComparisonOps(BaseJSON, base.BaseComparisonOpsTests):
308270
def test_compare_array(self, data, comparison_op, request):
309271
if comparison_op.__name__ in ["eq", "ne"]:
310272
mark = pytest.mark.xfail(reason="Comparison methods not implemented")
311273
request.applymarker(mark)
312274
super().test_compare_array(data, comparison_op)
313275

276+
@pytest.mark.xfail(reason="ValueError: Must have equal len keys and value")
277+
def test_setitem_loc_scalar_mixed(self, data):
278+
super().test_setitem_loc_scalar_mixed(data)
279+
280+
@pytest.mark.xfail(reason="ValueError: Must have equal len keys and value")
281+
def test_setitem_loc_scalar_multiple_homogoneous(self, data):
282+
super().test_setitem_loc_scalar_multiple_homogoneous(data)
283+
284+
@pytest.mark.xfail(reason="ValueError: Must have equal len keys and value")
285+
def test_setitem_iloc_scalar_mixed(self, data):
286+
super().test_setitem_iloc_scalar_mixed(data)
287+
288+
@pytest.mark.xfail(reason="ValueError: Must have equal len keys and value")
289+
def test_setitem_iloc_scalar_multiple_homogoneous(self, data):
290+
super().test_setitem_iloc_scalar_multiple_homogoneous(data)
291+
292+
@pytest.mark.parametrize(
293+
"mask",
294+
[
295+
np.array([True, True, True, False, False]),
296+
pd.array([True, True, True, False, False], dtype="boolean"),
297+
pd.array([True, True, True, pd.NA, pd.NA], dtype="boolean"),
298+
],
299+
ids=["numpy-array", "boolean-array", "boolean-array-na"],
300+
)
301+
def test_setitem_mask(self, data, mask, box_in_series, request):
302+
if box_in_series:
303+
mark = pytest.mark.xfail(
304+
reason="cannot set using a list-like indexer with a different length"
305+
)
306+
request.applymarker(mark)
307+
elif not isinstance(mask, np.ndarray):
308+
mark = pytest.mark.xfail(reason="Issues unwanted DeprecationWarning")
309+
request.applymarker(mark)
310+
super().test_setitem_mask(data, mask, box_in_series)
311+
312+
def test_setitem_mask_raises(self, data, box_in_series, request):
313+
if not box_in_series:
314+
mark = pytest.mark.xfail(reason="Fails to raise")
315+
request.applymarker(mark)
316+
317+
super().test_setitem_mask_raises(data, box_in_series)
318+
319+
@pytest.mark.xfail(
320+
reason="cannot set using a list-like indexer with a different length"
321+
)
322+
def test_setitem_mask_boolean_array_with_na(self, data, box_in_series):
323+
super().test_setitem_mask_boolean_array_with_na(data, box_in_series)
324+
325+
@pytest.mark.parametrize(
326+
"idx",
327+
[[0, 1, 2], pd.array([0, 1, 2], dtype="Int64"), np.array([0, 1, 2])],
328+
ids=["list", "integer-array", "numpy-array"],
329+
)
330+
def test_setitem_integer_array(self, data, idx, box_in_series, request):
331+
if box_in_series:
332+
mark = pytest.mark.xfail(
333+
reason="cannot set using a list-like indexer with a different length"
334+
)
335+
request.applymarker(mark)
336+
super().test_setitem_integer_array(data, idx, box_in_series)
337+
338+
@pytest.mark.xfail(reason="list indices must be integers or slices, not NAType")
339+
@pytest.mark.parametrize(
340+
"idx, box_in_series",
341+
[
342+
([0, 1, 2, pd.NA], False),
343+
pytest.param(
344+
[0, 1, 2, pd.NA], True, marks=pytest.mark.xfail(reason="GH-31948")
345+
),
346+
(pd.array([0, 1, 2, pd.NA], dtype="Int64"), False),
347+
(pd.array([0, 1, 2, pd.NA], dtype="Int64"), False),
348+
],
349+
ids=["list-False", "list-True", "integer-array-False", "integer-array-True"],
350+
)
351+
def test_setitem_integer_with_missing_raises(self, data, idx, box_in_series):
352+
super().test_setitem_integer_with_missing_raises(data, idx, box_in_series)
353+
354+
@pytest.mark.xfail(reason="Fails to raise")
355+
def test_setitem_scalar_key_sequence_raise(self, data):
356+
super().test_setitem_scalar_key_sequence_raise(data)
357+
358+
def test_setitem_with_expansion_dataframe_column(self, data, full_indexer, request):
359+
if "full_slice" in request.node.name:
360+
mark = pytest.mark.xfail(reason="slice is not iterable")
361+
request.applymarker(mark)
362+
super().test_setitem_with_expansion_dataframe_column(data, full_indexer)
363+
364+
@pytest.mark.xfail(reason="slice is not iterable")
365+
def test_setitem_frame_2d_values(self, data):
366+
super().test_setitem_frame_2d_values(data)
367+
368+
@pytest.mark.xfail(
369+
reason="cannot set using a list-like indexer with a different length"
370+
)
371+
@pytest.mark.parametrize("setter", ["loc", None])
372+
def test_setitem_mask_broadcast(self, data, setter):
373+
super().test_setitem_mask_broadcast(data, setter)
374+
375+
@pytest.mark.xfail(
376+
reason="cannot set using a slice indexer with a different length"
377+
)
378+
def test_setitem_slice(self, data, box_in_series):
379+
super().test_setitem_slice(data, box_in_series)
380+
381+
@pytest.mark.xfail(reason="slice object is not iterable")
382+
def test_setitem_loc_iloc_slice(self, data):
383+
super().test_setitem_loc_iloc_slice(data)
384+
385+
@pytest.mark.xfail(reason="slice object is not iterable")
386+
def test_setitem_slice_mismatch_length_raises(self, data):
387+
super().test_setitem_slice_mismatch_length_raises(data)
388+
389+
@pytest.mark.xfail(reason="slice object is not iterable")
390+
def test_setitem_slice_array(self, data):
391+
super().test_setitem_slice_array(data)
392+
393+
@pytest.mark.xfail(reason="Fail to raise")
394+
def test_setitem_invalid(self, data, invalid_scalar):
395+
super().test_setitem_invalid(data, invalid_scalar)
396+
397+
@pytest.mark.xfail(reason="only integer scalar arrays can be converted")
398+
def test_setitem_2d_values(self, data):
399+
super().test_setitem_2d_values(data)
314400

315-
class TestPrinting(BaseJSON, base.BasePrintingTests):
316-
pass
401+
@pytest.mark.xfail(reason="data type 'json' not understood")
402+
@pytest.mark.parametrize("engine", ["c", "python"])
403+
def test_EA_types(self, engine, data, request):
404+
super().test_EA_types(engine, data, request)
317405

318406

319407
def custom_assert_series_equal(left, right, *args, **kwargs):

0 commit comments

Comments
 (0)