Adding functional test to /flights endpoint

This commit is contained in:
shadad 2023-10-26 13:59:23 +00:00
parent b0638f195a
commit fcc2189674
2 changed files with 113 additions and 28 deletions

View File

@ -1,3 +1,6 @@
import random
from datetime import datetime
import pytest
from src.api.db import Base, SessionLocal, engine
@ -42,3 +45,67 @@ def get_flight():
return session.query(Flight).filter(Flight.id == flight_id).first()
return get_flight
@pytest.fixture(scope="module")
def create_flights_on_database(test_database, create_flight):
test_database.query(Flight).delete()
flights_with_last_updated = []
for flight in flights:
flights_with_last_updated.append(create_flight(flight))
return flights_with_last_updated
@pytest.fixture(scope="module")
def random_origin():
return random.choice(flights).origin
@pytest.fixture(scope="module")
def last_update():
return datetime(2024, 10, 26, 9, 15, 0)
@pytest.fixture(scope="module")
def flight_to_create():
return random.choice(flights)
flights = [
Flight(
flight_code="ABC123",
status="pending",
origin="SLA",
destination="AEP",
departure_time=datetime(2023, 10, 23, 12, 0, 0),
arrival_time=datetime(2023, 10, 24, 12, 0, 0),
gate="10",
),
Flight(
flight_code="ABC124",
status="pending",
origin="AEP",
destination="SLA",
departure_time=datetime(2023, 10, 24, 12, 0, 0),
arrival_time=datetime(2023, 10, 25, 12, 0, 0),
gate="10",
),
Flight(
flight_code="XYZ789",
status="delayed",
origin="JFK",
destination="LAX",
departure_time=datetime(2023, 10, 25, 14, 30, 0),
arrival_time=datetime(2023, 10, 25, 18, 45, 0),
gate="5",
),
Flight(
flight_code="DEF456",
status="on-time",
origin="ORD",
destination="DFW",
departure_time=datetime(2023, 10, 26, 9, 15, 0),
arrival_time=datetime(2023, 10, 26, 11, 30, 0),
gate="7",
),
]

View File

@ -19,27 +19,6 @@ creating_flight = {
"gate": "10",
}
flights = [
Flight(
flight_code="ABC123",
status="pending",
origin="SLA",
destination="AEP",
departure_time=datetime(2023, 10, 23, 12, 0, 0),
arrival_time=datetime(2023, 10, 24, 12, 0, 0),
gate="10",
),
Flight(
flight_code="ABC124",
status="pending",
origin="AEP",
destination="SLA",
departure_time=datetime(2023, 10, 24, 12, 0, 0),
arrival_time=datetime(2023, 10, 25, 12, 0, 0),
gate="10",
),
]
def test_post_flight(test_database, get_flight):
test_database.query(Flight).delete()
@ -53,9 +32,9 @@ def test_post_flight(test_database, get_flight):
assert db_retrieved_flight.flight_code == creating_flight["flight_code"]
def test_patch_flight(test_database, create_flight):
def test_patch_flight(test_database, create_flight, flight_to_create):
test_database.query(Flight).delete()
created_flight = create_flight(flights[0])
created_flight = create_flight(flight_to_create)
api_call_retrieved_flight = client.patch(
f"/flights/{created_flight.id}", data=json.dumps({"status": "on-boarding"})
)
@ -65,10 +44,49 @@ def test_patch_flight(test_database, create_flight):
assert api_call_retrieved_flight_data["status"] == "on-boarding"
def test_all_flights(test_database, create_flight):
test_database.query(Flight).delete()
create_flight(flights[0])
create_flight(flights[1])
def test_all_flights(create_flights_on_database):
resp = client.get("/flights")
print(resp.json())
data = resp.json()
assert resp.status_code == 200
compare_flight_arrays(data, create_flights_on_database)
def test_all_flights_by_origin(random_origin, create_flights_on_database):
filtered_flights = [
flight
for flight in create_flights_on_database
if flight.origin == random_origin
]
resp = client.get(f"/flights?origin={random_origin}")
data = resp.json()
if len(filtered_flights) > 0:
assert resp.status_code == 200
compare_flight_arrays(data, filtered_flights)
else:
assert resp.status_code == 404
def test_all_flights_updated_since(
last_update, random_origin, create_flights_on_database
):
filtered_flights = [
flight
for flight in create_flights_on_database
if flight.last_updated >= last_update and flight.origin == random_origin
]
resp = client.get(f"/flights?origin={random_origin}&lastUpdated={last_update}")
data = resp.json()
if len(filtered_flights) > 0:
assert resp.status_code == 200
compare_flight_arrays(data, filtered_flights)
else:
assert resp.status_code == 404
def compare_flight_arrays(retrieved, pivotal):
assert len(retrieved) == len(pivotal)
for retrieved_flight, filtered in zip(retrieved, pivotal):
assert retrieved_flight["flight_code"] == filtered.flight_code