Skip to content

Commit

Permalink
Merge pull request #1258 from DSD-DBS/feat-improve-pytest-execution-time
Browse files Browse the repository at this point in the history
feat: Improve test runtime by only calling `migrate_db` once
  • Loading branch information
MoritzWeber0 authored Feb 27, 2024
2 parents a36efaa + bb97425 commit 6af1ddb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
40 changes: 25 additions & 15 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,48 @@


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

_engine = sqlalchemy.create_engine(database_url.replace("***", "test"))
with pytest.MonkeyPatch.context() as monkeypatch:
_engine = sqlalchemy.create_engine(
database_url.replace("***", "test")
)

yield _engine
session_local = orm.sessionmaker(
autocommit=False, autoflush=False, bind=_engine
)

monkeypatch.setattr(database, "engine", _engine)
monkeypatch.setattr(database, "SessionLocal", session_local)

migration.migrate_db(
_engine, str(_engine.url).replace("***", "test")
)

yield _engine


@pytest.fixture(name="db")
def fixture_db(
postgresql: engine.Engine, monkeypatch: pytest.MonkeyPatch
) -> t.Generator[orm.Session, None, None]:
session_local = orm.sessionmaker(
with orm.sessionmaker(
autocommit=False, autoflush=False, bind=postgresql
)

monkeypatch.setattr(database, "engine", postgresql)
monkeypatch.setattr(database, "SessionLocal", session_local)

delete_all_tables_if_existent(postgresql)
migration.migrate_db(
postgresql, str(postgresql.url).replace("***", "test")
)

with session_local() as session:
)() as session:

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

app.dependency_overrides[database.get_db] = mock_get_db

def commit(*args, **kwargs):
session.flush()
session.expire_all()

monkeypatch.setattr(session, "commit", commit)

yield session


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def test_get_all_pipelines_of_capellamodel(

assert response.status_code == 200
assert len(response.json()) == 1
assert response.json()[0]["id"] == 1


@pytest.mark.usefixtures(
Expand Down
8 changes: 6 additions & 2 deletions backend/tests/users/test_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ def test_create_and_delete_token(
response = client.post("/api/v1/users/current/tokens", json=POST_TOKEN)
assert response.status_code == 200

response = client.delete("/api/v1/users/current/tokens/1")
token_id = response.json()["id"]

response = client.delete(f"/api/v1/users/current/tokens/{token_id}")
assert response.status_code == 204


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

response = client.delete("/api/v1/users/current/tokens/1")
token_id = response.json()[0]["id"]

response = client.delete(f"/api/v1/users/current/tokens/{token_id}")
assert response.status_code == 204

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

0 comments on commit 6af1ddb

Please sign in to comment.