Added base file for serie

This commit is contained in:
Rick
2025-11-08 12:19:34 +01:00
parent 79d76e780c
commit 076765e5c5
7 changed files with 376 additions and 0 deletions

View File

@@ -0,0 +1,179 @@
import uuid
from fastapi import status
from fastapi.testclient import TestClient
from sqlmodel import Session
from app.core.config import settings
from app.tests.utils.serie import create_random_serie
def test_create_serie(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None:
data = {
"name": "Zondag spel",
"contact": "Rick",
}
response = client.post(
f"{settings.API_V1_STR}/series/",
headers=superuser_token_headers,
json=data,
)
assert response.status_code == status.HTTP_200_OK
content = response.json()
assert content["name"] == data["name"]
assert content["contact"] == data["contact"]
assert "id" in content
def test_create_serie_no_permissions(client: TestClient, normal_user_token_headers: dict[str, str], db: Session) -> None:
data = {
"name": "Zondag Spel",
"contact": "Rick",
}
response = client.post(
f"{settings.API_V1_STR}/series/",
headers=normal_user_token_headers,
json=data,
)
assert response.status_code == status.HTTP_403_FORBIDDEN
assert response.json()["detail"] == "Not enough permissions"
def test_read_serie(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None:
serie = create_random_serie(db)
response = client.get(
f"{settings.API_V1_STR}/series/{serie.id}",
headers=superuser_token_headers,
)
assert response.status_code == status.HTTP_200_OK
content = response.json()
assert content["id"] == str(serie.id)
assert content["name"] == serie.name
assert content["contact"] == serie.contact
def test_read_serie_not_found(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None:
response = client.get(
f"{settings.API_V1_STR}/series/{uuid.uuid4()}",
headers=superuser_token_headers,
)
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.json()["detail"] == "Serie not found"
def test_read_serie_no_permission(client: TestClient, normal_user_token_headers: dict[str, str], db: Session) -> None:
serie = create_random_serie(db)
response = client.get(
f"{settings.API_V1_STR}/series/{serie.id}",
headers=normal_user_token_headers,
)
assert response.status_code == status.HTTP_403_FORBIDDEN
assert response.json()["detail"] == "Not enough permissions"
def test_read_series(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None:
create_random_serie(db)
create_random_serie(db)
response = client.get(
f"{settings.API_V1_STR}/series/",
headers=superuser_token_headers,
)
assert response.status_code == status.HTTP_200_OK
content = response.json()
assert "count" in content
assert content["count"] >= 2
assert "data" in content
assert isinstance(content["data"], list)
assert len(content["data"]) <= content["count"]
def test_read_series_no_permissions(client: TestClient, normal_user_token_headers: dict[str, str], db: Session) -> None:
create_random_serie(db)
create_random_serie(db)
response = client.get(
f"{settings.API_V1_STR}/series/",
headers=normal_user_token_headers,
)
assert response.status_code == status.HTTP_200_OK
content = response.json()
assert "count" in content
assert content["count"] == 0
assert "data" in content
assert isinstance(content["data"], list)
assert len(content["data"]) == 0
def test_update_serie(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None:
serie = create_random_serie(db)
data = {
"name": "Updated name",
"contact": "Updated contact",
}
response = client.put(
f"{settings.API_V1_STR}/series/{serie.id}",
headers=superuser_token_headers,
json=data,
)
assert response.status_code == status.HTTP_200_OK
content = response.json()
assert content["id"] == str(serie.id)
assert content["name"] == data["name"]
assert content["contact"] == data["contact"]
def test_update_serie_not_found(client: TestClient, superuser_token_headers: dict[str, str]) -> None:
data = {
"name": "Not found",
"contact": "Not found",
}
response = client.put(
f"{settings.API_V1_STR}/series/{uuid.uuid4()}",
headers=superuser_token_headers,
json=data,
)
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.json()["detail"] == "Serie not found"
def test_update_serie_no_permissions(client: TestClient, normal_user_token_headers: dict[str, str], db: Session) -> None:
serie = create_random_serie(db)
data = {
"name": "No permissions",
"contact": "No permissions",
}
response = client.put(
f"{settings.API_V1_STR}/series/{serie.id}",
headers=normal_user_token_headers,
json=data,
)
assert response.status_code == status.HTTP_403_FORBIDDEN
assert response.json()["detail"] == "Not enough permissions"
def test_delete_serie(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None:
serie = create_random_serie(db)
response = client.delete(
f"{settings.API_V1_STR}/series/{serie.id}",
headers=superuser_token_headers,
)
assert response.status_code == status.HTTP_200_OK
assert response.json()["message"] == "Serie deleted successfully"
def test_delete_serie_not_found(client: TestClient, superuser_token_headers: dict[str, str]) -> None:
response = client.delete(
f"{settings.API_V1_STR}/series/{uuid.uuid4()}",
headers=superuser_token_headers,
)
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.json()["detail"] == "Serie not found"
def test_delete_serie_no_permissions(client: TestClient, normal_user_token_headers: dict[str, str], db: Session) -> None:
serie = create_random_serie(db)
response = client.delete(
f"{settings.API_V1_STR}/series/{serie.id}",
headers=normal_user_token_headers,
)
assert response.status_code == status.HTTP_403_FORBIDDEN
assert response.json()["detail"] == "Not enough permissions"

View File

@@ -0,0 +1,12 @@
from sqlmodel import Session
from app.models.serie import Serie, SerieCreate
from app.tests.utils.utils import random_lower_string
def create_random_serie(db: Session, name: str = None) -> Serie:
if not name:
name = random_lower_string()
serie_in = SerieCreate(name=name)
return Serie.create(session=db, create_obj=serie_in)