Skip to content

Instantly share code, notes, and snippets.

@kingbuzzman
Last active May 13, 2020 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingbuzzman/9fe5470e31a421aa88b2a64e5447e147 to your computer and use it in GitHub Desktop.
Save kingbuzzman/9fe5470e31a421aa88b2a64e5447e147 to your computer and use it in GitHub Desktop.
Potential bug found in queryset when using Q() -- doubles up the counts by doing an extra join
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Stolen from: https://mlvin.xyz/django-single-file-project.html
import datetime
import inspect
import os
import sys
from types import ModuleType
import django
from django.conf import settings
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# The current name of the file, which will be the name of our app
APP_LABEL, _ = os.path.splitext(os.path.basename(os.path.abspath(__file__)))
# Migrations folder need to be created, and django needs to be told where it is
APP_MIGRATION_MODULE = '%s_migrations' % APP_LABEL
APP_MIGRATION_PATH = os.path.join(BASE_DIR, APP_MIGRATION_MODULE)
# Create the folder and a __init__.py if they don't exist
if not os.path.exists(APP_MIGRATION_PATH):
os.makedirs(APP_MIGRATION_PATH)
open(os.path.join(APP_MIGRATION_PATH, '__init__.py'), 'w').close()
# Hack to trick Django into thinking this file is actually a package
sys.modules[APP_LABEL] = sys.modules[__name__]
sys.modules[APP_LABEL].__path__ = [os.path.abspath(__file__)]
settings.configure(
DEBUG=True,
ROOT_URLCONF='%s.urls' % APP_LABEL,
MIDDLEWARE=(),
INSTALLED_APPS=[APP_LABEL],
MIGRATION_MODULES={APP_LABEL: APP_MIGRATION_MODULE},
SITE_ID=1,
DATABASES={
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
},
LOGGING={
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
'format': "%(levelname)s %(message)s",
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple',
}
},
'loggers': {
'django.db.backends': {'handlers': ['console'], 'level': 'DEBUG', 'propagate': False},
'django.db.backends.schema': {'level': 'ERROR'}, # Causes sql logs to duplicate -- really annoying
}
},
STATIC_URL='/static/'
)
django.setup()
from django.apps import apps # noqa: E402 isort:skip
# Setup the AppConfig so we don't have to add the app_label to all our models
def get_containing_app_config(module):
if module == '__main__':
return apps.get_app_config(APP_LABEL)
return apps._get_containing_app_config(module)
apps._get_containing_app_config = apps.get_containing_app_config
apps.get_containing_app_config = get_containing_app_config
# Your code below this line
# ##############################################################################
from django.db import models # noqa: E402 isort:skip
from django.test import TestCase # noqa: E402 isort:skip
class ModelA(models.Model):
somecriteria = models.CharField(max_length=50)
class ModelC(models.Model):
unimportant = models.CharField(max_length=50)
class ModelB(models.Model):
somerelation = models.ForeignKey(ModelA, on_delete=models.CASCADE)
m2m = models.ManyToManyField(
ModelC,
through='ModelBC'
)
class ModelBC(models.Model):
b = models.ForeignKey(ModelB, on_delete=models.CASCADE)
c = models.ForeignKey(ModelC, on_delete=models.CASCADE)
urlpatterns = []
class SimpleTestCase(TestCase):
def setUp(self):
a = ModelA.objects.create(somecriteria='1')
b = ModelB.objects.create(somerelation=a)
c1 = ModelC.objects.create(unimportant='1')
c2 = ModelC.objects.create(unimportant='2')
ModelBC.objects.create(b=b, c=c1)
ModelBC.objects.create(b=b, c=c2)
def test_sanity_check(self):
self.assertEqual(1, ModelA.objects.count())
self.assertEqual(1, ModelB.objects.count())
self.assertEqual(2, ModelC.objects.count())
self.assertEqual(2, ModelBC.objects.count())
def test_ok_query(self):
from django.db.models import Count
queryset = ModelB.objects.select_related('a').annotate(num_c=Count('modelbc__id')).values('num_c')
queryset = queryset.filter(modelbc__c__unimportant='1', somerelation__somecriteria='1')
self.assertEqual([{'num_c': 2}], list(queryset))
def test_wtf_query(self):
from django.db.models import Count, Q
queryset = ModelB.objects.select_related('a').annotate(num_c=Count('modelbc__id')).values('num_c')
queryset = queryset.filter(Q(modelbc__c__unimportant='1') | Q(somerelation__somecriteria='1'))
self.assertEqual([{'num_c': 2}], list(queryset))
# Your code above this line
# ##############################################################################
# Used so you can do 'from <name of file>.models import *'
models_module = ModuleType('%s.models' % (APP_LABEL))
tests_module = ModuleType('%s.tests' % (APP_LABEL))
urls_module = ModuleType('%s.urls' % (APP_LABEL))
urls_module.urlpatterns = urlpatterns
for variable_name, value in list(locals().items()):
# We are only interested in models
if inspect.isclass(value) and issubclass(value, models.Model):
setattr(models_module, variable_name, value)
# We are only interested in tests
if inspect.isclass(value) and issubclass(value, TestCase):
setattr(tests_module, variable_name, value)
# Setup the fake modules
sys.modules[models_module.__name__] = models_module
sys.modules[tests_module.__name__] = tests_module
sys.modules[urls_module.__name__] = urls_module
sys.modules[APP_LABEL].models = models_module
sys.modules[APP_LABEL].tests = tests_module
sys.modules[APP_LABEL].urls = urls_module
if __name__ == "__main__":
# Hack to fix tests
argv = [arg for arg in sys.argv if not arg.startswith('-')]
if len(argv) == 2 and argv[1] == 'test':
sys.argv.append(APP_LABEL)
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
else:
from django.core.wsgi import get_wsgi_application
get_wsgi_application()
@kingbuzzman
Copy link
Author

kingbuzzman commented May 13, 2020

In order to run the code above run the following:

pip install django==1.11
python testquery.py makemigrations
python testquery.py test
pip install django==2.2
python testquery.py test
pip install django==3
python testquery.py test

If you want the REAL copy and paste version:

docker run -it --rm python:3.7 bash -c '
curl -L https://gist.githubusercontent.com/kingbuzzman/9fe5470e31a421aa88b2a64e5447e147/raw/testquery.py > testquery.py
pip install django==1.11
python testquery.py makemigrations
python testquery.py test
pip install django==2.2
python testquery.py test
pip install django==3
python testquery.py test
'

"bug" reported @ https://code.djangoproject.com/ticket/31581#ticket

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment