Skip to content

Commit

Permalink
add tests for test_recipe_create_from_text
Browse files Browse the repository at this point in the history
  • Loading branch information
domezi committed Dec 3, 2024
1 parent a9a233c commit 4cf74f9
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import json

import pytest
from fastapi.testclient import TestClient

from mealie.schema.openai.recipe import (
OpenAIRecipe,
OpenAIRecipeIngredient,
OpenAIRecipeInstruction,
OpenAIRecipeNotes,
)
from mealie.services.openai import OpenAIService
from tests.utils import api_routes
from tests.utils.factories import random_int, random_string
from tests.utils.fixture_schemas import TestUser

def test_openai_create_recipe_from_text(
api_client: TestClient,
monkeypatch: pytest.MonkeyPatch,
unique_user: TestUser,
):
# Mock OpenAI response
async def mock_get_response(self, prompt: str, message: str, *args, **kwargs) -> str | None:
data = OpenAIRecipe(
name=random_string(),
description=random_string(),
recipe_yield=random_string(),
total_time=random_string(),
prep_time=random_string(),
perform_time=random_string(),
ingredients=[OpenAIRecipeIngredient(text=random_string()) for _ in range(random_int(5, 10))],
instructions=[OpenAIRecipeInstruction(text=random_string()) for _ in range(1, random_int(5, 10))],
notes=[OpenAIRecipeNotes(text=random_string()) for _ in range(random_int(2, 5))],
)
return data.model_dump_json()

monkeypatch.setattr(OpenAIService, "get_response", mock_get_response)

# Test basic recipe creation from text
recipe_text = "Test recipe with some ingredients and instructions"
r = api_client.post(
api_routes.recipes_create_from_text,
data={"text": recipe_text},
headers=unique_user.token,
)
assert r.status_code == 201

# Verify recipe was created
slug: str = json.loads(r.text)
r = api_client.get(api_routes.recipes_slug(slug), headers=unique_user.token)
assert r.status_code == 200

def test_openai_create_recipe_from_text_with_translation(
api_client: TestClient,
monkeypatch: pytest.MonkeyPatch,
unique_user: TestUser,
):
# Mock OpenAI response with translated content
async def mock_get_response(self, prompt: str, message: str, *args, **kwargs) -> str | None:
# Check if translation is requested in the message
is_spanish = "translate the recipe to Spanish" in message

data = OpenAIRecipe(
name="Paella" if is_spanish else "Seafood Rice",
description="Delicioso arroz con mariscos" if is_spanish else "Delicious rice with seafood",
recipe_yield="4 porciones" if is_spanish else "4 servings",
total_time="1 hora" if is_spanish else "1 hour",
prep_time="20 minutos" if is_spanish else "20 minutes",
perform_time="40 minutos" if is_spanish else "40 minutes",
ingredients=[
OpenAIRecipeIngredient(text="Arroz" if is_spanish else "Rice"),
OpenAIRecipeIngredient(text="Mariscos" if is_spanish else "Seafood"),
],
instructions=[
OpenAIRecipeInstruction(text="Cocinar el arroz" if is_spanish else "Cook the rice"),
OpenAIRecipeInstruction(text="Añadir mariscos" if is_spanish else "Add seafood"),
],
notes=[OpenAIRecipeNotes(text="Servir caliente" if is_spanish else "Serve hot")],
)
return data.model_dump_json()

monkeypatch.setattr(OpenAIService, "get_response", mock_get_response)

# Test recipe creation with translation
recipe_text = "Test recipe for translation"
r = api_client.post(
f"{api_routes.recipes_create_from_text}?translateLanguage=Spanish",
data={"text": recipe_text},
headers=unique_user.token,
)
assert r.status_code == 201

# Verify recipe was created with translated content
slug: str = json.loads(r.text)
r = api_client.get(api_routes.recipes_slug(slug), headers=unique_user.token)
assert r.status_code == 200
recipe_data = r.json()
assert "Paella" in recipe_data["name"]
assert "porciones" in recipe_data["recipeYield"]
2 changes: 2 additions & 0 deletions tests/utils/api_routes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@
"""`/api/recipes/create/html-or-json`"""
recipes_create_image = "/api/recipes/create/image"
"""`/api/recipes/create/image`"""
recipes_create_from_text = "/api/recipes/create/text"
"""`/api/recipes/create/text`"""
recipes_create_url = "/api/recipes/create/url"
"""`/api/recipes/create/url`"""
recipes_create_url_bulk = "/api/recipes/create/url/bulk"
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


0 comments on commit 4cf74f9

Please sign in to comment.