adding flight unit tests

This commit is contained in:
shadad00 2023-10-24 11:42:46 -03:00
parent 1458b4f1b5
commit 3d38de1581
No known key found for this signature in database
GPG Key ID: D2E16B7CA3BF586A
2 changed files with 47 additions and 49 deletions

View File

@ -1,49 +0,0 @@
from fastapi.testclient import TestClient
from src.api.main import app
client = TestClient(app)
def test_create_flight(test_db):
response = client.post(
"/flights",
json={
"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",
},
)
assert response.status_code == 200
assert response.json() == {
"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",
}
def test_get_flight(test_db):
response = client.get("/flights/1")
assert response.status_code == 200
# retrieved_fligt = Flight(**response.json())
assert response.json() == {
"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",
}
# assert retrieved_fligt.id == flight_instance.id
# assert retrieved_fligt.__eq__(flight_instance)

View File

@ -0,0 +1,47 @@
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