-
Notifications
You must be signed in to change notification settings - Fork 0
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
🏭 Factories #8
Merged
Merged
🏭 Factories #8
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fbe720b
Adds factories for test data
afred 3a89e2b
Adds async persistence implementation
mrharpo ce26c82
Moves pydantic-factories to dev dependencies
mrharpo 9652e05
Adds rest of factories
mrharpo f06e871
Removes factories for link tables; Adds factory for Batches
afred b70d093
Merge branch 'main' into sample-data-factories
mrharpo 4fd34b9
Add trailing comma for black
mrharpo 0534031
Moves factories to tests
mrharpo 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
__pycache__ | ||
|
||
# Development sqlite db | ||
chowda.sqlite | ||
*.sqlite |
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,12 @@ | ||
from sqlalchemy import create_engine | ||
from .config import ENGINE_URI | ||
|
||
engine = create_engine(ENGINE_URI, connect_args={'check_same_thread': False}, echo=True) | ||
|
||
|
||
def create_async_engine(): | ||
from sqlmodel.ext.asyncio.session import AsyncEngine | ||
|
||
return AsyncEngine( | ||
create_engine(ENGINE_URI, connect_args={'check_same_thread': False}, echo=True) | ||
) |
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,57 @@ | ||
from chowda.app import engine | ||
from chowda.config import ENGINE_URI | ||
from chowda.models import MediaFile, Collection, ClamsApp | ||
from pydantic_factories import ( | ||
ModelFactory, | ||
SyncPersistenceProtocol, | ||
AsyncPersistenceProtocol, | ||
) | ||
from typing import TypeVar, List | ||
from pydantic import BaseModel | ||
from sqlmodel import Session | ||
from sqlmodel.ext.asyncio.session import AsyncSession, AsyncEngine | ||
|
||
T = TypeVar("T", bound=BaseModel) | ||
|
||
|
||
class SyncPersistenceHandler(SyncPersistenceProtocol[T]): | ||
def save(self, data: T) -> T: | ||
with Session(engine) as session: | ||
session.add(data) | ||
session.commit() | ||
|
||
def save_many(self, data: List[T]) -> List[T]: | ||
with Session(engine) as session: | ||
for d in data: | ||
session.add(d) | ||
session.commit() | ||
|
||
|
||
class AsyncPersistenceHandler(AsyncPersistenceProtocol[T]): | ||
async def save(self, data: T) -> T: | ||
async with AsyncSession(engine) as session: | ||
session.add(data) | ||
await session.commit() | ||
|
||
async def save_many(self, data: List[T]) -> List[T]: | ||
async with AsyncSession(engine) as session: | ||
for d in data: | ||
session.add(d) | ||
await session.commit() | ||
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. Shamelessly copied from fastapi/sqlmodel#161 (comment) Unfortunately, running it gives me:
But I think that's enough proof it works, so we should include it and we'll test it when we swap out for an async driver. |
||
|
||
|
||
class ChowdaFactory(ModelFactory): | ||
__sync_persistence__ = SyncPersistenceHandler | ||
__async_persistence__ = AsyncPersistenceHandler | ||
|
||
|
||
class MediaFileFactory(ChowdaFactory): | ||
__model__ = MediaFile | ||
|
||
|
||
class CollectionFactory(ChowdaFactory): | ||
__model__ = Collection | ||
|
||
|
||
class ClamsAppFactory(ChowdaFactory): | ||
__model__ = ClamsApp |
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 |
---|---|---|
|
@@ -2,3 +2,4 @@ starlette-admin | |
uvicorn | ||
sqlmodel | ||
pydantic[email] | ||
pydantic-factories |
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.
Even creating the async engine throws the same
sqlalchemy.exc.InvalidRequestError
, hence the protection.