s3rius / FastAPI-template

Feature rich robust FastAPI template.
MIT License
1.79k stars 161 forks source link

feature: MongoDB support via beanie #187

Closed usefulalgorithm closed 7 months ago

usefulalgorithm commented 10 months ago

Adds support for MongoDB and Beanie. Probably fixes #119 ?

I did a simple crud app with virtually the same setup, seems to be working fine.

Demos

Todos / caveats / things to note:

s3rius commented 10 months ago

Actually, you can easily test the project generations by creating one command and running it everytime you make changes. For example.

fastapi_template --name myproject --db mongo --api-type rest --orm beanie --dummy --routers --quite --force

This command will be overriding all files within myproject folder. It helps me fixing all flake errors.

s3rius commented 10 months ago

Also, you should not start DB separately from the project. The main point is that the up command will bring all services app, including your application.

docker compose up --build
usefulalgorithm commented 10 months ago

Hi @s3rius , I think I addressed all the things you suggested:

I also made sure all flake8 warnings are gone, mypy does not complain, and that I was able to create & read dummy models with the containers started by docker compose.

However I wasn't able to get pytest to run successfully (both in fastapi_template and in the generated myproject). In fastapi_template running poetry run pytest breaks:

FAILED fastapi_template/tests/test_generator.py::test_default_with_db[sqlalchemy-mysql] - AssertionError
FAILED fastapi_template/tests/test_generator.py::test_default_with_db[tortoise-mysql] - AssertionError
FAILED fastapi_template/tests/test_generator.py::test_default_with_db[ormar-mysql] - AssertionError
FAILED fastapi_template/tests/test_generator.py::test_default_with_nosql_db[beanie-mongodb] - AssertionError

and running tests in myproject would always end up with an unauthenticated client. Are the tests meant to be run only when triggered by github / gitlab pipelines?

Thanks!

usefulalgorithm commented 10 months ago

Hi @s3rius, I modified the PR per your suggestions, please check out the changes and see if they make sense when you have time, thanks a bunch!

usefulalgorithm commented 8 months ago

Hi @s3rius , sorry for the lack of response. I've addressed all your suggestions in the latest commit, please take a look when you have time.

There's something I don't get in the generated test_dummy.py file:

async def test_creation(
    fastapi_app: FastAPI,
    client: AsyncClient,
) -> None:
    """Tests dummy instance creation."""
    url = fastapi_app.url_path_for("create_dummy_model")
    test_name = uuid.uuid4().hex
    response = await client.put(
        url,
        json={
            "name": test_name,
        },
    )   # Inserts a Dummy into DB
# ... snipped ...
@pytest.mark.anyio
async def test_getting(
    fastapi_app: FastAPI,
    client: AsyncClient,
) -> None:
    """Tests dummy instance retrieval."""
    dao = DummyDAO()
    test_name = uuid.uuid4().hex
    await dao.create_dummy_model(name=test_name)  # XXX Inserts another Dummy into DB??
    url = fastapi_app.url_path_for("get_dummy_models")
    response = await client.get(url)
    dummies = response.json()

    assert response.status_code == status.HTTP_200_OK
    assert len(dummies) == 1   # XXX Should be 2?

This the only test that's failing. What I don't understand is that how come in test_getting it is testing that the object we inserted is indeed the only object in the database, while the object inserted in test_creation is never cleaned up?

Thanks in advance for taking a look and answering the question!

I decided to remove the inserted object in test_creation, so that in test_getting it's going to report only one object in the database. Now all tests are passing on my machine.

Thanks!

usefulalgorithm commented 8 months ago

Hi @s3rius , I believe this is complete now. Thanks!

usefulalgorithm commented 8 months ago

@s3rius ping

s3rius commented 7 months ago

Okay. I don't know what is happening, but now mypy seems to complain about some types. Can you fix it?

Just add an [Any] to every AsyncConnectionPool like AsyncConnectionPool[Any]. Don't forget to add any in imports if it doesn't exists yet in the file.

usefulalgorithm commented 7 months ago

Sorry I missed a AsyncConnectionPool... can you trigger the test again? Thanks

usefulalgorithm commented 7 months ago

I don't get it...

s3rius commented 7 months ago

@usefulalgorithm, that's fine. Happens sometimes. Will rerun tests.

s3rius commented 7 months ago

I will try to fix the problem on develop branch. Thanks for your outstanding contribution.