Files
TallerCiCd/backend/tests/test_api.py
2025-12-16 09:32:14 +01:00

109 lines
3.3 KiB
Python

# CI/CD Workshop
# Copyright (C) 2025 OpenBokeron
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
def test_health():
response = client.get("/health")
assert response.status_code == 200
body = response.json()
assert body["status"] == "ok"
def test_menu_contains_fish():
response = client.get("/menu")
assert response.status_code == 200
body = response.json()
mains = [main.lower() for main in body["mains"]]
assert any("pescado" in main or "pedro" in main for main in mains)
assert body["menu_price"] == 5.5
deal = body["university_deal"]
assert deal["old_price"] == 4.5
assert deal["current_price"] == 5.5
def test_prices_random_list():
response = client.get("/prices")
assert response.status_code == 200
body = response.json()
items = body["items"]
assert isinstance(items, list)
assert len(items) >= 1
first = items[0]
assert "item" in first and "price" in first and "currency" in first
assert all(item["price"] <= 3 for item in items)
def test_build_history(monkeypatch):
jenkins_builds = [
{
"number": 205,
"result": "SUCCESS",
"timestamp": 1719992400000,
"duration": 75000,
"url": "http://jenkins.local/job/demo/205",
"changeSets": [
{
"items": [
{
"commitId": "9ac3f91d9bd0f0b7bfc5",
"msg": "Anade la API de Jenkins",
"author": {"fullName": "Dev One"},
}
]
}
],
},
{
"number": 204,
"result": None,
"timestamp": 1719988800000,
"duration": 1500,
"url": "http://jenkins.local/job/demo/204",
"changeSets": [],
},
]
monkeypatch.setattr("app.services.builds.fetch_builds", lambda limit=5: jenkins_builds)
response = client.get("/builds")
assert response.status_code == 200
body = response.json()
builds = body["builds"]
assert isinstance(builds, list)
assert len(builds) == 2
first = builds[0]
assert first["number"] == 205
assert first["status"] == "success"
assert first["finished_at"] == 1719992400000
assert first["duration_seconds"] == 75
assert first["url"].endswith("/205")
assert first["commits"] == [
{"commit": "9ac3f91", "message": "Anade la API de Jenkins", "author": "Dev One"}
]
second = builds[1]
assert second["number"] == 204
assert second["status"] == "running"
assert second["duration_seconds"] == 1
assert second["commits"] == []