Open dantownsend opened 4 years ago
@dantownsend It would be great if you add example of conftest.py
with postgres test database.
@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 not exactly, it didn't create a new test database
@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.
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 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 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 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 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 :)
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.