forked from kvesteri/sqlalchemy-utils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conftest.py
203 lines (148 loc) · 4.93 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import os
import warnings
import pytest
import sqlalchemy as sa
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base, synonym_for
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import sessionmaker
from sqlalchemy_utils import (
aggregates,
coercion_listener,
i18n,
InstrumentedList
)
from sqlalchemy_utils.types.pg_composite import remove_composite_listeners
@sa.event.listens_for(sa.engine.Engine, 'before_cursor_execute')
def count_sql_calls(conn, cursor, statement, parameters, context, executemany):
try:
conn.query_count += 1
except AttributeError:
conn.query_count = 0
warnings.simplefilter('error', sa.exc.SAWarning)
sa.event.listen(sa.orm.mapper, 'mapper_configured', coercion_listener)
def get_locale():
class Locale():
territories = {'FI': 'Finland'}
return Locale()
@pytest.fixture(scope='session')
def db_name():
return os.environ.get('SQLALCHEMY_UTILS_TEST_DB', 'sqlalchemy_utils_test')
@pytest.fixture(scope='session')
def postgresql_db_user():
return os.environ.get('SQLALCHEMY_UTILS_TEST_POSTGRESQL_USER', 'postgres')
@pytest.fixture(scope='session')
def mysql_db_user():
return os.environ.get('SQLALCHEMY_UTILS_TEST_MYSQL_USER', 'root')
@pytest.fixture
def postgresql_dsn(postgresql_db_user, db_name):
return 'postgres://{0}@localhost/{1}'.format(postgresql_db_user, db_name)
@pytest.fixture
def mysql_dsn(mysql_db_user, db_name):
return 'mysql+pymysql://{0}@localhost/{1}'.format(mysql_db_user, db_name)
@pytest.fixture
def sqlite_memory_dsn():
return 'sqlite:///:memory:'
@pytest.fixture
def sqlite_none_database_dsn():
return 'sqlite://'
@pytest.fixture
def sqlite_file_dsn():
return 'sqlite:///{0}.db'.format(db_name)
@pytest.fixture
def dsn(request):
if 'postgresql_dsn' in request.fixturenames:
return request.getfuncargvalue('postgresql_dsn')
elif 'mysql_dsn' in request.fixturenames:
return request.getfuncargvalue('mysql_dsn')
elif 'sqlite_file_dsn' in request.fixturenames:
return request.getfuncargvalue('sqlite_file_dsn')
elif 'sqlite_memory_dsn' in request.fixturenames:
pass # Return default
return request.getfuncargvalue('sqlite_memory_dsn')
@pytest.fixture
def engine(dsn):
engine = create_engine(dsn)
# engine.echo = True
return engine
@pytest.fixture
def connection(engine):
return engine.connect()
@pytest.fixture
def Base():
return declarative_base()
@pytest.fixture
def User(Base):
class User(Base):
__tablename__ = 'user'
id = sa.Column(sa.Integer, autoincrement=True, primary_key=True)
name = sa.Column(sa.Unicode(255))
return User
@pytest.fixture
def Category(Base):
class Category(Base):
__tablename__ = 'category'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.Unicode(255))
title = sa.Column(sa.Unicode(255))
@hybrid_property
def full_name(self):
return u'%s %s' % (self.title, self.name)
@full_name.expression
def full_name(self):
return sa.func.concat(self.title, ' ', self.name)
@hybrid_property
def articles_count(self):
return len(self.articles)
@articles_count.expression
def articles_count(cls):
Article = Base._decl_class_registry['Article']
return (
sa.select([sa.func.count(Article.id)])
.where(Article.category_id == cls.id)
.correlate(Article.__table__)
.label('article_count')
)
@property
def name_alias(self):
return self.name
@synonym_for('name')
@property
def name_synonym(self):
return self.name
return Category
@pytest.fixture
def Article(Base, Category):
class Article(Base):
__tablename__ = 'article'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.Unicode(255), index=True)
category_id = sa.Column(sa.Integer, sa.ForeignKey(Category.id))
category = sa.orm.relationship(
Category,
primaryjoin=category_id == Category.id,
backref=sa.orm.backref(
'articles',
collection_class=InstrumentedList
)
)
return Article
@pytest.fixture
def init_models(User, Category, Article):
pass
@pytest.fixture
def session(request, engine, connection, Base, init_models):
sa.orm.configure_mappers()
Base.metadata.create_all(connection)
Session = sessionmaker(bind=connection)
session = Session()
i18n.get_locale = get_locale
def teardown():
aggregates.manager.reset()
session.close_all()
Base.metadata.drop_all(connection)
remove_composite_listeners()
connection.close()
engine.dispose()
request.addfinalizer(teardown)
return session