Skip to content

Add test coverage to ensure that SQL tracker wrappers are applied only once to database cursors #1478

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

Merged
merged 4 commits into from
Jul 20, 2021
Merged
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
16 changes: 16 additions & 0 deletions tests/panels/test_sql.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import os
import unittest
from unittest.mock import patch

import django
from django.contrib.auth.models import User
Expand All @@ -10,6 +11,7 @@
from django.shortcuts import render
from django.test.utils import override_settings

import debug_toolbar.panels.sql.tracking as sql_tracking
from debug_toolbar import settings as dt_settings

from ..base import BaseTestCase
Expand Down Expand Up @@ -61,6 +63,20 @@ def test_recording_chunked_cursor(self):
# ensure query was logged
self.assertEqual(len(self.panel._queries), 1)

@patch("debug_toolbar.panels.sql.tracking.state", wraps=sql_tracking.state)
def test_cursor_wrapper_singleton(self, mock_state):
list(User.objects.all())

# ensure that cursor wrapping is applied only once
self.assertEqual(mock_state.Wrapper.call_count, 1)

@patch("debug_toolbar.panels.sql.tracking.state", wraps=sql_tracking.state)
def test_chunked_cursor_wrapper_singleton(self, mock_state):
list(User.objects.all().iterator())

# ensure that cursor wrapping is applied only once
self.assertEqual(mock_state.Wrapper.call_count, 1)

def test_generate_server_timing(self):
self.assertEqual(len(self.panel._queries), 0)

Expand Down