Skip to content

Commit f7052f5

Browse files
moinkluckyvs1
authored andcommitted
TST: Add match=msg to all pytest.raises in pandas/tests/io/json (pandas-dev#38705)
1 parent 5b10ed6 commit f7052f5

File tree

3 files changed

+118
-43
lines changed

3 files changed

+118
-43
lines changed

pandas/tests/io/json/test_json_table_schema.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,11 @@ def test_to_json_categorical_index(self):
439439
"ignore:an integer is required (got type float)*:DeprecationWarning"
440440
)
441441
def test_date_format_raises(self):
442-
with pytest.raises(ValueError):
442+
msg = (
443+
"Trying to write with `orient='table'` and `date_format='epoch'`. Table "
444+
"Schema requires dates to be formatted with `date_format='iso'`"
445+
)
446+
with pytest.raises(ValueError, match=msg):
443447
self.df.to_json(orient="table", date_format="epoch")
444448

445449
# others work

pandas/tests/io/json/test_pandas.py

+8-13
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ def __str__(self) -> str:
591591

592592
# the same with multiple columns threw segfaults
593593
df_mixed = DataFrame({"A": [binthing], "B": [1]}, columns=["A", "B"])
594-
with pytest.raises(OverflowError):
594+
with pytest.raises(OverflowError, match=msg):
595595
df_mixed.to_json()
596596

597597
# default_handler should resolve exceptions for non-string types
@@ -1259,19 +1259,14 @@ def test_to_json_large_numbers(self, bigNum):
12591259
def test_read_json_large_numbers(self, bigNum):
12601260
# GH20599
12611261

1262-
series = Series(bigNum, dtype=object, index=["articleId"])
1263-
json = '{"articleId":' + str(bigNum) + "}"
1264-
with pytest.raises(ValueError):
1265-
json = StringIO(json)
1266-
result = read_json(json)
1267-
tm.assert_series_equal(series, result)
1262+
json = StringIO('{"articleId":' + str(bigNum) + "}")
1263+
msg = r"Value is too small|Value is too big"
1264+
with pytest.raises(ValueError, match=msg):
1265+
read_json(json)
12681266

1269-
df = DataFrame(bigNum, dtype=object, index=["articleId"], columns=[0])
1270-
json = '{"0":{"articleId":' + str(bigNum) + "}}"
1271-
with pytest.raises(ValueError):
1272-
json = StringIO(json)
1273-
result = read_json(json)
1274-
tm.assert_frame_equal(df, result)
1267+
json = StringIO('{"0":{"articleId":' + str(bigNum) + "}}")
1268+
with pytest.raises(ValueError, match=msg):
1269+
read_json(json)
12751270

12761271
def test_read_json_large_numbers2(self):
12771272
# GH18842

pandas/tests/io/json/test_ujson.py

+105-29
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,11 @@ def test_double_precision(self):
241241
def test_invalid_double_precision(self, invalid_val):
242242
double_input = 30.12345678901234567890
243243
expected_exception = ValueError if isinstance(invalid_val, int) else TypeError
244-
245-
with pytest.raises(expected_exception):
244+
msg = (
245+
r"Invalid value '.*' for option 'double_precision', max is '15'|"
246+
r"an integer is required \(got type "
247+
)
248+
with pytest.raises(expected_exception, match=msg):
246249
ujson.encode(double_input, double_precision=invalid_val)
247250

248251
def test_encode_string_conversion2(self):
@@ -447,13 +450,13 @@ class O1:
447450
decoded_input.member = O2()
448451
decoded_input.member.member = decoded_input
449452

450-
with pytest.raises(OverflowError):
453+
with pytest.raises(OverflowError, match="Maximum recursion level reached"):
451454
ujson.encode(decoded_input)
452455

453456
def test_decode_jibberish(self):
454457
jibberish = "fdsa sda v9sa fdsa"
455-
456-
with pytest.raises(ValueError):
458+
msg = "Unexpected character found when decoding 'false'"
459+
with pytest.raises(ValueError, match=msg):
457460
ujson.decode(jibberish)
458461

459462
@pytest.mark.parametrize(
@@ -466,12 +469,13 @@ def test_decode_jibberish(self):
466469
],
467470
)
468471
def test_decode_broken_json(self, broken_json):
469-
with pytest.raises(ValueError):
472+
msg = "Expected object or value"
473+
with pytest.raises(ValueError, match=msg):
470474
ujson.decode(broken_json)
471475

472476
@pytest.mark.parametrize("too_big_char", ["[", "{"])
473477
def test_decode_depth_too_big(self, too_big_char):
474-
with pytest.raises(ValueError):
478+
with pytest.raises(ValueError, match="Reached object decoding depth limit"):
475479
ujson.decode(too_big_char * (1024 * 1024))
476480

477481
@pytest.mark.parametrize(
@@ -485,13 +489,27 @@ def test_decode_depth_too_big(self, too_big_char):
485489
],
486490
)
487491
def test_decode_bad_string(self, bad_string):
488-
with pytest.raises(ValueError):
492+
msg = (
493+
"Unexpected character found when decoding|"
494+
"Unmatched ''\"' when when decoding 'string'"
495+
)
496+
with pytest.raises(ValueError, match=msg):
489497
ujson.decode(bad_string)
490498

491-
@pytest.mark.parametrize("broken_json", ['{{1337:""}}', '{{"key":"}', "[[[true"])
492-
def test_decode_broken_json_leak(self, broken_json):
499+
@pytest.mark.parametrize(
500+
"broken_json, err_msg",
501+
[
502+
(
503+
'{{1337:""}}',
504+
"Key name of object must be 'string' when decoding 'object'",
505+
),
506+
('{{"key":"}', "Unmatched ''\"' when when decoding 'string'"),
507+
("[[[true", "Unexpected character found when decoding array value (2)"),
508+
],
509+
)
510+
def test_decode_broken_json_leak(self, broken_json, err_msg):
493511
for _ in range(1000):
494-
with pytest.raises(ValueError):
512+
with pytest.raises(ValueError, match=re.escape(err_msg)):
495513
ujson.decode(broken_json)
496514

497515
@pytest.mark.parametrize(
@@ -503,7 +521,12 @@ def test_decode_broken_json_leak(self, broken_json):
503521
],
504522
)
505523
def test_decode_invalid_dict(self, invalid_dict):
506-
with pytest.raises(ValueError):
524+
msg = (
525+
"Key name of object must be 'string' when decoding 'object'|"
526+
"No ':' found when decoding object value|"
527+
"Expected object or value"
528+
)
529+
with pytest.raises(ValueError, match=msg):
507530
ujson.decode(invalid_dict)
508531

509532
@pytest.mark.parametrize(
@@ -567,7 +590,7 @@ def test_dumps_ints_larger_than_maxsize(self, bigNum):
567590
assert str(bigNum) == encoding
568591

569592
# GH20599
570-
with pytest.raises(ValueError):
593+
with pytest.raises(ValueError, match="Value is too big"):
571594
assert ujson.loads(encoding) == bigNum
572595

573596
@pytest.mark.parametrize(
@@ -789,21 +812,70 @@ def test_0d_array(self):
789812
ujson.encode(np.array(1))
790813

791814
@pytest.mark.parametrize(
792-
"bad_input,exc_type,kwargs",
815+
"bad_input,exc_type,err_msg,kwargs",
793816
[
794-
([{}, []], ValueError, {}),
795-
([42, None], TypeError, {}),
796-
([["a"], 42], ValueError, {}),
797-
([42, {}, "a"], TypeError, {}),
798-
([42, ["a"], 42], ValueError, {}),
799-
(["a", "b", [], "c"], ValueError, {}),
800-
([{"a": "b"}], ValueError, {"labelled": True}),
801-
({"a": {"b": {"c": 42}}}, ValueError, {"labelled": True}),
802-
([{"a": 42, "b": 23}, {"c": 17}], ValueError, {"labelled": True}),
817+
(
818+
[{}, []],
819+
ValueError,
820+
"nesting not supported for object or variable length dtypes",
821+
{},
822+
),
823+
(
824+
[42, None],
825+
TypeError,
826+
"int() argument must be a string, a bytes-like object or a number, "
827+
"not 'NoneType'",
828+
{},
829+
),
830+
(
831+
[["a"], 42],
832+
ValueError,
833+
"Cannot decode multidimensional arrays with variable length elements "
834+
"to numpy",
835+
{},
836+
),
837+
(
838+
[42, {}, "a"],
839+
TypeError,
840+
"int() argument must be a string, a bytes-like object or a number, "
841+
"not 'dict'",
842+
{},
843+
),
844+
(
845+
[42, ["a"], 42],
846+
ValueError,
847+
"invalid literal for int() with base 10: 'a'",
848+
{},
849+
),
850+
(
851+
["a", "b", [], "c"],
852+
ValueError,
853+
"nesting not supported for object or variable length dtypes",
854+
{},
855+
),
856+
(
857+
[{"a": "b"}],
858+
ValueError,
859+
"Cannot decode multidimensional arrays with variable length elements "
860+
"to numpy",
861+
{"labelled": True},
862+
),
863+
(
864+
{"a": {"b": {"c": 42}}},
865+
ValueError,
866+
"labels only supported up to 2 dimensions",
867+
{"labelled": True},
868+
),
869+
(
870+
[{"a": 42, "b": 23}, {"c": 17}],
871+
ValueError,
872+
"cannot reshape array of size 3 into shape (2,1)",
873+
{"labelled": True},
874+
),
803875
],
804876
)
805-
def test_array_numpy_except(self, bad_input, exc_type, kwargs):
806-
with pytest.raises(exc_type):
877+
def test_array_numpy_except(self, bad_input, exc_type, err_msg, kwargs):
878+
with pytest.raises(exc_type, match=re.escape(err_msg)):
807879
ujson.decode(ujson.dumps(bad_input), numpy=True, **kwargs)
808880

809881
def test_array_numpy_labelled(self):
@@ -1034,7 +1106,11 @@ def test_datetime_index(self):
10341106
],
10351107
)
10361108
def test_decode_invalid_array(self, invalid_arr):
1037-
with pytest.raises(ValueError):
1109+
msg = (
1110+
"Expected object or value|Trailing data|"
1111+
"Unexpected character found when decoding array value"
1112+
)
1113+
with pytest.raises(ValueError, match=msg):
10381114
ujson.decode(invalid_arr)
10391115

10401116
@pytest.mark.parametrize("arr", [[], [31337]])
@@ -1049,18 +1125,18 @@ def test_decode_extreme_numbers(self, extreme_num):
10491125
"too_extreme_num", ["9223372036854775808", "-90223372036854775809"]
10501126
)
10511127
def test_decode_too_extreme_numbers(self, too_extreme_num):
1052-
with pytest.raises(ValueError):
1128+
with pytest.raises(ValueError, match="Value is too big|Value is too small"):
10531129
ujson.decode(too_extreme_num)
10541130

10551131
def test_decode_with_trailing_whitespaces(self):
10561132
assert {} == ujson.decode("{}\n\t ")
10571133

10581134
def test_decode_with_trailing_non_whitespaces(self):
1059-
with pytest.raises(ValueError):
1135+
with pytest.raises(ValueError, match="Trailing data"):
10601136
ujson.decode("{}\n\t a")
10611137

10621138
def test_decode_array_with_big_int(self):
1063-
with pytest.raises(ValueError):
1139+
with pytest.raises(ValueError, match="Value is too big"):
10641140
ujson.loads("[18446098363113800555]")
10651141

10661142
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)