forked from kvesteri/postgresql-audit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
136 lines (102 loc) · 2.86 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
# -*- coding: utf-8 -*-
import os
import pytest
import sqlalchemy as sa
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from postgresql_audit import VersioningManager
@pytest.fixture
def db_user():
return os.environ.get('POSTGRESQL_AUDIT_TEST_USER', 'postgres')
@pytest.fixture
def db_name():
return os.environ.get('POSTGRESQL_AUDIT_TEST_DB', 'postgresql_audit_test')
@pytest.fixture
def dns(db_user, db_name):
return 'postgresql://{}@localhost/{}'.format(db_user, db_name)
@pytest.fixture
def base():
return declarative_base()
@pytest.yield_fixture
def engine(dns):
engine = create_engine(dns)
engine.echo = bool(os.environ.get('POSTGRESQL_AUDIT_TEST_ECHO'))
yield engine
engine.dispose()
@pytest.yield_fixture
def connection(engine):
conn = engine.connect()
conn.execute('CREATE EXTENSION IF NOT EXISTS btree_gist')
yield conn
conn.close()
@pytest.yield_fixture
def session(connection):
Session = sessionmaker(bind=connection)
session = Session()
yield session
session.expunge_all()
session.close_all()
@pytest.yield_fixture
def versioning_manager(base):
vm = VersioningManager()
vm.init(base)
yield vm
vm.remove_listeners()
@pytest.fixture
def activity_cls(versioning_manager):
return versioning_manager.activity_cls
@pytest.fixture
def transaction_cls(versioning_manager):
return versioning_manager.transaction_cls
@pytest.fixture
def user_class(base):
class User(base):
__tablename__ = 'user'
__versioned__ = {}
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String(100))
age = sa.Column(sa.Integer)
return User
@pytest.fixture
def article_class(base):
class Article(base):
__tablename__ = 'article'
__versioned__ = {}
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String(100))
return Article
@pytest.fixture
def models(user_class, article_class):
return [user_class, article_class]
@pytest.yield_fixture
def table_creator(
base,
versioning_manager,
connection,
session,
models
):
sa.orm.configure_mappers()
tx = connection.begin()
versioning_manager.transaction_cls.__table__.create(connection)
versioning_manager.activity_cls.__table__.create(connection)
base.metadata.create_all(connection)
tx.commit()
session.commit()
yield
session.expunge_all()
base.metadata.drop_all(connection)
session.commit()
@pytest.fixture
def article(session, article_class):
article = article_class(name='Some article')
session.add(article)
session.commit()
return article
@pytest.fixture
def user(session, user_class):
user = user_class(name='John', age=15)
session.add(user)
session.commit()
return user