| 1 |
from __future__ import annotations |
| 2 |
|
| •3 |
import pytest |
| 4 |
import sqlalchemy as sa |
| •5 |
import sqlalchemy.orm as sa_orm |
| 6 |
|
| 7 |
from flask_sqlalchemy import SQLAlchemy |
| 8 |
|
| 9 |
|
| 10 |
def test_bind_key_default(db: SQLAlchemy) -> None: |
| 11 |
class User(db.Model): |
| 12 |
id = sa.Column(sa.Integer, primary_key=True) |
| 13 |
|
| 14 |
assert User.metadata is db.metadata |
| 15 |
|
| 16 |
|
| 17 |
def test_metadata_per_bind(db: SQLAlchemy) -> None: |
| 18 |
class User(db.Model): |
| 19 |
__bind_key__ = "other" |
| 20 |
id = sa.Column(sa.Integer, primary_key=True) |
| 21 |
|
| 22 |
assert User.metadata is db.metadatas["other"] |
| 23 |
|
| 24 |
|
| 25 |
def test_multiple_binds_same_table_name(db: SQLAlchemy) -> None: |
| 26 |
class UserA(db.Model): |
| 27 |
__tablename__ = "user" |
| 28 |
id = sa.Column(sa.Integer, primary_key=True) |
| 29 |
|
| 30 |
class UserB(db.Model): |
| 31 |
__bind_key__ = "other" |
| 32 |
__tablename__ = "user" |
| 33 |
id = sa.Column(sa.Integer, primary_key=True) |
| 34 |
|
| 35 |
assert UserA.metadata is db.metadata |
| 36 |
assert UserB.metadata is db.metadatas["other"] |
| 37 |
assert UserA.__table__.metadata is not UserB.__table__.metadata |
| 38 |
|
| 39 |
|
| 40 |
def test_inherit_parent(db: SQLAlchemy) -> None: |
| 41 |
class User(db.Model): |
| 42 |
__bind_key__ = "auth" |
| 43 |
id = sa.Column(sa.Integer, primary_key=True) |
| 44 |
type = sa.Column(sa.String) |
| 45 |
__mapper_args__ = {"polymorphic_on": type, "polymorphic_identity": "user"} |
| 46 |
|
| 47 |
class Admin(User): |
| 48 |
id = sa.Column(sa.Integer, sa.ForeignKey(User.id), primary_key=True) |
| 49 |
__mapper_args__ = {"polymorphic_identity": "admin"} |
| 50 |
|
| 51 |
assert "admin" in db.metadatas["auth"].tables |
| 52 |
# inherits metadata, doesn't set it directly |
| 53 |
assert "metadata" not in Admin.__dict__ |
| 54 |
|
| 55 |
|
| 56 |
def test_inherit_abstract_parent(db: SQLAlchemy) -> None: |
| 57 |
class AbstractUser(db.Model): |
| 58 |
__abstract__ = True |
| 59 |
__bind_key__ = "auth" |
| 60 |
|
| 61 |
class User(AbstractUser): |
| 62 |
id = sa.Column(sa.Integer, primary_key=True) |
| 63 |
|
| 64 |
assert "user" in db.metadatas["auth"].tables |
| 65 |
assert "metadata" not in User.__dict__ |
| 66 |
|
| 67 |
|
| 68 |
def test_explicit_metadata(db: SQLAlchemy) -> None: |
| 69 |
other_metadata = sa.MetaData() |
| 70 |
|
| 71 |
class User(db.Model): |
| 72 |
__bind_key__ = "other" |
| 73 |
metadata = other_metadata |
| 74 |
id = sa.Column(sa.Integer, primary_key=True) |
| 75 |
|
| 76 |
assert User.__table__.metadata is other_metadata |
| 77 |
assert "other" not in db.metadatas |
| 78 |
|
| 79 |
|
| 80 |
def test_explicit_table(db: SQLAlchemy) -> None: |
| •81 |
if not issubclass(db.Model, (sa_orm.MappedAsDataclass)): |
| 82 |
user_table = db.Table( |
| 83 |
"user", |
| 84 |
sa.Column("id", sa.Integer, primary_key=True), |
| 85 |
bind_key="auth", |
| 86 |
) |
| 87 |
|
| 88 |
class User(db.Model): |
| 89 |
__bind_key__ = "other" |
| 90 |
__table__ = user_table |
| 91 |
|
| 92 |
assert User.__table__.metadata is db.metadatas["auth"] |
| 93 |
assert "other" not in db.metadatas |
| 94 |
else: |
| 95 |
pytest.skip("Explicit table binding with mapped dataclasses not supported") |