Skip to content

Commit 5959b52

Browse files
committed
improve pytest setup
1 parent 2cc7689 commit 5959b52

File tree

4 files changed

+33
-22
lines changed

4 files changed

+33
-22
lines changed

pyrightconfig.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"venvPath": "/Users/brian/Library/Caches/pypoetry/virtualenvs/",
3+
"venv": "teufa-Dr74w4Ag-py3.12"
4+
}

tests/conftest.py

+22-14
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
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-
@pytest.fixture
11-
def app():
10+
def pytest_configure():
1211
app = create_app()
13-
with app.app_context():
14-
dbm.Base.metadata.create_all(db.engine)
12+
app.app_context().push()
13+
14+
15+
def create_default_tenant():
16+
tenant = dbm.Tenant(
17+
name="Default",
18+
hostname="localhost",
19+
)
20+
db.session.add(tenant)
21+
db.session.commit()
1522

16-
tenant = dbm.Tenant(
17-
name="Default",
18-
hostname="localhost",
19-
)
20-
db.session.add(tenant)
21-
db.session.commit()
22-
g.tenant = tenant
23+
g.tenant = tenant
24+
25+
26+
@pytest.fixture
27+
def app():
28+
dbm.Base.metadata.create_all(db.engine)
2329

24-
yield app
30+
with current_app.app_context():
31+
create_default_tenant()
32+
yield current_app
2533

26-
dbm.Base.metadata.drop_all(db.engine)
34+
dbm.Base.metadata.drop_all(db.engine)
2735

2836

2937
@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):

teufa/v1_api/flights.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ def get(self, flight_id):
4444
return dao.Error(message="Flight not found").model_dump_json(), 404
4545

4646
res = dao.GetFlightResponse(
47-
**{
48-
"flight": {
47+
flight=dao.Flight(
48+
**{
4949
"id": flight.id,
5050
"departure_icao": flight.departure_icao,
5151
"arrival_icao": flight.arrival_icao,
5252
"aircraft_id": flight.aircraft_id,
5353
}
54-
}
54+
)
5555
)
5656

5757
return res.model_dump_json()

0 commit comments

Comments
 (0)