5
5
import base64
6
6
import json
7
7
8
- from debug_toolbar .panels .sql .forms import _reconstruct_params
8
+ from debug_toolbar .panels .sql .decoders import DebugToolbarJSONDecoder
9
9
from debug_toolbar .panels .sql .tracking import NormalCursorMixin
10
10
11
11
from ..base import BaseTestCase
@@ -33,7 +33,6 @@ class TestCursor(NormalCursorMixin):
33
33
"""Test cursor that can be instantiated"""
34
34
35
35
def __init__ (self ):
36
- # Initialize with mock objects
37
36
self .cursor = MockCursor ()
38
37
self .db = MockConnection ()
39
38
self .logger = MockLogger ()
@@ -44,33 +43,21 @@ class GeoDjangoBinaryParameterTest(BaseTestCase):
44
43
45
44
def test_binary_parameter_encoding_decoding (self ):
46
45
"""Test that binary parameters are properly encoded and decoded"""
47
- # Create a test cursor with the _decode method
48
46
cursor = TestCursor ()
49
47
50
48
# Test binary data similar to GeoDjango EWKB geometry
51
49
binary_data = b"\x01 \x01 \x00 \x00 \x20 \xe6 \x10 \x00 \x00 \xff \xfe \xfd "
52
-
53
- # Test encoding (what happens when query is logged)
54
50
encoded = cursor ._decode (binary_data )
55
51
56
- # Should be marked as binary data
57
52
self .assertIsInstance (encoded , dict )
58
53
self .assertIn ("__djdt_binary__" , encoded )
59
54
60
- # Should be base64 encoded
61
55
expected_b64 = base64 .b64encode (binary_data ).decode ("ascii" )
62
56
self .assertEqual (encoded ["__djdt_binary__" ], expected_b64 )
63
57
64
- # Test JSON serialization (what happens in tracking.py)
65
58
json_params = json .dumps ([encoded ])
59
+ reconstructed = json .loads (json_params , cls = DebugToolbarJSONDecoder )
66
60
67
- # Test parsing back from JSON
68
- parsed = json .loads (json_params )
69
-
70
- # Test reconstruction (what happens in forms.py)
71
- reconstructed = _reconstruct_params (parsed )
72
-
73
- # Should recover original binary data
74
61
self .assertEqual (len (reconstructed ), 1 )
75
62
self .assertEqual (reconstructed [0 ], binary_data )
76
63
self .assertIsInstance (reconstructed [0 ], bytes )
@@ -79,26 +66,19 @@ def test_mixed_parameter_types(self):
79
66
"""Test that mixed parameter types are handled correctly"""
80
67
cursor = TestCursor ()
81
68
82
- # Test with mixed types including binary data
83
69
params = [
84
70
"string_param" ,
85
71
42 ,
86
- b"\x01 \x02 \x03 " , # binary data
72
+ b"\x01 \x02 \x03 " ,
87
73
None ,
88
74
["nested" , "list" ],
89
75
]
90
76
91
- # Encode each parameter
92
77
encoded_params = [cursor ._decode (p ) for p in params ]
93
78
94
- # Serialize to JSON
95
79
json_str = json .dumps (encoded_params )
80
+ reconstructed = json .loads (json_str , cls = DebugToolbarJSONDecoder )
96
81
97
- # Parse and reconstruct
98
- parsed = json .loads (json_str )
99
- reconstructed = _reconstruct_params (parsed )
100
-
101
- # Check each parameter
102
82
self .assertEqual (reconstructed [0 ], "string_param" ) # string unchanged
103
83
self .assertEqual (reconstructed [1 ], 42 ) # int unchanged
104
84
self .assertEqual (reconstructed [2 ], b"\x01 \x02 \x03 " ) # binary restored
@@ -109,65 +89,49 @@ def test_nested_binary_data(self):
109
89
"""Test binary data nested in lists and dicts"""
110
90
cursor = TestCursor ()
111
91
112
- # Test nested structures with binary data
113
92
nested_params = [
114
93
[b"\x01 \x02 " , "string" , b"\x03 \x04 " ],
115
94
{"key" : b"\x05 \x06 " , "other" : "value" },
116
95
]
117
96
118
- # Encode
119
97
encoded = [cursor ._decode (p ) for p in nested_params ]
120
98
121
- # Serialize and parse
122
99
json_str = json .dumps (encoded )
123
- parsed = json .loads (json_str )
124
- reconstructed = _reconstruct_params (parsed )
100
+ reconstructed = json .loads (json_str , cls = DebugToolbarJSONDecoder )
125
101
126
- # Check nested list
127
102
self .assertEqual (reconstructed [0 ][0 ], b"\x01 \x02 " )
128
103
self .assertEqual (reconstructed [0 ][1 ], "string" )
129
104
self .assertEqual (reconstructed [0 ][2 ], b"\x03 \x04 " )
130
105
131
- # Check nested dict
132
106
self .assertEqual (reconstructed [1 ]["key" ], b"\x05 \x06 " )
133
107
self .assertEqual (reconstructed [1 ]["other" ], "value" )
134
108
135
109
def test_empty_binary_data (self ):
136
110
"""Test handling of empty binary data"""
137
111
cursor = TestCursor ()
138
112
139
- # Test empty bytes
140
113
empty_bytes = b""
141
114
encoded = cursor ._decode (empty_bytes )
142
115
143
- # Should still be marked as binary
144
116
self .assertIsInstance (encoded , dict )
145
117
self .assertIn ("__djdt_binary__" , encoded )
146
118
147
- # Reconstruct
148
119
json_str = json .dumps ([encoded ])
149
- parsed = json .loads (json_str )
150
- reconstructed = _reconstruct_params (parsed )
120
+ reconstructed = json .loads (json_str , cls = DebugToolbarJSONDecoder )
151
121
152
122
self .assertEqual (reconstructed [0 ], empty_bytes )
153
123
154
124
def test_bytearray_support (self ):
155
125
"""Test that bytearray is also handled as binary data"""
156
126
cursor = TestCursor ()
157
127
158
- # Test bytearray
159
128
byte_array = bytearray (b"\x01 \x02 \x03 \x04 " )
160
129
encoded = cursor ._decode (byte_array )
161
130
162
- # Should be marked as binary
163
131
self .assertIn ("__djdt_binary__" , encoded )
164
132
165
- # Reconstruct (should become bytes, not bytearray)
166
133
json_str = json .dumps ([encoded ])
167
- parsed = json .loads (json_str )
168
- reconstructed = _reconstruct_params (parsed )
134
+ reconstructed = json .loads (json_str , cls = DebugToolbarJSONDecoder )
169
135
170
- # Should be equal in content (bytes vs bytearray comparison works)
171
136
self .assertEqual (reconstructed [0 ], byte_array )
172
- # Should be bytes type after reconstruction
173
137
self .assertIsInstance (reconstructed [0 ], bytes )
0 commit comments