From 9efa204dd8fe1defda825c142287ebabf7d4ecbc Mon Sep 17 00:00:00 2001 From: Tim Schilling Date: Wed, 11 Dec 2019 10:11:16 -0600 Subject: [PATCH 1/4] Test with postgresql and mariadb on multiple versions of Django. --- tox.ini | 49 +++++++++++-------------------------------------- 1 file changed, 11 insertions(+), 38 deletions(-) diff --git a/tox.ini b/tox.ini index be08e2083..f7d26a2e5 100644 --- a/tox.ini +++ b/tox.ini @@ -3,18 +3,21 @@ envlist = flake8 style readme - py{35,36,37}-dj111 - py{35,36,37,38}-dj22 - py{36,37,38}-dj30 - py{36,37,38}-djmaster - postgresql - mariadb + py{35,36,37}-dj111-sqlite + py{35,36,37,38}-dj22-sqlite + py{36,37,38}-dj30-sqlite + py{36,37,38}-djmaster-sqlite + py37-dj111-{postgresql,mariadb} + py{37,38}-dj{22,30}-{postgresql,mariadb} [testenv] deps = dj111: Django==1.11.* dj22: Django==2.2.* dj30: Django==3.0.* + sqlite: mock + postgresql: psycopg2-binary + mariadb: mysqlclient djmaster: https://github.com/django/django/archive/master.tar.gz coverage Jinja2 @@ -23,38 +26,8 @@ deps = sqlparse setenv = PYTHONPATH = {toxinidir} -whitelist_externals = make -pip_pre = True -commands = make coverage TEST_ARGS='{posargs:tests}' - -[testenv:postgresql] -deps = - Django==1.11.* - coverage - Jinja2 - html5lib - psycopg2-binary - selenium<4.0 - sqlparse -setenv = - PYTHONPATH = {toxinidir} - DJANGO_DATABASE_ENGINE = postgresql -whitelist_externals = make -pip_pre = True -commands = make coverage TEST_ARGS='{posargs:tests}' - -[testenv:mariadb] -deps = - Django==2.2.* - coverage - Jinja2 - html5lib - mysqlclient<1.4 - selenium<4.0 - sqlparse -setenv = - PYTHONPATH = {toxinidir} - DJANGO_DATABASE_ENGINE = mysql + postgresql: DJANGO_DATABASE_ENGINE = postgresql + mariadb: DJANGO_DATABASE_ENGINE = mysql whitelist_externals = make pip_pre = True commands = make coverage TEST_ARGS='{posargs:tests}' From 863664d5cbddcf9e48cfd4b5de2f7ba64f27246f Mon Sep 17 00:00:00 2001 From: tschilling Date: Thu, 19 Dec 2019 21:22:09 -0600 Subject: [PATCH 2/4] Handle postgres CursorDebugWrapper in debugsqlshell. --- debug_toolbar/management/commands/debugsqlshell.py | 11 ++++++++--- tests/commands/test_debugsqlshell.py | 11 ++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/debug_toolbar/management/commands/debugsqlshell.py b/debug_toolbar/management/commands/debugsqlshell.py index ea39f3e1c..a532ea5ba 100644 --- a/debug_toolbar/management/commands/debugsqlshell.py +++ b/debug_toolbar/management/commands/debugsqlshell.py @@ -2,12 +2,17 @@ import sqlparse from django.core.management.commands.shell import Command # noqa -from django.db.backends import utils as db_backends_utils +from django.db import connection + +if connection.vendor == "postgresql": + from django.db.backends.postgresql import base as base_module +else: + from django.db.backends import utils as base_module # 'debugsqlshell' is the same as the 'shell'. -class PrintQueryWrapper(db_backends_utils.CursorDebugWrapper): +class PrintQueryWrapper(base_module.CursorDebugWrapper): def execute(self, sql, params=()): start_time = time() try: @@ -20,4 +25,4 @@ def execute(self, sql, params=()): print("{} [{:.2f}ms]".format(formatted_sql, duration)) -db_backends_utils.CursorDebugWrapper = PrintQueryWrapper +base_module.CursorDebugWrapper = PrintQueryWrapper diff --git a/tests/commands/test_debugsqlshell.py b/tests/commands/test_debugsqlshell.py index 54cd248e0..9939c5ca9 100644 --- a/tests/commands/test_debugsqlshell.py +++ b/tests/commands/test_debugsqlshell.py @@ -3,15 +3,20 @@ from django.contrib.auth.models import User from django.core import management -from django.db.backends import utils as db_backends_utils +from django.db import connection from django.test import TestCase from django.test.utils import override_settings +if connection.vendor == "postgresql": + from django.db.backends.postgresql import base as base_module +else: + from django.db.backends import utils as base_module + @override_settings(DEBUG=True) class DebugSQLShellTestCase(TestCase): def setUp(self): - self.original_cursor_wrapper = db_backends_utils.CursorDebugWrapper + self.original_wrapper = base_module.CursorDebugWrapper # Since debugsqlshell monkey-patches django.db.backends.utils, we can # test it simply by loading it, without executing it. But we have to # undo the monkey-patch on exit. @@ -20,7 +25,7 @@ def setUp(self): management.load_command_class(app_name, command_name) def tearDown(self): - db_backends_utils.CursorDebugWrapper = self.original_cursor_wrapper + base_module.CursorDebugWrapper = self.original_wrapper def test_command(self): original_stdout, sys.stdout = sys.stdout, io.StringIO() From 59af30fcdc6f475aa66ac8b226dfb6bfa4e711da Mon Sep 17 00:00:00 2001 From: tschilling Date: Mon, 13 Jan 2020 19:28:09 -0600 Subject: [PATCH 3/4] Expand travis tests. --- .travis.yml | 70 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 18 deletions(-) diff --git a/.travis.yml b/.travis.yml index 357fe6041..ba33fff97 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,35 +7,67 @@ matrix: - env: TOXENV=style - env: TOXENV=readme - python: 3.5 - env: TOXENV=py35-dj111 + env: TOXENV=py35-dj111-sqlite - python: 3.6 - env: TOXENV=py36-dj111 + env: TOXENV=py36-dj111-sqlite - python: 3.7 - env: TOXENV=py37-dj111 + env: TOXENV=py37-dj111-sqlite - python: 3.5 - env: TOXENV=py35-dj22 + env: TOXENV=py35-dj22-sqlite - python: 3.6 - env: TOXENV=py36-dj22 + env: TOXENV=py36-dj22-sqlite - python: 3.7 - env: TOXENV=py37-dj22 + env: TOXENV=py37-dj22-sqlite - python: 3.8 - env: TOXENV=py38-dj22 + env: TOXENV=py38-dj22-sqlite - python: 3.6 - env: TOXENV=py36-dj30 + env: TOXENV=py36-dj30-sqlite - python: 3.7 - env: TOXENV=py37-dj30 + env: TOXENV=py37-dj30-sqlite - python: 3.8 - env: TOXENV=py38-dj30 + env: TOXENV=py38-dj30-sqlite - python: 3.6 - env: TOXENV=py36-djmaster + env: TOXENV=py36-djmaster-sqlite - python: 3.7 - env: TOXENV=py37-djmaster + env: TOXENV=py37-djmaster-sqlite - python: 3.8 - env: TOXENV=py38-djmaster - - env: TOXENV=postgresql + env: TOXENV=py38-djmaster-sqlite + - python: 3.7 + env: TOXENV=py37-dj111-postgresql + addons: + postgresql: "9.5" + - python: 3.8 + env: TOXENV=py38-dj22-postgresql + addons: + postgresql: "9.5" + - python: 3.8 + env: TOXENV=py38-dj30-postgresql addons: postgresql: "9.5" - - env: TOXENV=mariadb + - python: 3.7 + env: TOXENV=py37-dj111-mariadb + addons: + mariadb: "10.3" + script: + # working around https://travis-ci.community/t/mariadb-build-error-with-xenial/3160 + - mysql -u root -e "DROP USER IF EXISTS 'travis'@'%';" + - mysql -u root -e "CREATE USER 'travis'@'%';" + - mysql -u root -e "CREATE DATABASE debug_toolbar;" + - mysql -u root -e "GRANT ALL PRIVILEGES ON *.* TO 'travis'@'%';"; + - tox -v + - python: 3.8 + env: TOXENV=py38-dj22-mariadb + addons: + mariadb: "10.3" + script: + # working around https://travis-ci.community/t/mariadb-build-error-with-xenial/3160 + - mysql -u root -e "DROP USER IF EXISTS 'travis'@'%';" + - mysql -u root -e "CREATE USER 'travis'@'%';" + - mysql -u root -e "CREATE DATABASE debug_toolbar;" + - mysql -u root -e "GRANT ALL PRIVILEGES ON *.* TO 'travis'@'%';"; + - tox -v + - python: 3.8 + env: TOXENV=py38-dj30-mariadb addons: mariadb: "10.3" script: @@ -46,9 +78,11 @@ matrix: - mysql -u root -e "GRANT ALL PRIVILEGES ON *.* TO 'travis'@'%';"; - tox -v allow_failures: - - env: TOXENV=py36-djmaster - - env: TOXENV=py37-djmaster - - env: TOXENV=py38-djmaster + - env: TOXENV=py36-djmaster-sqlite + - env: TOXENV=py37-djmaster-sqlite + - env: TOXENV=py38-djmaster-sqlite + - env: TOXENV=py38-djmaster-postgresql + - env: TOXENV=py38-djmaster-mariadb install: - pip install tox codecov From 0caa826a971f5fbde7696f9520e5ef9439e7b500 Mon Sep 17 00:00:00 2001 From: Tim Schilling Date: Fri, 31 Jan 2020 10:01:32 -0600 Subject: [PATCH 4/4] Only use postgres CursorDebugWrapper with Django 3 --- debug_toolbar/management/commands/debugsqlshell.py | 3 ++- tests/commands/test_debugsqlshell.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/debug_toolbar/management/commands/debugsqlshell.py b/debug_toolbar/management/commands/debugsqlshell.py index a532ea5ba..78e09e27d 100644 --- a/debug_toolbar/management/commands/debugsqlshell.py +++ b/debug_toolbar/management/commands/debugsqlshell.py @@ -1,10 +1,11 @@ from time import time +import django import sqlparse from django.core.management.commands.shell import Command # noqa from django.db import connection -if connection.vendor == "postgresql": +if connection.vendor == "postgresql" and django.VERSION >= (3, 0, 0): from django.db.backends.postgresql import base as base_module else: from django.db.backends import utils as base_module diff --git a/tests/commands/test_debugsqlshell.py b/tests/commands/test_debugsqlshell.py index 9939c5ca9..9520d0dd8 100644 --- a/tests/commands/test_debugsqlshell.py +++ b/tests/commands/test_debugsqlshell.py @@ -1,13 +1,14 @@ import io import sys +import django from django.contrib.auth.models import User from django.core import management from django.db import connection from django.test import TestCase from django.test.utils import override_settings -if connection.vendor == "postgresql": +if connection.vendor == "postgresql" and django.VERSION >= (3, 0, 0): from django.db.backends.postgresql import base as base_module else: from django.db.backends import utils as base_module