Skip to content
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

Examples needed #1

Open
dantownsend opened this issue Jul 30, 2020 · 9 comments
Open

Examples needed #1

dantownsend opened this issue Jul 30, 2020 · 9 comments
Labels
help wanted Extra attention is needed

Comments

@dantownsend
Copy link
Member

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 dantownsend added the help wanted Extra attention is needed label Jul 30, 2020
@falled10
Copy link

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

@dantownsend
Copy link
Member Author

@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:

piccolo-orm/piccolo#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?

@falled10
Copy link

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

@dantownsend
Copy link
Member Author

@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.

@sinisaos
Copy link
Member

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.

@dantownsend
Copy link
Member Author

@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.

@sinisaos
Copy link
Member

@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?

@dantownsend
Copy link
Member Author

@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.

@sinisaos
Copy link
Member

@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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants