fix: preserve stored recipe slugs during hydration (#7294)

Co-authored-by: Michael Genson <71845777+michael-genson@users.noreply.github.com>
Co-authored-by: Michael Genson <genson.michael@gmail.com>
This commit is contained in:
harshitlarl
2026-03-27 20:38:48 +05:30
committed by GitHub
parent 63c549ae5c
commit c029a639fb
5 changed files with 77 additions and 4 deletions

View File

@@ -98,8 +98,8 @@ def test_get_all_public_recipes(
@pytest.mark.parametrize(
"query_filter, recipe_data, should_fetch",
[
('slug = "mypublicslug"', {"slug": "mypublicslug"}, True),
('slug = "mypublicslug"', {"slug": "notmypublicslug"}, False),
('slug = "mypublicslug"', {"slug": "mypublicslug", "name": "mypublicslug"}, True),
('slug = "mypublicslug"', {"slug": "notmypublicslug", "name": "notmypublicslug"}, False),
("settings.public = FALSE", {}, False),
("settings.public <> TRUE", {}, False),
],

View File

@@ -1357,6 +1357,60 @@ def test_recipe_crud_404(api_client: TestClient, unique_user: TestUser):
assert response.status_code == 404
def test_patch_recipe_after_name_changes_without_slug_update(api_client: TestClient, unique_user: TestUser):
original_name = "Nourish Bowls (Zuppa Copycat)"
translated_name = "Bols nourrissants (copie de Zuppa)"
response = api_client.post(api_routes.recipes, json={"name": original_name}, headers=unique_user.token)
assert response.status_code == 201
original_slug = response.json()
session = unique_user.repos.session
recipe = session.query(RecipeModel).filter(RecipeModel.slug == original_slug).one()
recipe.name = translated_name
session.commit()
response = api_client.get(api_routes.recipes_slug(original_slug), headers=unique_user.token)
assert response.status_code == 200
recipe_payload = response.json()
assert recipe_payload["name"] == translated_name
assert recipe_payload["slug"] == original_slug
response = api_client.patch(
api_routes.recipes_slug(original_slug),
json={"description": "Translated without changing the stored slug"},
headers=unique_user.token,
)
assert response.status_code == 200
patched_recipe = response.json()
assert patched_recipe["slug"] == original_slug
assert patched_recipe["description"] == "Translated without changing the stored slug"
def test_put_recipe_name_change_updates_slug(api_client: TestClient, unique_user: TestUser):
original_name = "Original Recipe Name"
renamed_name = "Renamed Recipe Name"
response = api_client.post(api_routes.recipes, json={"name": original_name}, headers=unique_user.token)
assert response.status_code == 201
original_slug = response.json()
response = api_client.get(api_routes.recipes_slug(original_slug), headers=unique_user.token)
assert response.status_code == 200
recipe_payload = response.json()
recipe_payload["name"] = renamed_name
response = api_client.put(api_routes.recipes_slug(original_slug), json=recipe_payload, headers=unique_user.token)
assert response.status_code == 200
renamed_recipe = response.json()
assert renamed_recipe["slug"] == slugify(renamed_name)
assert renamed_recipe["name"] == renamed_name
def test_create_recipe_same_name(api_client: TestClient, unique_user: TestUser):
slug = random_string(10)