Fix composite type null handling

This commit is contained in:
Konsta Vesterinen
2015-05-30 08:17:50 +03:00
parent b7a9281679
commit afe4db7bbc
4 changed files with 40 additions and 1 deletions

View File

@@ -4,6 +4,12 @@ Changelog
Here you can see the full list of changes between each SQLAlchemy-Utils release. Here you can see the full list of changes between each SQLAlchemy-Utils release.
0.30.7 (2015-05-28)
^^^^^^^^^^^^^^^^^^^
- Fix CompositeType null handling
0.30.6 (2015-05-28) 0.30.6 (2015-05-28)
^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^

View File

@@ -90,4 +90,4 @@ from .types import ( # noqa
WeekDaysType WeekDaysType
) )
__version__ = '0.30.6' __version__ = '0.30.7'

View File

@@ -204,6 +204,8 @@ class CompositeType(UserDefinedType, SchemaType):
def bind_processor(self, dialect): def bind_processor(self, dialect):
def process(value): def process(value):
if value is None:
return None
processed_value = [] processed_value = []
for i, column in enumerate(self.columns): for i, column in enumerate(self.columns):
if isinstance(column.type, TypeDecorator): if isinstance(column.type, TypeDecorator):
@@ -219,6 +221,8 @@ class CompositeType(UserDefinedType, SchemaType):
def result_processor(self, dialect, coltype): def result_processor(self, dialect, coltype):
def process(value): def process(value):
if value is None:
return None
cls = value.__class__ cls = value.__class__
kwargs = {} kwargs = {}
for column in self.columns: for column in self.columns:

View File

@@ -219,6 +219,35 @@ class TestCompositeTypeWithRangeTypeInsideArray(TestCase):
) )
assert account.categories[1].name == 'good' assert account.categories[1].name == 'good'
def test_parameter_processing_with_nulls_as_composite_fields(self):
account = self.Account(
categories=[
(None, 'bad'),
(intervals.DecimalInterval([18, 20]), None)
]
)
self.session.add(account)
self.session.commit()
assert account.categories[0].scale is None
assert account.categories[0].name == 'bad'
assert (
account.categories[1].scale == intervals.DecimalInterval([18, 20])
)
assert account.categories[1].name is None
def test_parameter_processing_with_nulls_as_composites(self):
account = self.Account(
categories=[
(None, None),
None
]
)
self.session.add(account)
self.session.commit()
assert account.categories[0].scale is None
assert account.categories[0].name is None
assert account.categories[1] is None
class TestCompositeTypeWhenTypeAlreadyExistsInDatabase(TestCase): class TestCompositeTypeWhenTypeAlreadyExistsInDatabase(TestCase):
dns = 'postgres://postgres@localhost/sqlalchemy_utils_test' dns = 'postgres://postgres@localhost/sqlalchemy_utils_test'