-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Binary parameter handling for GeoDjango #2169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eduzen
wants to merge
7
commits into
django-commons:main
Choose a base branch
from
eduzen:dev/GeoDjango-423
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
db28e81
Binary parameter handling for GeoDjango
5205a50
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f8e2f67
Update the checklist for: have added an item to the Pending section o…
f3ac881
[pre-commit.ci] pre-commit autoupdate (#2170)
pre-commit-ci[bot] a622823
Add DebugToolBarJSONDecoder class for better organization of code
eduzen 8555739
Fix tests for new DebugToolbarJSONDecoder
eduzen 457879b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
""" | ||
Tests for GeoDjango binary parameter handling fix | ||
""" | ||
|
||
import base64 | ||
import json | ||
|
||
from debug_toolbar.panels.sql.forms import _reconstruct_params | ||
from debug_toolbar.panels.sql.tracking import NormalCursorMixin | ||
|
||
from ..base import BaseTestCase | ||
|
||
|
||
class MockCursor: | ||
"""Mock cursor for testing""" | ||
|
||
|
||
class MockConnection: | ||
"""Mock database connection for testing""" | ||
|
||
vendor = "postgresql" | ||
alias = "default" | ||
|
||
|
||
class MockLogger: | ||
"""Mock logger for testing""" | ||
|
||
def record(self, **kwargs): | ||
pass | ||
|
||
|
||
class TestCursor(NormalCursorMixin): | ||
"""Test cursor that can be instantiated""" | ||
|
||
def __init__(self): | ||
# Initialize with mock objects | ||
self.cursor = MockCursor() | ||
self.db = MockConnection() | ||
self.logger = MockLogger() | ||
|
||
|
||
class GeoDjangoBinaryParameterTest(BaseTestCase): | ||
"""Test cases for GeoDjango binary parameter handling""" | ||
|
||
def test_binary_parameter_encoding_decoding(self): | ||
"""Test that binary parameters are properly encoded and decoded""" | ||
# Create a test cursor with the _decode method | ||
cursor = TestCursor() | ||
|
||
# Test binary data similar to GeoDjango EWKB geometry | ||
binary_data = b"\x01\x01\x00\x00\x20\xe6\x10\x00\x00\xff\xfe\xfd" | ||
|
||
# Test encoding (what happens when query is logged) | ||
encoded = cursor._decode(binary_data) | ||
|
||
# Should be marked as binary data | ||
self.assertIsInstance(encoded, dict) | ||
self.assertIn("__djdt_binary__", encoded) | ||
|
||
# Should be base64 encoded | ||
expected_b64 = base64.b64encode(binary_data).decode("ascii") | ||
self.assertEqual(encoded["__djdt_binary__"], expected_b64) | ||
|
||
# Test JSON serialization (what happens in tracking.py) | ||
json_params = json.dumps([encoded]) | ||
|
||
# Test parsing back from JSON | ||
parsed = json.loads(json_params) | ||
|
||
# Test reconstruction (what happens in forms.py) | ||
reconstructed = _reconstruct_params(parsed) | ||
|
||
# Should recover original binary data | ||
self.assertEqual(len(reconstructed), 1) | ||
self.assertEqual(reconstructed[0], binary_data) | ||
self.assertIsInstance(reconstructed[0], bytes) | ||
|
||
def test_mixed_parameter_types(self): | ||
"""Test that mixed parameter types are handled correctly""" | ||
cursor = TestCursor() | ||
|
||
# Test with mixed types including binary data | ||
params = [ | ||
"string_param", | ||
42, | ||
b"\x01\x02\x03", # binary data | ||
None, | ||
["nested", "list"], | ||
] | ||
|
||
# Encode each parameter | ||
encoded_params = [cursor._decode(p) for p in params] | ||
|
||
# Serialize to JSON | ||
json_str = json.dumps(encoded_params) | ||
|
||
# Parse and reconstruct | ||
parsed = json.loads(json_str) | ||
reconstructed = _reconstruct_params(parsed) | ||
|
||
# Check each parameter | ||
self.assertEqual(reconstructed[0], "string_param") # string unchanged | ||
self.assertEqual(reconstructed[1], 42) # int unchanged | ||
self.assertEqual(reconstructed[2], b"\x01\x02\x03") # binary restored | ||
self.assertIsNone(reconstructed[3]) # None unchanged | ||
self.assertEqual(reconstructed[4], ["nested", "list"]) # list unchanged | ||
|
||
def test_nested_binary_data(self): | ||
"""Test binary data nested in lists and dicts""" | ||
cursor = TestCursor() | ||
|
||
# Test nested structures with binary data | ||
nested_params = [ | ||
[b"\x01\x02", "string", b"\x03\x04"], | ||
{"key": b"\x05\x06", "other": "value"}, | ||
] | ||
|
||
# Encode | ||
encoded = [cursor._decode(p) for p in nested_params] | ||
|
||
# Serialize and parse | ||
json_str = json.dumps(encoded) | ||
parsed = json.loads(json_str) | ||
reconstructed = _reconstruct_params(parsed) | ||
|
||
# Check nested list | ||
self.assertEqual(reconstructed[0][0], b"\x01\x02") | ||
self.assertEqual(reconstructed[0][1], "string") | ||
self.assertEqual(reconstructed[0][2], b"\x03\x04") | ||
|
||
# Check nested dict | ||
self.assertEqual(reconstructed[1]["key"], b"\x05\x06") | ||
self.assertEqual(reconstructed[1]["other"], "value") | ||
|
||
def test_empty_binary_data(self): | ||
"""Test handling of empty binary data""" | ||
cursor = TestCursor() | ||
|
||
# Test empty bytes | ||
empty_bytes = b"" | ||
encoded = cursor._decode(empty_bytes) | ||
|
||
# Should still be marked as binary | ||
self.assertIsInstance(encoded, dict) | ||
self.assertIn("__djdt_binary__", encoded) | ||
|
||
# Reconstruct | ||
json_str = json.dumps([encoded]) | ||
parsed = json.loads(json_str) | ||
reconstructed = _reconstruct_params(parsed) | ||
|
||
self.assertEqual(reconstructed[0], empty_bytes) | ||
|
||
def test_bytearray_support(self): | ||
"""Test that bytearray is also handled as binary data""" | ||
cursor = TestCursor() | ||
|
||
# Test bytearray | ||
byte_array = bytearray(b"\x01\x02\x03\x04") | ||
encoded = cursor._decode(byte_array) | ||
|
||
# Should be marked as binary | ||
self.assertIn("__djdt_binary__", encoded) | ||
|
||
# Reconstruct (should become bytes, not bytearray) | ||
json_str = json.dumps([encoded]) | ||
parsed = json.loads(json_str) | ||
reconstructed = _reconstruct_params(parsed) | ||
|
||
# Should be equal in content (bytes vs bytearray comparison works) | ||
self.assertEqual(reconstructed[0], byte_array) | ||
# Should be bytes type after reconstruction | ||
self.assertIsInstance(reconstructed[0], bytes) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think this would be improved by making use of the
json.loads(..., cls)
argument?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand you correctly, @tim-schilling, you're suggesting that we create our own DebugToolbarJSONDecoder and use it like this: json.loads(query["params"], cls=DebugToolbarJSONDecoder), so we can encapsulate all the base64 logic inside of
_reconstruct_params
in custom JSON decoder class?