1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-08-05 21:45:25 +02:00

chore: file generation cleanup (#1736)

This PR does too many things :( 

1. Major refactoring of the dev/scripts and dev/code-generation folders. 

Primarily this was removing duplicate code and cleaning up some poorly written code snippets as well as making them more idempotent so then can be re-run over and over again but still maintain the same results. This is working on my machine, but I've been having problems in CI and comparing diffs so running generators in CI will have to wait. 

2. Re-Implement using the generated api routes for testing

This was a _huge_ refactor that touched damn near every test file but now we have auto-generated typed routes with inline hints and it's used for nearly every test excluding a few that use classes for better parameterization. This should greatly reduce errors when writing new tests. 

3. Minor Perf improvements for the All Recipes endpoint

  A. Removed redundant loops
  B. Uses orjson to do the encoding directly and returns a byte response instead of relying on the default 
       jsonable_encoder.

4. Fix some TS type errors that cropped up for seemingly no reason half way through the PR.

See this issue https://github.com/phillipdupuis/pydantic-to-typescript/issues/28

Basically, the generated TS type is not-correct since Pydantic will automatically fill in null fields. The resulting TS type is generated with a ? to indicate it can be null even though we _know_ that i can't be.
This commit is contained in:
Hayden 2022-10-18 14:49:41 -08:00 committed by GitHub
parent a8f0fb14a7
commit 9ecef4c25f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
107 changed files with 2520 additions and 1948 deletions

View file

@ -1,4 +1,5 @@
from pathlib import Path
from typing import Generator
import pytest
import sqlalchemy
@ -9,30 +10,21 @@ from mealie.repos.repository_factory import AllRepositories
from mealie.schema.recipe.recipe_bulk_actions import ExportTypes
from mealie.schema.recipe.recipe_category import CategorySave, TagSave
from tests import utils
from tests.utils import api_routes
from tests.utils.factories import random_string
from tests.utils.fixture_schemas import TestUser
class Routes:
create_recipes = "/api/recipes"
bulk_tag = "api/recipes/bulk-actions/tag"
bulk_categorize = "api/recipes/bulk-actions/categorize"
bulk_delete = "api/recipes/bulk-actions/delete"
bulk_export = "api/recipes/bulk-actions/export"
bulk_export_download = f"{bulk_export}/download"
bulk_export_purge = f"{bulk_export}/purge"
@pytest.fixture(scope="function")
def ten_slugs(api_client: TestClient, unique_user: TestUser, database: AllRepositories) -> list[str]:
def ten_slugs(
api_client: TestClient, unique_user: TestUser, database: AllRepositories
) -> Generator[list[str], None, None]:
slugs = []
slugs: list[str] = []
for _ in range(10):
payload = {"name": random_string(length=20)}
response = api_client.post(Routes.create_recipes, json=payload, headers=unique_user.token)
response = api_client.post(api_routes.recipes, json=payload, headers=unique_user.token)
assert response.status_code == 201
response_data = response.json()
@ -59,14 +51,16 @@ def test_bulk_tag_recipes(
payload = {"recipes": ten_slugs, "tags": tags}
response = api_client.post(Routes.bulk_tag, json=utils.jsonify(payload), headers=unique_user.token)
response = api_client.post(
api_routes.recipes_bulk_actions_tag, json=utils.jsonify(payload), headers=unique_user.token
)
assert response.status_code == 200
# Validate Recipes are Tagged
for slug in ten_slugs:
recipe = database.recipes.get_one(slug)
for tag in recipe.tags:
for tag in recipe.tags: # type: ignore
assert tag.slug in [x["slug"] for x in tags]
@ -85,14 +79,16 @@ def test_bulk_categorize_recipes(
payload = {"recipes": ten_slugs, "categories": categories}
response = api_client.post(Routes.bulk_categorize, json=utils.jsonify(payload), headers=unique_user.token)
response = api_client.post(
api_routes.recipes_bulk_actions_categorize, json=utils.jsonify(payload), headers=unique_user.token
)
assert response.status_code == 200
# Validate Recipes are Categorized
for slug in ten_slugs:
recipe = database.recipes.get_one(slug)
for cat in recipe.recipe_category:
for cat in recipe.recipe_category: # type: ignore
assert cat.slug in [x["slug"] for x in categories]
@ -105,7 +101,7 @@ def test_bulk_delete_recipes(
payload = {"recipes": ten_slugs}
response = api_client.post(Routes.bulk_delete, json=payload, headers=unique_user.token)
response = api_client.post(api_routes.recipes_bulk_actions_delete, json=payload, headers=unique_user.token)
assert response.status_code == 200
# Validate Recipes are Tagged
@ -120,11 +116,11 @@ def test_bulk_export_recipes(api_client: TestClient, unique_user: TestUser, ten_
"export_type": ExportTypes.JSON.value,
}
response = api_client.post(Routes.bulk_export, json=payload, headers=unique_user.token)
response = api_client.post(api_routes.recipes_bulk_actions_export, json=payload, headers=unique_user.token)
assert response.status_code == 202
# Get All Exports Available
response = api_client.get(Routes.bulk_export, headers=unique_user.token)
response = api_client.get(api_routes.recipes_bulk_actions_export, headers=unique_user.token)
assert response.status_code == 200
response_data = response.json()
@ -133,7 +129,9 @@ def test_bulk_export_recipes(api_client: TestClient, unique_user: TestUser, ten_
export_path = response_data[0]["path"]
# Get Export Token
response = api_client.get(f"{Routes.bulk_export_download}?path={export_path}", headers=unique_user.token)
response = api_client.get(
f"{api_routes.recipes_bulk_actions_export_download}?path={export_path}", headers=unique_user.token
)
assert response.status_code == 200
response_data = response.json()
@ -150,11 +148,11 @@ def test_bulk_export_recipes(api_client: TestClient, unique_user: TestUser, ten_
assert len(response.content) > 0
# Purge Export
response = api_client.delete(Routes.bulk_export_purge, headers=unique_user.token)
response = api_client.delete(api_routes.recipes_bulk_actions_export_purge, headers=unique_user.token)
assert response.status_code == 200
# Validate Export was purged
response = api_client.get(Routes.bulk_export, headers=unique_user.token)
response = api_client.get(api_routes.recipes_bulk_actions_export, headers=unique_user.token)
assert response.status_code == 200
response_data = response.json()

View file

@ -1,17 +1,10 @@
import pytest
from fastapi.testclient import TestClient
from tests.utils import api_routes
from tests.utils.fixture_schemas import TestUser
class Routes:
base = "/api/recipes"
bulk = "/api/recipes/create-url/bulk"
def item(item_id: str) -> str:
return f"{Routes.base}/{item_id}"
@pytest.mark.skip("Long Running Scraper")
def test_bulk_import(api_client: TestClient, unique_user: TestUser):
recipes = {
@ -26,10 +19,10 @@ def test_bulk_import(api_client: TestClient, unique_user: TestUser):
"best-chocolate-chip-cookies",
]
response = api_client.post(Routes.bulk, json=recipes, headers=unique_user.token)
response = api_client.post(api_routes.recipes_create_url_bulk, json=recipes, headers=unique_user.token)
assert response.status_code == 201
for slug in slugs:
response = api_client.get(Routes.item(slug), headers=unique_user.token)
response = api_client.get(api_routes.recipes_slug(slug), headers=unique_user.token)
assert response.status_code == 200

View file

@ -3,32 +3,19 @@ from fastapi.testclient import TestClient
from pydantic import UUID4
from mealie.schema.recipe.recipe import Recipe
from tests.utils import api_routes
from tests.utils.factories import random_string
from tests.utils.fixture_schemas import TestUser
class Routes:
base = "/api/comments"
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}"
def recipe_comments(recipe_slug: str) -> str:
return f"{Routes.recipe(recipe_slug)}/comments"
@pytest.fixture(scope="function")
def unique_recipe(api_client: TestClient, unique_user: TestUser):
payload = {"name": random_string(length=20)}
response = api_client.post(Routes.recipes, json=payload, headers=unique_user.token)
response = api_client.post(api_routes.recipes, json=payload, headers=unique_user.token)
assert response.status_code == 201
response_data = response.json()
recipe_response = api_client.get(Routes.recipe(response_data), headers=unique_user.token)
recipe_response = api_client.get(api_routes.recipes_slug(response_data), headers=unique_user.token)
return Recipe(**recipe_response.json())
@ -45,7 +32,7 @@ def random_comment(recipe_id: UUID4) -> dict:
def test_create_comment(api_client: TestClient, unique_recipe: Recipe, unique_user: TestUser):
# Create Comment
create_data = random_comment(unique_recipe.id)
response = api_client.post(Routes.base, json=create_data, headers=unique_user.token)
response = api_client.post(api_routes.comments, json=create_data, headers=unique_user.token)
assert response.status_code == 201
response_data = response.json()
@ -55,7 +42,7 @@ def test_create_comment(api_client: TestClient, unique_recipe: Recipe, unique_us
assert response_data["userId"] == unique_user.user_id
# Check for Proper Association
response = api_client.get(Routes.recipe_comments(unique_recipe.slug), headers=unique_user.token)
response = api_client.get(api_routes.recipes_slug_comments(unique_recipe.slug), headers=unique_user.token)
assert response.status_code == 200
response_data = response.json()
@ -69,7 +56,7 @@ def test_create_comment(api_client: TestClient, unique_recipe: Recipe, unique_us
def test_update_comment(api_client: TestClient, unique_recipe: Recipe, unique_user: TestUser):
# Create Comment
create_data = random_comment(unique_recipe.id)
response = api_client.post(Routes.base, json=create_data, headers=unique_user.token)
response = api_client.post(api_routes.comments, json=create_data, headers=unique_user.token)
assert response.status_code == 201
comment_id = response.json()["id"]
@ -78,7 +65,7 @@ def test_update_comment(api_client: TestClient, unique_recipe: Recipe, unique_us
update_data = random_comment(unique_recipe.id)
update_data["id"] = comment_id
response = api_client.put(Routes.item(comment_id), json=update_data, headers=unique_user.token)
response = api_client.put(api_routes.comments_item_id(comment_id), json=update_data, headers=unique_user.token)
assert response.status_code == 200
@ -92,16 +79,16 @@ def test_update_comment(api_client: TestClient, unique_recipe: Recipe, unique_us
def test_delete_comment(api_client: TestClient, unique_recipe: Recipe, unique_user: TestUser):
# Create Comment
create_data = random_comment(unique_recipe.id)
response = api_client.post(Routes.base, json=create_data, headers=unique_user.token)
response = api_client.post(api_routes.comments, json=create_data, headers=unique_user.token)
assert response.status_code == 201
# Delete Comment
comment_id = response.json()["id"]
response = api_client.delete(Routes.item(comment_id), headers=unique_user.token)
response = api_client.delete(api_routes.comments_item_id(comment_id), headers=unique_user.token)
assert response.status_code == 200
# Validate Deletion
response = api_client.get(Routes.item(comment_id), headers=unique_user.token)
response = api_client.get(api_routes.comments_item_id(comment_id), headers=unique_user.token)
assert response.status_code == 404
@ -109,15 +96,15 @@ def test_delete_comment(api_client: TestClient, unique_recipe: Recipe, unique_us
def test_admin_can_delete(api_client: TestClient, unique_recipe: Recipe, unique_user: TestUser, admin_user: TestUser):
# Create Comment
create_data = random_comment(unique_recipe.id)
response = api_client.post(Routes.base, json=create_data, headers=unique_user.token)
response = api_client.post(api_routes.comments, json=create_data, headers=unique_user.token)
assert response.status_code == 201
# Delete Comment
comment_id = response.json()["id"]
response = api_client.delete(Routes.item(comment_id), headers=admin_user.token)
response = api_client.delete(api_routes.comments_item_id(comment_id), headers=admin_user.token)
assert response.status_code == 200
# Validate Deletion
response = api_client.get(Routes.item(comment_id), headers=admin_user.token)
response = api_client.get(api_routes.comments_item_id(comment_id), headers=admin_user.token)
assert response.status_code == 404

View file

@ -14,8 +14,7 @@ from mealie.schema.recipe.recipe import RecipeCategory
from mealie.services.recipe.recipe_data_service import RecipeDataService
from mealie.services.scraper.scraper_strategies import RecipeScraperOpenGraph
from tests import data, utils
from tests.utils import routes
from tests.utils.app_routes import AppRoutes
from tests.utils import api_routes
from tests.utils.factories import random_string
from tests.utils.fixture_schemas import TestUser
from tests.utils.recipe_data import RecipeSiteTestCase, get_recipe_test_cases
@ -58,7 +57,6 @@ def open_graph_override(html: str):
@pytest.mark.parametrize("recipe_data", recipe_test_data)
def test_create_by_url(
api_client: TestClient,
api_routes: AppRoutes,
recipe_data: RecipeSiteTestCase,
unique_user: TestUser,
monkeypatch: MonkeyPatch,
@ -82,7 +80,7 @@ def test_create_by_url(
lambda *_: "TEST_IMAGE",
)
api_client.delete(api_routes.recipes_recipe_slug(recipe_data.expected_slug), headers=unique_user.token)
api_client.delete(api_routes.recipes_slug(recipe_data.expected_slug), headers=unique_user.token)
response = api_client.post(
api_routes.recipes_create_url, json={"url": recipe_data.url, "include_tags": False}, headers=unique_user.token
@ -94,7 +92,6 @@ def test_create_by_url(
def test_create_by_url_with_tags(
api_client: TestClient,
api_routes: AppRoutes,
unique_user: TestUser,
monkeypatch: MonkeyPatch,
):
@ -128,7 +125,7 @@ def test_create_by_url_with_tags(
slug = "nutty-umami-noodles-with-scallion-brown-butter-and-snow-peas"
# Get the recipe
response = api_client.get(api_routes.recipes_recipe_slug(slug), headers=unique_user.token)
response = api_client.get(api_routes.recipes_slug(slug), headers=unique_user.token)
assert response.status_code == 200
# Verifiy the tags are present
@ -162,7 +159,7 @@ def test_read_update(
unique_user: TestUser,
recipe_categories: list[RecipeCategory],
):
recipe_url = routes.recipes.Recipe.item(recipe_data.expected_slug)
recipe_url = api_routes.recipes_slug(recipe_data.expected_slug)
response = api_client.get(recipe_url, headers=unique_user.token)
assert response.status_code == 200
@ -197,7 +194,7 @@ def test_read_update(
@pytest.mark.parametrize("recipe_data", recipe_test_data)
def test_rename(api_client: TestClient, recipe_data: RecipeSiteTestCase, unique_user: TestUser):
recipe_url = routes.recipes.Recipe.item(recipe_data.expected_slug)
recipe_url = api_routes.recipes_slug(recipe_data.expected_slug)
response = api_client.get(recipe_url, headers=unique_user.token)
assert response.status_code == 200
@ -216,18 +213,18 @@ def test_rename(api_client: TestClient, recipe_data: RecipeSiteTestCase, unique_
@pytest.mark.parametrize("recipe_data", recipe_test_data)
def test_delete(api_client: TestClient, recipe_data: RecipeSiteTestCase, unique_user: TestUser):
response = api_client.delete(routes.recipes.Recipe.item(recipe_data.expected_slug), headers=unique_user.token)
response = api_client.delete(api_routes.recipes_slug(recipe_data.expected_slug), headers=unique_user.token)
assert response.status_code == 200
def test_recipe_crud_404(api_client: TestClient, api_routes: AppRoutes, unique_user: TestUser):
response = api_client.put(routes.recipes.Recipe.item("test"), json={"test": "stest"}, headers=unique_user.token)
def test_recipe_crud_404(api_client: TestClient, unique_user: TestUser):
response = api_client.put(api_routes.recipes_slug("test"), json={"test": "stest"}, headers=unique_user.token)
assert response.status_code == 404
response = api_client.get(routes.recipes.Recipe.item("test"), headers=unique_user.token)
response = api_client.get(api_routes.recipes_slug("test"), headers=unique_user.token)
assert response.status_code == 404
response = api_client.delete(routes.recipes.Recipe.item("test"), headers=unique_user.token)
response = api_client.delete(api_routes.recipes_slug("test"), headers=unique_user.token)
assert response.status_code == 404
response = api_client.patch(api_routes.recipes_create_url, json={"test": "stest"}, headers=unique_user.token)
@ -237,11 +234,11 @@ def test_recipe_crud_404(api_client: TestClient, api_routes: AppRoutes, unique_u
def test_create_recipe_same_name(api_client: TestClient, unique_user: TestUser):
slug = random_string(10)
response = api_client.post(routes.recipes.Recipe.base, json={"name": slug}, headers=unique_user.token)
response = api_client.post(api_routes.recipes, json={"name": slug}, headers=unique_user.token)
assert response.status_code == 201
assert json.loads(response.text) == slug
response = api_client.post(routes.recipes.Recipe.base, json={"name": slug}, headers=unique_user.token)
response = api_client.post(api_routes.recipes, json={"name": slug}, headers=unique_user.token)
assert response.status_code == 201
assert json.loads(response.text) == f"{slug}-1"
@ -250,10 +247,10 @@ def test_create_recipe_too_many_time(api_client: TestClient, unique_user: TestUs
slug = random_string(10)
for _ in range(10):
response = api_client.post(routes.recipes.Recipe.base, json={"name": slug}, headers=unique_user.token)
response = api_client.post(api_routes.recipes, json={"name": slug}, headers=unique_user.token)
assert response.status_code == 201
response = api_client.post(routes.recipes.Recipe.base, json={"name": slug}, headers=unique_user.token)
response = api_client.post(api_routes.recipes, json={"name": slug}, headers=unique_user.token)
assert response.status_code == 400
@ -262,19 +259,19 @@ def test_delete_recipe_same_name(api_client: TestClient, unique_user: utils.Test
# Create recipe for both users
for user in (unique_user, g2_user):
response = api_client.post(routes.recipes.Recipe.base, json={"name": slug}, headers=user.token)
response = api_client.post(api_routes.recipes, json={"name": slug}, headers=user.token)
assert response.status_code == 201
assert json.loads(response.text) == slug
# Delete recipe for user 1
response = api_client.delete(routes.recipes.Recipe.item(slug), headers=unique_user.token)
response = api_client.delete(api_routes.recipes_slug(slug), headers=unique_user.token)
assert response.status_code == 200
# Ensure recipe for user 2 still exists
response = api_client.get(routes.recipes.Recipe.item(slug), headers=g2_user.token)
response = api_client.get(api_routes.recipes_slug(slug), headers=g2_user.token)
assert response.status_code == 200
# Make sure recipe for user 1 doesn't exist
response = api_client.get(routes.recipes.Recipe.item(slug), headers=unique_user.token)
response = api_client.get(routes.recipes.Recipe.item(slug), headers=unique_user.token)
response = api_client.get(api_routes.recipes_slug(slug), headers=unique_user.token)
response = api_client.get(api_routes.recipes_slug(slug), headers=unique_user.token)
assert response.status_code == 404

View file

@ -1,21 +1,13 @@
from fastapi.testclient import TestClient
from tests.utils import api_routes
from tests.utils.factories import random_string
from tests.utils.fixture_schemas import TestUser
class Routes:
base = "/api/recipes"
exports = base + "/exports"
@staticmethod
def item(slug: str, file_name: str) -> str:
return f"/api/recipes/{slug}/exports?template_name={file_name}"
def test_get_available_exports(api_client: TestClient, unique_user: TestUser) -> None:
# Get Templates
response = api_client.get(Routes.exports, headers=unique_user.token)
response = api_client.get(api_routes.recipes_exports, headers=unique_user.token)
# Assert Templates are Available
assert response.status_code == 200
@ -29,12 +21,14 @@ def test_get_available_exports(api_client: TestClient, unique_user: TestUser) ->
def test_render_jinja_template(api_client: TestClient, unique_user: TestUser) -> None:
# Create Recipe
recipe_name = random_string()
response = api_client.post(Routes.base, json={"name": recipe_name}, headers=unique_user.token)
response = api_client.post(api_routes.recipes, json={"name": recipe_name}, headers=unique_user.token)
assert response.status_code == 201
slug = response.json()
# Render Template
response = api_client.get(Routes.item(slug, "recipes.md"), headers=unique_user.token)
response = api_client.get(
api_routes.recipes_slug_exports(slug) + "?template_name=recipes.md", headers=unique_user.token
)
assert response.status_code == 200
# Assert Template is Rendered Correctly

View file

@ -1,30 +1,24 @@
from typing import Generator
import pytest
import sqlalchemy
from fastapi.testclient import TestClient
from mealie.repos.repository_factory import AllRepositories
from tests.utils import api_routes
from tests.utils.factories import random_string
from tests.utils.fixture_schemas import TestUser
class Routes:
create_recipes = "/api/recipes"
def base(item_id: int) -> str:
return f"api/users/{item_id}/favorites"
def toggle(item_id: int, slug: str) -> str:
return f"{Routes.base(item_id)}/{slug}"
@pytest.fixture(scope="function")
def ten_slugs(api_client: TestClient, unique_user: TestUser, database: AllRepositories) -> list[str]:
def ten_slugs(
api_client: TestClient, unique_user: TestUser, database: AllRepositories
) -> Generator[list[str], None, None]:
slugs = []
for _ in range(10):
payload = {"name": random_string(length=20)}
response = api_client.post(Routes.create_recipes, json=payload, headers=unique_user.token)
response = api_client.post(api_routes.recipes, json=payload, headers=unique_user.token)
assert response.status_code == 201
response_data = response.json()
@ -41,26 +35,30 @@ def ten_slugs(api_client: TestClient, unique_user: TestUser, database: AllReposi
def test_recipe_favorites(api_client: TestClient, unique_user: TestUser, ten_slugs: list[str]):
# Check that the user has no favorites
response = api_client.get(Routes.base(unique_user.user_id), headers=unique_user.token)
response = api_client.get(api_routes.users_id_favorites(unique_user.user_id), headers=unique_user.token)
assert response.status_code == 200
assert response.json()["favoriteRecipes"] == []
# Add a few recipes to the user's favorites
for slug in ten_slugs:
response = api_client.post(Routes.toggle(unique_user.user_id, slug), headers=unique_user.token)
response = api_client.post(
api_routes.users_id_favorites_slug(unique_user.user_id, slug), headers=unique_user.token
)
assert response.status_code == 200
# Check that the user has the recipes in their favorites
response = api_client.get(Routes.base(unique_user.user_id), headers=unique_user.token)
response = api_client.get(api_routes.users_id_favorites(unique_user.user_id), headers=unique_user.token)
assert response.status_code == 200
assert len(response.json()["favoriteRecipes"]) == 10
# Remove a few recipes from the user's favorites
for slug in ten_slugs[:5]:
response = api_client.delete(Routes.toggle(unique_user.user_id, slug), headers=unique_user.token)
response = api_client.delete(
api_routes.users_id_favorites_slug(unique_user.user_id, slug), headers=unique_user.token
)
assert response.status_code == 200
# Check that the user has the recipes in their favorites
response = api_client.get(Routes.base(unique_user.user_id), headers=unique_user.token)
response = api_client.get(api_routes.users_id_favorites(unique_user.user_id), headers=unique_user.token)
assert response.status_code == 200
assert len(response.json()["favoriteRecipes"]) == 5

View file

@ -5,18 +5,11 @@ from fastapi.testclient import TestClient
from mealie.schema.recipe.recipe_ingredient import CreateIngredientFood
from tests import utils
from tests.utils import api_routes
from tests.utils.factories import random_string
from tests.utils.fixture_schemas import TestUser
class Routes:
base = "/api/foods"
@staticmethod
def item(item_id: int) -> str:
return f"{Routes.base}/{item_id}"
@pytest.fixture(scope="function")
def food(api_client: TestClient, unique_user: TestUser) -> Generator[dict, None, None]:
data = CreateIngredientFood(
@ -24,13 +17,13 @@ def food(api_client: TestClient, unique_user: TestUser) -> Generator[dict, None,
description=random_string(10),
).dict(by_alias=True)
response = api_client.post(Routes.base, json=data, headers=unique_user.token)
response = api_client.post(api_routes.foods, json=data, headers=unique_user.token)
assert response.status_code == 201
yield response.json()
response = api_client.delete(Routes.item(response.json()["id"]), headers=unique_user.token)
response = api_client.delete(api_routes.foods_item_id(response.json()["id"]), headers=unique_user.token)
def test_create_food(api_client: TestClient, unique_user: TestUser):
@ -39,12 +32,12 @@ def test_create_food(api_client: TestClient, unique_user: TestUser):
description=random_string(10),
).dict(by_alias=True)
response = api_client.post(Routes.base, json=data, headers=unique_user.token)
response = api_client.post(api_routes.foods, json=data, headers=unique_user.token)
assert response.status_code == 201
def test_read_food(api_client: TestClient, food: dict, unique_user: TestUser):
response = api_client.get(Routes.item(food["id"]), headers=unique_user.token)
response = api_client.get(api_routes.foods_item_id(food["id"]), headers=unique_user.token)
assert response.status_code == 200
as_json = response.json()
@ -60,7 +53,7 @@ def test_update_food(api_client: TestClient, food: dict, unique_user: TestUser):
"name": random_string(10),
"description": random_string(10),
}
response = api_client.put(Routes.item(food["id"]), json=update_data, headers=unique_user.token)
response = api_client.put(api_routes.foods_item_id(food["id"]), json=update_data, headers=unique_user.token)
assert response.status_code == 200
as_json = response.json()
@ -72,11 +65,11 @@ def test_update_food(api_client: TestClient, food: dict, unique_user: TestUser):
def test_delete_food(api_client: TestClient, food: dict, unique_user: TestUser):
id = food["id"]
response = api_client.delete(Routes.item(id), headers=unique_user.token)
response = api_client.delete(api_routes.foods_item_id(id), headers=unique_user.token)
assert response.status_code == 200
response = api_client.get(Routes.item(id), headers=unique_user.token)
response = api_client.get(api_routes.foods_item_id(id), headers=unique_user.token)
assert response.status_code == 404
@ -94,7 +87,7 @@ def test_food_extras(
new_food_data: dict = {"name": random_string()}
new_food_data["extras"] = {key_str_1: val_str_1}
response = api_client.post(Routes.base, json=new_food_data, headers=unique_user.token)
response = api_client.post(api_routes.foods, json=new_food_data, headers=unique_user.token)
food_as_json = utils.assert_derserialize(response, 201)
# make sure the extra persists
@ -105,7 +98,9 @@ def test_food_extras(
# add more extras to the food
food_as_json["extras"][key_str_2] = val_str_2
response = api_client.put(Routes.item(food_as_json["id"]), json=food_as_json, headers=unique_user.token)
response = api_client.put(
api_routes.foods_item_id(food_as_json["id"]), json=food_as_json, headers=unique_user.token
)
food_as_json = utils.assert_derserialize(response, 200)
# make sure both the new extra and original extra persist

View file

@ -3,14 +3,10 @@ from fastapi.testclient import TestClient
from mealie.schema.recipe.recipe_ingredient import RegisteredParser
from tests.unit_tests.test_ingredient_parser import TestIngredient, crf_exists, test_ingredients
from tests.utils import api_routes
from tests.utils.fixture_schemas import TestUser
class Routes:
ingredient = "/api/parser/ingredient"
ingredients = "/api/parser/ingredients"
def assert_ingredient(api_response: dict, test_ingredient: TestIngredient):
assert api_response["ingredient"]["quantity"] == pytest.approx(test_ingredient.quantity)
assert api_response["ingredient"]["unit"]["name"] == test_ingredient.unit
@ -22,7 +18,7 @@ def assert_ingredient(api_response: dict, test_ingredient: TestIngredient):
@pytest.mark.parametrize("test_ingredient", test_ingredients)
def test_recipe_ingredient_parser_nlp(api_client: TestClient, test_ingredient: TestIngredient, unique_user: TestUser):
payload = {"parser": RegisteredParser.nlp, "ingredient": test_ingredient.input}
response = api_client.post(Routes.ingredient, json=payload, headers=unique_user.token)
response = api_client.post(api_routes.parser_ingredient, json=payload, headers=unique_user.token)
assert response.status_code == 200
assert_ingredient(response.json(), test_ingredient)
@ -30,7 +26,7 @@ def test_recipe_ingredient_parser_nlp(api_client: TestClient, test_ingredient: T
@pytest.mark.skipif(not crf_exists(), reason="CRF++ not installed")
def test_recipe_ingredients_parser_nlp(api_client: TestClient, unique_user: TestUser):
payload = {"parser": RegisteredParser.nlp, "ingredients": [x.input for x in test_ingredients]}
response = api_client.post(Routes.ingredients, json=payload, headers=unique_user.token)
response = api_client.post(api_routes.parser_ingredients, json=payload, headers=unique_user.token)
assert response.status_code == 200
for api_ingredient, test_ingredient in zip(response.json(), test_ingredients):

View file

@ -1,20 +1,16 @@
from fastapi.testclient import TestClient
from tests.utils import api_routes
from tests.utils.factories import random_string
from tests.utils.fixture_schemas import TestUser
class Routes:
base = "/api/recipes"
user = "/api/users/self"
def test_ownership_on_new_with_admin(api_client: TestClient, admin_user: TestUser):
recipe_name = random_string()
response = api_client.post(Routes.base, json={"name": recipe_name}, headers=admin_user.token)
response = api_client.post(api_routes.recipes, json={"name": recipe_name}, headers=admin_user.token)
assert response.status_code == 201
recipe = api_client.get(Routes.base + f"/{recipe_name}", headers=admin_user.token).json()
recipe = api_client.get(api_routes.recipes + f"/{recipe_name}", headers=admin_user.token).json()
assert recipe["userId"] == admin_user.user_id
assert recipe["groupId"] == admin_user.group_id
@ -22,10 +18,10 @@ def test_ownership_on_new_with_admin(api_client: TestClient, admin_user: TestUse
def test_ownership_on_new_with_user(api_client: TestClient, g2_user: TestUser):
recipe_name = random_string()
response = api_client.post(Routes.base, json={"name": recipe_name}, headers=g2_user.token)
response = api_client.post(api_routes.recipes, json={"name": recipe_name}, headers=g2_user.token)
assert response.status_code == 201
response = api_client.get(Routes.base + f"/{recipe_name}", headers=g2_user.token)
response = api_client.get(api_routes.recipes + f"/{recipe_name}", headers=g2_user.token)
assert response.status_code == 200
@ -38,9 +34,9 @@ def test_ownership_on_new_with_user(api_client: TestClient, g2_user: TestUser):
def test_get_all_only_includes_group_recipes(api_client: TestClient, unique_user: TestUser):
for _ in range(5):
recipe_name = random_string()
response = api_client.post(Routes.base, json={"name": recipe_name}, headers=unique_user.token)
response = api_client.post(api_routes.recipes, json={"name": recipe_name}, headers=unique_user.token)
response = api_client.get(Routes.base, headers=unique_user.token)
response = api_client.get(api_routes.recipes, headers=unique_user.token)
assert response.status_code == 200
@ -56,14 +52,14 @@ def test_get_all_only_includes_group_recipes(api_client: TestClient, unique_user
def test_unique_slug_by_group(api_client: TestClient, unique_user: TestUser, g2_user: TestUser) -> None:
create_data = {"name": random_string()}
response = api_client.post(Routes.base, json=create_data, headers=unique_user.token)
response = api_client.post(api_routes.recipes, json=create_data, headers=unique_user.token)
assert response.status_code == 201
response = api_client.post(Routes.base, json=create_data, headers=g2_user.token)
response = api_client.post(api_routes.recipes, json=create_data, headers=g2_user.token)
assert response.status_code == 201
# Try to create a recipe again with the same name and check that the name was incremented
response = api_client.post(Routes.base, json=create_data, headers=g2_user.token)
response = api_client.post(api_routes.recipes, json=create_data, headers=g2_user.token)
assert response.status_code == 201
assert response.json() == create_data["name"] + "-1"
@ -73,20 +69,20 @@ def test_user_locked_recipe(api_client: TestClient, user_tuple: list[TestUser])
# Setup Recipe
recipe_name = random_string()
response = api_client.post(Routes.base, json={"name": recipe_name}, headers=usr_1.token)
response = api_client.post(api_routes.recipes, json={"name": recipe_name}, headers=usr_1.token)
assert response.status_code == 201
# Get Recipe
response = api_client.get(Routes.base + f"/{recipe_name}", headers=usr_1.token)
response = api_client.get(api_routes.recipes + f"/{recipe_name}", headers=usr_1.token)
assert response.status_code == 200
recipe = response.json()
# Lock Recipe
recipe["settings"]["locked"] = True
response = api_client.put(Routes.base + f"/{recipe_name}", json=recipe, headers=usr_1.token)
response = api_client.put(api_routes.recipes + f"/{recipe_name}", json=recipe, headers=usr_1.token)
# Try To Update Recipe with User 2
response = api_client.put(Routes.base + f"/{recipe_name}", json=recipe, headers=usr_2.token)
response = api_client.put(api_routes.recipes + f"/{recipe_name}", json=recipe, headers=usr_2.token)
assert response.status_code == 403
@ -95,15 +91,15 @@ def test_other_user_cant_lock_recipe(api_client: TestClient, user_tuple: list[Te
# Setup Recipe
recipe_name = random_string()
response = api_client.post(Routes.base, json={"name": recipe_name}, headers=usr_1.token)
response = api_client.post(api_routes.recipes, json={"name": recipe_name}, headers=usr_1.token)
assert response.status_code == 201
# Get Recipe
response = api_client.get(Routes.base + f"/{recipe_name}", headers=usr_2.token)
response = api_client.get(api_routes.recipes + f"/{recipe_name}", headers=usr_2.token)
assert response.status_code == 200
recipe = response.json()
# Lock Recipe
recipe["settings"]["locked"] = True
response = api_client.put(Routes.base + f"/{recipe_name}", json=recipe, headers=usr_2.token)
response = api_client.put(api_routes.recipes + f"/{recipe_name}", json=recipe, headers=usr_2.token)
assert response.status_code == 403

View file

@ -1,27 +1,21 @@
from typing import Generator
import pytest
import sqlalchemy
from fastapi.testclient import TestClient
from mealie.repos.repository_factory import AllRepositories
from mealie.schema.recipe.recipe_share_token import RecipeShareTokenSave
from tests.utils import api_routes
from tests.utils.factories import random_string
from tests.utils.fixture_schemas import TestUser
class Routes:
base = "/api/shared/recipes"
create_recipes = "/api/recipes"
@staticmethod
def item(item_id: str):
return f"{Routes.base}/{item_id}"
@pytest.fixture(scope="function")
def slug(api_client: TestClient, unique_user: TestUser, database: AllRepositories) -> str:
def slug(api_client: TestClient, unique_user: TestUser, database: AllRepositories) -> Generator[str, None, None]:
payload = {"name": random_string(length=20)}
response = api_client.post(Routes.create_recipes, json=payload, headers=unique_user.token)
response = api_client.post(api_routes.recipes, json=payload, headers=unique_user.token)
assert response.status_code == 201
response_data = response.json()
@ -50,7 +44,7 @@ def test_recipe_share_tokens_get_all(
tokens.append(token)
# Get All Tokens
response = api_client.get(Routes.base, headers=unique_user.token)
response = api_client.get(api_routes.shared_recipes, headers=unique_user.token)
assert response.status_code == 200
response_data = response.json()
@ -72,7 +66,7 @@ def test_recipe_share_tokens_get_all_with_id(
)
tokens.append(token)
response = api_client.get(Routes.base + "?recipe_id=" + str(recipe.id), headers=unique_user.token)
response = api_client.get(api_routes.shared_recipes + "?recipe_id=" + str(recipe.id), headers=unique_user.token)
assert response.status_code == 200
response_data = response.json()
@ -92,10 +86,12 @@ def test_recipe_share_tokens_create_and_get_one(
"recipeId": str(recipe.id),
}
response = api_client.post(Routes.base, json=payload, headers=unique_user.token)
response = api_client.post(api_routes.shared_recipes, json=payload, headers=unique_user.token)
assert response.status_code == 201
response = api_client.get(Routes.item(response.json()["id"]), json=payload, headers=unique_user.token)
response = api_client.get(
api_routes.shared_recipes_item_id(response.json()["id"]), json=payload, headers=unique_user.token
)
assert response.status_code == 200
response_data = response.json()
@ -116,7 +112,7 @@ def test_recipe_share_tokens_delete_one(
)
# Delete Token
response = api_client.delete(Routes.item(token.id), headers=unique_user.token)
response = api_client.delete(api_routes.shared_recipes_item_id(token.id), headers=unique_user.token)
assert response.status_code == 200
# Get Token

View file

@ -5,7 +5,7 @@ from fastapi.testclient import TestClient
from mealie.schema.recipe.recipe import Recipe
from mealie.schema.recipe.recipe_step import IngredientReferences
from tests.utils import jsonify, routes
from tests.utils import api_routes, jsonify
from tests.utils.fixture_schemas import TestUser
@ -13,7 +13,6 @@ def test_associate_ingredient_with_step(api_client: TestClient, unique_user: Tes
recipe: Recipe = random_recipe
# Associate an ingredient with a step
steps = {} # key=step_id, value=ingredient_id
for idx, step in enumerate(recipe.recipe_instructions):
@ -26,7 +25,7 @@ def test_associate_ingredient_with_step(api_client: TestClient, unique_user: Tes
steps[idx] = [str(ingredient.reference_id) for ingredient in ingredients]
response = api_client.put(
routes.recipes.Recipe.item(recipe.slug),
api_routes.recipes_slug(recipe.slug),
json=jsonify(recipe.dict()),
headers=unique_user.token,
)
@ -35,8 +34,7 @@ def test_associate_ingredient_with_step(api_client: TestClient, unique_user: Tes
# Get Recipe and check that the ingredient is associated with the step
response = api_client.get(routes.recipes.Recipe.item(recipe.slug), headers=unique_user.token)
response = api_client.get(api_routes.recipes_slug(recipe.slug), headers=unique_user.token)
assert response.status_code == 200
data: dict = json.loads(response.text)

View file

@ -2,18 +2,11 @@ import pytest
from fastapi.testclient import TestClient
from mealie.schema.recipe.recipe_ingredient import CreateIngredientUnit
from tests.utils import api_routes
from tests.utils.factories import random_bool, random_string
from tests.utils.fixture_schemas import TestUser
class Routes:
base = "/api/units"
@staticmethod
def item(item_id: int) -> str:
return f"{Routes.base}/{item_id}"
@pytest.fixture(scope="function")
def unit(api_client: TestClient, unique_user: TestUser):
data = CreateIngredientUnit(
@ -24,13 +17,13 @@ def unit(api_client: TestClient, unique_user: TestUser):
use_abbreviation=random_bool(),
).dict(by_alias=True)
response = api_client.post(Routes.base, json=data, headers=unique_user.token)
response = api_client.post(api_routes.units, json=data, headers=unique_user.token)
assert response.status_code == 201
yield response.json()
response = api_client.delete(Routes.item(response.json()["id"]), headers=unique_user.token)
response = api_client.delete(api_routes.units_item_id(response.json()["id"]), headers=unique_user.token)
def test_create_unit(api_client: TestClient, unique_user: TestUser):
@ -39,12 +32,12 @@ def test_create_unit(api_client: TestClient, unique_user: TestUser):
description=random_string(10),
).dict(by_alias=True)
response = api_client.post(Routes.base, json=data, headers=unique_user.token)
response = api_client.post(api_routes.units, json=data, headers=unique_user.token)
assert response.status_code == 201
def test_read_unit(api_client: TestClient, unit: dict, unique_user: TestUser):
response = api_client.get(Routes.item(unit["id"]), headers=unique_user.token)
response = api_client.get(api_routes.units_item_id(unit["id"]), headers=unique_user.token)
assert response.status_code == 200
as_json = response.json()
@ -67,7 +60,7 @@ def test_update_unit(api_client: TestClient, unit: dict, unique_user: TestUser):
"useAbbreviation": not unit["useAbbreviation"],
}
response = api_client.put(Routes.item(unit["id"]), json=update_data, headers=unique_user.token)
response = api_client.put(api_routes.units_item_id(unit["id"]), json=update_data, headers=unique_user.token)
assert response.status_code == 200
as_json = response.json()
@ -82,9 +75,9 @@ def test_update_unit(api_client: TestClient, unit: dict, unique_user: TestUser):
def test_delete_unit(api_client: TestClient, unit: dict, unique_user: TestUser):
item_id = unit["id"]
response = api_client.delete(Routes.item(item_id), headers=unique_user.token)
response = api_client.delete(api_routes.units_item_id(item_id), headers=unique_user.token)
assert response.status_code == 200
response = api_client.get(Routes.item(item_id), headers=unique_user.token)
response = api_client.get(api_routes.units_item_id(item_id), headers=unique_user.token)
assert response.status_code == 404