-
-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add recursion guard (Sourcery refactored) #469
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5c0356d
feat: add recursion guard
adhtruong 1ff9dc9
fix: avoid 3.10+ set syntax
adhtruong d197838
fix: avoid 3.10+ set syntax
adhtruong 74a3ad8
fix: avoid 3.9+ typing syntax
adhtruong 82ae1ff
fix: revert lock file changes
adhtruong 66475db
fix: address PR comments
adhtruong a22a980
fix: adjust tests
adhtruong 7f2d81b
fix: make interface backwards compatible. Favour Null over Ignore
adhtruong 45c3e3e
fix: PR comments
adhtruong fc314a4
Merge branch 'main' into add-recursion-guard
guacs b679b39
Merge branch 'main' into add-recursion-guard
guacs b444845
fix: PR updates
adhtruong 21d4cdc
Merge branch 'main' into add-recursion-guard
guacs b6988bd
'Refactored by Sourcery'
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
docs/examples/library_factories/sqlalchemy_factory/conftest.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,11 @@ | ||
from collections.abc import Iterable | ||
|
||
import pytest | ||
|
||
from docs.examples.library_factories.sqlalchemy_factory.test_example_4 import BaseFactory | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def _remove_default_factories() -> Iterable[None]: | ||
yield | ||
BaseFactory._base_factories.remove(BaseFactory) # noqa: SLF001 |
58 changes: 58 additions & 0 deletions
58
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,58 @@ | ||
from typing import List | ||
|
||
from sqlalchemy import ForeignKey, create_engine | ||
from sqlalchemy.orm import DeclarativeBase, Mapped, Session, mapped_column, relationship | ||
|
||
from polyfactory.factories.sqlalchemy_factory import SQLAlchemyFactory, T | ||
|
||
|
||
class Base(DeclarativeBase): | ||
... | ||
|
||
|
||
class Author(Base): | ||
__tablename__ = "authors" | ||
|
||
id: Mapped[int] = mapped_column(primary_key=True) | ||
name: Mapped[str] | ||
|
||
books: Mapped[List["Book"]] = relationship( | ||
"Book", | ||
uselist=True, | ||
back_populates="author", | ||
) | ||
|
||
|
||
class Book(Base): | ||
__tablename__ = "books" | ||
|
||
id: Mapped[int] = mapped_column(primary_key=True) | ||
author_id: Mapped[int] = mapped_column(ForeignKey(Author.id), nullable=False) | ||
author: Mapped[Author] = relationship( | ||
"Author", | ||
uselist=False, | ||
back_populates="books", | ||
) | ||
|
||
|
||
class BaseFactory(SQLAlchemyFactory[T]): | ||
__is_base_factory__ = True | ||
__set_relationships__ = True | ||
__randomize_collection_length__ = True | ||
__min_collection_length__ = 3 | ||
|
||
|
||
def test_custom_sqla_factory() -> None: | ||
engine = create_engine("sqlite:///:memory:") | ||
Base.metadata.create_all(engine) | ||
session = Session(engine) | ||
|
||
BaseFactory.__session__ = session # Or using a callable that returns a session | ||
|
||
author = BaseFactory.create_factory(Author).create_sync() | ||
assert author.id is not None | ||
assert author.id == author.books[0].author_id | ||
|
||
book = BaseFactory.create_factory(Book).create_sync() | ||
assert book.id is not None | ||
assert book.author.books == [book] |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
BaseFactory._infer_model_type
refactored with the following changes:reintroduce-else
)assign-if-exp
)