Skip to content

Commit c9957b9

Browse files
committed
support for sqlalchemy augmented types
1 parent f085551 commit c9957b9

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

mixer/backend/sqlalchemy.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from sqlalchemy import func
88
# from sqlalchemy.orm.interfaces import MANYTOONE
99
from sqlalchemy.orm.collections import InstrumentedList
10+
from sqlalchemy.sql.type_api import TypeDecorator
1011
try:
1112
from sqlalchemy.orm.relationships import RelationshipProperty
1213
except ImportError:
@@ -174,6 +175,12 @@ def make_fabric(self, column, field_name=None, fake=False, kwargs=None): # noqa
174175
).blend, **kwargs)
175176

176177
ftype = type(column.type)
178+
179+
# augmented types created with TypeDecorator
180+
# don't directly inherit from the base types
181+
if TypeDecorator in ftype.__bases__:
182+
ftype = ftype.impl
183+
177184
stype = self.__factory.cls_to_simple(ftype)
178185

179186
if stype is str:

tests/test_sqlalchemy.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
)
1616
from sqlalchemy.orm import relation, sessionmaker, relationship, scoped_session
1717
from sqlalchemy.ext.declarative import declarative_base
18+
from sqlalchemy import types
1819
from random import randrange
1920
import pytest
2021

@@ -33,6 +34,10 @@ class Profile(BASE):
3334
user = relationship("User", uselist=False, backref="profile")
3435

3536

37+
class AugmentedType(types.TypeDecorator):
38+
impl = String
39+
40+
3641
class User(BASE):
3742
__tablename__ = 'user'
3843

@@ -44,8 +49,8 @@ class User(BASE):
4449
created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
4550
enum = Column(Enum('one', 'two'), nullable=False)
4651
random = Column(Integer, default=lambda: randrange(993, 995))
47-
4852
profile_id = Column(Integer, ForeignKey('profile.id'), nullable=False)
53+
augmented = Column(AugmentedType, default='augmented', nullable=False)
4954

5055

5156
class Role(BASE):
@@ -81,6 +86,7 @@ def test_typemixer():
8186
assert user.profile
8287
assert user.profile.user == user
8388
assert user.enum in ('one', 'two')
89+
assert user.augmented == 'augmented'
8490

8591
user = mixer.blend(name='John', updated_at=mixer.RANDOM)
8692
assert user.name == 'John'

0 commit comments

Comments
 (0)