Skip to content

Commit 6af1ddb

Browse files
authored
Merge pull request #1258 from DSD-DBS/feat-improve-pytest-execution-time
feat: Improve test runtime by only calling `migrate_db` once
2 parents a36efaa + bb97425 commit 6af1ddb

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

backend/tests/conftest.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,38 +32,48 @@
3232

3333

3434
@pytest.fixture(name="postgresql", scope="session")
35-
def fixture_postgresql() -> t.Generator[engine.Engine, None, None]:
35+
def fixture_postgreql_engine() -> t.Generator[engine.Engine, None, None]:
3636
with postgres.PostgresContainer(image="postgres:14.1") as _postgres:
3737
database_url = _postgres.get_connection_url()
3838

39-
_engine = sqlalchemy.create_engine(database_url.replace("***", "test"))
39+
with pytest.MonkeyPatch.context() as monkeypatch:
40+
_engine = sqlalchemy.create_engine(
41+
database_url.replace("***", "test")
42+
)
4043

41-
yield _engine
44+
session_local = orm.sessionmaker(
45+
autocommit=False, autoflush=False, bind=_engine
46+
)
47+
48+
monkeypatch.setattr(database, "engine", _engine)
49+
monkeypatch.setattr(database, "SessionLocal", session_local)
50+
51+
migration.migrate_db(
52+
_engine, str(_engine.url).replace("***", "test")
53+
)
54+
55+
yield _engine
4256

4357

4458
@pytest.fixture(name="db")
4559
def fixture_db(
4660
postgresql: engine.Engine, monkeypatch: pytest.MonkeyPatch
4761
) -> t.Generator[orm.Session, None, None]:
48-
session_local = orm.sessionmaker(
62+
with orm.sessionmaker(
4963
autocommit=False, autoflush=False, bind=postgresql
50-
)
51-
52-
monkeypatch.setattr(database, "engine", postgresql)
53-
monkeypatch.setattr(database, "SessionLocal", session_local)
54-
55-
delete_all_tables_if_existent(postgresql)
56-
migration.migrate_db(
57-
postgresql, str(postgresql.url).replace("***", "test")
58-
)
59-
60-
with session_local() as session:
64+
)() as session:
6165

6266
def mock_get_db() -> orm.Session:
6367
return session
6468

6569
app.dependency_overrides[database.get_db] = mock_get_db
6670

71+
def commit(*args, **kwargs):
72+
session.flush()
73+
session.expire_all()
74+
75+
monkeypatch.setattr(session, "commit", commit)
76+
6777
yield session
6878

6979

backend/tests/projects/toolmodels/pipelines/test_pipelines.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ def test_get_all_pipelines_of_capellamodel(
6868

6969
assert response.status_code == 200
7070
assert len(response.json()) == 1
71-
assert response.json()[0]["id"] == 1
7271

7372

7473
@pytest.mark.usefixtures(

backend/tests/users/test_tokens.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ def test_create_and_delete_token(
7676
response = client.post("/api/v1/users/current/tokens", json=POST_TOKEN)
7777
assert response.status_code == 200
7878

79-
response = client.delete("/api/v1/users/current/tokens/1")
79+
token_id = response.json()["id"]
80+
81+
response = client.delete(f"/api/v1/users/current/tokens/{token_id}")
8082
assert response.status_code == 204
8183

8284

@@ -91,7 +93,9 @@ def test_token_lifecycle(
9193
response_string = response.content.decode("utf-8")
9294
assert len(json.loads(response_string)) == 1
9395

94-
response = client.delete("/api/v1/users/current/tokens/1")
96+
token_id = response.json()[0]["id"]
97+
98+
response = client.delete(f"/api/v1/users/current/tokens/{token_id}")
9599
assert response.status_code == 204
96100

97101
response = client.get("/api/v1/users/current/tokens")

0 commit comments

Comments
 (0)