Skip to content

Commit 8555739

Browse files
committed
Fix tests for new DebugToolbarJSONDecoder
1 parent a622823 commit 8555739

File tree

2 files changed

+7
-59
lines changed

2 files changed

+7
-59
lines changed

debug_toolbar/panels/sql/forms.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,6 @@
1212
from debug_toolbar.toolbar import DebugToolbar
1313

1414

15-
def _reconstruct_params(params):
16-
"""
17-
Reconstruct parameters that were encoded for JSON storage,
18-
especially binary data that was base64 encoded.
19-
"""
20-
if isinstance(params, list):
21-
return [_reconstruct_params(param) for param in params]
22-
elif isinstance(params, dict):
23-
if "__djdt_binary__" in params:
24-
# Reconstruct binary data from base64
25-
return base64.b64decode(params["__djdt_binary__"])
26-
else:
27-
return {key: _reconstruct_params(value) for key, value in params.items()}
28-
else:
29-
return params
30-
3115

3216
class SQLSelectForm(forms.Form):
3317
"""

tests/panels/test_sql_geodjango_fix.py

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import base64
66
import json
77

8-
from debug_toolbar.panels.sql.forms import _reconstruct_params
8+
from debug_toolbar.panels.sql.decoders import DebugToolbarJSONDecoder
99
from debug_toolbar.panels.sql.tracking import NormalCursorMixin
1010

1111
from ..base import BaseTestCase
@@ -33,7 +33,6 @@ class TestCursor(NormalCursorMixin):
3333
"""Test cursor that can be instantiated"""
3434

3535
def __init__(self):
36-
# Initialize with mock objects
3736
self.cursor = MockCursor()
3837
self.db = MockConnection()
3938
self.logger = MockLogger()
@@ -44,33 +43,21 @@ class GeoDjangoBinaryParameterTest(BaseTestCase):
4443

4544
def test_binary_parameter_encoding_decoding(self):
4645
"""Test that binary parameters are properly encoded and decoded"""
47-
# Create a test cursor with the _decode method
4846
cursor = TestCursor()
4947

5048
# Test binary data similar to GeoDjango EWKB geometry
5149
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)
5450
encoded = cursor._decode(binary_data)
5551

56-
# Should be marked as binary data
5752
self.assertIsInstance(encoded, dict)
5853
self.assertIn("__djdt_binary__", encoded)
5954

60-
# Should be base64 encoded
6155
expected_b64 = base64.b64encode(binary_data).decode("ascii")
6256
self.assertEqual(encoded["__djdt_binary__"], expected_b64)
6357

64-
# Test JSON serialization (what happens in tracking.py)
6558
json_params = json.dumps([encoded])
59+
reconstructed = json.loads(json_params, cls=DebugToolbarJSONDecoder)
6660

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
7461
self.assertEqual(len(reconstructed), 1)
7562
self.assertEqual(reconstructed[0], binary_data)
7663
self.assertIsInstance(reconstructed[0], bytes)
@@ -79,26 +66,19 @@ def test_mixed_parameter_types(self):
7966
"""Test that mixed parameter types are handled correctly"""
8067
cursor = TestCursor()
8168

82-
# Test with mixed types including binary data
8369
params = [
8470
"string_param",
8571
42,
86-
b"\x01\x02\x03", # binary data
72+
b"\x01\x02\x03",
8773
None,
8874
["nested", "list"],
8975
]
9076

91-
# Encode each parameter
9277
encoded_params = [cursor._decode(p) for p in params]
9378

94-
# Serialize to JSON
9579
json_str = json.dumps(encoded_params)
80+
reconstructed = json.loads(json_str, cls=DebugToolbarJSONDecoder)
9681

97-
# Parse and reconstruct
98-
parsed = json.loads(json_str)
99-
reconstructed = _reconstruct_params(parsed)
100-
101-
# Check each parameter
10282
self.assertEqual(reconstructed[0], "string_param") # string unchanged
10383
self.assertEqual(reconstructed[1], 42) # int unchanged
10484
self.assertEqual(reconstructed[2], b"\x01\x02\x03") # binary restored
@@ -109,65 +89,49 @@ def test_nested_binary_data(self):
10989
"""Test binary data nested in lists and dicts"""
11090
cursor = TestCursor()
11191

112-
# Test nested structures with binary data
11392
nested_params = [
11493
[b"\x01\x02", "string", b"\x03\x04"],
11594
{"key": b"\x05\x06", "other": "value"},
11695
]
11796

118-
# Encode
11997
encoded = [cursor._decode(p) for p in nested_params]
12098

121-
# Serialize and parse
12299
json_str = json.dumps(encoded)
123-
parsed = json.loads(json_str)
124-
reconstructed = _reconstruct_params(parsed)
100+
reconstructed = json.loads(json_str, cls=DebugToolbarJSONDecoder)
125101

126-
# Check nested list
127102
self.assertEqual(reconstructed[0][0], b"\x01\x02")
128103
self.assertEqual(reconstructed[0][1], "string")
129104
self.assertEqual(reconstructed[0][2], b"\x03\x04")
130105

131-
# Check nested dict
132106
self.assertEqual(reconstructed[1]["key"], b"\x05\x06")
133107
self.assertEqual(reconstructed[1]["other"], "value")
134108

135109
def test_empty_binary_data(self):
136110
"""Test handling of empty binary data"""
137111
cursor = TestCursor()
138112

139-
# Test empty bytes
140113
empty_bytes = b""
141114
encoded = cursor._decode(empty_bytes)
142115

143-
# Should still be marked as binary
144116
self.assertIsInstance(encoded, dict)
145117
self.assertIn("__djdt_binary__", encoded)
146118

147-
# Reconstruct
148119
json_str = json.dumps([encoded])
149-
parsed = json.loads(json_str)
150-
reconstructed = _reconstruct_params(parsed)
120+
reconstructed = json.loads(json_str, cls=DebugToolbarJSONDecoder)
151121

152122
self.assertEqual(reconstructed[0], empty_bytes)
153123

154124
def test_bytearray_support(self):
155125
"""Test that bytearray is also handled as binary data"""
156126
cursor = TestCursor()
157127

158-
# Test bytearray
159128
byte_array = bytearray(b"\x01\x02\x03\x04")
160129
encoded = cursor._decode(byte_array)
161130

162-
# Should be marked as binary
163131
self.assertIn("__djdt_binary__", encoded)
164132

165-
# Reconstruct (should become bytes, not bytearray)
166133
json_str = json.dumps([encoded])
167-
parsed = json.loads(json_str)
168-
reconstructed = _reconstruct_params(parsed)
134+
reconstructed = json.loads(json_str, cls=DebugToolbarJSONDecoder)
169135

170-
# Should be equal in content (bytes vs bytearray comparison works)
171136
self.assertEqual(reconstructed[0], byte_array)
172-
# Should be bytes type after reconstruction
173137
self.assertIsInstance(reconstructed[0], bytes)

0 commit comments

Comments
 (0)