Skip currency tests if babel not installed
This commit is contained in:
@@ -1,8 +1,12 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
babel = None
|
||||||
|
try:
|
||||||
|
import babel
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
import six
|
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
|
from sqlalchemy_utils.utils import str_coercible
|
||||||
|
|
||||||
|
|
||||||
@@ -54,6 +58,10 @@ class Currency(object):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
def __init__(self, code):
|
def __init__(self, code):
|
||||||
|
if babel is None:
|
||||||
|
raise ImproperlyConfigured(
|
||||||
|
"'babel' package is required in order to use Currency class."
|
||||||
|
)
|
||||||
if isinstance(code, Currency):
|
if isinstance(code, Currency):
|
||||||
self.code = code
|
self.code = code
|
||||||
elif isinstance(code, six.string_types):
|
elif isinstance(code, six.string_types):
|
||||||
@@ -75,7 +83,7 @@ class Currency(object):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def symbol(self):
|
def symbol(self):
|
||||||
return get_currency_symbol(self.code, i18n.get_locale())
|
return babel.numbers.get_currency_symbol(self.code, i18n.get_locale())
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
@@ -1,6 +1,12 @@
|
|||||||
|
babel = None
|
||||||
|
try:
|
||||||
|
import babel
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
import six
|
import six
|
||||||
from sqlalchemy import types
|
from sqlalchemy import types
|
||||||
|
|
||||||
|
from sqlalchemy_utils import ImproperlyConfigured
|
||||||
from sqlalchemy_utils.primitives import Currency
|
from sqlalchemy_utils.primitives import Currency
|
||||||
|
|
||||||
from .scalar_coercible import ScalarCoercible
|
from .scalar_coercible import ScalarCoercible
|
||||||
@@ -50,6 +56,14 @@ class CurrencyType(types.TypeDecorator, ScalarCoercible):
|
|||||||
impl = types.String(3)
|
impl = types.String(3)
|
||||||
python_type = Currency
|
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):
|
def process_bind_param(self, value, dialect):
|
||||||
if isinstance(value, Currency):
|
if isinstance(value, Currency):
|
||||||
return value.code
|
return value.code
|
||||||
|
@@ -1,14 +1,15 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import six
|
import six
|
||||||
from babel import Locale
|
|
||||||
from pytest import mark, raises
|
from pytest import mark, raises
|
||||||
|
|
||||||
from sqlalchemy_utils import Currency, i18n
|
from sqlalchemy_utils import Currency, i18n
|
||||||
|
from sqlalchemy_utils.primitives.currency import babel # noqa
|
||||||
|
|
||||||
|
|
||||||
|
@mark.skipif('babel is None')
|
||||||
class TestCurrency(object):
|
class TestCurrency(object):
|
||||||
def setup_method(self, method):
|
def setup_method(self, method):
|
||||||
i18n.get_locale = lambda: Locale('en')
|
i18n.get_locale = lambda: babel.Locale('en')
|
||||||
|
|
||||||
def test_init(self):
|
def test_init(self):
|
||||||
assert Currency('USD') == Currency(Currency('USD'))
|
assert Currency('USD') == Currency(Currency('USD'))
|
||||||
|
@@ -1,15 +1,17 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
from babel import Locale
|
from pytest import mark
|
||||||
|
|
||||||
from sqlalchemy_utils import Currency, CurrencyType, i18n
|
from sqlalchemy_utils import Currency, CurrencyType, i18n
|
||||||
|
from sqlalchemy_utils.types.currency import babel
|
||||||
from tests import TestCase
|
from tests import TestCase
|
||||||
|
|
||||||
|
|
||||||
|
@mark.skipif('babel is None')
|
||||||
class TestCurrencyType(TestCase):
|
class TestCurrencyType(TestCase):
|
||||||
def setup_method(self, method):
|
def setup_method(self, method):
|
||||||
TestCase.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):
|
def create_models(self):
|
||||||
class User(self.Base):
|
class User(self.Base):
|
||||||
|
Reference in New Issue
Block a user