Skip currency tests if babel not installed

This commit is contained in:
Konsta Vesterinen
2015-04-07 23:27:27 +03:00
parent c3e61756ce
commit 2bdebeeed8
4 changed files with 32 additions and 7 deletions

View File

@@ -1,8 +1,12 @@
# -*- coding: utf-8 -*-
babel = None
try:
import babel
except ImportError:
pass
import six
from babel.numbers import get_currency_symbol
from sqlalchemy_utils import i18n
from sqlalchemy_utils import i18n, ImproperlyConfigured
from sqlalchemy_utils.utils import str_coercible
@@ -54,6 +58,10 @@ class Currency(object):
"""
def __init__(self, code):
if babel is None:
raise ImproperlyConfigured(
"'babel' package is required in order to use Currency class."
)
if isinstance(code, Currency):
self.code = code
elif isinstance(code, six.string_types):
@@ -75,7 +83,7 @@ class Currency(object):
@property
def symbol(self):
return get_currency_symbol(self.code, i18n.get_locale())
return babel.numbers.get_currency_symbol(self.code, i18n.get_locale())
@property
def name(self):

View File

@@ -1,6 +1,12 @@
babel = None
try:
import babel
except ImportError:
pass
import six
from sqlalchemy import types
from sqlalchemy_utils import ImproperlyConfigured
from sqlalchemy_utils.primitives import Currency
from .scalar_coercible import ScalarCoercible
@@ -50,6 +56,14 @@ class CurrencyType(types.TypeDecorator, ScalarCoercible):
impl = types.String(3)
python_type = Currency
def __init__(self, *args, **kwargs):
if babel is None:
raise ImproperlyConfigured(
"'babel' package is required in order to use CurrencyType."
)
super(CurrencyType, self).__init__(*args, **kwargs)
def process_bind_param(self, value, dialect):
if isinstance(value, Currency):
return value.code

View File

@@ -1,14 +1,15 @@
# -*- coding: utf-8 -*-
import six
from babel import Locale
from pytest import mark, raises
from sqlalchemy_utils import Currency, i18n
from sqlalchemy_utils.primitives.currency import babel # noqa
@mark.skipif('babel is None')
class TestCurrency(object):
def setup_method(self, method):
i18n.get_locale = lambda: Locale('en')
i18n.get_locale = lambda: babel.Locale('en')
def test_init(self):
assert Currency('USD') == Currency(Currency('USD'))

View File

@@ -1,15 +1,17 @@
# -*- coding: utf-8 -*-
import sqlalchemy as sa
from babel import Locale
from pytest import mark
from sqlalchemy_utils import Currency, CurrencyType, i18n
from sqlalchemy_utils.types.currency import babel
from tests import TestCase
@mark.skipif('babel is None')
class TestCurrencyType(TestCase):
def setup_method(self, method):
TestCase.setup_method(self, method)
i18n.get_locale = lambda: Locale('en')
i18n.get_locale = lambda: babel.Locale('en')
def create_models(self):
class User(self.Base):