forked from kvesteri/postgresql-audit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conftest.py
115 lines (85 loc) · 2.48 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
# -*- 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 versioning_manager
@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 'postgres://{}@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 = True
yield engine
engine.dispose()
@pytest.yield_fixture()
def connection(engine):
conn = engine.connect()
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.fixture()
def activity_cls(base):
versioning_manager.init(base)
return versioning_manager.activity_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, connection, session, models, activity_cls):
sa.orm.configure_mappers()
tx = connection.begin()
connection.execute('DROP SCHEMA IF EXISTS audit CASCADE')
versioning_manager.activity_cls.__table__.create(connection)
base.metadata.create_all(connection)
tx.commit()
yield
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