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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
"""
this file tests that absolute imports can be used in declarative
mappings while guaranteeing that the Mapped name is not locally present
"""
from __future__ import annotations
import typing
import sqlalchemy
from sqlalchemy import orm
import sqlalchemy.orm
import sqlalchemy.testing
import sqlalchemy.testing.fixtures
try:
x = Mapped # type: ignore
except NameError:
pass
else:
raise Exception("Mapped name **must not be imported in this file**")
class MappedColumnTest(
sqlalchemy.testing.fixtures.TestBase, sqlalchemy.testing.AssertsCompiledSQL
):
__dialect__ = "default"
def test_fully_qualified_mapped_name(self, decl_base):
"""test #8853 *again*, as reported in #9335 this failed to be fixed"""
class Foo(decl_base):
__tablename__ = "foo"
id: sqlalchemy.orm.Mapped[int] = sqlalchemy.orm.mapped_column(
primary_key=True
)
data: sqlalchemy.orm.Mapped[int] = sqlalchemy.orm.mapped_column()
data2: sqlalchemy.orm.Mapped[int]
data3: orm.Mapped[int]
self.assert_compile(
sqlalchemy.select(Foo),
"SELECT foo.id, foo.data, foo.data2, foo.data3 FROM foo",
)
@sqlalchemy.testing.variation(
"construct", ["Mapped", "WriteOnlyMapped", "DynamicMapped"]
)
def test_fully_qualified_writeonly_mapped_name(self, decl_base, construct):
"""futher variation in issue #10412"""
class Foo(decl_base):
__tablename__ = "foo"
id: sqlalchemy.orm.Mapped[int] = sqlalchemy.orm.mapped_column(
primary_key=True
)
if construct.Mapped:
bars: orm.Mapped[typing.List[Bar]] = orm.relationship()
elif construct.WriteOnlyMapped:
bars: orm.WriteOnlyMapped[typing.List[Bar]] = (
orm.relationship()
)
elif construct.DynamicMapped:
bars: orm.DynamicMapped[typing.List[Bar]] = orm.relationship()
else:
construct.fail()
class Bar(decl_base):
__tablename__ = "bar"
id: sqlalchemy.orm.Mapped[int] = sqlalchemy.orm.mapped_column(
primary_key=True
)
foo_id: sqlalchemy.orm.Mapped[int] = sqlalchemy.orm.mapped_column(
sqlalchemy.ForeignKey("foo.id")
)
self.assert_compile(
sqlalchemy.select(Foo).join(Foo.bars),
"SELECT foo.id FROM foo JOIN bar ON foo.id = bar.foo_id",
)
|