Skip to content

Commit

Permalink
Merge pull request #34063 from dimagi/bmb/fix-ucr-pagination
Browse files Browse the repository at this point in the history
actually paginate the ucr expressions page
  • Loading branch information
biyeun authored Feb 6, 2024
2 parents 22fa186 + 1f76c4f commit 11cdf74
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
48 changes: 47 additions & 1 deletion corehq/apps/userreports/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from django.test import SimpleTestCase
from django.test import SimpleTestCase, TestCase, RequestFactory
from django.utils.safestring import SafeString

from corehq.apps.userreports.models import UCRExpression
from corehq.apps.userreports.views import UCRExpressionListView
from corehq.apps.userreports.const import UCR_NAMED_EXPRESSION
from ..views import NamedExpressionHighlighter


Expand All @@ -15,3 +18,46 @@ def test_named_filter_is_highlighted(self):
def test_result_is_html_safe(self):
highlighted = NamedExpressionHighlighter.highlight_links('NamedFilter:abc')
self.assertIsInstance(highlighted, SafeString)


class UCRExpressionListViewTests(TestCase):
def test_paginated_list_limits_results(self):
expressions = [self._create_expression(i) for i in range(3)]
UCRExpression.objects.bulk_create(expressions)
view = self._create_view(page=1, limit=2)

names = {expression['itemData']['name'] for expression in view.paginated_list}
self.assertSetEqual(names, {'expression0', 'expression1'})

def test_paginated_list_respects_page(self):
expressions = [self._create_expression(i) for i in range(3)]
UCRExpression.objects.bulk_create(expressions)
view = self._create_view(page=2, limit=2)

names = {expression['itemData']['name'] for expression in view.paginated_list}
self.assertSetEqual(names, {'expression2'})

def test_default_limit(self):
expressions = [self._create_expression(i) for i in range(12)]
UCRExpression.objects.bulk_create(expressions)
view = self._create_view(page=1)

self.assertEqual(len(list(view.paginated_list)), 10)

@staticmethod
def _create_expression(index):
return UCRExpression(
name=f'expression{index}',
domain='test-domain',
expression_type=UCR_NAMED_EXPRESSION
)

@staticmethod
def _create_view(page=1, limit=None):
view = UCRExpressionListView()
view.args = ['test-domain']
kwargs = {'page': page}
if limit:
kwargs['limit'] = limit
view.request = RequestFactory().get('/', kwargs)
return view
2 changes: 1 addition & 1 deletion corehq/apps/userreports/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1749,7 +1749,7 @@ def page_context(self):

@property
def paginated_list(self):
for expression in self.base_query.all():
for expression in self.base_query[self.skip:self.skip + self.limit]:
yield {
"itemData": self._item_data(expression),
"template": "base-ucr-statement-template",
Expand Down

0 comments on commit 11cdf74

Please sign in to comment.