47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
"""Tests del contrato (niveles 1 y 2). NO los modifiquéis para que pasen: arreglad la API.
|
|
Ejecutar desde api/: python -m pytest -q
|
|
"""
|
|
import importlib
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
API_DIR = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(API_DIR))
|
|
|
|
|
|
@pytest.fixture()
|
|
def cliente(tmp_path, monkeypatch):
|
|
monkeypatch.setenv("DB_PATH", str(tmp_path / "votos.db"))
|
|
monkeypatch.setenv("CENSO_PATH", str(API_DIR.parent / "alumnos.csv"))
|
|
import app as modulo
|
|
importlib.reload(modulo)
|
|
return modulo.app.test_client()
|
|
|
|
|
|
def test_opciones(cliente):
|
|
assert cliente.get("/api/opciones").get_json() == ["Ibiza", "Praga"]
|
|
|
|
|
|
def test_nivel1_voto_valido_cuenta(cliente):
|
|
r = cliente.post("/api/votos", json={"codigo": "A0001", "opcion": "Ibiza"})
|
|
assert r.status_code == 201
|
|
assert cliente.get("/api/resultados").get_json() == {"Ibiza": 1, "Praga": 0, "total": 1}
|
|
|
|
|
|
def test_nivel2_repetir_codigo_da_409(cliente):
|
|
cliente.post("/api/votos", json={"codigo": "A0002", "opcion": "Praga"})
|
|
r = cliente.post("/api/votos", json={"codigo": "A0002", "opcion": "Ibiza"})
|
|
assert r.status_code == 409
|
|
|
|
|
|
def test_nivel2_codigo_fuera_del_censo_da_403(cliente):
|
|
r = cliente.post("/api/votos", json={"codigo": "Z9999", "opcion": "Ibiza"})
|
|
assert r.status_code == 403
|
|
|
|
|
|
def test_nivel2_opcion_desconocida_da_400(cliente):
|
|
r = cliente.post("/api/votos", json={"codigo": "A0003", "opcion": "Roma"})
|
|
assert r.status_code == 400
|