-
-
Notifications
You must be signed in to change notification settings - Fork 85
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(sqla_factory): added __set_association_proxy__ attribute #624
Open
nisemenov
wants to merge
4
commits into
litestar-org:main
Choose a base branch
from
nisemenov:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e0c8f11
fix(sqla_factory): added an async context manager in SQLAASyncPersist…
nisemenov cc73480
feat(factories): added some async methods
nisemenov ea39ed9
feat(sqla_factory): added __set_association_proxy__ attribute
nisemenov a6e7782
test(sqla_factory): refactored some tests after adding an async conte…
nisemenov 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
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 |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
from sqlalchemy import ARRAY, Column, Numeric, String, inspect, types | ||
from sqlalchemy.dialects import mssql, mysql, postgresql, sqlite | ||
from sqlalchemy.exc import NoInspectionAvailable | ||
from sqlalchemy.ext.associationproxy import AssociationProxy | ||
from sqlalchemy.orm import InstanceState, Mapper | ||
except ImportError as e: | ||
msg = "sqlalchemy is not installed" | ||
|
@@ -52,16 +53,18 @@ def __init__(self, session: AsyncSession) -> None: | |
self.session = session | ||
|
||
async def save(self, data: T) -> T: | ||
self.session.add(data) | ||
await self.session.commit() | ||
await self.session.refresh(data) | ||
async with self.session as session: | ||
session.add(data) | ||
await session.commit() | ||
await session.refresh(data) | ||
return data | ||
|
||
async def save_many(self, data: list[T]) -> list[T]: | ||
self.session.add_all(data) | ||
await self.session.commit() | ||
for batch_item in data: | ||
await self.session.refresh(batch_item) | ||
async with self.session as session: | ||
session.add_all(data) | ||
await session.commit() | ||
for batch_item in data: | ||
await session.refresh(batch_item) | ||
return data | ||
|
||
|
||
|
@@ -76,6 +79,8 @@ class SQLAlchemyFactory(Generic[T], BaseFactory[T]): | |
"""Configuration to consider columns with foreign keys as a field or not.""" | ||
__set_relationships__: ClassVar[bool] = False | ||
"""Configuration to consider relationships property as a model field or not.""" | ||
__set_association_proxy__: ClassVar[bool] = False | ||
"""Configuration to consider AssociationProxy property as a model field or not.""" | ||
|
||
__session__: ClassVar[Session | Callable[[], Session] | None] = None | ||
__async_session__: ClassVar[AsyncSession | Callable[[], AsyncSession] | None] = None | ||
|
@@ -213,6 +218,23 @@ def get_model_fields(cls) -> list[FieldMeta]: | |
random=cls.__random__, | ||
), | ||
) | ||
if cls.__set_association_proxy__: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. Adding as a feature makes sense here. Can appropriate tests and documentation be added for this feature please? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! Ok, I'd try to do that. |
||
for name, attr in table.all_orm_descriptors.items(): | ||
if isinstance(attr, AssociationProxy): | ||
target_collection = table.relationships.get(attr.target_collection) | ||
if target_collection: | ||
target_class = target_collection.entity.class_ | ||
target_attr = getattr(target_class, attr.value_attr) | ||
if target_attr: | ||
class_ = target_attr.entity.class_ | ||
annotation = class_ if not target_collection.uselist else List[class_] # type: ignore[valid-type] | ||
fields_meta.append( | ||
FieldMeta.from_type( | ||
name=name, | ||
annotation=annotation, | ||
random=cls.__random__, | ||
) | ||
) | ||
|
||
return fields_meta | ||
|
||
|
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.
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.
Is this behaviour generally used for other async methods or more specific to SQLA relationships? If specific to SQLA then should live within that factory
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.
I should check a bit, but could suppose this is more specific for SQLAlchemy proxy. If this behaviour is only for the Alchemy, I'll try to think something and leave it within SQLAlchemy factory.