Skip to content

Commit a46facb

Browse files
committed
Add pytest-django and test things
1 parent 30a5966 commit a46facb

File tree

9 files changed

+84
-0
lines changed

9 files changed

+84
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ dist
55
*.pyc
66
django_babel.egg-info
77
.tox
8+
htmlcov

manage.py

Whitespace-only changes.

setup.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
[wheel]
22
universal = 1
3+
4+
[tool:pytest]
5+
DJANGO_SETTINGS_MODULE = tests.settings
6+
norecursedirs = .git .tox .eggs .cache htmlcov venv*

tests/__init__.py

Whitespace-only changes.

tests/settings.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
SECRET_KEY = 'x'
2+
USE_I18N = True
3+
ROOT_URLCONF = 'tests.urls'
4+
INSTALLED_APPS = [
5+
'django_babel',
6+
'tests',
7+
]
8+
MIDDLEWARE_CLASSES = [
9+
'django.middleware.locale.LocaleMiddleware',
10+
'django_babel.middleware.LocaleMiddleware',
11+
]
12+
TEMPLATES = [
13+
{
14+
'NAME': 'default',
15+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
16+
'APP_DIRS': True,
17+
'OPTIONS': {
18+
'context_processors': [
19+
'django.template.context_processors.i18n',
20+
],
21+
},
22+
},
23+
]

tests/templates/test.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{% load babel %}
2+
language_code={{ LANGUAGE_CODE }}
3+
language_name={{ locale.language_name }}
4+
date={{ date|datefmt }}
5+
datetime={{ date|datetimefmt }}
6+
time={{ date|timefmt }}
7+
number={{ number|numberfmt }}
8+
decimal={{ number|decimalfmt }}
9+
currency={{ number|currencyfmt:"EUR" }}
10+
percent={{ number|percentfmt }}
11+
scientificfmt={{ number|scientificfmt }}

tests/test_render.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -- encoding: utf-8 --
2+
from __future__ import unicode_literals
3+
4+
import pytest
5+
6+
7+
@pytest.mark.parametrize('locale', ('en', 'fi', 'sv', 'pt-BR'))
8+
def test_babel_render(client, locale):
9+
"""
10+
Test the middleware and the rendery bits.
11+
"""
12+
response = client.get('/', HTTP_ACCEPT_LANGUAGE=locale)
13+
# "Parse" the key-value format
14+
lines = response.content.decode('utf-8').strip().splitlines()
15+
content = dict(kv.split('=', 1) for kv in lines)
16+
# See that we're rendering in the locale we expect
17+
assert content['language_code'] == locale.lower()
18+
# check that we could access `babel.Locale.language_name`
19+
assert content['language_name'] == {
20+
'en': 'English',
21+
'fi': 'suomi',
22+
'sv': 'svenska',
23+
'pt-BR': 'português',
24+
}[locale]
25+
# The rest are not really tested (aside from smoke tests) further;
26+
# the Babel test suite has taken care of that.

tests/urls.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import time
2+
3+
from django.conf.urls import url
4+
from django.shortcuts import render
5+
from django.utils.timezone import now
6+
7+
8+
def test_view(request):
9+
return render(request, 'test.txt', {
10+
'date': now(),
11+
'number': time.time(),
12+
'locale': request.locale,
13+
})
14+
15+
16+
urlpatterns = [
17+
url('^$', test_view),
18+
]

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ deps =
66
coverage
77
pytest
88
pytest-cov
9+
pytest-django
910
python-coveralls
1011
django15: Django>=1.5,<1.6
1112
django16: Django>=1.6,<1.7

0 commit comments

Comments
 (0)