File: test_weekdays.py

package info (click to toggle)
python-sqlalchemy-utils 0.41.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,252 kB
  • sloc: python: 13,566; makefile: 141
file content (68 lines) | stat: -rw-r--r-- 1,799 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import pytest
import sqlalchemy as sa

from sqlalchemy_utils import i18n
from sqlalchemy_utils.compat import _select_args
from sqlalchemy_utils.primitives import WeekDays
from sqlalchemy_utils.types import WeekDaysType


@pytest.fixture
def Schedule(Base):
    class Schedule(Base):
        __tablename__ = 'schedule'
        id = sa.Column(sa.Integer, primary_key=True)
        working_days = sa.Column(WeekDaysType)

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


@pytest.fixture
def init_models(Schedule):
    pass


@pytest.fixture
def set_get_locale():
    i18n.get_locale = lambda: i18n.babel.Locale('en')


@pytest.mark.usefixtures('set_get_locale')
@pytest.mark.skipif('i18n.babel is None')
class WeekDaysTypeTestCase:
    def test_color_parameter_processing(self, session, Schedule):
        schedule = Schedule(
            working_days=b'0001111'
        )
        session.add(schedule)
        session.commit()

        schedule = session.query(Schedule).first()
        assert isinstance(schedule.working_days, WeekDays)

    def test_scalar_attributes_get_coerced_to_objects(self, Schedule):
        schedule = Schedule(working_days=b'1010101')

        assert isinstance(schedule.working_days, WeekDays)

    def test_compilation(self, Schedule, session):
        query = sa.select(*_select_args(Schedule.working_days))
        # the type should be cacheable and not throw exception
        session.execute(query)


@pytest.mark.usefixtures('sqlite_memory_dsn')
class TestWeekDaysTypeOnSQLite(WeekDaysTypeTestCase):
    pass


@pytest.mark.usefixtures('postgresql_dsn')
class TestWeekDaysTypeOnPostgres(WeekDaysTypeTestCase):
    pass


@pytest.mark.usefixtures('mysql_dsn')
class TestWeekDaysTypeOnMySQL(WeekDaysTypeTestCase):
    pass