Bump supported fastapi versions (#1110)
* Bump supported fastapi version to <=0.97, change all fastapi tests from starlette client to httpx.AsyncClient * Add lifecycle manager to fastapi tests * Fix coverage * Add python 3.11 to test suite, bump version
This commit is contained in:
@ -6,8 +6,9 @@ import databases
|
||||
import pydantic
|
||||
import pytest
|
||||
import sqlalchemy
|
||||
from asgi_lifespan import LifespanManager
|
||||
from fastapi import FastAPI
|
||||
from starlette.testclient import TestClient
|
||||
from httpx import AsyncClient
|
||||
|
||||
import ormar
|
||||
from ormar import post_save, property_field
|
||||
@ -153,34 +154,35 @@ async def create_user7(user: RandomModel):
|
||||
return await user.save()
|
||||
|
||||
|
||||
def test_excluding_fields_in_endpoints():
|
||||
client = TestClient(app)
|
||||
with client as client:
|
||||
@pytest.mark.asyncio
|
||||
async def test_excluding_fields_in_endpoints():
|
||||
client = AsyncClient(app=app, base_url="http://testserver")
|
||||
async with client as client, LifespanManager(app):
|
||||
user = {
|
||||
"email": "test@domain.com",
|
||||
"password": "^*^%A*DA*IAAA",
|
||||
"first_name": "John",
|
||||
"last_name": "Doe",
|
||||
}
|
||||
response = client.post("/users/", json=user)
|
||||
response = await client.post("/users/", json=user)
|
||||
created_user = User(**response.json())
|
||||
assert created_user.pk is not None
|
||||
assert created_user.password is None
|
||||
|
||||
user2 = {"email": "test@domain.com", "first_name": "John", "last_name": "Doe"}
|
||||
|
||||
response = client.post("/users/", json=user2)
|
||||
response = await client.post("/users/", json=user2)
|
||||
created_user = User(**response.json())
|
||||
assert created_user.pk is not None
|
||||
assert created_user.password is None
|
||||
|
||||
response = client.post("/users2/", json=user)
|
||||
response = await client.post("/users2/", json=user)
|
||||
created_user2 = User(**response.json())
|
||||
assert created_user2.pk is not None
|
||||
assert created_user2.password is None
|
||||
|
||||
# response has only 3 fields from UserBase
|
||||
response = client.post("/users3/", json=user)
|
||||
response = await client.post("/users3/", json=user)
|
||||
assert list(response.json().keys()) == ["email", "first_name", "last_name"]
|
||||
|
||||
timestamp = datetime.datetime.now()
|
||||
@ -192,7 +194,7 @@ def test_excluding_fields_in_endpoints():
|
||||
"last_name": "Doe",
|
||||
"timestamp": str(timestamp),
|
||||
}
|
||||
response = client.post("/users4/", json=user3)
|
||||
response = await client.post("/users4/", json=user3)
|
||||
assert list(response.json().keys()) == [
|
||||
"id",
|
||||
"email",
|
||||
@ -209,7 +211,7 @@ def test_excluding_fields_in_endpoints():
|
||||
assert isinstance(user_instance.timestamp, datetime.datetime)
|
||||
assert user_instance.timestamp == timestamp
|
||||
|
||||
response = client.post("/users4/", json=user3)
|
||||
response = await client.post("/users4/", json=user3)
|
||||
assert list(response.json().keys()) == [
|
||||
"id",
|
||||
"email",
|
||||
@ -226,11 +228,12 @@ def test_excluding_fields_in_endpoints():
|
||||
)
|
||||
|
||||
|
||||
def test_adding_fields_in_endpoints():
|
||||
client = TestClient(app)
|
||||
with client as client:
|
||||
@pytest.mark.asyncio
|
||||
async def test_adding_fields_in_endpoints():
|
||||
client = AsyncClient(app=app, base_url="http://testserver")
|
||||
async with client as client, LifespanManager(app):
|
||||
user3 = {"last_name": "Test", "full_name": "deleted"}
|
||||
response = client.post("/random/", json=user3)
|
||||
response = await client.post("/random/", json=user3)
|
||||
assert list(response.json().keys()) == [
|
||||
"id",
|
||||
"password",
|
||||
@ -242,7 +245,7 @@ def test_adding_fields_in_endpoints():
|
||||
assert response.json().get("full_name") == "John Test"
|
||||
|
||||
user3 = {"last_name": "Test"}
|
||||
response = client.post("/random/", json=user3)
|
||||
response = await client.post("/random/", json=user3)
|
||||
assert list(response.json().keys()) == [
|
||||
"id",
|
||||
"password",
|
||||
@ -254,11 +257,12 @@ def test_adding_fields_in_endpoints():
|
||||
assert response.json().get("full_name") == "John Test"
|
||||
|
||||
|
||||
def test_adding_fields_in_endpoints2():
|
||||
client = TestClient(app)
|
||||
with client as client:
|
||||
@pytest.mark.asyncio
|
||||
async def test_adding_fields_in_endpoints2():
|
||||
client = AsyncClient(app=app, base_url="http://testserver")
|
||||
async with client as client, LifespanManager(app):
|
||||
user3 = {"last_name": "Test"}
|
||||
response = client.post("/random2/", json=user3)
|
||||
response = await client.post("/random2/", json=user3)
|
||||
assert list(response.json().keys()) == [
|
||||
"id",
|
||||
"password",
|
||||
@ -270,7 +274,8 @@ def test_adding_fields_in_endpoints2():
|
||||
assert response.json().get("full_name") == "John Test"
|
||||
|
||||
|
||||
def test_excluding_property_field_in_endpoints2():
|
||||
@pytest.mark.asyncio
|
||||
async def test_excluding_property_field_in_endpoints2():
|
||||
|
||||
dummy_registry = {}
|
||||
|
||||
@ -278,10 +283,10 @@ def test_excluding_property_field_in_endpoints2():
|
||||
async def after_save(sender, instance, **kwargs):
|
||||
dummy_registry[instance.pk] = instance.dict()
|
||||
|
||||
client = TestClient(app)
|
||||
with client as client:
|
||||
client = AsyncClient(app=app, base_url="http://testserver")
|
||||
async with client as client, LifespanManager(app):
|
||||
user3 = {"last_name": "Test"}
|
||||
response = client.post("/random3/", json=user3)
|
||||
response = await client.post("/random3/", json=user3)
|
||||
assert list(response.json().keys()) == [
|
||||
"id",
|
||||
"password",
|
||||
|
||||
Reference in New Issue
Block a user