Skip to content

Commit d3730a6

Browse files
committed
Fix tests for serializable changes with selenium.
The majority were difficulities with caching and settings.
1 parent f4ff5f4 commit d3730a6

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

tests/panels/test_history.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from debug_toolbar.store import get_store
99
from debug_toolbar.toolbar import DebugToolbar
1010

11-
from .. import settings as test_settings
1211
from ..base import BaseTestCase, IntegrationTestCase
1312

1413
rf = RequestFactory()
@@ -110,14 +109,17 @@ def test_history_headers(self):
110109
request_id = list(get_store().request_ids())[0]
111110
self.assertEqual(response.headers["djdt-request-id"], request_id)
112111

113-
@override_settings(
114-
DEBUG_TOOLBAR_CONFIG={"OBSERVE_REQUEST_CALLBACK": lambda request: False}
115-
)
116112
def test_history_headers_unobserved(self):
117113
"""Validate the headers aren't injected from the history panel."""
114+
with self.settings(
115+
DEBUG_TOOLBAR_CONFIG={"OBSERVE_REQUEST_CALLBACK": lambda request: False}
116+
):
117+
DebugToolbar.get_observe_request.cache_clear()
118+
response = self.client.get("/json_view/")
119+
self.assertNotIn("djdt-request-id", response.headers)
120+
# Clear it again to avoid conflicting with another test
121+
# Specifically, DebugToolbarLiveTestCase.test_ajax_refresh
118122
DebugToolbar.get_observe_request.cache_clear()
119-
response = self.client.get("/json_view/")
120-
self.assertNotIn("djdt-request-id", response.headers)
121123

122124
def test_history_sidebar(self):
123125
"""Validate the history sidebar view."""
@@ -145,7 +147,9 @@ def test_history_sidebar_includes_history(self):
145147
panel_keys,
146148
)
147149

148-
@override_settings(DEBUG_TOOLBAR_CONFIG={"RENDER_PANELS": False})
150+
@override_settings(
151+
DEBUG_TOOLBAR_CONFIG={"RENDER_PANELS": False, "RESULTS_CACHE_SIZE": 1}
152+
)
149153
def test_history_sidebar_expired_request_id(self):
150154
"""Validate the history sidebar view."""
151155
self.client.get("/json_view/")
@@ -158,8 +162,7 @@ def test_history_sidebar_expired_request_id(self):
158162
self.PANEL_KEYS,
159163
)
160164
# Make enough requests to unset the original
161-
for _i in range(test_settings.DEBUG_TOOLBAR_CONFIG["RESULTS_CACHE_SIZE"]):
162-
self.client.get("/json_view/")
165+
self.client.get("/json_view/")
163166

164167
# Querying old request_id should return in empty response
165168
data = {"request_id": request_id, "exclude_history": True}

tests/test_integration.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
import re
3-
import time
43
import unittest
54
from unittest.mock import patch
65

@@ -648,7 +647,7 @@ def test_basic_jinja(self):
648647
# This should be 2 templates rendered, including base.html See
649648
# JinjaTemplateTestCase.test_django_jinja2_parent_template_instrumented
650649
self.assertIn("Templates (1 rendered)", template_panel.text)
651-
self.assertIn("base.html", template_panel.text)
650+
self.assertIn("basic.jinja", template_panel.text)
652651

653652
@override_settings(
654653
DEBUG_TOOLBAR_CONFIG={
@@ -831,10 +830,13 @@ def test_ajax_refresh(self):
831830
make_ajax = self.selenium.find_element(By.ID, "click_for_ajax")
832831
make_ajax.click()
833832
# Need to wait until the ajax request is over and json_view is displayed on the toolbar
834-
time.sleep(2)
835-
history_panel = self.wait.until(
836-
lambda selenium: self.selenium.find_element(By.ID, "djdt-HistoryPanel")
833+
self.wait.until(
834+
lambda selenium: self.selenium.find_element(
835+
By.CSS_SELECTOR, "#djdt-HistoryPanel a.HistoryPanel small"
836+
).text
837+
== "/json_view/"
837838
)
839+
history_panel = self.selenium.find_element(By.ID, "djdt-HistoryPanel")
838840
self.assertNotIn("/ajax/", history_panel.text)
839841
self.assertIn("/json_view/", history_panel.text)
840842

tests/test_store.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,12 @@ def test_set(self):
6464
self.assertEqual(list(self.store.request_ids()), ["foo"])
6565

6666
def test_set_max_size(self):
67-
existing = self.store._config["RESULTS_CACHE_SIZE"]
68-
self.store._config["RESULTS_CACHE_SIZE"] = 1
69-
self.store.save_panel("foo", "foo.panel", "foo.value")
70-
self.store.save_panel("bar", "bar.panel", {"a": 1})
71-
self.assertEqual(list(self.store.request_ids()), ["bar"])
72-
self.assertEqual(self.store.panel("foo", "foo.panel"), {})
73-
self.assertEqual(self.store.panel("bar", "bar.panel"), {"a": 1})
74-
# Restore the existing config setting since this config is shared.
75-
self.store._config["RESULTS_CACHE_SIZE"] = existing
67+
with self.settings(DEBUG_TOOLBAR_CONFIG={"RESULTS_CACHE_SIZE": 1}):
68+
self.store.save_panel("foo", "foo.panel", "foo.value")
69+
self.store.save_panel("bar", "bar.panel", {"a": 1})
70+
self.assertEqual(list(self.store.request_ids()), ["bar"])
71+
self.assertEqual(self.store.panel("foo", "foo.panel"), {})
72+
self.assertEqual(self.store.panel("bar", "bar.panel"), {"a": 1})
7673

7774
def test_clear(self):
7875
self.store.save_panel("bar", "bar.panel", {"a": 1})

0 commit comments

Comments
 (0)