Task #6205 » test_flask_sqlalchemy.py
1 |
#!/usr/bin/env python3
|
---|---|
2 |
from flask import Flask |
3 |
from flask_sqlalchemy import SQLAlchemy |
4 |
import time |
5 |
|
6 |
app = Flask(__name__) |
7 |
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://' |
8 |
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False |
9 |
db = SQLAlchemy(app) |
10 |
|
11 |
|
12 |
def isolated(): |
13 |
print(db.session.query('NOW()').first()[0]) |
14 |
|
15 |
|
16 |
def main(): |
17 |
print('Flask-SQLAlchemy session lifetime management test (times in one section should always differ)') |
18 |
|
19 |
print('No commit') |
20 |
print(db.session.query('NOW()').first()[0]) |
21 |
time.sleep(1) |
22 |
print(db.session.query('NOW()').first()[0]) |
23 |
print(db.session.query('NOW()').first()[0]) |
24 |
print() |
25 |
|
26 |
print('Isolated context') |
27 |
isolated() |
28 |
time.sleep(1) |
29 |
isolated() |
30 |
print() |
31 |
|
32 |
print('Commit - simple') |
33 |
db.session.commit() |
34 |
print(db.session.query('NOW()').first()[0]) |
35 |
time.sleep(1) |
36 |
print(db.session.query('NOW()').first()[0]) |
37 |
print() |
38 |
|
39 |
print('Commit - transaction start') |
40 |
db.session.commit() |
41 |
print(db.session.query('NOW()').first()[0]) |
42 |
db.session.commit() |
43 |
time.sleep(4) |
44 |
print(db.session.query('NOW()').first()[0]) |
45 |
print() |
46 |
|
47 |
print('Rollback') |
48 |
db.session.rollback() |
49 |
print(db.session.query('NOW()').first()[0]) |
50 |
db.session.rollback() |
51 |
time.sleep(1) |
52 |
print(db.session.query('NOW()').first()[0]) |
53 |
print() |
54 |
|
55 |
|
56 |
if __name__ == '__main__': |
57 |
main() |