From edf07d19ff1d866fa856f39b49e84e6b41af00bd Mon Sep 17 00:00:00 2001 From: ewandor Date: Wed, 11 Jan 2023 17:38:03 +0100 Subject: [PATCH] Implenting unit testing" --- back/schemas.py | 5 ++++ main.py | 28 +++++++++++------------ test_client.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 14 deletions(-) create mode 100644 test_client.py diff --git a/back/schemas.py b/back/schemas.py index 235bb4c..d8800a0 100644 --- a/back/schemas.py +++ b/back/schemas.py @@ -1,4 +1,5 @@ from pydantic import BaseModel +from typing import List class PeopleCreate(BaseModel): @@ -8,6 +9,10 @@ class PeopleCreate(BaseModel): country: str +class PeopleList(BaseModel): + people: List[PeopleCreate] + + class AverageAgeByCountry(BaseModel): country: str average_age: float diff --git a/main.py b/main.py index c5b009e..7265b8b 100644 --- a/main.py +++ b/main.py @@ -2,42 +2,42 @@ from fastapi import FastAPI from typing import List -from back.schemas import PeopleCreate, AverageAgeByCountry, CountByCountry, GenderRepartition +from back.schemas import PeopleList, AverageAgeByCountry, CountByCountry, GenderRepartition app = FastAPI() @app.post("/people") -async def create_people(people_list: PeopleCreate) -> dict: +async def create_people(people_list: PeopleList) -> dict: return { "message": "People list has been succesfully imported", - "records_created": 12 + "records_created": 10 } @app.get("/average_age", response_model=List[AverageAgeByCountry]) -async def read_average_age(name: str): +async def read_average_age() -> List[AverageAgeByCountry]: return [ - {'country': 'FR', 'average_age': 22.7}, - {'country': 'BE', 'average_age': 14.7}, - {'country': 'DE', 'average_age': 27.7}, - {'country': 'IT', 'average_age': 32.7}, + {'country': 'FR', 'average_age': 15.0}, + {'country': 'BE', 'average_age': 25.0}, + {'country': 'DE', 'average_age': 23.3}, + {'country': 'IT', 'average_age': 45.0}, ] @app.get("/count_by_country", response_model=List[CountByCountry]) -async def read_count_by_country(): +async def read_count_by_country() -> List[CountByCountry]: return [ {'country': 'FR', 'people_count': 2}, {'country': 'BE', 'people_count': 4}, {'country': 'DE', 'people_count': 3}, - {'country': 'IT', 'people_count': 7}, + {'country': 'IT', 'people_count': 1}, ] -@app.get("/count_by_gender/{country}", response_model=GenderRepartition) -async def read_count_by_gender(country: str): +@app.get("/gender_repartition/{country}", response_model=GenderRepartition) +async def read_count_by_gender(country: str) -> GenderRepartition: return { - "female_proportion": 51.2, - "male_proportion": 48.8 + "female_proportion": 33.3, + "male_proportion": 66.6 } diff --git a/test_client.py b/test_client.py new file mode 100644 index 0000000..76cf22e --- /dev/null +++ b/test_client.py @@ -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"}, + ] +} \ No newline at end of file