forked from dag/flask-zodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
74 lines (53 loc) · 1.58 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
72
73
74
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.fixturenames:
apps = []
for storage in STORAGES:
app = Flask(__name__)
db.init_app(app)
app.config["ZODB_STORAGE"] = storage
apps.append(app)
metafunc.parametrize("app", apps)
def test_single_app_shortcut():
app = Flask(__name__)
zodb = ZODB(app)
assert app.extensions["zodb"].zodb is zodb
def test_connection(app):
with app.app_context():
assert db.is_connected
db["answer"] = 42
assert db["answer"] == 42
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
def test_transfer_count(app):
with app.app_context():
db['answer'] = 42
transaction.commit()
assert db.transfers