linting functional tests

This commit is contained in:
shadad 2023-10-24 23:06:32 +00:00
parent 3d38de1581
commit fa67d26d94
7 changed files with 174 additions and 6 deletions

3
+ Normal file
View File

@ -0,0 +1,3 @@
#!/bin/bash
ENV_DEV_FILE=/home/shadad/fids/flights-domain/.env.dev.example
sudo docker compose -f flights-domain/docker-compose.yml --env-file $ENV_DEV_FILE up --abort-on-container-exit --renew-anon-volumes

10
build-test.sh Executable file
View File

@ -0,0 +1,10 @@
#!/bin/bash -e
FLIGHTS_INFO_PROD_IMAGE_NAME=flights-information:prod
FLIGHTS_INFO_TEST_IMAGE_NAME=flights-information:test
FLIGHTS_INFORMATION=flights-domain/flights-information
sudo docker build $FLIGHTS_INFORMATION -f $FLIGHTS_INFORMATION/Dockerfile.prod -t ${FLIGHTS_INFO_PROD_IMAGE_NAME}
sudo docker build $FLIGHTS_INFORMATION -f $FLIGHTS_INFORMATION/Dockerfile.test --build-arg "BASE_IMAGE=$FLIGHTS_INFO_PROD_IMAGE_NAME" -t ${FLIGHTS_INFO_TEST_IMAGE_NAME}

View File

@ -1,4 +1,5 @@
POSTGRES_USER=user
POSTGRES_PASS=password
POSTGRES_DB=api_dev
APP_SETTINGS=src.config.DevelopmentConfig
APP_SETTINGS=src.config.DevelopmentConfig
API_IMAGE=flights-information:test

View File

@ -1,10 +1,44 @@
import pytest
from src.api.db import Base, engine
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_db():
Base.metadata.create_all(bind=engine)
yield
Base.metadata.drop_all(bind=engine)
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,
)
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

View File

@ -0,0 +1,74 @@
import json
from datetime import datetime
from fastapi.testclient import TestClient
from src.api.main import app
from src.api.models.flight import Flight
client = TestClient(app)
creating_flight = {
"flight_code": "ABC123",
"status": "pending",
"origin": "SLA",
"destination": "AEP",
"departure_time": datetime(2023, 10, 23, 12, 0, 0).isoformat(),
"arrival_time": datetime(2023, 10, 24, 12, 0, 0).isoformat(),
"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()
api_call_retrieved_flight = client.post(
"/flights", data=json.dumps(creating_flight)
)
api_call_retrieved_flight_data = api_call_retrieved_flight.json()
db_retrieved_flight = get_flight(api_call_retrieved_flight_data["id"])
assert db_retrieved_flight.flight_code == creating_flight["flight_code"]
def test_patch_flight(test_database, create_flight):
test_database.query(Flight).delete()
created_flight = create_flight(flights[0])
api_call_retrieved_flight = client.patch(
f"/flights/{created_flight.id}", data=json.dumps({"status": "on-boarding"})
)
assert api_call_retrieved_flight.status_code == 200
api_call_retrieved_flight_data = api_call_retrieved_flight.json()
assert api_call_retrieved_flight_data["id"] == created_flight.id
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])
resp = client.get("/flights")
print(resp.json())
assert resp.status_code == 200

View File

@ -0,0 +1,43 @@
from fastapi.testclient import TestClient
import src.api.cruds.flight
from src.api.main import app
client = TestClient(app)
mocked_flight = {
"id": 1,
"flight_code": "ABC125",
"status": "En ruta",
"origin": "Ciudad B",
"destination": "Ciudad A",
"departure_time": "2023-10-10 10:00 AM",
"arrival_time": "2023-10-10 12:00 PM",
"gate": "A2",
}
class AttrDict(dict):
def __init__(self, *args, **kwargs):
super(AttrDict, self).__init__(*args, **kwargs)
self.__dict__ = self
def test_not_found_flight(monkeypatch):
def mock_get_flight_by_id(db, id):
return None
monkeypatch.setattr(src.api.cruds.flight, "get_flight_by_id", mock_get_flight_by_id)
resp = client.get("/flights/1")
assert resp.status_code == 404
def test_successful_get_flight(monkeypatch):
def mock_get_flight_by_id(db, id):
return mocked_flight
monkeypatch.setattr(src.api.cruds.flight, "get_flight_by_id", mock_get_flight_by_id)
response = client.get("/flights/1")
assert response.status_code == 200
assert response.json() == mocked_flight

3
run_test.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
ENV_DEV_FILE=/home/shadad/fids/flights-domain/.env.dev.example
sudo docker compose -f flights-domain/docker-compose.yml --env-file $ENV_DEV_FILE up