-
Updated: Original question missed the Beanie extension in I'd like to use Pydantic Factories on Beanie models. These models are an extension of from beanie import Document, init_beanie
from motor.motor_asyncio import AsyncIOMotorClient
from starlite import Starlite, State
import myapp.dependencies.get_settings
class Widget(Document):
name: str
async def initialize_beanie(state: State) -> None:
settings = get_settings()
cx = AsyncIOMotorClient(settings.mongodb_url, uuidRepresentation="standard")
db = getattr(cx, settings.mongodb_db)
await init_beanie(
database=db,
document_models=[Widget],
allow_index_dropping=True,
)
app = Starlite(
route_handlers=[SandboxController],
on_startup=[initialize_beanie],
dependencies={"settings": Provide(get_settings, use_cache=True)},
) I'm trying to figure out where would be best to do something similar when using import pytest
from pydantic_factories import ModelFactory
from myapp.models import Widget
class WidgetFactory(ModelFactory[Widget]):
__model__ = Widget
@pytest.fixture()
def widget():
return WidgetFactory.build()
def test_widget(widget: Widget):
assert isinstance(widget.name, str) The above code will raise Would it make sense to extend |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
import asyncio
from random import random
import pytest
from beanie import init_beanie
from mongomock_motor import AsyncMongoMockClient
from myapp.models import Widget
@pytest.fixture(scope="session", autouse=True)
def faker_seed():
client = AsyncMongoMockClient()
asyncio.run(init_beanie(database=client.beanie_test, document_models=[Widget]))
return random() That gets me passed In reality, though, my data has references / links and the moment I try and add a model that has a link things blow up ( Example that demonstrates recursion error:
from beanie import Document, Indexed, Link
class Parent(Document):
name: Indexed(str, unique=True)
class Settings: # Beanie configuration
name = "sandbox-parents"
class Config: # Pydantic configuration
schema_extra = {
"example": {
"name": "Parent Name Goes Here",
},
}
class Child(Document):
parent: Link[Parent]
name: Indexed(str, unique=True)
class Settings: # Beanie configuration
name = "sandbox-children"
class Config: # Pydantic configuration
schema_extra = {
"example": {
"parent": "document ID of parent goes here",
"name": "Child Name Goes Here",
},
}
import asyncio
from random import random
import pytest
from beanie import init_beanie
from mongomock_motor import AsyncMongoMockClient
from myapp.models import Child, Parent
@pytest.fixture(scope="session", autouse=True)
def faker_seed():
client = AsyncMongoMockClient()
asyncio.run(
init_beanie(database=client.beanie_test, document_models=[Parent, Child])
)
return random()
import pytest
from pydantic_factories import ModelFactory
from myapp.models import Child, Parent
class ParentFactory(ModelFactory[Parent]):
__model__ = Parent
@pytest.fixture()
def parent():
return ParentFactory.build()
class ChildFactory(ModelFactory[Child]):
__model__ = Child
@pytest.fixture()
def child():
return ChildFactory.build()
@pytest.mark.asyncio()
async def test_parent(parent: Parent):
assert isinstance(parent.name, str)
@pytest.mark.asyncio()
async def test_child(child: Child):
assert isinstance(child.name, str) Results in:
...on the |
Beta Was this translation helpful? Give feedback.
-
I enabled discussions in the pydantic-factories repo, please move there and delete this discussion |
Beta Was this translation helpful? Give feedback.
I enabled discussions in the pydantic-factories repo, please move there and delete this discussion