117 lines
2.8 KiB
Python
117 lines
2.8 KiB
Python
import random
|
|
from datetime import datetime
|
|
|
|
import pytest
|
|
|
|
from src.api.db import Base, SessionLocal, engine
|
|
from src.api.models.flight import Flight
|
|
|
|
session = SessionLocal()
|
|
base = Base
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def test_database():
|
|
base.metadata.drop_all(bind=engine)
|
|
base.metadata.create_all(bind=engine)
|
|
yield session
|
|
session.close()
|
|
base.metadata.drop_all(bind=engine)
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def create_flight():
|
|
def create_flight(flight: Flight):
|
|
db_flight = Flight(
|
|
flight_code=flight.flight_code,
|
|
status=flight.status,
|
|
origin=flight.origin,
|
|
destination=flight.destination,
|
|
departure_time=flight.departure_time,
|
|
arrival_time=flight.arrival_time,
|
|
gate=flight.gate,
|
|
user_id=flight.user_id,
|
|
)
|
|
session.add(db_flight)
|
|
session.commit()
|
|
session.refresh(db_flight)
|
|
return db_flight
|
|
|
|
return create_flight
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def get_flight():
|
|
def get_flight(flight_id: int):
|
|
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",
|
|
user_id=1,
|
|
),
|
|
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",
|
|
user_id=1,
|
|
),
|
|
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",
|
|
user_id=1,
|
|
),
|
|
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",
|
|
user_id=1,
|
|
),
|
|
]
|