Skip to content

Instantly share code, notes, and snippets.

@purpleP
Created May 16, 2016
Embed
What would you like to do?
from pytest import fixture, mark
from sqlalchemy import (
create_engine,
Column,
String,
Integer,
ForeignKey
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy.sql import select
from sqlalchemy.sql.expression import union, literal, alias
from itertools import chain
from subprocess import call
from datetime import date
Base = declarative_base()
@fixture()
def mysql(request):
call(('docker run -itd --name testdb -p 3306:3306 '
'-e MYSQL_ROOT_PASSWORD=root percona'), shell=True)
def fin():
call('docker rm -f testdb')
request.addfinilizer(fin)
return create_engine('mysql://root:root@127.0.0.1:3306')
@fixture()
def sqlite():
return create_engine('sqlite:///:memory:')
@fixture(params=('sqlite', 'mysql'))
def session(request):
engine = request.getfuncargvalue(request.param)
Base.metadata.create_all(engine)
return sessionmaker(bind=engine)()
def test_date(session):
dates = (
date(2016, 1, 1),
date(2016, 1, 2),
)
selects = tuple(select((literal(d),)) for d in dates)
data = alias(union(*selects, use_labels=True), 'dates')
stmt = select((data,))
result = session.execute(stmt).fetchall()
assert tuple(chain.from_iterable(result)) == dates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment