Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Apr 11, 2022
1 parent 58c14d1 commit a1b6a9f
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions tests/test_json_compat.py
Expand Up @@ -14,14 +14,15 @@
"""

import ujson
import json as pjson
import itertools as it
import json as pjson
from collections import defaultdict

import ujson

JSON_IMPLS = {
'ujson': ujson,
'pjson': pjson,
"ujson": ujson,
"pjson": pjson,
}


Expand Down Expand Up @@ -54,14 +55,14 @@ def test_dumps_compatability():

# Define the data we will test
# data = {'a': [1, 2, 3, named_product]}
data = {'a': [1, 2, 3]}
data = {"a": [1, 2, 3]}

# Define the parameters we will test
NULL_CHAR = '\x00'
UTF_SURROGATE0000 = '\udc80'
UTF_SURROGATE1024 = '\udfff'
NULL_CHAR = "\x00"
UTF_SURROGATE0000 = "\udc80"
UTF_SURROGATE1024 = "\udfff"
param_basis = {
'indent': [
"indent": [
# -1,
# -2,
# ' ',
Expand All @@ -73,19 +74,19 @@ def test_dumps_compatability():
UTF_SURROGATE0000,
# UTF_SURROGATE1024,
],
'ensure_ascii': [False],
"ensure_ascii": [False],
# 'ensure_ascii': [True, False, None],
# 'sort_keys': [True, False, None],
# 'default': [None, str],
'module': list(JSON_IMPLS.keys()),
"module": list(JSON_IMPLS.keys()),
}
kwargs_keys = ['indent', 'default', 'ensure_ascii', 'sort_keys']
kwargs_keys = ["indent", "default", "ensure_ascii", "sort_keys"]
kwargs_keys = [k for k in kwargs_keys if k in param_basis]
param_grid = named_product(param_basis)
results = []
for params in param_grid:
params_key = pjson.dumps(params, default=str)
module = JSON_IMPLS[params['module']]
module = JSON_IMPLS[params["module"]]
kwargs = {k: params[k] for k in kwargs_keys if k in params}
try:
result = module.dumps(data, **kwargs)
Expand All @@ -95,18 +96,18 @@ def test_dumps_compatability():
else:
error = 0
row = {
'params_key': params_key,
"params_key": params_key,
**params,
'data': data,
'result': result,
'error': error,
"data": data,
"result": result,
"error": error,
}
results.append(row)

print(pjson.dumps(results, indent=' ', default=repr))
print(pjson.dumps(results, indent=" ", default=repr))

def grouper(row):
return tuple([(k, row[k]) for k in kwargs_keys])
return tuple((k, row[k]) for k in kwargs_keys)

grouped_results = group_items(results, key=grouper)

Expand All @@ -115,20 +116,20 @@ def grouper(row):

for group_key, group in grouped_results.items():
assert len(group) == 2
module_to_row = {r['module']: r for r in group}
module_to_row = {r["module"]: r for r in group}
assert len(module_to_row) == 2

ujson_row = module_to_row['ujson']
pjson_row = module_to_row['pjson']
ujson_row = module_to_row["ujson"]
pjson_row = module_to_row["pjson"]

if ujson_row['error'] and pjson_row['error']:
if ujson_row["error"] and pjson_row["error"]:
# Both implementations errored
agree_keys.append(group_key)
else:
# Check if the results from all implementations are the same
agree_keys.append(group_key)
u_result = ujson_row['result']
p_result = pjson_row['result']
u_result = ujson_row["result"]
p_result = pjson_row["result"]

try:
p_val = pjson.loads(p_result)
Expand All @@ -142,19 +143,20 @@ def grouper(row):

if p_val != u_val:
import difflib
print(f'Disagree on {group_key}')
print(' * p_result = {!r}'.format(p_result))
print(' * u_result = {!r}'.format(u_result))
print(''.join(list(difflib.ndiff([str(p_val)], [str(u_val)]))))

print(f"Disagree on {group_key}")
print(f" * p_result = {p_result!r}")
print(f" * u_result = {u_result!r}")
print("".join(list(difflib.ndiff([str(p_val)], [str(u_val)]))))
diagree_keys.append(group_key)
else:
agree_keys.append(group_key)

print('Num Agree: {}'.format(len(agree_keys)))
print('Num Disagree: {}'.format(len(diagree_keys)))
print(f"Num Agree: {len(agree_keys)}")
print(f"Num Disagree: {len(diagree_keys)}")


if __name__ == '__main__':
if __name__ == "__main__":
"""
CommandLine:
python ~/code/ultrajson/tests/test_json_compat.py
Expand Down

0 comments on commit a1b6a9f

Please sign in to comment.