Skip to content

Commit

Permalink
Add a failing unit test for the query-for-each-row issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jieter authored and intiocean committed Feb 27, 2017
1 parent 962f502 commit 8036d81
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
1 change: 0 additions & 1 deletion tests/app/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# coding: utf-8
from __future__ import unicode_literals

from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
Expand Down
33 changes: 31 additions & 2 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
from random import randint

import django_tables2 as tables
import pytest
from django.db.models.functions import Length
from django.utils import six
from django_tables2 import RequestConfig

import pytest

from .app.models import Occupation, Person, PersonProxy, Player
from .utils import build_request

Expand Down Expand Up @@ -406,7 +407,35 @@ class Meta:
assert Person.objects.get(pk=person2.pk) == person2


def test_issue_361(per_page=5):
def test_single_query_for_non_paginated_table(settings):
'''
A non-paginated table should not generate a query for each row, but only
one query to count the rows and one to fetch the rows.
'''
from django.db import connection
settings.DEBUG = True

for i in range(10):
Person.objects.create(first_name='Bob %d' % randint(0, 200), last_name='Builder')

num_queries_before = len(connection.queries)

class PersonTable(tables.Table):
class Meta:
model = Person
fields = ('first_name', 'last_name')

table = PersonTable(Person.objects.all())
request = build_request('/')
RequestConfig(request, paginate=False).configure(table)
table.as_html(request)

# print '\n'.join(q['sql'] for q in connection.queries)
assert len(connection.queries) - num_queries_before == 2


def test_issue_361(settings):
settings.DEBUG = True

bob = Person.objects.create(first_name='Bob', last_name='Builder')
eve = Person.objects.create(first_name='Eve', last_name='Dropper')
Expand Down

0 comments on commit 8036d81

Please sign in to comment.