Implenting unit testing"

This commit is contained in:
2023-01-11 17:38:03 +01:00
parent c8d8bf9cb6
commit edf07d19ff
3 changed files with 80 additions and 14 deletions

View File

@@ -1,4 +1,5 @@
from pydantic import BaseModel from pydantic import BaseModel
from typing import List
class PeopleCreate(BaseModel): class PeopleCreate(BaseModel):
@@ -8,6 +9,10 @@ class PeopleCreate(BaseModel):
country: str country: str
class PeopleList(BaseModel):
people: List[PeopleCreate]
class AverageAgeByCountry(BaseModel): class AverageAgeByCountry(BaseModel):
country: str country: str
average_age: float average_age: float

28
main.py
View File

@@ -2,42 +2,42 @@ from fastapi import FastAPI
from typing import List from typing import List
from back.schemas import PeopleCreate, AverageAgeByCountry, CountByCountry, GenderRepartition from back.schemas import PeopleList, AverageAgeByCountry, CountByCountry, GenderRepartition
app = FastAPI() app = FastAPI()
@app.post("/people") @app.post("/people")
async def create_people(people_list: PeopleCreate) -> dict: async def create_people(people_list: PeopleList) -> dict:
return { return {
"message": "People list has been succesfully imported", "message": "People list has been succesfully imported",
"records_created": 12 "records_created": 10
} }
@app.get("/average_age", response_model=List[AverageAgeByCountry]) @app.get("/average_age", response_model=List[AverageAgeByCountry])
async def read_average_age(name: str): async def read_average_age() -> List[AverageAgeByCountry]:
return [ return [
{'country': 'FR', 'average_age': 22.7}, {'country': 'FR', 'average_age': 15.0},
{'country': 'BE', 'average_age': 14.7}, {'country': 'BE', 'average_age': 25.0},
{'country': 'DE', 'average_age': 27.7}, {'country': 'DE', 'average_age': 23.3},
{'country': 'IT', 'average_age': 32.7}, {'country': 'IT', 'average_age': 45.0},
] ]
@app.get("/count_by_country", response_model=List[CountByCountry]) @app.get("/count_by_country", response_model=List[CountByCountry])
async def read_count_by_country(): async def read_count_by_country() -> List[CountByCountry]:
return [ return [
{'country': 'FR', 'people_count': 2}, {'country': 'FR', 'people_count': 2},
{'country': 'BE', 'people_count': 4}, {'country': 'BE', 'people_count': 4},
{'country': 'DE', 'people_count': 3}, {'country': 'DE', 'people_count': 3},
{'country': 'IT', 'people_count': 7}, {'country': 'IT', 'people_count': 1},
] ]
@app.get("/count_by_gender/{country}", response_model=GenderRepartition) @app.get("/gender_repartition/{country}", response_model=GenderRepartition)
async def read_count_by_gender(country: str): async def read_count_by_gender(country: str) -> GenderRepartition:
return { return {
"female_proportion": 51.2, "female_proportion": 33.3,
"male_proportion": 48.8 "male_proportion": 66.6
} }

61
test_client.py Normal file
View File

@@ -0,0 +1,61 @@
from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
def test_create_people():
response = client.post(
"/people",
json=people_fixture
)
assert response.status_code == 200
assert response.json()['records_created'] == 10
def test_read_average_age():
response = client.get("/average_age")
assert response.status_code == 200
r = response.json()
assert r[0]['average_age'] == 15.0
assert r[1]['average_age'] == 25.0
assert r[2]['average_age'] == 23.3
assert r[3]['average_age'] == 45.0
def test_read_count_by_country():
response = client.get("/count_by_country")
assert response.status_code == 200
r = response.json()
assert r[0]['people_count'] == 2
assert r[1]['people_count'] == 4
assert r[2]['people_count'] == 3
assert r[3]['people_count'] == 1
def test_read_count_by_gender():
response = client.get("/gender_repartition/DE")
assert response.status_code == 200
r = response.json()
assert r['female_proportion'] == 33.3
people_fixture = {
"people": [
{"name": "Alice", "age": 10, "gender": "F", "country": "FR"},
{"name": "Bob", "age": 20, "gender": "M", "country": "FR"},
{"name": "Charlie", "age": 10, "gender": "M", "country": "BE"},
{"name": "David", "age": 20, "gender": "M", "country": "BE"},
{"name": "Evelyn", "age": 30, "gender": "F", "country": "BE"},
{"name": "Fanny", "age": 40, "gender": "F", "country": "BE"},
{"name": "Ghislain", "age": 20, "gender": "M", "country": "DE"},
{"name": "Hector", "age": 22, "gender": "M", "country": "DE"},
{"name": "Isabelle", "age": 28, "gender": "F", "country": "DE"},
{"name": "Jocelyne", "age": 45, "gender": "F", "country": "IT"},
]
}