Add pytest-django and test things
This commit is contained in:
parent
30a59664d6
commit
a46facb352
|
@ -5,3 +5,4 @@ dist
|
|||
*.pyc
|
||||
django_babel.egg-info
|
||||
.tox
|
||||
htmlcov
|
||||
|
|
|
@ -1,2 +1,6 @@
|
|||
[wheel]
|
||||
universal = 1
|
||||
|
||||
[tool:pytest]
|
||||
DJANGO_SETTINGS_MODULE = tests.settings
|
||||
norecursedirs = .git .tox .eggs .cache htmlcov venv*
|
||||
|
|
|
@ -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',
|
||||
],
|
||||
},
|
||||
},
|
||||
]
|
|
@ -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 }}
|
|
@ -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.
|
|
@ -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),
|
||||
]
|
Loading…
Reference in New Issue