Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrd committed Nov 20, 2022
1 parent 6074dd4 commit 3906389
Showing 1 changed file with 29 additions and 57 deletions.
86 changes: 29 additions & 57 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,41 @@
import itertools
import orjson
from dictdatabase import utils, io_unsafe, byte_codes
from dictdatabase import utils, byte_codes


def test_seek_index_through_value_bytes(use_test_dir):
v = b'{"a": 1, "b": {}}'
vc = b'{"a":1,"b":{}}'

assert utils.seek_index_through_value_bytes(v, 5) == 7
assert utils.seek_index_through_value_bytes(v, 6) == 7
assert utils.seek_index_through_value_bytes(vc, 5) == 6

assert utils.seek_index_through_value_bytes(v, 13) == 16
vc = b'{"a":1,"b":{}}'
assert utils.seek_index_through_value_bytes(vc, 5) == 6
assert utils.seek_index_through_value_bytes(vc, 11) == 13


n = b'{"a": 1234, "b": {"c": 2}}'
assert utils.seek_index_through_value_bytes(n, 5) == 10
assert utils.seek_index_through_value_bytes(n, 6) == 10





def load_with_orjson(bytes, key):
# print("load with orjson", bytes)
return orjson.loads(bytes)[key]


def load_with_seeker(bytes, key):
key_bytes = f"\"{key}\":".encode()
a_val_start = bytes.find(key_bytes) + len(key_bytes)
if bytes[a_val_start] == byte_codes.SPACE:
a_val_start += 1
a_val_end = utils.seek_index_through_value_bytes(bytes, a_val_start)
return orjson.loads(bytes[a_val_start:a_val_end])


def test_seek_index_through_value_bytes_2(use_test_dir):
def load_with_orjson(bytes, key):
return orjson.loads(bytes)[key]


def orjson_dump_with_indent(data):
return orjson.dumps(data, option=orjson.OPT_INDENT_2 | orjson.OPT_SORT_KEYS)

def orjson_dump_without_indent(data):
return orjson.dumps(data, option=orjson.OPT_SORT_KEYS)

orjson_dump_settings = [orjson_dump_with_indent, orjson_dump_without_indent]
def load_with_seeker(bytes, key):
key_bytes = f"\"{key}\":".encode()
a_val_start = bytes.find(key_bytes) + len(key_bytes)
if bytes[a_val_start] == byte_codes.SPACE:
a_val_start += 1
a_val_end = utils.seek_index_through_value_bytes(bytes, a_val_start)
return orjson.loads(bytes[a_val_start:a_val_end])

values = [
# Lists
[],
[{}],
[""],
[1],
[1, 2, 3],
["xs", "value", "c"],
["xs", -123.3, "c"],
[1, "xs", 2, "value", 3, "c"],
[1, "xs", 2, "value", 3, "c", [1, 2, 3], [1, 2, 3], [1, 2, 3]],
[{}, {}, {}],
Expand All @@ -61,19 +44,19 @@ def orjson_dump_without_indent(data):
[{"xs": 1}, {"value": 2}, {"c": 3}, {"xs": 1}, {"value": 2}, {"c": 3}, [1, 2, 3], [1, 2, 3], [1, 2, 3]],
# Dicts
{},
{"": ""},
{"x": []},
{"xs": 1},
{"xs": 1, "value": 2},
{"xs": 1, "value": 2, "c": 3},
{"xs": []},
{"xs": [], "value": []},
{"xs": [], "value": {}},
{"xs": -3.3, "value": ""},
# Numbers
1,
1234,
1.3,
-1.3,
32.3,
0,
-1.3,
-0,
# Strings
"",
Expand All @@ -90,25 +73,14 @@ def orjson_dump_without_indent(data):
"\"\"\\",
"\"\\\"",
"\\\"\"",
# Booleans
True,
None,
False,
]

for dumper, v1, v2 in itertools.product(orjson_dump_settings, values, values):

obj = {"a": v1, "b": v2}

json_bytes = dumper(obj)


a_from_orjson = load_with_orjson(json_bytes, "a")
a_from_seeker = load_with_seeker(json_bytes, "a")

b_from_orjson = load_with_orjson(json_bytes, "b")
b_from_seeker = load_with_seeker(json_bytes, "b")

# print("obj", obj)
# print("a_from_orjson", a_from_orjson)
# print("a_from_seeker", a_from_seeker)
assert a_from_orjson == a_from_seeker
# print("b_from_orjson", b_from_orjson)
# print("b_from_seeker", b_from_seeker)
assert b_from_orjson == b_from_seeker
for indent, v1, v2 in itertools.product([False, True], values, values):
option = orjson.OPT_SORT_KEYS | (orjson.OPT_INDENT_2 if indent else 0)
json_bytes = orjson.dumps({"a": v1, "b": v2}, option=option)
assert load_with_orjson(json_bytes, "a") == load_with_seeker(json_bytes, "a")
assert load_with_orjson(json_bytes, "b") == load_with_seeker(json_bytes, "b")

0 comments on commit 3906389

Please sign in to comment.