| 1 |
From: James Page <[email protected]> |
| 2 |
Date: Fri, 14 Feb 2025 09:31:37 +0000 |
| 3 |
Subject: tests: MappedAsDataclass - skip table binding |
| 4 |
|
| 5 |
With SQLAlchemy 2.0.36 mapping as a dataclass while also providing |
| 6 |
a ``__table__`` attribute is not supported. |
| 7 |
|
| 8 |
Skip associated test when ``MappedAsDataclass`` is in use. |
| 9 |
|
| 10 |
Fixes: #1378 |
| 11 |
--- |
| 12 |
tests/test_model_bind.py | 29 +++++++++++++++++------------ |
| 13 |
1 file changed, 17 insertions(+), 12 deletions(-) |
| 14 |
|
| 15 |
diff --git a/tests/test_model_bind.py b/tests/test_model_bind.py |
| 16 |
index 7c633c8..6f1c602 100644 |
| 17 |
--- a/tests/test_model_bind.py |
| 18 |
+++ b/tests/test_model_bind.py |
| 19 |
@@ -1,6 +1,8 @@ |
| 20 |
from __future__ import annotations |
| 21 |
|
| 22 |
+import pytest |
| 23 |
import sqlalchemy as sa |
| 24 |
+import sqlalchemy.orm as sa_orm |
| 25 |
|
| 26 |
from flask_sqlalchemy import SQLAlchemy |
| 27 |
|
| 28 |
@@ -76,15 +78,18 @@ def test_explicit_metadata(db: SQLAlchemy) -> None: |
| 29 |
|
| 30 |
|
| 31 |
def test_explicit_table(db: SQLAlchemy) -> None: |
| 32 |
- user_table = db.Table( |
| 33 |
- "user", |
| 34 |
- sa.Column("id", sa.Integer, primary_key=True), |
| 35 |
- bind_key="auth", |
| 36 |
- ) |
| 37 |
- |
| 38 |
- class User(db.Model): |
| 39 |
- __bind_key__ = "other" |
| 40 |
- __table__ = user_table |
| 41 |
- |
| 42 |
- assert User.__table__.metadata is db.metadatas["auth"] |
| 43 |
- assert "other" not in db.metadatas |
| 44 |
+ if not issubclass(db.Model, (sa_orm.MappedAsDataclass)): |
| 45 |
+ user_table = db.Table( |
| 46 |
+ "user", |
| 47 |
+ sa.Column("id", sa.Integer, primary_key=True), |
| 48 |
+ bind_key="auth", |
| 49 |
+ ) |
| 50 |
+ |
| 51 |
+ class User(db.Model): |
| 52 |
+ __bind_key__ = "other" |
| 53 |
+ __table__ = user_table |
| 54 |
+ |
| 55 |
+ assert User.__table__.metadata is db.metadatas["auth"] |
| 56 |
+ assert "other" not in db.metadatas |
| 57 |
+ else: |
| 58 |
+ pytest.skip("Explicit table binding with mapped dataclasses not supported") |