Skip to content

Commit 5205a50

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent db28e81 commit 5205a50

File tree

3 files changed

+46
-44
lines changed

3 files changed

+46
-44
lines changed

debug_toolbar/panels/sql/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import json
21
import base64
2+
import json
33

44
from django import forms
55
from django.core.exceptions import ValidationError

debug_toolbar/panels/sql/tracking.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import base64
12
import contextlib
23
import contextvars
34
import datetime
45
import json
5-
import base64
66
from time import perf_counter
77

88
import django.test.testcases
@@ -130,7 +130,7 @@ def _decode(self, param):
130130
# Handle binary data (e.g., GeoDjango EWKB geometry data)
131131
if isinstance(param, (bytes, bytearray)):
132132
# Mark as binary data for later reconstruction
133-
return {"__djdt_binary__": base64.b64encode(param).decode('ascii')}
133+
return {"__djdt_binary__": base64.b64encode(param).decode("ascii")}
134134

135135
# make sure datetime, date and time are converted to string by force_str
136136
CONVERT_TYPES = (datetime.datetime, datetime.date, datetime.time)
Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
22
Tests for GeoDjango binary parameter handling fix
33
"""
4-
import json
4+
55
import base64
6-
import unittest
6+
import json
77

88
from debug_toolbar.panels.sql.forms import _reconstruct_params
99
from debug_toolbar.panels.sql.tracking import NormalCursorMixin
@@ -13,23 +13,25 @@
1313

1414
class MockCursor:
1515
"""Mock cursor for testing"""
16-
pass
1716

1817

1918
class MockConnection:
2019
"""Mock database connection for testing"""
20+
2121
vendor = "postgresql"
2222
alias = "default"
2323

2424

2525
class MockLogger:
2626
"""Mock logger for testing"""
27+
2728
def record(self, **kwargs):
2829
pass
2930

3031

3132
class TestCursor(NormalCursorMixin):
3233
"""Test cursor that can be instantiated"""
34+
3335
def __init__(self):
3436
# Initialize with mock objects
3537
self.cursor = MockCursor()
@@ -44,30 +46,30 @@ def test_binary_parameter_encoding_decoding(self):
4446
"""Test that binary parameters are properly encoded and decoded"""
4547
# Create a test cursor with the _decode method
4648
cursor = TestCursor()
47-
49+
4850
# 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+
5153
# Test encoding (what happens when query is logged)
5254
encoded = cursor._decode(binary_data)
53-
55+
5456
# Should be marked as binary data
5557
self.assertIsInstance(encoded, dict)
5658
self.assertIn("__djdt_binary__", encoded)
57-
59+
5860
# Should be base64 encoded
59-
expected_b64 = base64.b64encode(binary_data).decode('ascii')
61+
expected_b64 = base64.b64encode(binary_data).decode("ascii")
6062
self.assertEqual(encoded["__djdt_binary__"], expected_b64)
61-
63+
6264
# Test JSON serialization (what happens in tracking.py)
6365
json_params = json.dumps([encoded])
64-
66+
6567
# Test parsing back from JSON
6668
parsed = json.loads(json_params)
67-
69+
6870
# Test reconstruction (what happens in forms.py)
6971
reconstructed = _reconstruct_params(parsed)
70-
72+
7173
# Should recover original binary data
7274
self.assertEqual(len(reconstructed), 1)
7375
self.assertEqual(reconstructed[0], binary_data)
@@ -76,96 +78,96 @@ def test_binary_parameter_encoding_decoding(self):
7678
def test_mixed_parameter_types(self):
7779
"""Test that mixed parameter types are handled correctly"""
7880
cursor = TestCursor()
79-
81+
8082
# Test with mixed types including binary data
8183
params = [
8284
"string_param",
8385
42,
84-
b'\x01\x02\x03', # binary data
86+
b"\x01\x02\x03", # binary data
8587
None,
8688
["nested", "list"],
8789
]
88-
90+
8991
# Encode each parameter
9092
encoded_params = [cursor._decode(p) for p in params]
91-
93+
9294
# Serialize to JSON
9395
json_str = json.dumps(encoded_params)
94-
96+
9597
# Parse and reconstruct
9698
parsed = json.loads(json_str)
9799
reconstructed = _reconstruct_params(parsed)
98-
100+
99101
# Check each parameter
100102
self.assertEqual(reconstructed[0], "string_param") # string unchanged
101103
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
103105
self.assertIsNone(reconstructed[3]) # None unchanged
104106
self.assertEqual(reconstructed[4], ["nested", "list"]) # list unchanged
105107

106108
def test_nested_binary_data(self):
107109
"""Test binary data nested in lists and dicts"""
108110
cursor = TestCursor()
109-
111+
110112
# Test nested structures with binary data
111113
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"},
114116
]
115-
117+
116118
# Encode
117119
encoded = [cursor._decode(p) for p in nested_params]
118-
120+
119121
# Serialize and parse
120122
json_str = json.dumps(encoded)
121123
parsed = json.loads(json_str)
122124
reconstructed = _reconstruct_params(parsed)
123-
125+
124126
# Check nested list
125-
self.assertEqual(reconstructed[0][0], b'\x01\x02')
127+
self.assertEqual(reconstructed[0][0], b"\x01\x02")
126128
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+
129131
# Check nested dict
130-
self.assertEqual(reconstructed[1]["key"], b'\x05\x06')
132+
self.assertEqual(reconstructed[1]["key"], b"\x05\x06")
131133
self.assertEqual(reconstructed[1]["other"], "value")
132134

133135
def test_empty_binary_data(self):
134136
"""Test handling of empty binary data"""
135137
cursor = TestCursor()
136-
138+
137139
# Test empty bytes
138-
empty_bytes = b''
140+
empty_bytes = b""
139141
encoded = cursor._decode(empty_bytes)
140-
142+
141143
# Should still be marked as binary
142144
self.assertIsInstance(encoded, dict)
143145
self.assertIn("__djdt_binary__", encoded)
144-
146+
145147
# Reconstruct
146148
json_str = json.dumps([encoded])
147149
parsed = json.loads(json_str)
148150
reconstructed = _reconstruct_params(parsed)
149-
151+
150152
self.assertEqual(reconstructed[0], empty_bytes)
151153

152154
def test_bytearray_support(self):
153155
"""Test that bytearray is also handled as binary data"""
154156
cursor = TestCursor()
155-
157+
156158
# Test bytearray
157-
byte_array = bytearray(b'\x01\x02\x03\x04')
159+
byte_array = bytearray(b"\x01\x02\x03\x04")
158160
encoded = cursor._decode(byte_array)
159-
161+
160162
# Should be marked as binary
161163
self.assertIn("__djdt_binary__", encoded)
162-
164+
163165
# Reconstruct (should become bytes, not bytearray)
164166
json_str = json.dumps([encoded])
165167
parsed = json.loads(json_str)
166168
reconstructed = _reconstruct_params(parsed)
167-
169+
168170
# Should be equal in content (bytes vs bytearray comparison works)
169171
self.assertEqual(reconstructed[0], byte_array)
170172
# Should be bytes type after reconstruction
171-
self.assertIsInstance(reconstructed[0], bytes)
173+
self.assertIsInstance(reconstructed[0], bytes)

0 commit comments

Comments
 (0)