11import itertools
22import orjson
3- from dictdatabase import utils , io_unsafe , byte_codes
3+ from dictdatabase import utils , byte_codes
44
55
66def test_seek_index_through_value_bytes (use_test_dir ):
77 v = b'{"a": 1, "b": {}}'
8- vc = b'{"a":1,"b":{}}'
9-
108 assert utils .seek_index_through_value_bytes (v , 5 ) == 7
119 assert utils .seek_index_through_value_bytes (v , 6 ) == 7
12- assert utils .seek_index_through_value_bytes (vc , 5 ) == 6
13-
1410 assert utils .seek_index_through_value_bytes (v , 13 ) == 16
11+ vc = b'{"a":1,"b":{}}'
12+ assert utils .seek_index_through_value_bytes (vc , 5 ) == 6
1513 assert utils .seek_index_through_value_bytes (vc , 11 ) == 13
16-
17-
1814 n = b'{"a": 1234, "b": {"c": 2}}'
1915 assert utils .seek_index_through_value_bytes (n , 5 ) == 10
2016 assert utils .seek_index_through_value_bytes (n , 6 ) == 10
2117
2218
23-
24-
25-
26- def load_with_orjson (bytes , key ):
27- # print("load with orjson", bytes)
28- return orjson .loads (bytes )[key ]
29-
30-
31- def load_with_seeker (bytes , key ):
32- key_bytes = f"\" { key } \" :" .encode ()
33- a_val_start = bytes .find (key_bytes ) + len (key_bytes )
34- if bytes [a_val_start ] == byte_codes .SPACE :
35- a_val_start += 1
36- a_val_end = utils .seek_index_through_value_bytes (bytes , a_val_start )
37- return orjson .loads (bytes [a_val_start :a_val_end ])
38-
39-
4019def test_seek_index_through_value_bytes_2 (use_test_dir ):
20+ def load_with_orjson (bytes , key ):
21+ return orjson .loads (bytes )[key ]
4122
42-
43- def orjson_dump_with_indent (data ):
44- return orjson .dumps (data , option = orjson .OPT_INDENT_2 | orjson .OPT_SORT_KEYS )
45-
46- def orjson_dump_without_indent (data ):
47- return orjson .dumps (data , option = orjson .OPT_SORT_KEYS )
48-
49- orjson_dump_settings = [orjson_dump_with_indent , orjson_dump_without_indent ]
23+ def load_with_seeker (bytes , key ):
24+ key_bytes = f"\" { key } \" :" .encode ()
25+ a_val_start = bytes .find (key_bytes ) + len (key_bytes )
26+ if bytes [a_val_start ] == byte_codes .SPACE :
27+ a_val_start += 1
28+ a_val_end = utils .seek_index_through_value_bytes (bytes , a_val_start )
29+ return orjson .loads (bytes [a_val_start :a_val_end ])
5030
5131 values = [
5232 # Lists
5333 [],
34+ [{}],
35+ ["" ],
36+ [1 ],
5437 [1 , 2 , 3 ],
55- ["xs" , "value" , "c" ],
38+ ["xs" , - 123.3 , "c" ],
5639 [1 , "xs" , 2 , "value" , 3 , "c" ],
5740 [1 , "xs" , 2 , "value" , 3 , "c" , [1 , 2 , 3 ], [1 , 2 , 3 ], [1 , 2 , 3 ]],
5841 [{}, {}, {}],
@@ -61,19 +44,19 @@ def orjson_dump_without_indent(data):
6144 [{"xs" : 1 }, {"value" : 2 }, {"c" : 3 }, {"xs" : 1 }, {"value" : 2 }, {"c" : 3 }, [1 , 2 , 3 ], [1 , 2 , 3 ], [1 , 2 , 3 ]],
6245 # Dicts
6346 {},
47+ {"" : "" },
48+ {"x" : []},
6449 {"xs" : 1 },
6550 {"xs" : 1 , "value" : 2 },
66- {"xs" : 1 , "value" : 2 , "c" : 3 },
67- {"xs" : []},
68- {"xs" : [], "value" : []},
51+ {"xs" : [], "value" : {}},
6952 {"xs" : - 3.3 , "value" : "" },
7053 # Numbers
7154 1 ,
7255 1234 ,
7356 1.3 ,
74- - 1.3 ,
7557 32.3 ,
7658 0 ,
59+ - 1.3 ,
7760 - 0 ,
7861 # Strings
7962 "" ,
@@ -90,25 +73,14 @@ def orjson_dump_without_indent(data):
9073 "\" \" \\ " ,
9174 "\" \\ \" " ,
9275 "\\ \" \" " ,
76+ # Booleans
77+ True ,
78+ None ,
79+ False ,
9380 ]
9481
95- for dumper , v1 , v2 in itertools .product (orjson_dump_settings , values , values ):
96-
97- obj = {"a" : v1 , "b" : v2 }
98-
99- json_bytes = dumper (obj )
100-
101-
102- a_from_orjson = load_with_orjson (json_bytes , "a" )
103- a_from_seeker = load_with_seeker (json_bytes , "a" )
104-
105- b_from_orjson = load_with_orjson (json_bytes , "b" )
106- b_from_seeker = load_with_seeker (json_bytes , "b" )
107-
108- # print("obj", obj)
109- # print("a_from_orjson", a_from_orjson)
110- # print("a_from_seeker", a_from_seeker)
111- assert a_from_orjson == a_from_seeker
112- # print("b_from_orjson", b_from_orjson)
113- # print("b_from_seeker", b_from_seeker)
114- assert b_from_orjson == b_from_seeker
82+ for indent , v1 , v2 in itertools .product ([False , True ], values , values ):
83+ option = orjson .OPT_SORT_KEYS | (orjson .OPT_INDENT_2 if indent else 0 )
84+ json_bytes = orjson .dumps ({"a" : v1 , "b" : v2 }, option = option )
85+ assert load_with_orjson (json_bytes , "a" ) == load_with_seeker (json_bytes , "a" )
86+ assert load_with_orjson (json_bytes , "b" ) == load_with_seeker (json_bytes , "b" )
0 commit comments