Skip to content

Fix SQL selected / SQL explain for gis queries #1426

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 3 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add Postgis to tests, attemt to add regression test
  • Loading branch information
jieter committed Oct 26, 2021
commit 41c0bb6d7663cd1a31f61f62b913ddd67b2671e8
8 changes: 8 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.db import models


Expand All @@ -23,3 +24,10 @@ class Binary(models.Model):

class PostgresJSON(models.Model):
field = JSONField()


if settings.USE_GIS:
from django.contrib.gis.db import models as gismodels

class Location(gismodels.Model):
point = gismodels.PointField()
14 changes: 8 additions & 6 deletions tests/panels/test_staticfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ def test_default_case(self):
)
self.assertEqual(self.panel.num_used, 0)
self.assertNotEqual(self.panel.num_found, 0)
self.assertEqual(
self.panel.get_staticfiles_apps(), ["django.contrib.admin", "debug_toolbar"]
)
expected_apps = ["django.contrib.admin", "debug_toolbar"]
if settings.USE_GIS:
expected_apps = ["django.contrib.gis"] + expected_apps
self.assertEqual(self.panel.get_staticfiles_apps(), expected_apps)
self.assertEqual(
self.panel.get_staticfiles_dirs(), finders.FileSystemFinder().locations
)
Expand Down Expand Up @@ -74,9 +75,10 @@ def test_finder_directory_does_not_exist(self):
)
self.assertEqual(self.panel.num_used, 0)
self.assertNotEqual(self.panel.num_found, 0)
self.assertEqual(
self.panel.get_staticfiles_apps(), ["django.contrib.admin", "debug_toolbar"]
)
expected_apps = ["django.contrib.admin", "debug_toolbar"]
if settings.USE_GIS:
expected_apps = ["django.contrib.gis"] + expected_apps
self.assertEqual(self.panel.get_staticfiles_apps(), expected_apps)
self.assertEqual(
self.panel.get_staticfiles_dirs(), finders.FileSystemFinder().locations
)
9 changes: 8 additions & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
"tests",
]

USE_GIS = os.getenv("DB_BACKEND") in ("postgis",)

if USE_GIS:
INSTALLED_APPS = ["django.contrib.gis"] + INSTALLED_APPS

MEDIA_URL = "/media/" # Avoids https://code.djangoproject.com/ticket/21451

MIDDLEWARE = [
Expand Down Expand Up @@ -82,7 +87,9 @@

DATABASES = {
"default": {
"ENGINE": "django.db.backends.%s" % os.getenv("DB_BACKEND", "sqlite3"),
"ENGINE": "django.{}db.backends.{}".format(
"contrib.gis." if USE_GIS else "", os.getenv("DB_BACKEND", "sqlite3")
),
"NAME": os.getenv("DB_NAME", ":memory:"),
"USER": os.getenv("DB_USER"),
"PASSWORD": os.getenv("DB_PASSWORD"),
Expand Down
24 changes: 24 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import json
import os
import re
import unittest

import django
import html5lib
from django.conf import settings
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from django.core import signing
from django.core.cache import cache
Expand All @@ -16,6 +18,7 @@
from debug_toolbar.forms import SignedDataForm
from debug_toolbar.middleware import DebugToolbarMiddleware, show_toolbar
from debug_toolbar.panels import Panel
from debug_toolbar.panels.sql.forms import SQLSelectForm
from debug_toolbar.toolbar import DebugToolbar

from .base import BaseTestCase, IntegrationTestCase
Expand Down Expand Up @@ -293,6 +296,27 @@ def test_sql_explain_checks_show_toolbar(self):
)
self.assertEqual(response.status_code, 404)

@unittest.skipUnless(settings.USE_GIS, "Test only valid with gis support")
def test_sql_explain_gis(self):
from django.contrib.gis.geos import GEOSGeometry
from .models import Location

db_table = Location._meta.db_table

url = "/__debug__/sql_explain/"
geom = GEOSGeometry("POLYGON((0 0, 0 1, 1 1, 0 0))")
data = {
"sql": f'SELECT "{db_table}"."point" FROM "{db_table}" WHERE "{db_table}"."point" @ {geom.hex} LIMIT 1',
"raw_sql": f'SELECT "{db_table}"."point" FROM "{db_table}" WHERE "{db_table}"."point" @ %s LIMIT 1',
"params": json.dumps([geom.hex]),
"alias": "default",
"duration": "0",
}
data["hash"] = SQLSelectForm().make_hash(data)

response = self.client.post(url, data=data)
self.assertEqual(response.status_code, 200)

@unittest.skipUnless(
connection.vendor == "postgresql", "Test valid only on PostgreSQL"
)
Expand Down
8 changes: 5 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ envlist =
docs
style
packaging
py{36,37}-dj{22,31,32}-{sqlite,postgresql,mysql}
py{38,39}-dj{22,31,32,40,main}-{sqlite,postgresql,mysql}
py{310}-dj{40,main}-{sqlite,postgresql,mysql}
py{36,37}-dj{22,31,32}-{sqlite,postgresql,postgis,mysql}
py{38,39}-dj{22,31,32,40,main}-{sqlite,postgresql,postgis,mysql}
py{310}-dj{40,main}-{sqlite,postgresql,postgis,mysql}

[testenv]
deps =
Expand All @@ -15,6 +15,7 @@ deps =
dj40: Django>=4.0a1,<4.1
sqlite: mock
postgresql: psycopg2-binary
postgis: psycopg2-binary
mysql: mysqlclient
djmain: https://github.com/django/django/archive/main.tar.gz
coverage
Expand Down Expand Up @@ -97,4 +98,5 @@ python =
DB_BACKEND =
mysql: mysql
postgresql: postgresql
postgis: postgresql
sqlite3: sqlite