This repository has been archived by the owner on Jun 12, 2020. It is now read-only.
forked from dag/flask-zodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
71 lines (50 loc) · 1.55 KB
/
tests.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
from __future__ import with_statement
import pytest
import transaction
from flask import Flask
from flask_zodb import ZODB
from ZODB.MappingStorage import MappingStorage
STORAGES = [
'memory://',
(MappingStorage, {}),
MappingStorage,
]
db = ZODB()
def pytest_generate_tests(metafunc):
if 'app' in metafunc.funcargnames:
for storage in STORAGES:
app = Flask(__name__)
db.init_app(app)
app.config['ZODB_STORAGE'] = storage
metafunc.addcall(dict(app=app))
def test_single_app_shortcut():
app = Flask(__name__)
zodb = ZODB(app)
assert app.extensions['zodb'].zodb is zodb
def test_connection(app):
assert not db.is_connected
with app.test_request_context():
assert not db.is_connected
db['answer'] = 42
assert db['answer'] == 42
assert db.is_connected
assert not db.is_connected
def test_commit_transaction(app):
with app.test_request_context():
db['answer'] = 42
with app.test_request_context():
assert db['answer'] == 42
def test_abort_transaction_on_failure(app):
with pytest.raises(ZeroDivisionError):
with app.test_request_context():
db['answer'] = 42
assert db['answer'] == 42
1/0
with app.test_request_context():
assert 'answer' not in db
def test_abort_transaction_if_doomed(app):
with app.test_request_context():
db['answer'] = 42
transaction.doom()
with app.test_request_context():
assert 'answer' not in db