Hayden 9ecef4c25f
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.
2022-10-18 14:49:41 -08:00

48 lines
1.5 KiB
Python

import json
import random
from fastapi.testclient import TestClient
from mealie.schema.recipe.recipe import Recipe
from mealie.schema.recipe.recipe_step import IngredientReferences
from tests.utils import api_routes, jsonify
from tests.utils.fixture_schemas import TestUser
def test_associate_ingredient_with_step(api_client: TestClient, unique_user: TestUser, random_recipe: Recipe):
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):
ingredients = random.choices(recipe.recipe_ingredient, k=2)
step.ingredient_references = [
IngredientReferences(reference_id=ingredient.reference_id) for ingredient in ingredients
]
steps[idx] = [str(ingredient.reference_id) for ingredient in ingredients]
response = api_client.put(
api_routes.recipes_slug(recipe.slug),
json=jsonify(recipe.dict()),
headers=unique_user.token,
)
assert response.status_code == 200
# Get Recipe and check that the ingredient is associated with the step
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)
for idx, stp in enumerate(data.get("recipeInstructions")):
all_refs = [ref["referenceId"] for ref in stp.get("ingredientReferences")]
assert len(all_refs) == 2
assert all(ref in steps[idx] for ref in all_refs)