-
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sqla_factory): added __set_association_proxy__ attribute (#629)
Co-authored-by: Andrew Truong <40660973+adhtruong@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
187 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
docs/examples/library_factories/sqlalchemy_factory/test_example_association_proxy.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from __future__ import annotations | ||
|
||
from sqlalchemy import ForeignKey | ||
from sqlalchemy.ext.associationproxy import AssociationProxy, association_proxy | ||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship | ||
|
||
from polyfactory.factories.sqlalchemy_factory import SQLAlchemyFactory | ||
|
||
|
||
class Base(DeclarativeBase): ... | ||
|
||
|
||
class User(Base): | ||
__tablename__ = "users" | ||
|
||
id: Mapped[int] = mapped_column(primary_key=True) | ||
name: Mapped[str] | ||
|
||
user_keyword_associations: Mapped[list["UserKeywordAssociation"]] = relationship( | ||
back_populates="user", | ||
) | ||
keywords: AssociationProxy[list["Keyword"]] = association_proxy( | ||
"user_keyword_associations", | ||
"keyword", | ||
creator=lambda keyword_obj: UserKeywordAssociation(keyword=keyword_obj), | ||
) | ||
|
||
|
||
class UserKeywordAssociation(Base): | ||
__tablename__ = "user_keyword" | ||
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), primary_key=True) | ||
keyword_id: Mapped[int] = mapped_column(ForeignKey("keywords.id"), primary_key=True) | ||
|
||
user: Mapped[User] = relationship(back_populates="user_keyword_associations") | ||
keyword: Mapped["Keyword"] = relationship() | ||
|
||
|
||
class Keyword(Base): | ||
__tablename__ = "keywords" | ||
id: Mapped[int] = mapped_column(primary_key=True) | ||
keyword: Mapped[str] | ||
|
||
|
||
class UserFactory(SQLAlchemyFactory[User]): ... | ||
|
||
|
||
class UserFactoryWithAssociation(SQLAlchemyFactory[User]): | ||
__set_association_proxy__ = True | ||
|
||
|
||
def test_sqla_factory() -> None: | ||
user = UserFactory.build() | ||
assert not user.user_keyword_associations | ||
assert not user.keywords | ||
|
||
|
||
def test_sqla_factory_with_association() -> None: | ||
user = UserFactoryWithAssociation.build() | ||
assert isinstance(user.user_keyword_associations[0], UserKeywordAssociation) | ||
assert isinstance(user.keywords[0], Keyword) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from typing import Optional | ||
|
||
from sqlalchemy import Column, ForeignKey, Integer, String | ||
from sqlalchemy.ext.associationproxy import association_proxy | ||
from sqlalchemy.orm import relationship | ||
from sqlalchemy.orm.decl_api import DeclarativeMeta, registry | ||
|
||
from polyfactory.factories.sqlalchemy_factory import SQLAlchemyFactory | ||
|
||
_registry = registry() | ||
|
||
|
||
class Base(metaclass=DeclarativeMeta): | ||
__abstract__ = True | ||
__allow_unmapped__ = True | ||
|
||
registry = _registry | ||
metadata = _registry.metadata | ||
|
||
|
||
class User(Base): | ||
__tablename__ = "users" | ||
|
||
id = Column(Integer, primary_key=True) | ||
name = Column(String) | ||
|
||
user_keyword_associations = relationship( | ||
"UserKeywordAssociation", | ||
back_populates="user", | ||
) | ||
keywords = association_proxy( | ||
"user_keyword_associations", "keyword", creator=lambda keyword_obj: UserKeywordAssociation(keyword=keyword_obj) | ||
) | ||
|
||
|
||
class UserKeywordAssociation(Base): | ||
__tablename__ = "user_keyword" | ||
|
||
user_id = Column(Integer, ForeignKey("users.id"), primary_key=True) | ||
keyword_id = Column(Integer, ForeignKey("keywords.id"), primary_key=True) | ||
|
||
user = relationship(User, back_populates="user_keyword_associations") | ||
keyword = relationship("Keyword") | ||
|
||
# for prevent mypy error: Unexpected keyword argument "keyword" for "UserKeywordAssociation" [call-arg] | ||
def __init__(self, keyword: Optional["Keyword"] = None): | ||
self.keyword = keyword | ||
|
||
|
||
class Keyword(Base): | ||
__tablename__ = "keywords" | ||
|
||
id = Column(Integer, primary_key=True) | ||
keyword = Column(String) | ||
|
||
|
||
def test_association_proxy() -> None: | ||
class UserFactory(SQLAlchemyFactory[User]): | ||
__set_association_proxy__ = True | ||
|
||
user = UserFactory.build() | ||
assert isinstance(user.keywords[0], Keyword) | ||
assert isinstance(user.user_keyword_associations[0], UserKeywordAssociation) | ||
|
||
|
||
def test_complex_association_proxy() -> None: | ||
class KeywordFactory(SQLAlchemyFactory[Keyword]): ... | ||
|
||
class ComplexUserFactory(SQLAlchemyFactory[User]): | ||
__set_association_proxy__ = True | ||
|
||
keywords = KeywordFactory.batch(3) | ||
|
||
user = ComplexUserFactory.build() | ||
assert isinstance(user, User) | ||
assert isinstance(user.keywords[0], Keyword) | ||
assert len(user.keywords) == 3 | ||
assert isinstance(user.user_keyword_associations[0], UserKeywordAssociation) | ||
assert len(user.user_keyword_associations) == 3 |