Skip to content

Commit 73e0206

Browse files
committed
Fix tests
1 parent d3a30c5 commit 73e0206

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

src/flask_sqlalchemy/extension.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ def _make_declarative_base(
511511
{"metaclass": type(declarative_bases[0])},
512512
lambda ns: ns.update(body),
513513
)
514-
elif not isinstance(model_class, sa.orm.DeclarativeMeta):
514+
elif not isinstance(model_class, sa_orm.DeclarativeMeta):
515515
metadata = self._make_metadata(None)
516516
model = sa_orm.declarative_base(
517517
metadata=metadata, cls=model_class, name="Model", metaclass=DefaultMeta

tests/conftest.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ class Todo(db.Model):
7575
sa.String, nullable=True, default=None
7676
)
7777

78-
elif issubclass(
79-
db.Model, (sa_orm.DeclarativeBaseNoMeta, sa_orm.DeclarativeBaseNoMeta)
80-
):
78+
elif issubclass(db.Model, (sa_orm.DeclarativeBase, sa_orm.DeclarativeBaseNoMeta)):
8179

8280
class Todo(db.Model): # type: ignore[no-redef]
8381
id: sa_orm.Mapped[int] = sa_orm.mapped_column(sa.Integer, primary_key=True)

tests/test_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class User(db.Model):
6363

6464
@pytest.mark.usefixtures("app_ctx")
6565
def test_too_many_bases(app: Flask) -> None:
66-
class Base(sa.orm.DeclarativeBase, sa.orm.DeclarativeBaseNoMeta): # type: ignore[misc]
66+
class Base(sa_orm.DeclarativeBase, sa_orm.DeclarativeBaseNoMeta): # type: ignore[misc]
6767
pass
6868

6969
with pytest.raises(ValueError):

tests/test_view_query.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66
import sqlalchemy as sa
7+
import sqlalchemy.orm as sa_orm
78
from flask import Flask
89
from werkzeug.exceptions import NotFound
910

@@ -64,20 +65,40 @@ def test_paginate(db: SQLAlchemy, Todo: t.Any) -> None:
6465
# This test creates its own inline model so that it can use that as the type
6566
@pytest.mark.usefixtures("app_ctx")
6667
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)
7090

7191
db.create_all()
7292

73-
item: Quiz = Quiz(topic="Python")
74-
db.session.add(item)
93+
todo = Todo()
94+
todo.title = "Python"
95+
db.session.add(todo)
7596
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
79100
if hasattr(t, "assert_type"):
80-
t.assert_type(result, Quiz)
101+
t.assert_type(result, Todo)
81102
with pytest.raises(NotFound):
82-
assert db.get_or_404(Quiz, 2)
103+
assert db.get_or_404(Todo, 2)
83104
db.drop_all()

0 commit comments

Comments
 (0)