Skip to content

Commit

Permalink
Add test case for duplicate outer join (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
jieter committed Mar 14, 2018
1 parent ca135bb commit 4f7cf39
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
6 changes: 5 additions & 1 deletion tests/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ class CategoryQueryset(models.QuerySet):
class Category(models.Model):
name = models.CharField(max_length=255)
title = models.CharField(max_length=255)
slug = models.SlugField()
parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True)

i18n = TranslationField(fields=('name', 'title'))
i18n = TranslationField(fields=('name', 'title', 'slug'))

objects = CategoryQueryset.as_manager()

Expand All @@ -44,6 +46,8 @@ class Blog(models.Model):
category = models.ForeignKey(Category, null=True, blank=True, on_delete=models.CASCADE)
site = models.ForeignKey(Site, null=True, blank=True, on_delete=models.CASCADE)

is_active = models.BooleanField(default=True)

i18n = TranslationField(
fields=('title', 'body'),
required_languages=('nl', )
Expand Down
27 changes: 25 additions & 2 deletions tests/test_querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from modeltrans.translator import translate_model

from .app.models import Attribute, Blog, BlogAttr, Category, Site
from .utils import CreateTestModel, load_wiki
from .utils import CreateTestModel, debug_queryset, load_wiki


def key(queryset, key, sep=' '):
Expand Down Expand Up @@ -119,7 +119,14 @@ class FilterTest(TestCase):

@classmethod
def setUpTestData(self):
birds = Category.objects.create(name='Birds', name_nl='Vogels')
ornithurae = Category.objects.create(name='Ornithurae')
birds = Category.objects.create(
name='Birds',
name_nl='Vogels',
slug='birds',
slug_nl='vogels',
parent=ornithurae
)

for title, title_nl, category in FilterTest.data:
b = Blog.objects.create(title=title, i18n={'title_nl': title_nl})
Expand Down Expand Up @@ -207,6 +214,22 @@ def test_filter_Q_object(self):
b = Blog.objects.get(Q(title_i18n='Kikker'))
self.assertEqual(b.title, 'Frog')

def test_filter_Q_object_or(self):
def qs(slug, active=True):
return Blog.objects.filter(
Q(category__slug_i18n=slug) |
Q(category__parent__slug_i18n=slug),
is_active=active
)
with override('en'):
print('language = en')
debug_queryset(qs('birds'))

with override('nl'):
print('\nlanguage = nl')
debug_queryset(qs('birds'))
debug_queryset(qs('vogels'))

def test_filter_F_expression(self):
Blog.objects.create(title='foo', title_nl=20, title_fr=10)
Blog.objects.create(title='bar', title_nl=20, title_fr=30)
Expand Down
12 changes: 12 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import print_function

import json
import os

import sqlparse
from django.db import connection

from .app.models import Blog, Category
Expand Down Expand Up @@ -59,3 +62,12 @@ def load_wiki():
kwargs['body' + lang] = item['body']

Blog.objects.create(category=wiki, **kwargs)


def debug_queryset(qs):
print('result:', qs)
sql, params = qs.query.sql_with_params()
cursor = connection.cursor()
query = cursor.mogrify(sql, params)

print(sqlparse.format(query, reindent=True, wrap_after=120))

0 comments on commit 4f7cf39

Please sign in to comment.