piccolo_examples icon indicating copy to clipboard operation
piccolo_examples copied to clipboard

Examples needed

Open dantownsend opened this issue 5 years ago • 12 comments

Let me know a use case that you have for an async ORM, and I'll add an example which uses Piccolo.

Alternatively add your own example, or let me know about a project which is using it, and I'll list it here.

dantownsend avatar Jul 30 '20 19:07 dantownsend

@dantownsend It would be great if you add example of conftest.py with postgres test database.

falled10 avatar Oct 13 '20 15:10 falled10

@falled10 You're right - there aren't any examples or docs yet when it comes to testing. I've created an issue to add them:

https://github.com/piccolo-orm/piccolo/issues/14

In Piccolo's own test suite, I tend to launch pytest using a shell script, which specifies a different piccolo_conf file to use for tests, which points to a test database. Something like this:

#!/bin/bash
export PICCOLO_CONF="piccolo_conf_test"
python -m pytest -s $@

Does that help?

dantownsend avatar Oct 13 '20 15:10 dantownsend

@dantownsend not exactly, it didn't create a new test database

falled10 avatar Oct 13 '20 20:10 falled10

@falled10 This workaround should work. It assumes you're using Piccolo migrations.

Create a file like run-tests.sh in the root of your project:

#!/bin/bash
psql -c "DROP DATABASE IF EXISTS my_test_db;"
psql -c "CREATE DATABASE my_test_db;"
export PICCOLO_CONF="piccolo_conf_test"
piccolo migrations forwards all
python -m pytest tests/

Also have a piccolo_conf_test.py file in the root of your project.

from piccolo_conf import * 
from piccolo.engine.postgres import PostgresEngine

DB = PostgresEngine(
    config={
        "host": "localhost",
        "port": "5432",
        "user": "postgres",
        "password": "",
        "database": "my_test_db",
    }
)

If you're not using Piccolo migrations, you can alternatively call MyTable.create_table().run_sync() in the setup method of your test, and MyTable.alter().drop_table().run_sync() in the teardown method.

It's my intention to document this properly, and to potentially create a custom test runner, to remove the need for the shell script.

dantownsend avatar Oct 13 '20 21:10 dantownsend

Hi Daniel This is my first try with FastAPI (thanks to FastAPIWrapper). It's nothing special but it has way to protect endpoints (routes with unsafe HTTP methods) with auth dependencies thru FastAPIKwargs. It's simple backend for forum app https://github.com/sinisaos/headless-forum-fastapi. If you like it you can put in piccolo_example repo or if you don't it dosen't matter.

sinisaos avatar Dec 11 '20 08:12 sinisaos

@sinisaos Amazing, thanks. I had a look yesterday, and am really impressed. I'll play around with it some more today, and will add it to the docs.

dantownsend avatar Dec 12 '20 11:12 dantownsend

@dantownsend Thank you very much. I'm glad you like it. If you want I can make PR to piccolo_api docs and add something like this to task example in FastAPIWrapper section

from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer
from piccolo_api.fastapi.endpoints import FastAPIWrapper, FastAPIKwargs
from piccolo_api.crud.endpoints import PiccoloCRUD

from my_app.tables import Task

app = FastAPI()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="accounts/login")

FastAPIWrapper(
    root_url="/task/",
    fastapi_app=app,
    piccolo_crud=PiccoloCRUD(
        table=Task,
        read_only=False,
    ),
    fastapi_kwargs=FastAPIKwargs(
        all_routes={'tags': ['Task']},  # Added to all endpoints
        get={'deprecated': True},  # Just added to the 'get' endpoint
        post={"dependencies": [Depends(oauth2_scheme)]}, # protected route
        put={"dependencies": [Depends(oauth2_scheme)]},  # protected route
        patch={"dependencies": [Depends(oauth2_scheme)]},  # protected route
        delete_single={"dependencies": [Depends(oauth2_scheme)]},  # protected route
    )
)

What do you think?

sinisaos avatar Dec 12 '20 16:12 sinisaos

@sinisaos That's a good idea. I haven't actually used the OAuth2 features of FastAPI before - I'm learning about them from your project. I like how it integrates with the OpenAPI docs. How about creating a sub page in the Piccolo API docs which has some advanced recipes for using FastAPI with Piccolo? There could be a page about authentication, including your example code.

dantownsend avatar Dec 13 '20 18:12 dantownsend

@dantownsend Great. That's better idea and you can use any code sample you need. I also never used FastAPI before (I always experimenting with Starlette which I like better but OpenAPI integration from FastAPI is great). I just mix example from FastAPI security section with great login() function from Piccolo BaseUser. I must admit that FastAPIWrapper is actually guilty for trying FastAPI for the first time :)

sinisaos avatar Dec 13 '20 20:12 sinisaos