File: test_currency.py

package info (click to toggle)
python-sqlalchemy-utils 0.30.12-2~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 1,056 kB
  • sloc: python: 10,350; makefile: 160
file content (39 lines) | stat: -rw-r--r-- 1,105 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# -*- coding: utf-8 -*-
import sqlalchemy as sa
from pytest import mark

from sqlalchemy_utils import Currency, CurrencyType, i18n
from tests import TestCase


@mark.skipif('i18n.babel is None')
class TestCurrencyType(TestCase):
    def setup_method(self, method):
        TestCase.setup_method(self, method)
        i18n.get_locale = lambda: i18n.babel.Locale('en')

    def create_models(self):
        class User(self.Base):
            __tablename__ = 'user'
            id = sa.Column(sa.Integer, primary_key=True)
            currency = sa.Column(CurrencyType)

            def __repr__(self):
                return 'User(%r)' % self.id

        self.User = User

    def test_parameter_processing(self):
        user = self.User(
            currency=Currency('USD')
        )

        self.session.add(user)
        self.session.commit()

        user = self.session.query(self.User).first()
        assert user.currency.name == u'US Dollar'

    def test_scalar_attributes_get_coerced_to_objects(self):
        user = self.User(currency='USD')
        assert isinstance(user.currency, Currency)