-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sqlalchemy-factory): add SQLAlchemy custom types section to docs
- Loading branch information
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
docs/examples/library_factories/sqlalchemy_factory/test_example_4.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,31 @@ | ||
import datetime | ||
from typing import Any | ||
|
||
from sqlalchemy import DateTime, types | ||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column | ||
|
||
from polyfactory.factories.sqlalchemy_factory import SQLAlchemyFactory | ||
|
||
|
||
class TZAwareDateTime(types.TypeDecorator): | ||
impl = DateTime(timezone=True) | ||
|
||
|
||
class Base(DeclarativeBase): | ||
... | ||
|
||
|
||
class Author(Base): | ||
__tablename__ = "authors" | ||
|
||
id: Mapped[int] = mapped_column(primary_key=True) | ||
first_publication_at: Mapped[Any] = mapped_column(type_=TZAwareDateTime(), nullable=False) | ||
|
||
|
||
class AuthorFactory(SQLAlchemyFactory[Author]): | ||
__model__ = Author | ||
|
||
|
||
def test_sqla_type_decorator_custom_type_factory() -> None: | ||
author = AuthorFactory.build() | ||
assert isinstance(author.first_publication_at, datetime.datetime) |
35 changes: 35 additions & 0 deletions
35
docs/examples/library_factories/sqlalchemy_factory/test_example_5.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,35 @@ | ||
import datetime | ||
from typing import Any, Callable, Dict | ||
|
||
from sqlalchemy import types | ||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column | ||
|
||
from polyfactory.factories.sqlalchemy_factory import SQLAlchemyFactory | ||
|
||
|
||
class CustomType(types.UserDefinedType): | ||
... | ||
|
||
|
||
class Base(DeclarativeBase): | ||
... | ||
|
||
|
||
class Author(Base): | ||
__tablename__ = "authors" | ||
|
||
id: Mapped[int] = mapped_column(primary_key=True) | ||
first_publication_at: Mapped[Any] = mapped_column(type_=CustomType()) | ||
|
||
|
||
class AuthorFactory(SQLAlchemyFactory[Author]): | ||
__model__ = Author | ||
|
||
@classmethod | ||
def get_sqlalchemy_types(cls) -> Dict[Any, Callable[[], Any]]: | ||
return {**super().get_sqlalchemy_types(), CustomType: lambda: cls.__faker__.date_time()} | ||
|
||
|
||
def test_sqla_user_defined_type_custom_type_factory() -> None: | ||
author = AuthorFactory.build() | ||
assert isinstance(author.first_publication_at, datetime.datetime) |
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