Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/django_devbar/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def _record(sql, params, duration):
query_log = _query_log.get()
query_log.append(
{
"sql": sql,
"sql": str(sql),
"duration": round(duration, 2),
"params_hash": params_hash,
}
Expand Down
28 changes: 28 additions & 0 deletions tests/test_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
from django_devbar.tracker import SQLTruncator, truncate_sql


class UnhashableSQL:
"""Mock for psycopg3's sql.Composed which is not hashable."""

def __init__(self, value):
self.value = value

def __str__(self):
return self.value

def __hash__(self):
raise TypeError("unhashable type: 'Composed'")


class TestTracker:
def test_reset_clears_stats(self):
tracker._query_count.set(5)
Expand Down Expand Up @@ -174,6 +187,21 @@ def mock_execute(*args):
assert stats["queries"][1]["is_duplicate"] is True
assert stats["queries"][2]["is_duplicate"] is False

def test_unhashable_sql_object(self):
"""Regression test for issue #27: psycopg3 Composed SQL objects are not hashable."""
tracker.reset()

def mock_execute(*args):
return "result"

composed_sql = UnhashableSQL("SELECT * FROM users WHERE id = %s")
tracker.tracking_wrapper(mock_execute, composed_sql, [1], False, {})
tracker.tracking_wrapper(mock_execute, composed_sql, [1], False, {})

# Should not raise TypeError: unhashable type
stats = tracker.get_stats()
assert stats["count"] == 2


class TestSQLTruncator:
def test_short_queries_unchanged(self):
Expand Down