Skip to content

Commit

Permalink
Merge pull request #38 from asacristani/pytest_alembic
Browse files Browse the repository at this point in the history
pytest_alembic
  • Loading branch information
asacristani authored Apr 14, 2024
2 parents 4954adf + cf6d424 commit 0ace0a2
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
run: pip install .

- name: Install pytest
run: pip install pytest pytest-cov pytest-asyncio pytest-mock
run: pip install pytest pytest-cov pytest-asyncio pytest-mock pytest-alembic "pytest-mock-resources[docker]==2.10.2"

- name: Run Unit Tests
run: make test
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,23 @@ up:
test:
pytest -vx --cov=app --cov-report term-missing --cov-fail-under=95

test_no_migrations:
pytest -m 'not alembic' -vx --cov=app --cov-report term-missing --cov-fail-under=95

test_only_migrations:
pytest -m 'alembic'

data_test:
echo wip

alembic_downgrade:
docker-compose run app alembic -c /app/app/core/db/migrations/alembic.ini downgrade base

migration_file:
docker-compose run app alembic -c /app/app/core/db/migrations/alembic.ini revision --autogenerate -m "your commit"
docker-compose run app alembic -c /app/app/core/db/migrations/alembic.ini revision --autogenerate

format:
black .
trunk fmt

generate_sdk:
# Need the docker running
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ Also, it is possible you want to modify the expiry time of access/refresh tokens
### Testing

- [ ] Integrity tests
- [x] pytest-alembic
- [x] Cover 100% with unit-testing

### Quality code
Expand Down
24 changes: 24 additions & 0 deletions app/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,35 @@

import pytest
from fastapi.testclient import TestClient
from pytest_alembic.config import Config
from pytest_mock_resources import create_postgres_fixture
from sqlmodel import SQLModel, create_engine
from sqlmodel.pool import StaticPool

from app.core.db.session import DBSessionMiddleware, db


@pytest.fixture
def alembic_config():
"""Override this fixture to configure the exact
alembic context setup required."""
migration_path = "app/core/db/migrations/"
return Config(
config_options={
"file": migration_path + "alembic.ini",
"script_location": migration_path,
}
)


# Configuration of pytest mock postgres fixtures
# @pytest.fixture(scope="session")
# def pmr_postgres_config():
# return PostgresConfig(image="postgres:16")


alembic_engine = create_postgres_fixture()

engine = create_engine(
"sqlite://",
echo=False,
Expand Down
26 changes: 18 additions & 8 deletions app/core/db/migrations/env.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from logging.config import fileConfig

from alembic import context
from sqlalchemy import engine_from_config, pool
from sqlalchemy import MetaData, engine_from_config, pool
from sqlmodel import SQLModel

from app.settings import settings
Expand All @@ -20,7 +20,13 @@
# for 'autogenerate' support
from app.core.db.migrations.models import *

target_metadata = SQLModel.metadata
meta = MetaData()
for table in SQLModel.metadata.tables.values():
if table.name != "testmodel":
meta._add_table(table.name, table.schema, table)


target_metadata = meta


# other values from the config, defined by the needs of env.py,
Expand Down Expand Up @@ -60,15 +66,19 @@ def run_migrations_online() -> None:
and associate a connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
connectable = context.config.attributes.get("connection", None)

if connectable is None:
connectable = engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)

with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
connection=connection,
target_metadata=target_metadata,
)

with context.begin_transaction():
Expand Down
17 changes: 17 additions & 0 deletions app/core/db/migrations/test_migrations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from pytest_alembic import tests as test_alembic


def test_model_definitions_match_ddl(alembic_runner):
test_alembic.test_model_definitions_match_ddl(alembic_runner)


def test_single_head_revision(alembic_runner):
test_alembic.test_single_head_revision(alembic_runner)


def test_up_down_consistency(alembic_runner):
test_alembic.test_up_down_consistency(alembic_runner)


def test_upgrade(alembic_runner):
test_alembic.test_upgrade(alembic_runner)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""init
"""empty message
Revision ID: b130fa25b02a
Revision ID: 1f9f63ce8990
Revises:
Create Date: 2023-08-10 01:50:14.507840
Create Date: 2024-04-14 12:53:01.483371
"""

Expand All @@ -13,7 +13,7 @@
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "b130fa25b02a"
revision: str = "1f9f63ce8990"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3.9"
services:
db:
image: postgres:latest
image: postgres:16
ports:
- 5432:5432
env_file:
Expand Down
116 changes: 115 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ mypy = "^1.5.1"
build = "^1.0.3"
wheel = "^0.41.2"
twine = "^4.0.2"
pytest-alembic = "^0.11.0"
pytest-mock-resources = { extras = ["docker"], version = "^2.10.2" }


[build-system]
Expand Down

0 comments on commit 0ace0a2

Please sign in to comment.