|  | 
| 4 | 4 | 
 | 
| 5 | 5 | import pytest | 
| 6 | 6 | import sqlalchemy as sa | 
|  | 7 | +import sqlalchemy.orm as sa_orm | 
| 7 | 8 | from flask import Flask | 
| 8 | 9 | from werkzeug.exceptions import NotFound | 
| 9 | 10 | 
 | 
| @@ -64,20 +65,40 @@ def test_paginate(db: SQLAlchemy, Todo: t.Any) -> None: | 
| 64 | 65 | # This test creates its own inline model so that it can use that as the type | 
| 65 | 66 | @pytest.mark.usefixtures("app_ctx") | 
| 66 | 67 | def test_view_get_or_404_typed(db: SQLAlchemy, app: Flask) -> None: | 
| 67 |  | -    class Quiz(db.Model): | 
| 68 |  | -        id = sa.Column(sa.Integer, primary_key=True) | 
| 69 |  | -        topic = sa.Column(sa.String) | 
|  | 68 | +    # Copied and pasted from conftest.py | 
|  | 69 | +    if issubclass(db.Model, (sa_orm.MappedAsDataclass)): | 
|  | 70 | + | 
|  | 71 | +        class Todo(db.Model): | 
|  | 72 | +            id: sa_orm.Mapped[int] = sa_orm.mapped_column( | 
|  | 73 | +                sa.Integer, init=False, primary_key=True | 
|  | 74 | +            ) | 
|  | 75 | +            title: sa_orm.Mapped[str] = sa_orm.mapped_column( | 
|  | 76 | +                sa.String, nullable=True, default=None | 
|  | 77 | +            ) | 
|  | 78 | + | 
|  | 79 | +    elif issubclass(db.Model, (sa_orm.DeclarativeBase, sa_orm.DeclarativeBaseNoMeta)): | 
|  | 80 | + | 
|  | 81 | +        class Todo(db.Model):  # type: ignore[no-redef] | 
|  | 82 | +            id: sa_orm.Mapped[int] = sa_orm.mapped_column(sa.Integer, primary_key=True) | 
|  | 83 | +            title: sa_orm.Mapped[str] = sa_orm.mapped_column(sa.String, nullable=True) | 
|  | 84 | + | 
|  | 85 | +    else: | 
|  | 86 | + | 
|  | 87 | +        class Todo(db.Model):  # type: ignore[no-redef] | 
|  | 88 | +            id = sa.Column(sa.Integer, primary_key=True) | 
|  | 89 | +            title = sa.Column(sa.String) | 
| 70 | 90 | 
 | 
| 71 | 91 |     db.create_all() | 
| 72 | 92 | 
 | 
| 73 |  | -    item: Quiz = Quiz(topic="Python") | 
| 74 |  | -    db.session.add(item) | 
|  | 93 | +    todo = Todo() | 
|  | 94 | +    todo.title = "Python" | 
|  | 95 | +    db.session.add(todo) | 
| 75 | 96 |     db.session.commit() | 
| 76 |  | -    result = db.get_or_404(Quiz, 1) | 
| 77 |  | -    assert result.topic == "Python" | 
| 78 |  | -    assert result is item | 
|  | 97 | +    result = db.get_or_404(Todo, 1) | 
|  | 98 | +    assert result.title == "Python" | 
|  | 99 | +    assert result is todo | 
| 79 | 100 |     if hasattr(t, "assert_type"): | 
| 80 |  | -        t.assert_type(result, Quiz) | 
|  | 101 | +        t.assert_type(result, Todo) | 
| 81 | 102 |     with pytest.raises(NotFound): | 
| 82 |  | -        assert db.get_or_404(Quiz, 2) | 
|  | 103 | +        assert db.get_or_404(Todo, 2) | 
| 83 | 104 |     db.drop_all() | 
0 commit comments