|
18 | 18 |
|
19 | 19 | from datetime import date, datetime, time, timezone |
20 | 20 | from decimal import Decimal |
21 | | -from typing import Any |
| 21 | +from typing import Any, List, Optional |
22 | 22 |
|
23 | 23 | import pytest |
24 | 24 | from sqlalchemy.sql import func |
25 | 25 | from sqlalchemy.orm import ( |
26 | | - declarative_base, sessionmaker, relationship, Session, joinedload, |
27 | | - subqueryload |
| 26 | + DeclarativeBase, Mapped, mapped_column, sessionmaker, relationship, |
| 27 | + Session, joinedload, subqueryload |
28 | 28 | ) |
29 | 29 | from sqlalchemy import ( |
30 | | - create_engine, Column, ForeignKey, Integer, BigInteger, SmallInteger, |
| 30 | + create_engine, ForeignKey, BigInteger, SmallInteger, |
31 | 31 | Float, Numeric, String, Boolean, Date, Time, DateTime, Text, JSON, or_, |
32 | 32 | and_, text |
33 | 33 | ) |
|
40 | 40 | from ..utils.test_environment import TestEnvironment |
41 | 41 | from ..utils.test_environment_features import TestEnvironmentFeatures |
42 | 42 |
|
43 | | -class Base: |
44 | | - __allow_unmapped__ = True |
| 43 | +class Base(DeclarativeBase): |
| 44 | + pass |
45 | 45 |
|
46 | | -Base = declarative_base(cls=Base) |
47 | 46 |
|
48 | 47 | class TestModel(Base): |
49 | 48 | """Basic test model for SQLAlchemy ORM functionality""" |
50 | 49 | __tablename__ = 'sqlalchemy_test_model' |
51 | 50 |
|
52 | | - id = Column(Integer, primary_key=True) |
| 51 | + id: Mapped[int] = mapped_column(primary_key=True) |
| 52 | + name: Mapped[str] = mapped_column(String(100)) |
| 53 | + email: Mapped[str] = mapped_column(String(254), unique=True) |
| 54 | + age: Mapped[int] = mapped_column() |
| 55 | + is_active: Mapped[Optional[bool]] = mapped_column(Boolean, default=True) |
| 56 | + created_at: Mapped[Optional[datetime]] = mapped_column(DateTime, default=datetime.now(timezone.utc)) |
53 | 57 |
|
54 | | - name = Column(String(100), nullable=False) |
55 | | - email = Column(String(254), nullable=False, unique=True) |
56 | | - age = Column(Integer, nullable=False) |
57 | | - is_active = Column(Boolean, default=True) |
58 | | - created_at = Column(DateTime, default=datetime.now(timezone.utc)) |
59 | 58 |
|
60 | 59 | class DataTypeModel(Base): |
61 | 60 | """Model for testing various data types""" |
62 | 61 | __tablename__ = 'sqlalchemy_data_type_model' |
63 | 62 |
|
64 | | - id = Column(Integer, primary_key=True) |
| 63 | + id: Mapped[int] = mapped_column(primary_key=True) |
65 | 64 |
|
66 | 65 | # String fields |
67 | | - string_field = Column(String(255)) |
68 | | - text_field = Column(Text) |
| 66 | + string_field: Mapped[Optional[str]] = mapped_column(String(255)) |
| 67 | + text_field: Mapped[Optional[str]] = mapped_column(Text) |
69 | 68 |
|
70 | 69 | # Numeric fields |
71 | | - integer_field = Column(Integer) |
72 | | - small_integer_field = Column(SmallInteger) |
73 | | - big_integer_field = Column(BigInteger) |
74 | | - numeric_field = Column(Numeric(10, 2)) |
75 | | - float_field = Column(Float) |
| 70 | + integer_field: Mapped[Optional[int]] = mapped_column() |
| 71 | + small_integer_field: Mapped[Optional[int]] = mapped_column(SmallInteger) |
| 72 | + big_integer_field: Mapped[Optional[int]] = mapped_column(BigInteger) |
| 73 | + numeric_field: Mapped[Optional[Decimal]] = mapped_column(Numeric(10, 2)) |
| 74 | + float_field: Mapped[Optional[float]] = mapped_column(Float) |
76 | 75 |
|
77 | 76 | # Boolean field |
78 | | - boolean_field = Column(Boolean, default=False) |
| 77 | + boolean_field: Mapped[Optional[bool]] = mapped_column(Boolean, default=False) |
79 | 78 |
|
80 | 79 | # Date/Time fields |
81 | | - date_field = Column(Date) |
82 | | - time_field = Column(Time) |
83 | | - datetime_field = Column(DateTime) |
| 80 | + date_field: Mapped[Optional[date]] = mapped_column(Date) |
| 81 | + time_field: Mapped[Optional[time]] = mapped_column(Time) |
| 82 | + datetime_field: Mapped[Optional[datetime]] = mapped_column(DateTime) |
84 | 83 |
|
85 | 84 | # JSON field (MySQL 5.7+) |
86 | | - json_field = Column(JSON) |
| 85 | + json_field: Mapped[Optional[Any]] = mapped_column(JSON) |
| 86 | + |
87 | 87 |
|
88 | 88 | class Author(Base): |
89 | 89 | """Author model for relationship testing""" |
90 | 90 | __tablename__ = 'sqlalchemy_author' |
91 | 91 |
|
92 | | - id = Column(Integer, primary_key=True) |
93 | | - name = Column(String(100), nullable=False) |
94 | | - email = Column(String(254), nullable=False) |
95 | | - birth_date = Column(Date) |
| 92 | + id: Mapped[int] = mapped_column(primary_key=True) |
| 93 | + name: Mapped[str] = mapped_column(String(100)) |
| 94 | + email: Mapped[str] = mapped_column(String(254)) |
| 95 | + birth_date: Mapped[Optional[date]] = mapped_column(Date) |
| 96 | + |
| 97 | + books: Mapped[List[Book]] = relationship(back_populates='author', cascade='all, delete-orphan') |
96 | 98 |
|
97 | | - books = relationship('Book', back_populates='author', cascade='all, delete-orphan') |
98 | 99 |
|
99 | 100 | class Book(Base): |
100 | 101 | """Book model for relationship testing""" |
101 | 102 | __tablename__ = 'sqlalchemy_book' |
102 | 103 |
|
103 | | - id = Column(Integer, primary_key=True) |
104 | | - title = Column(String(200), nullable=False) |
105 | | - author_id = Column(Integer, ForeignKey("sqlalchemy_author.id"), nullable=False) |
106 | | - publication_date = Column(Date, nullable=False) |
107 | | - pages = Column(Integer, nullable=False) |
108 | | - price = Column(Numeric(8, 2), nullable=False) |
| 104 | + id: Mapped[int] = mapped_column(primary_key=True) |
| 105 | + title: Mapped[str] = mapped_column(String(200)) |
| 106 | + author_id: Mapped[int] = mapped_column(ForeignKey("sqlalchemy_author.id")) |
| 107 | + publication_date: Mapped[date] = mapped_column(Date) |
| 108 | + pages: Mapped[int] = mapped_column() |
| 109 | + price: Mapped[Decimal] = mapped_column(Numeric(8, 2)) |
109 | 110 |
|
110 | | - author = relationship('Author', back_populates='books') |
| 111 | + author: Mapped[Author] = relationship(back_populates='books') |
111 | 112 |
|
112 | 113 | @enable_on_engines([DatabaseEngine.MYSQL]) # MySQL Specific until PG is implemented |
113 | 114 | @enable_on_deployments([DatabaseEngineDeployment.AURORA, DatabaseEngineDeployment.RDS_MULTI_AZ_CLUSTER]) |
|
0 commit comments