Skip to content

Commit

Permalink
maybe fixes broken tests??
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-genson committed Sep 26, 2024
1 parent 53bf00d commit c55654f
Showing 1 changed file with 122 additions and 124 deletions.
246 changes: 122 additions & 124 deletions tests/integration_tests/user_recipe_tests/test_recipe_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from tests.utils import api_routes
from tests.utils.factories import random_int, random_string
from tests.utils.fixture_schemas import TestUser
from tests.utils.recipe_data import RecipeSiteTestCase, get_recipe_test_cases
from tests.utils.recipe_data import get_recipe_test_cases

recipe_test_data = get_recipe_test_cases()

Expand Down Expand Up @@ -444,174 +444,172 @@ def test_create_recipe_from_zip_invalid_tag(api_client: TestClient, unique_user:
assert fetched_recipe.tags[0].slug == invalid_name


@pytest.mark.parametrize("recipe_data", recipe_test_data)
def test_read_update(
api_client: TestClient,
recipe_data: RecipeSiteTestCase,
unique_user: TestUser,
recipe_categories: list[RecipeCategory],
):
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
for recipe_data in recipe_test_data:
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

recipe = json.loads(response.text)
recipe = json.loads(response.text)

test_notes = [
{"title": "My Test Title1", "text": "My Test Text1"},
{"title": "My Test Title2", "text": "My Test Text2"},
]
test_notes = [
{"title": "My Test Title1", "text": "My Test Text1"},
{"title": "My Test Title2", "text": "My Test Text2"},
]

recipe["notes"] = test_notes
recipe["notes"] = test_notes

recipe["recipeCategory"] = [x.model_dump() for x in recipe_categories]
recipe["recipeCategory"] = [x.model_dump() for x in recipe_categories]

response = api_client.put(recipe_url, json=utils.jsonify(recipe), headers=unique_user.token)
response = api_client.put(recipe_url, json=utils.jsonify(recipe), headers=unique_user.token)

assert response.status_code == 200
assert json.loads(response.text).get("slug") == recipe_data.expected_slug
assert response.status_code == 200
assert json.loads(response.text).get("slug") == recipe_data.expected_slug

response = api_client.get(recipe_url, headers=unique_user.token)
assert response.status_code == 200
recipe = json.loads(response.text)
response = api_client.get(recipe_url, headers=unique_user.token)
assert response.status_code == 200
recipe = json.loads(response.text)

assert recipe["notes"] == test_notes
assert recipe["notes"] == test_notes

assert len(recipe["recipeCategory"]) == len(recipe_categories)
assert len(recipe["recipeCategory"]) == len(recipe_categories)

test_name = [x.name for x in recipe_categories]
for cats in zip(recipe["recipeCategory"], recipe_categories, strict=False):
assert cats[0]["name"] in test_name
test_name = [x.name for x in recipe_categories]
for cats in zip(recipe["recipeCategory"], recipe_categories, strict=False):
assert cats[0]["name"] in test_name


@pytest.mark.parametrize("recipe_data", recipe_test_data)
def test_duplicate(api_client: TestClient, recipe_data: RecipeSiteTestCase, unique_user: TestUser):
# Initial get of the original recipe
original_recipe_url = api_routes.recipes_slug(recipe_data.expected_slug)
response = api_client.get(original_recipe_url, headers=unique_user.token)
assert response.status_code == 200
initial_recipe = json.loads(response.text)
def test_duplicate(api_client: TestClient, unique_user: TestUser):
for recipe_data in recipe_test_data:
# Initial get of the original recipe
original_recipe_url = api_routes.recipes_slug(recipe_data.expected_slug)
response = api_client.get(original_recipe_url, headers=unique_user.token)
assert response.status_code == 200
initial_recipe = json.loads(response.text)

# Duplicate the recipe
recipe_duplicate_url = api_routes.recipes_slug_duplicate(recipe_data.expected_slug)
response = api_client.post(
recipe_duplicate_url,
headers=unique_user.token,
json={
"name": "Test Duplicate",
},
)
assert response.status_code == 201
# Duplicate the recipe
recipe_duplicate_url = api_routes.recipes_slug_duplicate(recipe_data.expected_slug)
response = api_client.post(
recipe_duplicate_url,
headers=unique_user.token,
json={
"name": "Test Duplicate",
},
)
assert response.status_code == 201

duplicate_recipe = json.loads(response.text)
assert duplicate_recipe["id"] != initial_recipe["id"]
assert duplicate_recipe["slug"].startswith("test-duplicate")
assert duplicate_recipe["name"].startswith("Test Duplicate")
duplicate_recipe = json.loads(response.text)
assert duplicate_recipe["id"] != initial_recipe["id"]
assert duplicate_recipe["slug"].startswith("test-duplicate")
assert duplicate_recipe["name"].startswith("Test Duplicate")

# Image should be copied (if it exists)
assert (
duplicate_recipe["image"] is None
and initial_recipe["image"] is None
or duplicate_recipe["image"] != initial_recipe["image"]
)
# Image should be copied (if it exists)
assert (
duplicate_recipe["image"] is None
and initial_recipe["image"] is None
or duplicate_recipe["image"] != initial_recipe["image"]
)

# Number of steps should be the same, but the text may have changed (link replacements)
assert len(duplicate_recipe["recipeInstructions"]) == len(initial_recipe["recipeInstructions"])

# Ingredients should have the same texts, but different ids
assert duplicate_recipe["recipeIngredient"] != initial_recipe["recipeIngredient"]
assert [i["note"] for i in duplicate_recipe["recipeIngredient"]] == [
i["note"] for i in initial_recipe["recipeIngredient"]
]

previous_categories = initial_recipe["recipeCategory"]
assert duplicate_recipe["recipeCategory"] == previous_categories

# Edit the duplicated recipe to make sure it doesn't affect the original
dup_notes = duplicate_recipe["notes"] or []
dup_notes.append({"title": "Test", "text": "Test"})
duplicate_recipe["notes"] = dup_notes
duplicate_recipe["recipeIngredient"][0]["note"] = "Different Ingredient"
new_recipe_url = api_routes.recipes_slug(duplicate_recipe.get("slug"))
response = api_client.put(new_recipe_url, json=duplicate_recipe, headers=unique_user.token)
assert response.status_code == 200
edited_recipe = json.loads(response.text)
# Number of steps should be the same, but the text may have changed (link replacements)
assert len(duplicate_recipe["recipeInstructions"]) == len(initial_recipe["recipeInstructions"])

# reload original
response = api_client.get(original_recipe_url, headers=unique_user.token)
assert response.status_code == 200
original_recipe = json.loads(response.text)
# Ingredients should have the same texts, but different ids
assert duplicate_recipe["recipeIngredient"] != initial_recipe["recipeIngredient"]
assert [i["note"] for i in duplicate_recipe["recipeIngredient"]] == [
i["note"] for i in initial_recipe["recipeIngredient"]
]

assert edited_recipe["notes"] == dup_notes
assert original_recipe.get("notes") != edited_recipe.get("notes")
assert original_recipe.get("recipeCategory") == previous_categories
previous_categories = initial_recipe["recipeCategory"]
assert duplicate_recipe["recipeCategory"] == previous_categories

# Make sure ingredient edits don't affect the original
original_ingredients = original_recipe.get("recipeIngredient")
edited_ingredients = edited_recipe.get("recipeIngredient")
# Edit the duplicated recipe to make sure it doesn't affect the original
dup_notes = duplicate_recipe["notes"] or []
dup_notes.append({"title": "Test", "text": "Test"})
duplicate_recipe["notes"] = dup_notes
duplicate_recipe["recipeIngredient"][0]["note"] = "Different Ingredient"
new_recipe_url = api_routes.recipes_slug(duplicate_recipe.get("slug"))
response = api_client.put(new_recipe_url, json=duplicate_recipe, headers=unique_user.token)
assert response.status_code == 200
edited_recipe = json.loads(response.text)

# reload original
response = api_client.get(original_recipe_url, headers=unique_user.token)
assert response.status_code == 200
original_recipe = json.loads(response.text)

assert edited_recipe["notes"] == dup_notes
assert original_recipe.get("notes") != edited_recipe.get("notes")
assert original_recipe.get("recipeCategory") == previous_categories

assert len(original_ingredients) == len(edited_ingredients)
# Make sure ingredient edits don't affect the original
original_ingredients = original_recipe.get("recipeIngredient")
edited_ingredients = edited_recipe.get("recipeIngredient")

assert original_ingredients[0]["note"] != edited_ingredients[0]["note"]
assert edited_ingredients[0]["note"] == "Different Ingredient"
assert original_ingredients[0]["referenceId"] != edited_ingredients[1]["referenceId"]
assert len(original_ingredients) == len(edited_ingredients)

for i in range(1, len(original_ingredients)):
assert original_ingredients[i]["referenceId"] != edited_ingredients[i]["referenceId"]
assert original_ingredients[0]["note"] != edited_ingredients[0]["note"]
assert edited_ingredients[0]["note"] == "Different Ingredient"
assert original_ingredients[0]["referenceId"] != edited_ingredients[1]["referenceId"]

def copy_info(ing):
return {k: v for k, v in ing.items() if k != "referenceId"}
for i in range(1, len(original_ingredients)):
assert original_ingredients[i]["referenceId"] != edited_ingredients[i]["referenceId"]

assert copy_info(original_ingredients[i]) == copy_info(edited_ingredients[i])
def copy_info(ing):
return {k: v for k, v in ing.items() if k != "referenceId"}

assert copy_info(original_ingredients[i]) == copy_info(edited_ingredients[i])


# This needs to happen after test_duplicate,
# otherwise that one will run into problems with comparing the instruction/ingredient lists
@pytest.mark.parametrize("recipe_data", recipe_test_data)
def test_update_with_empty_relationship(
api_client: TestClient,
recipe_data: RecipeSiteTestCase,
unique_user: TestUser,
):
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
for recipe_data in recipe_test_data:
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

recipe = json.loads(response.text)
recipe = json.loads(response.text)

recipe["recipeInstructions"] = []
recipe["recipeIngredient"] = []
recipe["recipeInstructions"] = []
recipe["recipeIngredient"] = []

response = api_client.put(recipe_url, json=utils.jsonify(recipe), headers=unique_user.token)
response = api_client.put(recipe_url, json=utils.jsonify(recipe), headers=unique_user.token)

assert response.status_code == 200
assert json.loads(response.text).get("slug") == recipe_data.expected_slug
assert response.status_code == 200
assert json.loads(response.text).get("slug") == recipe_data.expected_slug

response = api_client.get(recipe_url, headers=unique_user.token)
assert response.status_code == 200
recipe = json.loads(response.text)
response = api_client.get(recipe_url, headers=unique_user.token)
assert response.status_code == 200
recipe = json.loads(response.text)

assert recipe["recipeInstructions"] == []
assert recipe["recipeIngredient"] == []
assert recipe["recipeInstructions"] == []
assert recipe["recipeIngredient"] == []


@pytest.mark.parametrize("recipe_data", recipe_test_data)
def test_rename(api_client: TestClient, recipe_data: RecipeSiteTestCase, unique_user: TestUser):
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
def test_rename(api_client: TestClient, unique_user: TestUser):
for recipe_data in recipe_test_data:
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

recipe = json.loads(response.text)
new_name = recipe.get("name") + "-rename"
new_slug = slugify(new_name)
recipe["name"] = new_name
recipe = json.loads(response.text)
new_name = recipe.get("name") + "-rename"
new_slug = slugify(new_name)
recipe["name"] = new_name

response = api_client.put(recipe_url, json=recipe, headers=unique_user.token)
response = api_client.put(recipe_url, json=recipe, headers=unique_user.token)

assert response.status_code == 200
assert json.loads(response.text).get("slug") == new_slug
assert response.status_code == 200
assert json.loads(response.text).get("slug") == new_slug

recipe_data.expected_slug = new_slug
recipe_data.expected_slug = new_slug


def test_remove_notes(api_client: TestClient, unique_user: TestUser):
Expand Down Expand Up @@ -650,10 +648,10 @@ def test_remove_notes(api_client: TestClient, unique_user: TestUser):
assert len(recipe.get("notes", [])) == 0


@pytest.mark.parametrize("recipe_data", recipe_test_data)
def test_delete(api_client: TestClient, recipe_data: RecipeSiteTestCase, unique_user: TestUser):
response = api_client.delete(api_routes.recipes_slug(recipe_data.expected_slug), headers=unique_user.token)
assert response.status_code == 200
def test_delete(api_client: TestClient, unique_user: TestUser):
for recipe_data in recipe_test_data:
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, unique_user: TestUser):
Expand Down

0 comments on commit c55654f

Please sign in to comment.