-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_sa_default_behaviour.py
More file actions
63 lines (45 loc) · 1.43 KB
/
Copy pathtest_sa_default_behaviour.py
File metadata and controls
63 lines (45 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from typing import List
import pytest
import sqlalchemy as sa
from sqlalchemy.orm import (
DeclarativeBase,
Mapped,
mapped_column,
)
from sqlalchemy.dialects.postgresql import JSONB, ARRAY
class Base(DeclarativeBase):
pass
class User(Base):
__tablename__ = "user_account"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(sa.String(30))
aliases: Mapped[List[str]] = mapped_column(ARRAY(sa.String(128)))
addresses: Mapped[dict] = mapped_column(JSONB, default=dict)
@pytest.fixture(scope="module", autouse=True)
def _with_tables(session):
Base.metadata.create_all(session.bind)
yield
session.execute(sa.text("DROP TABLE user_account CASCADE;"))
session.commit()
def test_sa_array_not_mutable(session):
session.add(u := User(name="foo", aliases=["bar", "baz"]))
session.commit()
u.aliases.append("qux")
assert u.aliases == ["bar", "baz", "qux"]
session.commit()
assert u.aliases == ["bar", "baz"]
def test_sa_jsonb_not_mutable(session):
session.add(u := User(
name="bar",
aliases=["baz", "qux"],
addresses={
"home": "bar",
"work": "baz",
"email": "xyz@example.com"
}
))
session.commit()
u.addresses["email"] = "abc@example.com"
assert u.addresses["email"] == "abc@example.com"
session.commit()
assert u.addresses["email"] == "xyz@example.com"