Skip to content

Commit 29c2241

Browse files
committedFeb 8, 2019
Test for custom column types
1 parent 5f6db4d commit 29c2241

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed
 

‎tests/test_databases.py

+49-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313
DATABASE_URLS = [url.strip() for url in os.environ["TEST_DATABASE_URLS"].split(",")]
1414

1515

16+
class MyEpochType(sqlalchemy.types.TypeDecorator):
17+
impl = sqlalchemy.Integer
18+
19+
epoch = datetime.date(1970, 1, 1)
20+
21+
def process_bind_param(self, value, dialect):
22+
return (value - self.epoch).days
23+
24+
def process_result_value(self, value, dialect):
25+
return self.epoch + datetime.timedelta(days=value)
26+
27+
1628
metadata = sqlalchemy.MetaData()
1729

1830
notes = sqlalchemy.Table(
@@ -23,6 +35,7 @@
2335
sqlalchemy.Column("completed", sqlalchemy.Boolean),
2436
)
2537

38+
# Used to test DateTime
2639
articles = sqlalchemy.Table(
2740
"articles",
2841
metadata,
@@ -31,13 +44,23 @@
3144
sqlalchemy.Column("published", sqlalchemy.DateTime),
3245
)
3346

47+
# Used to test JSON
3448
session = sqlalchemy.Table(
3549
"session",
3650
metadata,
3751
sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True),
3852
sqlalchemy.Column("data", sqlalchemy.JSON),
3953
)
4054

55+
# Used to test custom column types
56+
custom_date = sqlalchemy.Table(
57+
"custom_date",
58+
metadata,
59+
sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True),
60+
sqlalchemy.Column("title", sqlalchemy.String(length=100)),
61+
sqlalchemy.Column("published", MyEpochType),
62+
)
63+
4164

4265
@pytest.fixture(autouse=True, scope="module")
4366
def create_test_database():
@@ -226,7 +249,7 @@ async def test_transaction_rollback_low_level(database_url):
226249
@async_adapter
227250
async def test_datetime_field(database_url):
228251
"""
229-
Test DataTime fields, to ensure records are coerced to proper Python types.
252+
Test DataTime columns, to ensure records are coerced to/from proper Python types.
230253
"""
231254

232255
async with Database(database_url) as database:
@@ -250,7 +273,7 @@ async def test_datetime_field(database_url):
250273
@async_adapter
251274
async def test_json_field(database_url):
252275
"""
253-
Test JSON fields, to ensure correct cross-database support.
276+
Test JSON columns, to ensure correct cross-database support.
254277
"""
255278

256279
async with Database(database_url) as database:
@@ -265,3 +288,27 @@ async def test_json_field(database_url):
265288
results = await database.fetch_all(query=query)
266289
assert len(results) == 1
267290
assert results[0]["data"] == {"text": "hello", "boolean": True, "int": 1}
291+
292+
293+
@pytest.mark.parametrize("database_url", DATABASE_URLS)
294+
@async_adapter
295+
async def test_custom_field(database_url):
296+
"""
297+
Test custom column types.
298+
"""
299+
300+
async with Database(database_url) as database:
301+
async with database.transaction(force_rollback=True):
302+
today = datetime.date.today()
303+
304+
# execute()
305+
query = custom_date.insert()
306+
values = {"title": "Hello, world", "published": today}
307+
await database.execute(query, values)
308+
309+
# fetch_all()
310+
query = custom_date.select()
311+
results = await database.fetch_all(query=query)
312+
assert len(results) == 1
313+
assert results[0]["title"] == "Hello, world"
314+
assert results[0]["published"] == today

0 commit comments

Comments
 (0)
Please sign in to comment.