From 0bf3aed28757cbefa1a62aac5826b85dbca34409 Mon Sep 17 00:00:00 2001 From: Michael Genson <71845777+michael-genson@users.noreply.github.com> Date: Fri, 23 Feb 2024 17:02:32 +0000 Subject: [PATCH] updated models/services/tests to include user_id --- mealie/routes/groups/controller_shopping_lists.py | 4 ++-- mealie/schema/group/group_shopping_list.py | 1 + mealie/services/group_services/shopping_lists.py | 7 ++++--- .../tasks/delete_old_checked_shopping_list_items.py | 3 ++- tests/fixtures/fixture_shopping_lists.py | 6 +++--- tests/unit_tests/repository_tests/test_pagination.py | 2 +- .../tasks/test_delete_old_checked_shopping_list_items.py | 8 ++++++-- 7 files changed, 19 insertions(+), 12 deletions(-) diff --git a/mealie/routes/groups/controller_shopping_lists.py b/mealie/routes/groups/controller_shopping_lists.py index a61ad3f397f1..2b5a8174e7dd 100644 --- a/mealie/routes/groups/controller_shopping_lists.py +++ b/mealie/routes/groups/controller_shopping_lists.py @@ -89,7 +89,7 @@ def publish_list_item_events(publisher: Callable, items_collection: ShoppingList class ShoppingListItemController(BaseCrudController): @cached_property def service(self): - return ShoppingListService(self.repos, self.group) + return ShoppingListService(self.repos, self.group, self.user) @cached_property def repo(self): @@ -154,7 +154,7 @@ router = APIRouter(prefix="/groups/shopping/lists", tags=["Group: Shopping Lists class ShoppingListController(BaseCrudController): @cached_property def service(self): - return ShoppingListService(self.repos, self.group) + return ShoppingListService(self.repos, self.group, self.user) @cached_property def repo(self): diff --git a/mealie/schema/group/group_shopping_list.py b/mealie/schema/group/group_shopping_list.py index ae5a27da5ece..83a6291768ff 100644 --- a/mealie/schema/group/group_shopping_list.py +++ b/mealie/schema/group/group_shopping_list.py @@ -190,6 +190,7 @@ class ShoppingListRecipeRefOut(MealieModel): class ShoppingListSave(ShoppingListCreate): group_id: UUID4 + user_id: UUID4 class ShoppingListSummary(ShoppingListSave): diff --git a/mealie/services/group_services/shopping_lists.py b/mealie/services/group_services/shopping_lists.py index b477d9bb5669..09350b603786 100644 --- a/mealie/services/group_services/shopping_lists.py +++ b/mealie/services/group_services/shopping_lists.py @@ -19,13 +19,14 @@ from mealie.schema.group.group_shopping_list import ( ) from mealie.schema.recipe.recipe_ingredient import IngredientFood, IngredientUnit, RecipeIngredient from mealie.schema.response.pagination import OrderDirection, PaginationQuery -from mealie.schema.user.user import GroupInDB +from mealie.schema.user.user import GroupInDB, UserOut class ShoppingListService: - def __init__(self, repos: AllRepositories, group: GroupInDB): + def __init__(self, repos: AllRepositories, group: GroupInDB, user: UserOut): self.repos = repos self.group = group + self.user = user self.shopping_lists = repos.group_shopping_lists self.list_items = repos.group_shopping_list_item self.list_item_refs = repos.group_shopping_list_item_references @@ -476,7 +477,7 @@ class ShoppingListService: return self.shopping_lists.get_one(shopping_list.id), items # type: ignore def create_one_list(self, data: ShoppingListCreate): - create_data = data.cast(ShoppingListSave, group_id=self.group.id) + create_data = data.cast(ShoppingListSave, group_id=self.group.id, user_id=self.user.id) new_list = self.shopping_lists.create(create_data) # type: ignore labels = self.repos.group_multi_purpose_labels.by_group(self.group.id).page_all( diff --git a/mealie/services/scheduler/tasks/delete_old_checked_shopping_list_items.py b/mealie/services/scheduler/tasks/delete_old_checked_shopping_list_items.py index cc532c8caa2a..1f33ec580133 100644 --- a/mealie/services/scheduler/tasks/delete_old_checked_shopping_list_items.py +++ b/mealie/services/scheduler/tasks/delete_old_checked_shopping_list_items.py @@ -60,7 +60,8 @@ def delete_old_checked_list_items(group_id: UUID4 | None = None): for group in groups: event_bus_service = EventBusService(session=session, group_id=group.id) - shopping_list_service = ShoppingListService(repos, group) + # user is passed as None since we don't use it here + shopping_list_service = ShoppingListService(repos, group, None) # type: ignore shopping_list_data = repos.group_shopping_lists.by_group(group.id).page_all( PaginationQuery(page=1, per_page=-1) ) diff --git a/tests/fixtures/fixture_shopping_lists.py b/tests/fixtures/fixture_shopping_lists.py index 835ea0f51e6a..2eae671ffd4c 100644 --- a/tests/fixtures/fixture_shopping_lists.py +++ b/tests/fixtures/fixture_shopping_lists.py @@ -29,7 +29,7 @@ def shopping_lists(database: AllRepositories, unique_user: TestUser): for _ in range(3): model = database.group_shopping_lists.create( - ShoppingListSave(name=random_string(10), group_id=unique_user.group_id), + ShoppingListSave(name=random_string(10), group_id=unique_user.group_id, user_id=unique_user.user_id), ) models.append(model) @@ -46,7 +46,7 @@ def shopping_lists(database: AllRepositories, unique_user: TestUser): @pytest.fixture(scope="function") def shopping_list(database: AllRepositories, unique_user: TestUser): model = database.group_shopping_lists.create( - ShoppingListSave(name=random_string(10), group_id=unique_user.group_id), + ShoppingListSave(name=random_string(10), group_id=unique_user.group_id, user_id=unique_user.user_id), ) yield model @@ -60,7 +60,7 @@ def shopping_list(database: AllRepositories, unique_user: TestUser): @pytest.fixture(scope="function") def list_with_items(database: AllRepositories, unique_user: TestUser): list_model = database.group_shopping_lists.create( - ShoppingListSave(name=random_string(10), group_id=unique_user.group_id), + ShoppingListSave(name=random_string(10), group_id=unique_user.group_id, user_id=unique_user.user_id), ) for _ in range(10): diff --git a/tests/unit_tests/repository_tests/test_pagination.py b/tests/unit_tests/repository_tests/test_pagination.py index 8d46134de8f3..6007878bf9b8 100644 --- a/tests/unit_tests/repository_tests/test_pagination.py +++ b/tests/unit_tests/repository_tests/test_pagination.py @@ -758,7 +758,7 @@ def test_pagination_order_by_nulls( def test_pagination_shopping_list_items_with_labels(database: AllRepositories, unique_user: TestUser): # create a shopping list and populate it with some items with labels, and some without labels shopping_list = database.group_shopping_lists.create( - ShoppingListSave(name=random_string(), group_id=unique_user.group_id) + ShoppingListSave(name=random_string(), group_id=unique_user.group_id, user_id=unique_user.user_id) ) labels = database.group_multi_purpose_labels.create_many( diff --git a/tests/unit_tests/services_tests/scheduler/tasks/test_delete_old_checked_shopping_list_items.py b/tests/unit_tests/services_tests/scheduler/tasks/test_delete_old_checked_shopping_list_items.py index b462745daba0..34705dee053a 100644 --- a/tests/unit_tests/services_tests/scheduler/tasks/test_delete_old_checked_shopping_list_items.py +++ b/tests/unit_tests/services_tests/scheduler/tasks/test_delete_old_checked_shopping_list_items.py @@ -14,7 +14,9 @@ def test_cleanup(database: AllRepositories, unique_user: TestUser): list_repo = database.group_shopping_lists.by_group(unique_user.group_id) list_item_repo = database.group_shopping_list_item - shopping_list = list_repo.create(ShoppingListSave(name=random_string(), group_id=unique_user.group_id)) + shopping_list = list_repo.create( + ShoppingListSave(name=random_string(), group_id=unique_user.group_id, user_id=unique_user.user_id) + ) unchecked_items = list_item_repo.create_many( [ ShoppingListItemCreate(note=random_string(), shopping_list_id=shopping_list.id) @@ -57,7 +59,9 @@ def test_no_cleanup(database: AllRepositories, unique_user: TestUser): list_repo = database.group_shopping_lists.by_group(unique_user.group_id) list_item_repo = database.group_shopping_list_item - shopping_list = list_repo.create(ShoppingListSave(name=random_string(), group_id=unique_user.group_id)) + shopping_list = list_repo.create( + ShoppingListSave(name=random_string(), group_id=unique_user.group_id, user_id=unique_user.user_id) + ) unchecked_items = list_item_repo.create_many( [ ShoppingListItemCreate(note=random_string(), shopping_list_id=shopping_list.id)