mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-08-05 21:45:25 +02:00
105 lines
2.9 KiB
Python
105 lines
2.9 KiB
Python
|
from dataclasses import dataclass
|
||
|
|
||
|
import pytest
|
||
|
from fastapi.testclient import TestClient
|
||
|
|
||
|
from tests.utils.factories import random_string
|
||
|
from tests.utils.fixture_schemas import TestUser
|
||
|
|
||
|
|
||
|
class Routes:
|
||
|
base = "/api/tools"
|
||
|
recipes = "/api/recipes"
|
||
|
|
||
|
def item(item_id: int) -> str:
|
||
|
return f"{Routes.base}/{item_id}"
|
||
|
|
||
|
def recipe(recipe_id: int) -> str:
|
||
|
return f"{Routes.recipes}/{recipe_id}"
|
||
|
|
||
|
|
||
|
@dataclass
|
||
|
class TestRecipeTool:
|
||
|
id: int
|
||
|
name: str
|
||
|
|
||
|
|
||
|
@pytest.fixture(scope="function")
|
||
|
def tool(api_client: TestClient, unique_user: TestUser) -> TestRecipeTool:
|
||
|
data = {"name": random_string(10)}
|
||
|
|
||
|
response = api_client.post(Routes.base, json=data, headers=unique_user.token)
|
||
|
|
||
|
assert response.status_code == 201
|
||
|
|
||
|
yield TestRecipeTool(id=response.json()["id"], name=data["name"])
|
||
|
|
||
|
try:
|
||
|
response = api_client.delete(Routes.item(response.json()["id"]), headers=unique_user.token)
|
||
|
except Exception:
|
||
|
pass
|
||
|
|
||
|
|
||
|
def test_create_tool(api_client: TestClient, unique_user: TestUser):
|
||
|
data = {"name": random_string(10)}
|
||
|
|
||
|
response = api_client.post(Routes.base, json=data, headers=unique_user.token)
|
||
|
assert response.status_code == 201
|
||
|
|
||
|
|
||
|
def test_read_tool(api_client: TestClient, tool: TestRecipeTool, unique_user: TestUser):
|
||
|
response = api_client.get(Routes.item(tool.id), headers=unique_user.token)
|
||
|
assert response.status_code == 200
|
||
|
|
||
|
as_json = response.json()
|
||
|
|
||
|
assert as_json["id"] == tool.id
|
||
|
assert as_json["name"] == tool.name
|
||
|
|
||
|
|
||
|
def test_update_tool(api_client: TestClient, tool: TestRecipeTool, unique_user: TestUser):
|
||
|
update_data = {"id": tool.id, "name": random_string(10)}
|
||
|
|
||
|
response = api_client.put(Routes.item(tool.id), json=update_data, headers=unique_user.token)
|
||
|
assert response.status_code == 200
|
||
|
|
||
|
as_json = response.json()
|
||
|
|
||
|
assert as_json["id"] == tool.id
|
||
|
assert as_json["name"] == update_data["name"]
|
||
|
|
||
|
|
||
|
def test_delete_tool(api_client: TestClient, tool: TestRecipeTool, unique_user: TestUser):
|
||
|
response = api_client.delete(Routes.item(tool.id), headers=unique_user.token)
|
||
|
assert response.status_code == 200
|
||
|
|
||
|
|
||
|
def test_recipe_tool_association(api_client: TestClient, tool: TestRecipeTool, unique_user: TestUser):
|
||
|
# Setup Recipe
|
||
|
recipe_data = {"name": random_string(10)}
|
||
|
|
||
|
response = api_client.post(Routes.recipes, json=recipe_data, headers=unique_user.token)
|
||
|
|
||
|
slug = response.json()
|
||
|
|
||
|
assert response.status_code == 201
|
||
|
|
||
|
# Get Recipe Data
|
||
|
response = api_client.get(Routes.recipe(slug), headers=unique_user.token)
|
||
|
|
||
|
as_json = response.json()
|
||
|
|
||
|
as_json["tools"] = [{"id": tool.id, "name": tool.name}]
|
||
|
|
||
|
# Update Recipe
|
||
|
response = api_client.put(Routes.recipe(slug), json=as_json, headers=unique_user.token)
|
||
|
|
||
|
assert response.status_code == 200
|
||
|
|
||
|
# Get Recipe Data
|
||
|
response = api_client.get(Routes.recipe(slug), headers=unique_user.token)
|
||
|
|
||
|
as_json = response.json()
|
||
|
|
||
|
assert as_json["tools"][0]["id"] == tool.id
|