| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import types |
| 4 |
import typing as t |
| 5 |
from pathlib import Path |
| 6 |
|
| 7 |
import pytest |
| 8 |
import sqlalchemy as sa |
| 9 |
import sqlalchemy.orm as sa_orm |
| 10 |
from flask import Flask |
| 11 |
from flask.ctx import AppContext |
| 12 |
|
| 13 |
from flask_sqlalchemy import SQLAlchemy |
| 14 |
from flask_sqlalchemy.model import Model |
| 15 |
|
| 16 |
|
| 17 |
@pytest.fixture |
| 18 |
def app(request: pytest.FixtureRequest, tmp_path: Path) -> Flask: |
| 19 |
app = Flask(request.module.__name__, instance_path=str(tmp_path / "instance")) |
| 20 |
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://" |
| 21 |
app.config["SQLALCHEMY_RECORD_QUERIES"] = False |
| 22 |
return app |
| 23 |
|
| 24 |
|
| 25 |
@pytest.fixture |
| 26 |
def app_ctx(app: Flask) -> t.Generator[AppContext, None, None]: |
| 27 |
with app.app_context() as ctx: |
| 28 |
yield ctx |
| 29 |
|
| 30 |
|
| 31 |
# Each test that uses the db or model_class fixture will be tested against |
| 32 |
# each of these possible base class setups. |
| 33 |
# The first one is None, which will trigger creation of a SQLAlchemy 1.x base. |
| 34 |
# The remaining four are used to create SQLAlchemy 2.x bases. |
| 35 |
# We defer creation of those classes until the fixture, |
| 36 |
# so that each test gets a fresh class with its own metadata. |
| 37 |
test_classes = [ |
| 38 |
Model, |
| 39 |
( |
| 40 |
"BaseDeclarativeBase", |
| 41 |
(sa_orm.DeclarativeBase,), |
| 42 |
{"metaclass": type(sa_orm.DeclarativeBase)}, |
| 43 |
), |
| 44 |
( |
| 45 |
"BaseDataclassDeclarativeBase", |
| 46 |
(sa_orm.MappedAsDataclass, sa_orm.DeclarativeBase), |
| 47 |
{"metaclass": type(sa_orm.DeclarativeBase)}, |
| 48 |
), |
| 49 |
( |
| 50 |
"BaseDeclarativeBaseNoMeta", |
| 51 |
(sa_orm.DeclarativeBaseNoMeta,), |
| 52 |
{"metaclass": type(sa_orm.DeclarativeBaseNoMeta)}, |
| 53 |
), |
| 54 |
( |
| 55 |
"BaseDataclassDeclarativeBaseNoMeta", |
| 56 |
( |
| 57 |
sa_orm.MappedAsDataclass, |
| 58 |
sa_orm.DeclarativeBaseNoMeta, |
| 59 |
), |
| 60 |
{"metaclass": type(sa_orm.DeclarativeBaseNoMeta)}, |
| 61 |
), |
| 62 |
] |
| 63 |
|
| 64 |
|
| 65 |
@pytest.fixture(params=test_classes) |
| 66 |
def db(app: Flask, request: pytest.FixtureRequest) -> SQLAlchemy: |
| 67 |
if request.param is not Model: |
| 68 |
return SQLAlchemy(app, model_class=types.new_class(*request.param)) |
| 69 |
else: |
| 70 |
return SQLAlchemy(app) |
| 71 |
|
| 72 |
|
| 73 |
@pytest.fixture(params=test_classes) |
| 74 |
def model_class(request: pytest.FixtureRequest) -> t.Any: |
| 75 |
if request.param is not Model: |
| 76 |
return types.new_class(*request.param) |
| 77 |
else: |
| 78 |
return request.param |
| 79 |
|
| 80 |
|
| 81 |
@pytest.fixture |
| 82 |
def Todo(app: Flask, db: SQLAlchemy) -> t.Generator[t.Any, None, None]: |
| 83 |
if issubclass(db.Model, (sa_orm.MappedAsDataclass)): |
| 84 |
|
| 85 |
class Todo(db.Model): |
| 86 |
id: sa_orm.Mapped[int] = sa_orm.mapped_column( |
| 87 |
sa.Integer, init=False, primary_key=True |
| 88 |
) |
| 89 |
title: sa_orm.Mapped[str] = sa_orm.mapped_column( |
| 90 |
sa.String, nullable=True, default=None |
| 91 |
) |
| 92 |
|
| 93 |
elif issubclass(db.Model, (sa_orm.DeclarativeBase, sa_orm.DeclarativeBaseNoMeta)): |
| 94 |
|
| 95 |
class Todo(db.Model): # type: ignore[no-redef] |
| 96 |
id: sa_orm.Mapped[int] = sa_orm.mapped_column(sa.Integer, primary_key=True) |
| 97 |
title: sa_orm.Mapped[str] = sa_orm.mapped_column(sa.String, nullable=True) |
| 98 |
|
| 99 |
else: |
| 100 |
|
| 101 |
class Todo(db.Model): # type: ignore[no-redef] |
| 102 |
id = sa.Column(sa.Integer, primary_key=True) |
| 103 |
title = sa.Column(sa.String) |
| 104 |
|
| 105 |
with app.app_context(): |
| 106 |
db.create_all() |
| 107 |
|
| 108 |
yield Todo |
| 109 |
|
| 110 |
with app.app_context(): |
| 111 |
db.drop_all() |