Skip to content
This repository was archived by the owner on Apr 17, 2021. It is now read-only.

Commit 3973e99

Browse files
committed
Add unit tests
1 parent b69a91d commit 3973e99

File tree

11 files changed

+286
-11
lines changed

11 files changed

+286
-11
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@
88
/django_bootstrap_pagination.egg-info
99
/dist
1010
/build
11+
/htmlcov
12+
.coverage

.travis.yml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
language: python
2+
python:
3+
- "2.6"
4+
- "2.7"
5+
# - "3.2" Has been dropped by many dependencies
6+
- "3.3"
7+
- "3.4"
8+
- "3.5"
9+
# - "nightly" Fails irregularly
10+
# - "pypy" # Can't install lxml
11+
# - "pypy3" # Can't install lxml
12+
env:
13+
matrix:
14+
- DJANGO="Django<1.3"
15+
- DJANGO="Django<1.4"
16+
- DJANGO="Django<1.5"
17+
- DJANGO="Django<1.6"
18+
- DJANGO="Django<1.7"
19+
- DJANGO="Django<1.8"
20+
- DJANGO="Django<1.9"
21+
- DJANGO="Django<1.10"
22+
install:
23+
# pip >=8 dropped Python 3.2 support
24+
- pip install "pip<8.0.0" wheel
25+
- pip install "$DJANGO"
26+
- pip install -r test-requirements.txt
27+
cache:
28+
directories:
29+
- $HOME/.cache/pip
30+
matrix:
31+
exclude:
32+
# Python 3.5 is supported as of django 1.8
33+
- {python: "3.5", env: DJANGO="Django<1.3"}
34+
- {python: "3.5", env: DJANGO="Django<1.4"}
35+
- {python: "3.5", env: DJANGO="Django<1.5"}
36+
- {python: "3.5", env: DJANGO="Django<1.6"}
37+
- {python: "3.5", env: DJANGO="Django<1.7"}
38+
- {python: "3.5", env: DJANGO="Django<1.8"}
39+
40+
# Python 3.4 is supported as of django 1.5
41+
- {python: "3.4", env: DJANGO="Django<1.3"}
42+
- {python: "3.4", env: DJANGO="Django<1.4"}
43+
- {python: "3.4", env: DJANGO="Django<1.5"}
44+
- {python: "3.4", env: DJANGO="Django<1.6"}
45+
46+
# Python 3.3 is supported as of django 1.5, but dropped by 1.9+
47+
- {python: "3.3", env: DJANGO="Django<1.3"}
48+
- {python: "3.3", env: DJANGO="Django<1.4"}
49+
- {python: "3.3", env: DJANGO="Django<1.5"}
50+
- {python: "3.3", env: DJANGO="Django<1.6"}
51+
- {python: "3.3", env: DJANGO="Django<1.10"}
52+
53+
# Python 3.2 is supported as of django 1.5, but dropped by 1.9+
54+
- {python: "3.2", env: DJANGO="Django<1.3"}
55+
- {python: "3.2", env: DJANGO="Django<1.4"}
56+
- {python: "3.2", env: DJANGO="Django<1.5"}
57+
- {python: "3.2", env: DJANGO="Django<1.6"}
58+
- {python: "3.2", env: DJANGO="Django<1.10"}
59+
60+
# Python 2.6 is supported up until django 1.7
61+
- {python: "2.6", env: DJANGO="Django<1.8"}
62+
- {python: "2.6", env: DJANGO="Django<1.9"}
63+
- {python: "2.6", env: DJANGO="Django<1.10"}
64+
65+
script: coverage run --source bootstrap_pagination ./run_tests.py
66+
after_success:
67+
- coverage report --fail-under=80

README.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
[![Stories in Ready](https://badge.waffle.io/jmcclell/django-bootstrap-pagination.png?label=ready)](https://waffle.io/jmcclell/django-bootstrap-pagination)
22
[![PyPi version](https://pypip.in/v/django-bootstrap-pagination/badge.png)](https://crate.io/packages/django-bootstrap-pagination/)
33
[![PyPi downloads](https://pypip.in/d/django-bootstrap-pagination/badge.png)](https://crate.io/packages/django-bootstrap-pagination/)
4+
[![Build Status](https://travis-ci.org/jmcclell/django-bootstrap-pagination.png.png?branch=master)](https://travis-ci.org/ondergetekende/livesscrape)
5+
6+
47
## Django Bootstrap Pagination
58

69
**Note:** This version is compatible with Bootstrap 3.x only! Please use the 1.0.x branch for Bootstrap 2 support.
@@ -10,17 +13,7 @@ work seamlessly with Django Page objects. By passing in a Page object and
1013
one or more optional arguments, Bootstrap pagination bars and pagers can
1114
be rendered with very little effort.
1215

13-
Compatible with Django **1.3+**
14-
15-
### Looking for collaborator(s)
16-
17-
I have not had as much time as I would like to maintain this project. Since it is being used by quite a few, I would love to have someone help out as an additional maintainer. If anyone is interested, shoot me a note.
18-
19-
High priority tasks:
20-
21-
- [ ] Full testing suite
22-
- [ ] TravisCI setup to run said tests for each PR (all supported versions of Python and Django)
23-
- [ ] Python 3 support (maintaing backwards compatibility if possible, not required)
16+
Compatible with Django **1.2+**
2417

2518
## Installation
2619

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
django>=1.2

run_tests.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
import os
3+
import unittest2
4+
5+
import django
6+
7+
8+
def runtests():
9+
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.test_settings'
10+
11+
try:
12+
django.setup()
13+
except AttributeError: # Happens before django 1.7
14+
pass
15+
16+
loader = unittest2.TestLoader()
17+
tests = loader.discover('tests')
18+
testRunner = unittest2.runner.TextTestRunner()
19+
result = testRunner.run(tests)
20+
if result.errors or result.failures:
21+
exit(1)
22+
23+
24+
if __name__ == "__main__":
25+
runtests()

test-requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mock
2+
lxml
3+
cssselect
4+
coverage
5+
unittest2

tests/__init__.py

Whitespace-only changes.

tests/test_helpers.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import django.conf
2+
import django.core.urlresolvers
3+
import django.http
4+
5+
import mock
6+
import unittest2 as unittest
7+
8+
from bootstrap_pagination.templatetags import bootstrap_pagination
9+
10+
11+
class TestHelpers(unittest.TestCase):
12+
def test_strToBool(self):
13+
self.assertTrue(bootstrap_pagination.strToBool('true'))
14+
self.assertFalse(bootstrap_pagination.strToBool('false'))
15+
16+
@mock.patch('bootstrap_pagination.templatetags.bootstrap_pagination.'
17+
'reverse')
18+
def test_get_page_url_view(self, mock_reverse):
19+
mock_reverse.return_value = "/some_nice_url"
20+
url = bootstrap_pagination.get_page_url(
21+
page_num=42,
22+
current_app='the_current_app',
23+
url_view_name='the_view',
24+
url_extra_args=["arg1"],
25+
url_extra_kwargs={"kwarg": "yes"},
26+
url_param_name='page',
27+
url_get_params=[],
28+
url_anchor="derp")
29+
30+
self.assertEqual(url, '/some_nice_url#derp')
31+
mock_reverse.assert_called_once_with('the_view',
32+
args=['arg1'],
33+
current_app='the_current_app',
34+
kwargs={'kwarg': 'yes',
35+
'page': 42})
36+
37+
@mock.patch('bootstrap_pagination.templatetags.bootstrap_pagination.'
38+
'reverse')
39+
def test_get_page_url_view_sub(self, mock_reverse):
40+
mock_reverse.side_effect = [
41+
django.core.urlresolvers.NoReverseMatch(),
42+
"/some_nice_url"
43+
]
44+
url = bootstrap_pagination.get_page_url(
45+
page_num=42,
46+
current_app='the_current_app',
47+
url_view_name='the_view',
48+
url_extra_args=["arg1"],
49+
url_extra_kwargs={"kwarg": "yes"},
50+
url_param_name='page',
51+
url_get_params=django.http.QueryDict(""),
52+
url_anchor="derp")
53+
54+
self.assertEqual(url, '/some_nice_url#derp')
55+
mock_reverse.assert_called_with('tests.the_view',
56+
args=['arg1'],
57+
current_app='the_current_app',
58+
kwargs={'kwarg': 'yes',
59+
'page': 42})
60+
61+
@mock.patch('bootstrap_pagination.templatetags.bootstrap_pagination.'
62+
'reverse')
63+
def test_get_page_url_straight(self, mock_reverse):
64+
mock_reverse.return_value = "/some_nice_url"
65+
url = bootstrap_pagination.get_page_url(
66+
page_num=42,
67+
current_app='the_current_app',
68+
url_view_name=None,
69+
url_extra_args=["arg1"],
70+
url_extra_kwargs={"kwarg": "yes"},
71+
url_param_name='page',
72+
url_get_params=django.http.QueryDict("arg2=val"),
73+
url_anchor="derp")
74+
75+
self.assertIn("arg2=val", url)
76+
self.assertIn("page=42", url)
77+
self.assertTrue(url.endswith("#derp"))
78+
79+
80+
if __name__ == '__main__':
81+
django.conf.settings.configure()
82+
unittest.main()

tests/test_pager.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import unittest2 as unittest
2+
3+
import lxml.html
4+
5+
try:
6+
from django.template.loader import get_template_from_string
7+
except ImportError:
8+
from django.template import Engine
9+
get_template_from_string = Engine.get_default().from_string
10+
11+
from django.template import Context
12+
import django.http
13+
from django.core.paginator import Paginator
14+
15+
16+
class PagerTestCase(unittest.TestCase):
17+
def test_example(self):
18+
template = get_template_from_string("""
19+
{% load bootstrap_pagination %}
20+
{% bootstrap_pager page_obj %}
21+
""")
22+
23+
objects = ["obj%02x" % idx
24+
for idx in range(30)]
25+
26+
paginator = Paginator(objects, 10)
27+
28+
c = Context({'page_obj': paginator.page(2),
29+
'request': django.http.HttpRequest()})
30+
html = lxml.html.fragment_fromstring(template.render(c))
31+
self.assertEqual(html.get('class'), 'pager')

tests/test_paginate.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import unittest2 as unittest
2+
3+
import lxml.html
4+
5+
try:
6+
from django.template.loader import get_template_from_string
7+
except ImportError:
8+
from django.template import Engine
9+
get_template_from_string = Engine.get_default().from_string
10+
11+
from django.template import Context
12+
import django.http
13+
from django.core.paginator import Paginator
14+
15+
16+
class PaginateTestCase(unittest.TestCase):
17+
def test_example(self):
18+
template = get_template_from_string("""
19+
{% load bootstrap_pagination %}
20+
{% bootstrap_paginate page_obj range=10 %}
21+
""")
22+
23+
objects = ["obj%02x" % idx
24+
for idx in range(30)]
25+
26+
paginator = Paginator(objects, 10)
27+
28+
c = Context({'page_obj': paginator.page(2),
29+
'request': django.http.HttpRequest()})
30+
html = lxml.html.fragment_fromstring(template.render(c))
31+
self.assertEqual(html.get('class'), 'pagination')
32+
self.assertEqual(
33+
html.cssselect('a[title=\"Current Page\"]')[0].text.strip(),
34+
'2')

0 commit comments

Comments
 (0)