Skip to content

Commit

Permalink
Merge pull request #2759 from michael-genson/fix/delete-all-notes
Browse files Browse the repository at this point in the history
fix: Can't Delete All Notes
  • Loading branch information
boc-the-git authored Nov 26, 2023
2 parents fcd4854 + c3c6bd6 commit 416d752
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
10 changes: 8 additions & 2 deletions mealie/db/models/_model_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,11 @@ class BaseMixins:
`self.update` method which directly passing arguments to the `__init__`
"""

def update(self, *args, **kwarg):
self.__init__(*args, **kwarg)
def update(self, *args, **kwargs):
self.__init__(*args, **kwargs)

# sqlalchemy doesn't like this method to remove all instances of a 1:many relationship,
# so we explicitly check for that here
for k, v in kwargs.items():
if hasattr(self, k) and v == []:
setattr(self, k, v)
37 changes: 37 additions & 0 deletions tests/integration_tests/user_recipe_tests/test_recipe_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from mealie.repos.repository_factory import AllRepositories
from mealie.schema.recipe.recipe import RecipeCategory, RecipeSummary, RecipeTag
from mealie.schema.recipe.recipe_notes import RecipeNote
from mealie.services.recipe.recipe_data_service import RecipeDataService
from mealie.services.scraper.recipe_scraper import DEFAULT_SCRAPER_STRATEGIES
from tests import data, utils
Expand Down Expand Up @@ -610,6 +611,42 @@ def test_rename(api_client: TestClient, recipe_data: RecipeSiteTestCase, unique_
recipe_data.expected_slug = new_slug


def test_remove_notes(api_client: TestClient, unique_user: TestUser):
# create recipe
recipe_create_url = api_routes.recipes
recipe_create_data = {"name": random_string()}
response = api_client.post(recipe_create_url, headers=unique_user.token, json=recipe_create_data)
assert response.status_code == 201
recipe_slug: str = response.json()

# get recipe and add a note
recipe_url = api_routes.recipes_slug(recipe_slug)
response = api_client.get(recipe_url, headers=unique_user.token)
assert response.status_code == 200

recipe = json.loads(response.text)
recipe["notes"] = [RecipeNote(title=random_string(), text=random_string()).dict()]
response = api_client.put(recipe_url, json=recipe, headers=unique_user.token)
assert response.status_code == 200

# get recipe and remove the note
response = api_client.get(recipe_url, headers=unique_user.token)
assert response.status_code == 200

recipe = json.loads(response.text)
assert len(recipe.get("notes", [])) == 1
recipe["notes"] = []
response = api_client.put(recipe_url, json=recipe, headers=unique_user.token)
assert response.status_code == 200

# verify the note is removed
response = api_client.get(recipe_url, headers=unique_user.token)
assert response.status_code == 200

recipe = json.loads(response.text)
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)
Expand Down

0 comments on commit 416d752

Please sign in to comment.