Add pytest-django and test things

This commit is contained in:
Aarni Koskela 2016-09-06 18:27:58 +03:00
parent 30a59664d6
commit a46facb352
9 changed files with 84 additions and 0 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@ dist
*.pyc
django_babel.egg-info
.tox
htmlcov

0
manage.py Normal file
View File

View File

@ -1,2 +1,6 @@
[wheel]
universal = 1
[tool:pytest]
DJANGO_SETTINGS_MODULE = tests.settings
norecursedirs = .git .tox .eggs .cache htmlcov venv*

0
tests/__init__.py Normal file
View File

23
tests/settings.py Normal file
View File

@ -0,0 +1,23 @@
SECRET_KEY = 'x'
USE_I18N = True
ROOT_URLCONF = 'tests.urls'
INSTALLED_APPS = [
'django_babel',
'tests',
]
MIDDLEWARE_CLASSES = [
'django.middleware.locale.LocaleMiddleware',
'django_babel.middleware.LocaleMiddleware',
]
TEMPLATES = [
{
'NAME': 'default',
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.i18n',
],
},
},
]

11
tests/templates/test.txt Normal file
View File

@ -0,0 +1,11 @@
{% load babel %}
language_code={{ LANGUAGE_CODE }}
language_name={{ locale.language_name }}
date={{ date|datefmt }}
datetime={{ date|datetimefmt }}
time={{ date|timefmt }}
number={{ number|numberfmt }}
decimal={{ number|decimalfmt }}
currency={{ number|currencyfmt:"EUR" }}
percent={{ number|percentfmt }}
scientificfmt={{ number|scientificfmt }}

26
tests/test_render.py Normal file
View File

@ -0,0 +1,26 @@
# -- encoding: utf-8 --
from __future__ import unicode_literals
import pytest
@pytest.mark.parametrize('locale', ('en', 'fi', 'sv', 'pt-BR'))
def test_babel_render(client, locale):
"""
Test the middleware and the rendery bits.
"""
response = client.get('/', HTTP_ACCEPT_LANGUAGE=locale)
# "Parse" the key-value format
lines = response.content.decode('utf-8').strip().splitlines()
content = dict(kv.split('=', 1) for kv in lines)
# See that we're rendering in the locale we expect
assert content['language_code'] == locale.lower()
# check that we could access `babel.Locale.language_name`
assert content['language_name'] == {
'en': 'English',
'fi': 'suomi',
'sv': 'svenska',
'pt-BR': 'português',
}[locale]
# The rest are not really tested (aside from smoke tests) further;
# the Babel test suite has taken care of that.

18
tests/urls.py Normal file
View File

@ -0,0 +1,18 @@
import time
from django.conf.urls import url
from django.shortcuts import render
from django.utils.timezone import now
def test_view(request):
return render(request, 'test.txt', {
'date': now(),
'number': time.time(),
'locale': request.locale,
})
urlpatterns = [
url('^$', test_view),
]

View File

@ -6,6 +6,7 @@ deps =
coverage
pytest
pytest-cov
pytest-django
python-coveralls
django15: Django>=1.5,<1.6
django16: Django>=1.6,<1.7