art049 / odmantic

Sync and Async ODM (Object Document Mapper) for MongoDB based on python type hints
http://art049.github.io/odmantic
ISC License
1.04k stars 94 forks source link

Question: how to mock/write propper unittests #403

Open jesusch opened 7 months ago

jesusch commented 7 months ago

Question

I'd really love to use odmantic/fastapi to replace my current database setup (dynamodb)

Yet any way I try to implement testcases seem to fail as mongomock doesn't support sessions

Any way to tell odmantic not to use them?

jesusch commented 4 months ago

given this code raises "RuntimeError: Event loop is closed" as soon as more then one test is performed

from typing import Optional
from unittest import IsolatedAsyncioTestCase
from odmantic import AIOEngine, Model, Field

class Publisher(Model):
    name: str
    founded: int = Field(ge=1440)
    location: Optional[str] = None

engine = AIOEngine()

class TestPatients(IsolatedAsyncioTestCase):

    async def test_one(self):
        await engine.save(
            Publisher(
                name="Penguin",
                founded=1935,
                location="London"
            )
        )

    async def test_two(self):
        await engine.save(
            Publisher(
                name="Penguin",
                founded=1935,
                location="London"
            )
        )
jesusch commented 4 months ago

If anyone interested - I started an example project which (still) fails https://github.com/jesusch/odmantic-test