Add tests for WeekDaysType

This commit is contained in:
Konsta Vesterinen
2014-03-27 13:39:35 +02:00
parent 82d1055df6
commit f7bdf2547b
7 changed files with 87 additions and 6 deletions

View File

@@ -3,6 +3,7 @@ addons:
before_script: before_script:
- psql -c 'create database sqlalchemy_utils_test;' -U postgres - psql -c 'create database sqlalchemy_utils_test;' -U postgres
- mysql -e 'create database sqlalchemy_continuum_test;'
language: python language: python

View File

@@ -96,3 +96,12 @@ UUIDType
.. autoclass:: UUIDType .. autoclass:: UUIDType
WeekDaysType
^^^^^^^^^^^^
.. module:: sqlalchemy_utils.types.weekdays
.. autoclass:: WeekDaysType

View File

@@ -53,6 +53,7 @@ from .types import (
TSVectorType, TSVectorType,
URLType, URLType,
UUIDType, UUIDType,
WeekDaysType
) )
@@ -113,4 +114,5 @@ __all__ = (
TSVectorType, TSVectorType,
URLType, URLType,
UUIDType, UUIDType,
WeekDaysType
) )

View File

@@ -21,7 +21,7 @@ from .timezone import TimezoneType
from .ts_vector import TSVectorType from .ts_vector import TSVectorType
from .url import URLType from .url import URLType
from .uuid import UUIDType from .uuid import UUIDType
from .weekdays import WeekDay, WeekDays, WeekDaysType from .weekdays import WeekDaysType
__all__ = ( __all__ = (
@@ -49,8 +49,6 @@ __all__ = (
TSVectorType, TSVectorType,
URLType, URLType,
UUIDType, UUIDType,
WeekDay,
WeekDays,
WeekDaysType, WeekDaysType,
) )

View File

@@ -0,0 +1,20 @@
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import BIT
class BitType(sa.types.TypeDecorator):
"""
BitType offers way of saving BITs into database.
"""
impl = sa.types.BINARY
def __init__(self, length=1, **kwargs):
self.length = length
sa.types.TypeDecorator.__init__(self, **kwargs)
def load_dialect_impl(self, dialect):
# Use the native BIT type for drivers that has it.
if dialect.name == 'postgresql':
return dialect.type_descriptor(BIT(self.length))
else:
return dialect.type_descriptor(type(self.impl)(self.length))

View File

@@ -1,11 +1,12 @@
import six import six
from sqlalchemy import types from sqlalchemy import types
from sqlalchemy.dialects.postgresql import BIT
from sqlalchemy_utils.primitives import WeekDay, WeekDays from sqlalchemy_utils.primitives import WeekDay, WeekDays
from .scalar_coercible import ScalarCoercible
from .bit import BitType
class WeekDaysType(types.TypeDecorator): class WeekDaysType(types.TypeDecorator, ScalarCoercible):
impl = BIT(WeekDay.NUM_WEEK_DAYS) impl = BitType(WeekDay.NUM_WEEK_DAYS)
def process_bind_param(self, value, dialect): def process_bind_param(self, value, dialect):
if isinstance(value, WeekDays): if isinstance(value, WeekDays):
@@ -17,3 +18,8 @@ class WeekDaysType(types.TypeDecorator):
def process_result_value(self, value, dialect): def process_result_value(self, value, dialect):
if value is not None: if value is not None:
return WeekDays(value) return WeekDays(value)
def _coerce(self, value):
if value is not None and not isinstance(value, WeekDays):
return WeekDays(value)
return value

View File

@@ -0,0 +1,45 @@
import sqlalchemy as sa
from sqlalchemy_utils.types import WeekDaysType
from sqlalchemy_utils.primitives import WeekDays
from tests import TestCase
class WeekDaysTypeTestCase(TestCase):
def create_models(self):
class Schedule(self.Base):
__tablename__ = 'schedule'
id = sa.Column(sa.Integer, primary_key=True)
working_days = sa.Column(WeekDaysType)
def __repr__(self):
return 'Schedule(%r)' % self.id
self.Schedule = Schedule
def test_color_parameter_processing(self):
schedule = self.Schedule(
working_days='0001111'
)
self.session.add(schedule)
self.session.commit()
schedule = self.session.query(self.Schedule).first()
assert isinstance(schedule.working_days, WeekDays)
def test_scalar_attributes_get_coerced_to_objects(self):
schedule = self.Schedule(working_days=u'1010101')
assert isinstance(schedule.working_days, WeekDays)
class TestWeekDaysTypeOnSQLite(WeekDaysTypeTestCase):
dns = 'sqlite:///:memory:'
class TestWeekDaysTypeOnPostgres(WeekDaysTypeTestCase):
dns = 'postgres://postgres@localhost/sqlalchemy_utils_test'
class TestWeekDaysTypeOnMySQL(WeekDaysTypeTestCase):
dns = 'mysql+pymysql://travis@localhost/sqlalchemy_utils_test'