From 75a35de450d3717c135851fbd74918e9111f9496 Mon Sep 17 00:00:00 2001 From: Peter Justin Date: Mon, 15 May 2017 21:13:00 +0200 Subject: [PATCH] add more test cases --- flask_babelplus/core.py | 2 +- flask_babelplus/utils.py | 6 +-- tests/tests.py | 105 +++++++++++++++++++++++++++++++++------ 3 files changed, 93 insertions(+), 20 deletions(-) diff --git a/flask_babelplus/core.py b/flask_babelplus/core.py index 5ce5396..8389b4c 100644 --- a/flask_babelplus/core.py +++ b/flask_babelplus/core.py @@ -15,7 +15,7 @@ except ImportError: from pytz import timezone else: - timezone = pytz.timezone + timezone = pytz.timezone # pragma: no cover from .constants import DEFAULT_LOCALE, DEFAULT_TIMEZONE,\ DEFAULT_DATE_FORMATS diff --git a/flask_babelplus/utils.py b/flask_babelplus/utils.py index d30a35c..11c3ed5 100644 --- a/flask_babelplus/utils.py +++ b/flask_babelplus/utils.py @@ -17,7 +17,7 @@ from pytz.gae import pytz except ImportError: from pytz import timezone, UTC -else: +else: # pragma: no cover timezone = pytz.timezone UTC = pytz.UTC @@ -114,7 +114,7 @@ def refresh(): return English text and a now German page. """ ctx = _get_current_context() - for key in 'babel_locale', 'babel_tzinfo': + for key in ('babel_locale', 'babel_tzinfo'): if hasattr(ctx, key): delattr(ctx, key) @@ -156,7 +156,7 @@ def force_locale(locale): setattr(ctx, key, value) -def _get_format(key, format): +def _get_format(key, format=None): """A small helper for the datetime formatting functions. Looks up format defaults for different kinds. """ diff --git a/tests/tests.py b/tests/tests.py index ffadeb1..8a692ca 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -1,16 +1,19 @@ # -*- coding: utf-8 -*- from __future__ import with_statement import unittest +import pytest from datetime import datetime, timedelta from decimal import Decimal -from babel import support +from pytz import timezone, UTC +from babel import support, Locale import flask import flask_babelplus as babel_ext from flask_babelplus import gettext, ngettext, pgettext, npgettext, \ lazy_gettext, lazy_pgettext from flask_babelplus._compat import text_type +from flask_babelplus.utils import get_state, _get_format class DateFormattingTestCase(unittest.TestCase): @@ -70,10 +73,18 @@ def test_custom_formats(self): b = babel_ext.Babel(app) b.date_formats['datetime'] = 'long' b.date_formats['datetime.long'] = 'MMMM d, yyyy h:mm:ss a' + + b.date_formats['date'] = 'long' + b.date_formats['date.short'] = 'MM d' + d = datetime(2010, 4, 12, 13, 46) with app.test_request_context(): assert babel_ext.format_datetime(d) == 'April 12, 2010 3:46:00 AM' + assert _get_format('datetime') == 'MMMM d, yyyy h:mm:ss a' + # none; returns the format + assert _get_format('datetime', 'medium') == 'medium' + assert _get_format('date', 'short') == 'MM d' def test_custom_locale_selector(self): app = flask.Flask(__name__) @@ -104,6 +115,7 @@ def test_refreshing(self): app = flask.Flask(__name__) babel_ext.Babel(app) d = datetime(2010, 4, 12, 13, 46) + babel_ext.refresh() # nothing should be refreshed (see case below) with app.test_request_context(): assert babel_ext.format_datetime(d) == 'Apr 12, 2010, 1:46:00 PM' app.config['BABEL_DEFAULT_TIMEZONE'] = 'Europe/Vienna' @@ -118,6 +130,9 @@ def test_force_locale(self): def select_locale(): return 'de_DE' + with babel_ext.force_locale('en_US'): + assert babel_ext.get_locale() is None + with app.test_request_context(): assert str(babel_ext.get_locale()) == 'de_DE' with babel_ext.force_locale('en_US'): @@ -230,6 +245,12 @@ def test_lazy_pgettext(self): assert text_type(first) == 'Hello Guest!' assert text_type(domain_first) == 'Hello Guest!' + def test_no_ctx_gettext(self): + app = flask.Flask(__name__) + babel_ext.Babel(app, default_locale='de_DE') + domain = babel_ext.get_domain() + assert domain.gettext('Yes') == 'Yes' + def test_list_translations(self): app = flask.Flask(__name__) b = babel_ext.Babel(app, default_locale='de_DE') @@ -241,6 +262,14 @@ def test_list_translations(self): assert len(translations) == 1 assert str(translations[0]) == 'de' + def test_get_translations(self): + app = flask.Flask(__name__) + babel_ext.Babel(app, default_locale='de_DE') + domain = babel_ext.get_domain() # using default domain + + # no app context + assert isinstance(domain.get_translations(), support.NullTranslations) + def test_domain(self): app = flask.Flask(__name__) babel_ext.Babel(app, default_locale='de_DE') @@ -283,29 +312,73 @@ def test_multiple_apps(self): assert 'de_DE' not in app2.extensions["babel"].domain.cache -class DomainTestCase(unittest.TestCase): +class IntegrationTestCase(unittest.TestCase): + def test_configure_jinja(self): + app = flask.Flask(__name__) + babel_ext.Babel(app, configure_jinja=False) + assert not app.jinja_env.filters.get("scientificformat") + + def test_get_state(self): + # app = None; app.extensions = False; babel = False; silent = True; + assert get_state(silent=True) is None - def test_get_translations(self): app = flask.Flask(__name__) - babel_ext.Babel(app, default_locale='de_DE') - domain = babel_ext.get_domain() # using default domain + with pytest.raises(RuntimeError): + with app.test_request_context(): + # app = app; silent = False + # babel not in app.extensions + get_state() - # no app context - assert isinstance(domain.get_translations(), support.NullTranslations) + # same as above, just silent + with app.test_request_context(): + assert get_state(app=app, silent=True) is None + + babel_ext.Babel(app) + with app.test_request_context(): + # should use current_app + assert get_state(app=None, silent=True) == app.extensions['babel'] + + def test_get_locale(self): + assert babel_ext.get_locale() is None - def test_no_ctx_gettext(self): app = flask.Flask(__name__) - babel_ext.Babel(app, default_locale='de_DE') - domain = babel_ext.get_domain() + babel_ext.Babel(app) + with app.app_context(): + assert babel_ext.get_locale() == Locale.parse("en") - assert domain.gettext('Yes') == 'Yes' + def test_get_timezone_none(self): + assert babel_ext.get_timezone() is None + + app = flask.Flask(__name__) + b = babel_ext.Babel(app) - def test_lazy_old_style_formatting(self): - lazy_string = lazy_gettext(u'Hello %(name)s') - assert lazy_string % {u'name': u'test'} == u'Hello test' + @b.timezoneselector + def tz_none(): + return None + with app.test_request_context(): + assert babel_ext.get_timezone() == UTC + + def test_get_timezone_vienna(self): + app = flask.Flask(__name__) + b = babel_ext.Babel(app) + + @b.timezoneselector + def tz_vienna(): + return timezone('Europe/Vienna') + with app.test_request_context(): + assert babel_ext.get_timezone() == timezone('Europe/Vienna') + + def test_convert_timezone(self): + app = flask.Flask(__name__) + babel_ext.Babel(app) + dt = datetime(2010, 4, 12, 13, 46) + + with app.test_request_context(): + dt_utc = babel_ext.to_utc(dt) + assert dt_utc.tzinfo is None - lazy_string = lazy_gettext(u'test') - assert u'Hello %s' % lazy_string == u'Hello test' + dt_usertz = babel_ext.to_user_timezone(dt_utc) + assert dt_usertz is not None if __name__ == '__main__':