Skip to content

Commit

Permalink
flag to test using sqlalchemy or alembic for db init
Browse files Browse the repository at this point in the history
  • Loading branch information
brassy-endomorph committed Sep 24, 2024
1 parent f6e46c1 commit 652b124
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ jobs:
run: poetry run make lint

- name: Test
run: poetry run make test
run: make test

- name: Test with alembic
run: make test
env:
PYTEST_ADDOPTS: '--alembic'
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ endif
.PHONY: test
test: ## Run the test suite
docker compose run --rm app \
poetry run pytest --cov hushline --cov-report term --cov-report html -vv tests/$(test)
poetry run pytest --cov hushline --cov-report term --cov-report html -vv $(PYTEST_ADDOPTS) tests/$(test)
5 changes: 5 additions & 0 deletions docs/DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,8 @@ Create a new revision:
```sh
make revision name="my db changes"
```

Test the migrations:
```sh
PYTEST_ADDOPTS='--alembic' make test
```
35 changes: 32 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import string
import time
from contextlib import contextmanager
from typing import Generator
from typing import TYPE_CHECKING, Any, Generator
from uuid import uuid4

import flask_migrate
import pytest
from flask import Flask
from flask.testing import FlaskClient
Expand All @@ -19,10 +20,24 @@
from hushline.db import db
from hushline.model import User, Username

if TYPE_CHECKING:
from _pytest.config.argparsing import Parser
else:
Parser = Any


CONN_FMT_STR = "postgresql+psycopg://hushline:hushline@postgres:5432/{database}"
TEMPLATE_DB_NAME = "app_db_template"


def pytest_addoption(parser: Parser) -> None:
parser.addoption(
"--alembic",
action="store_true",
help="Use alembic migrations for DB initialization",
)


def random_name(size: int) -> str:
return "".join([random.choice(string.ascii_lowercase) for _ in range(size)]) # noqa: S311

Expand All @@ -42,7 +57,7 @@ def temp_session(conn_str: str) -> Generator[Session, None, None]:


@pytest.fixture(scope="session")
def _db_template() -> Generator[None, None, None]:
def _db_template(request: pytest.FixtureRequest) -> Generator[None, None, None]:
"""A template database that all other databases are created from.
Effectively allows caching of `db.create_all()`"""

Expand All @@ -62,7 +77,11 @@ def _db_template() -> Generator[None, None, None]:
raise

db_uri = CONN_FMT_STR.format(database=TEMPLATE_DB_NAME)
init_db_via_create_all(db_uri)

if request.config.getoption("--alembic"):
init_db_via_alembic(db_uri)
else:
init_db_via_create_all(db_uri)

yield

Expand All @@ -82,6 +101,16 @@ def init_db_via_create_all(db_uri: str) -> None:
db.session.connection().connection.invalidate() # type: ignore


def init_db_via_alembic(db_uri: str) -> None:
# dumb hack to easily get the create_all() functionality
os.environ["SQLALCHEMY_DATABASE_URI"] = db_uri
app = create_app()
with app.app_context():
flask_migrate.upgrade()
db.session.close()
db.session.connection().connection.invalidate() # type: ignore


@pytest.fixture()
def database(_db_template: None) -> str:
"""A clean Postgres database from the template with DDLs applied"""
Expand Down

0 comments on commit 652b124

Please sign in to comment.