Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Integrate doc tests into test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
markfinger committed Oct 12, 2016
1 parent 1d01aad commit 2af2013
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 107 deletions.
70 changes: 1 addition & 69 deletions icekit/utils/pagination.py
@@ -1,78 +1,14 @@
import math
from django.utils import six
from el_pagination.utils import get_page_numbers
from pprint import pprint


def describe_page_numbers(current_page, total_count, per_page, page_numbers_at_ends=3, pages_numbers_around_current=3):
"""
Produces a description of how to display a paginated list's page numbers. Rather than just
spitting out a list of every page available, the page numbers returned will be trimmed
to display only the immediate numbers around the start, end, and the current page.
```
>>> pprint(describe_page_numbers(1, 500, 10))
{'current_page': 1,
'has_next': True,
'has_previous': False,
'next_page': 2,
'numbers': [1, 2, 3, 4, None, 48, 49, 50],
'per_page': 10,
'previous_page': 0,
'total_count': 500}
>>> pprint(describe_page_numbers(10, 500, 10))
{'current_page': 10,
'has_next': True,
'has_previous': True,
'next_page': 11,
'numbers': [1, 2, 3, None, 7, 8, 9, 10, 11, 12, 13, None, 48, 49, 50],
'per_page': 10,
'previous_page': 9,
'total_count': 500}
>>> pprint(describe_page_numbers(50, 500, 10))
{'current_page': 50,
'has_next': False,
'has_previous': True,
'next_page': 51,
'numbers': [1, 2, 3, None, 47, 48, 49, 50],
'per_page': 10,
'previous_page': 49,
'total_count': 500}
>>> pprint(describe_page_numbers(2, 40, 10))
{'current_page': 2,
'has_next': True,
'has_previous': True,
'next_page': 3,
'numbers': [1, 2, 3, 4],
'per_page': 10,
'previous_page': 1,
'total_count': 40}
# total_count / per_page == 0 - need float math
>>> pprint(describe_page_numbers(1, 1, 20))
{'current_page': 1,
'has_next': False,
'has_previous': False,
'next_page': 2,
'numbers': [1],
'per_page': 20,
'previous_page': 0,
'total_count': 1}
# empty results - total_count==0
>>> pprint(describe_page_numbers(1, 0, 20))
{'current_page': 1,
'has_next': False,
'has_previous': False,
'next_page': 2,
'numbers': [],
'per_page': 20,
'previous_page': 0,
'total_count': 0}
```
:param current_page: the current page number (page numbers should start at 1)
:param total_count: the total number of items that are being paginated
Expand Down Expand Up @@ -102,7 +38,3 @@ def describe_page_numbers(current_page, total_count, per_page, page_numbers_at_e
'total_count': total_count,
'per_page': per_page,
}

if __name__ == "__main__":
import doctest
doctest.testmod()
38 changes: 0 additions & 38 deletions icekit/utils/sequences.py
Expand Up @@ -37,40 +37,6 @@ def slice_sequences(sequences, start, end):
Performs a slice across multiple sequences.
Useful when paginating across chained collections.
```
>>> slice_sequences([[[1, 2, 3], 3], [[4, 5, 6, 7], 4]], 0, 2)
[1, 2]
>>> slice_sequences([[[1, 2, 3], 3], [[4, 5, 6, 7], 4]], 2, 4)
[3, 4]
>>> slice_sequences([[[1, 2, 3], 3], [[4, 5, 6, 7], 4]], 4, 6)
[5, 6]
>>> slice_sequences([[[1, 2, 3], 3], [[4, 5, 6, 7], 4]], 6, 8)
[7]
>>> slice_sequences([[[1, 2, 3], 3], [[4, 5, 6, 7], 4]], 0, 10)
[1, 2, 3, 4, 5, 6, 7]
>>> slice_sequences([[[1, 2, 3], 3], [[4, 5, 6, 7], 4]], 0, 4)
[1, 2, 3, 4]
>>> slice_sequences([[[1, 2, 3], 3], [[4, 5, 6, 7], 4]], 1, 5)
[2, 3, 4, 5]
>>> slice_sequences([[[1, 2, 3], 3], [[4, 5, 6, 7], 4]], 3, 11)
[4, 5, 6, 7]
>>> slice_sequences([[[1, 2, 3], 3], [[4, 5, 6, 7], 4]], 100, 200)
[]
>>> slice_sequences([[[1, 2, 3], 3], [[4, 5, 6, 7], 4]], -100, 200)
Traceback (most recent call last):
...
ValueError: Start and/or End out of range. Start: -100. End: 200
>>> slice_sequences([[[1, 2, 3], 3], [[4, 5, 6, 7], 4]], 200, 100)
Traceback (most recent call last):
...
ValueError: Start and/or End out of range. Start: 200. End: 100
>>> slice_sequences([[[1, 2, 3], 3], [[4, 5, 6, 7], 4]], 100, 100)
Traceback (most recent call last):
...
ValueError: Start and/or End out of range. Start: 100. End: 100
```
:param sequences: an iterable of iterables, each nested iterable should contain
a sequence and its size
:param start: starting index to apply the slice from
Expand Down Expand Up @@ -108,7 +74,3 @@ def slice_sequences(sequences, start, end):
break

return collected_items

if __name__ == "__main__":
import doctest
doctest.testmod()
115 changes: 115 additions & 0 deletions icekit/utils/tests.py
Expand Up @@ -7,6 +7,8 @@

from icekit.utils import testing
from icekit.tests.models import ImageTest
from icekit.utils.sequences import slice_sequences
from icekit.utils.pagination import describe_page_numbers


class TestingUtils(WebTest):
Expand Down Expand Up @@ -64,3 +66,116 @@ def test_test_image_create_and_delete(self):
# Ensure that the delete_test_image function removes the thumbnail
# style images.
self.assertFalse(os.path.exists(dst_thumb_file))

def test_slice_sequences(self):
self.assertEqual(
slice_sequences([[[1, 2, 3], 3], [[4, 5, 6, 7], 4]], 0, 2),
[1, 2],
)
self.assertEqual(
slice_sequences([[[1, 2, 3], 3], [[4, 5, 6, 7], 4]], 2, 4),
[3, 4],
)
self.assertEqual(
slice_sequences([[[1, 2, 3], 3], [[4, 5, 6, 7], 4]], 4, 6),
[5, 6],
)
self.assertEqual(
slice_sequences([[[1, 2, 3], 3], [[4, 5, 6, 7], 4]], 6, 8),
[7],
)
self.assertEqual(
slice_sequences([[[1, 2, 3], 3], [[4, 5, 6, 7], 4]], 0, 10),
[1, 2, 3, 4, 5, 6, 7],
)
self.assertEqual(
slice_sequences([[[1, 2, 3], 3], [[4, 5, 6, 7], 4]], 0, 4),
[1, 2, 3, 4],
)
self.assertEqual(
slice_sequences([[[1, 2, 3], 3], [[4, 5, 6, 7], 4]], 1, 5),
[2, 3, 4, 5],
)
self.assertEqual(
slice_sequences([[[1, 2, 3], 3], [[4, 5, 6, 7], 4]], 3, 11),
[4, 5, 6, 7],
)
self.assertEqual(
slice_sequences([[[1, 2, 3], 3], [[4, 5, 6, 7], 4]], 100, 200),
[],
)

def test_describe_page_numbers(self):
self.assertEqual(
describe_page_numbers(1, 500, 10),
{'current_page': 1,
'has_next': True,
'has_previous': False,
'next_page': 2,
'numbers': [1, 2, 3, 4, None, 48, 49, 50],
'per_page': 10,
'previous_page': 0,
'total_count': 500}
)

self.assertEqual(
describe_page_numbers(10, 500, 10),
{'current_page': 10,
'has_next': True,
'has_previous': True,
'next_page': 11,
'numbers': [1, 2, 3, None, 7, 8, 9, 10, 11, 12, 13, None, 48, 49, 50],
'per_page': 10,
'previous_page': 9,
'total_count': 500},
)

self.assertEqual(
describe_page_numbers(50, 500, 10),
{'current_page': 50,
'has_next': False,
'has_previous': True,
'next_page': 51,
'numbers': [1, 2, 3, None, 47, 48, 49, 50],
'per_page': 10,
'previous_page': 49,
'total_count': 500},
)

self.assertEqual(
describe_page_numbers(2, 40, 10),
{'current_page': 2,
'has_next': True,
'has_previous': True,
'next_page': 3,
'numbers': [1, 2, 3, 4],
'per_page': 10,
'previous_page': 1,
'total_count': 40},
)

self.assertEqual(
# total_count / per_page == 0 - need float math
describe_page_numbers(1, 1, 20),
{'current_page': 1,
'has_next': False,
'has_previous': False,
'next_page': 2,
'numbers': [1],
'per_page': 20,
'previous_page': 0,
'total_count': 1},
)

self.assertEqual(
# empty results - total_count==0
describe_page_numbers(1, 0, 20),
{'current_page': 1,
'has_next': False,
'has_previous': False,
'next_page': 2,
'numbers': [],
'per_page': 20,
'previous_page': 0,
'total_count': 0},
)

0 comments on commit 2af2013

Please sign in to comment.