Skip to content

Commit 016154a

Browse files
authored
improve pytest setup
1 parent bf6b0a1 commit 016154a

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

tests/conftest.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
import pytest
2-
from flask import Flask, g
2+
from flask import Flask, current_app, g
33
from flask.testing import FlaskClient
44

55
from teufa import db as dbm
66
from teufa.app import create_app
77
from teufa.ext import db
88

99

10+
def pytest_configure():
11+
app = create_app()
12+
app.app_context().push()
13+
14+
1015
@pytest.fixture
1116
def app():
12-
app = create_app()
13-
with app.app_context():
14-
dbm.Base.metadata.create_all(db.engine)
17+
dbm.Base.metadata.create_all(db.engine)
1518

16-
tenant = dbm.Tenant(
17-
name="Default",
18-
hostname="localhost",
19-
)
20-
db.session.add(tenant)
21-
db.session.commit()
22-
g.tenant = tenant
19+
tenant = dbm.Tenant(
20+
name="Default",
21+
hostname="localhost",
22+
)
23+
db.session.add(tenant)
24+
db.session.commit()
2325

24-
yield app
26+
with current_app.app_context():
27+
yield current_app
2528

26-
dbm.Base.metadata.drop_all(db.engine)
29+
dbm.Base.metadata.drop_all(db.engine)
2730

2831

2932
@pytest.fixture

teufa/app.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ def create_app():
1515

1616
@app.before_request
1717
def before_request():
18-
if not hasattr(g, "tenant"):
19-
hostname = request.host.split(":")[0]
20-
g.tenant = db.session.scalars(
21-
select(dbm.Tenant).filter_by(hostname=hostname).limit(1)
22-
).first()
18+
hostname = request.host.split(":")[0]
19+
g.tenant = db.session.scalars(
20+
select(dbm.Tenant).filter_by(hostname=hostname).limit(1)
21+
).first()
2322

2423
@app.teardown_request
2524
def teardown_request(exc):

0 commit comments

Comments
 (0)