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