Skip to content

Commit

Permalink
Fixing tests with locale
Browse files Browse the repository at this point in the history
  • Loading branch information
Sébastien Fievet committed Feb 11, 2018
1 parent 7654321 commit ecbd792
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
40 changes: 24 additions & 16 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from django.core import management
from django.template import Context, Engine
from django.utils import six
from django.utils import translation

LOCALES = ['en', 'fr', 'zh-Hans', 'ko-KR']


def get_template_from_string(template_code):
Expand All @@ -19,6 +22,12 @@ def get_template_from_string(template_code):
return Engine(**engine_options).from_string(template_code)


def to_locale(locale):
if django.VERSION < (1, 10):
return translation.to_locale(locale)
return locale


@pytest.mark.usefixtures("cleandir")
def test_compile_all(settings):
out = six.StringIO()
Expand All @@ -28,55 +37,54 @@ def test_compile_all(settings):

assert len(lines) == len(settings.LANGUAGES)
for locale, _ in settings.LANGUAGES:
assert "processing language %s" % locale in lines
assert "processing language %s" % to_locale(locale) in lines


LOCALIZED_CONTENT = {
'en': 'django',
'fr': '"Hello world!": "Bonjour \\u00e0 tous !"',
'zh-hans': '"Hello world!": "\\u5927\\u5bb6\\u597d\\uff01"',
'zh-Hans': '"Hello world!": "\\u5927\\u5bb6\\u597d\\uff01"',
'ko-KR': '"Hello world!": "\\uc548\\ub155\\ud558\\uc138\\uc694!"'
}


@pytest.mark.usefixtures("cleandir")
@pytest.mark.parametrize('locale', ['en', 'fr', 'zh-hans', 'ko-KR'])
@pytest.mark.parametrize('locale', LOCALES)
def test_compile(settings, locale):
out = six.StringIO()
management.call_command('compilejsi18n', verbosity=1, stdout=out,
locale=locale)
locale=to_locale(locale))
out.seek(0)
lines = [l.strip() for l in out.readlines()]

assert len(lines) == 1
assert lines[0] == "processing language %s" % locale
assert lines[0] == "processing language %s" % to_locale(locale)
filename = os.path.join(
settings.STATICI18N_ROOT, "jsi18n", locale, "djangojs.js")
settings.STATICI18N_ROOT, "jsi18n", to_locale(locale), "djangojs.js")
assert os.path.exists(filename)
with io.open(filename, "r", encoding="utf-8") as fp:
content = fp.read()
assert "django.catalog" in content
assert LOCALIZED_CONTENT[locale] in content


@pytest.mark.parametrize('locale', ['en', 'fr', 'zh-hans', 'ko-KR'])
@pytest.mark.parametrize('locale', LOCALES)
def test_compile_no_use_i18n(settings, locale):
"""Tests compilation when `USE_I18N = False`.
In this scenario, only the `settings.LANGUAGE_CODE` locale is processed
(it defaults to `en-us` for Django projects).
"""
settings.USE_I18N = False
settings.LANGUAGE_CODE = locale

out = six.StringIO()
management.call_command('compilejsi18n', verbosity=1, stdout=out)
management.call_command('compilejsi18n', verbosity=1, stdout=out,
locale=to_locale(locale))
out.seek(0)
lines = [l.strip() for l in out.readlines()]
assert len(lines) == 1
assert lines[0] == "processing language %s" % locale
assert lines[0] == "processing language %s" % to_locale(locale)
assert os.path.exists(os.path.join(
settings.STATIC_ROOT, "jsi18n", locale, "djangojs.js"))
settings.STATIC_ROOT, "jsi18n", to_locale(locale), "djangojs.js"))


@pytest.mark.parametrize('locale', ['en'])
Expand All @@ -88,7 +96,7 @@ def test_compile_with_output_format(settings, locale, output_format):
out.seek(0)
lines = [l.strip() for l in out.readlines()]
assert len(lines) == 1
assert lines[0] == "processing language %s" % locale
assert lines[0] == "processing language %s" % to_locale(locale)
assert os.path.exists(os.path.join(
settings.STATIC_ROOT, "jsi18n", locale, "djangojs.%s" % output_format))

Expand All @@ -100,7 +108,7 @@ def test_compile_locale_not_exists():
assert out.getvalue() == ""


@pytest.mark.parametrize('locale', ['en', 'fr', 'zh-hans', 'ko-KR'])
@pytest.mark.parametrize('locale', LOCALES)
def test_statici18n_templatetag(locale):
template = """
{% load statici18n %}
Expand All @@ -112,14 +120,14 @@ def test_statici18n_templatetag(locale):


@pytest.mark.usefixtures("cleandir")
@pytest.mark.parametrize('locale', ['en', 'fr', 'zh-hans', 'ko-KR'])
@pytest.mark.parametrize('locale', LOCALES)
def test_inlinei18n_templatetag(locale):
template = """
{% load statici18n %}
<script>{% inlinei18n LANGUAGE_CODE %}</script>
"""
management.call_command('compilejsi18n')
template = get_template_from_string(template)
rendered = template.render(Context({'LANGUAGE_CODE': locale})).strip()
rendered = template.render(Context({'LANGUAGE_CODE': to_locale(locale)})).strip()
assert 'var django = globals.django || (globals.django = {});' in rendered
assert '&quot;' not in rendered
2 changes: 1 addition & 1 deletion tests/test_project/project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
LANGUAGES = (
('en', 'English'),
('fr', 'French'),
('zh-hans', 'Simplified Chinese'),
('zh-Hans', 'Simplified Chinese'),
('ko-KR', 'Korean'),
)

Expand Down

0 comments on commit ecbd792

Please sign in to comment.