Debian Codegraph @

tests/test_model_bind.py

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