Пример #1
0
 def setUp(self) -> None:
     super().setUp()
     self.recipe = create_dummy_recipe(self.author)
     self.ingredient = create_dummy_meta_ingredient()
     RecipeIngredient.objects.create(quantity="3 kg",
                                     meta=self.ingredient,
                                     recipe=self.recipe)
Пример #2
0
 def test_comment_recipe_reviewed(self):
     recipe = create_dummy_recipe(self.author)
     response = self.client.post(
         reverse('koocook_core:recipes:comments',
                 kwargs={'item_id': recipe.id}),
         {"body": self.comment_body})
     with self.subTest():
         self.assertEqual(response.json()["current"]["body"],
                          self.comment_body.source)
Пример #3
0
    def test_delete_recipe(self):
        recipe = create_dummy_recipe(self.author)
        response = self.client.delete(
            reverse("koocook_core:recipes:detail",
                    kwargs={'recipe_id': recipe.id}), recipe)
        with self.subTest():
            self.assertEqual(response.status_code, 200)
            with self.assertRaises(Recipe.DoesNotExist):
                Recipe.objects.get(pk=recipe.id)

        recipe = create_dummy_recipe(self.author)
        self.client.login(username=self.user2.username, password=self.password)
        response = self.client.delete(
            reverse("koocook_core:recipes:detail",
                    kwargs={'recipe_id': recipe.id}), recipe)
        with self.subTest(
                "That a non-author deletes the recipe should return a forbidden code"
        ):
            self.assertEqual(response.status_code, 403)
Пример #4
0
    def test_recipe_search_listview(self):
        response = self.client.get(reverse("koocook_core:search"))

        with self.subTest(
                "Search view must display all recipes with no filters"):
            self.assertQuerysetEqual(self.BLANK_QS,
                                     response.context["object_list"].all())

        recipe = create_dummy_recipe(self.author)
        response = self.client.get(reverse("koocook_core:search"),
                                   {'kw': recipe.name})
        with self.subTest(
                "Search view must display recipes containing search keywords"):
            self.assertEqual(response.context["object_list"].all()[0].name,
                             recipe.name)

        response = self.client.get(reverse("koocook_core:search"),
                                   {'popular': 'true'})
        self.client.get(
            reverse("koocook_core:recipes:detail",
                    kwargs={'recipe_id': recipe.id}))
        with self.subTest("Search by popularity alone"):
            self.assertEqual(response.context["object_list"][0].view_count, 1)

        # response = self.client.get(reverse("koocook_core:search"), {'name_asc': '1'})
        # self.client.get(reverse("koocook_core:recipes:detail", kwargs={'recipe_id': recipe.id}))
        # with self.subTest("Sort by name"):
        #     self.assertEqual(response.context["object_list"][0].id, recipe.id)

        response = self.client.get(
            reverse("koocook_core:search") + '?order=athinmajig')
        with self.subTest("Sort by non-existent field"):
            self.assertEqual(response.context["object_list"][0].id, recipe.id)

        response = self.client.get(
            reverse("koocook_core:search") + '?order=name')
        with self.subTest("Sort by name"):
            self.assertEqual(response.context["object_list"][0].id, recipe.id)

        response = self.client.get(
            reverse("koocook_core:search") + '?order=name&ordering=desc')
        with self.subTest("Sort by name in descending order"):
            self.assertEqual(response.context["object_list"][0].id, recipe.id)

        response = self.client.get(
            reverse("koocook_core:search") + '?ingredients=Pepper')
        with self.subTest("Search by ingredient"):
            self.assertQuerysetEqual(self.BLANK_QS,
                                     response.context["object_list"].all())

        response = self.client.get(
            reverse("koocook_core:search") + '?cookware=Athinmajig')
        with self.subTest("Search by cookware"):
            self.assertQuerysetEqual(self.BLANK_QS,
                                     response.context["object_list"].all())
Пример #5
0
    def test_recipe_user_listview(self):
        response = self.client.get(reverse("koocook_core:recipes:user"))
        with self.subTest("User has no recipes"):
            self.assertEqual(list(response.context["user_recipes"]), [])
        recipe = create_dummy_recipe(self.author)
        response = self.client.get(reverse("koocook_core:recipes:user"))
        with self.subTest("Checking status code"):
            self.assertEqual(response.status_code, 200)

        with self.subTest("User has a recipe"):
            self.assertEqual(response.context["user_recipes"][0].id, recipe.id)
Пример #6
0
    def test_recipe_update_view(self):
        recipe = create_dummy_recipe(self.author)
        response = self.client.get(
            reverse("koocook_core:recipes:edit", kwargs={'pk': recipe.id}))

        with self.subTest("Empty ingredients"):
            self.assertEqual(response.context["ingredients"], '[]')

        with self.subTest("Empty tags"):
            self.assertEqual(response.context["tags"], '[]')

        recipe_body = create_dummy_recipe_body(self.author)
        recipe_body.update(
            {'cookware_list': '[{"name": "Whisk"}, {"name": "Spatula"}]'})
        recipe_body.update({
            'tags':
            '[{"name": "dummyTag", "label": {"name": "dummyLabel"}}]'
        })
        recipe_body.update({
            'ingredients':
            '[{"quantity": {"number": "5", "unit": "tbsp"}, "name": "Pepper"}]'
        })
        recipe_body.update({'recipe_instructions': '{}'})
        self.client.post(
            reverse("koocook_core:recipes:edit", kwargs={'pk': recipe.id}),
            recipe_body)
        response = self.client.get(
            reverse("koocook_core:recipes:edit", kwargs={'pk': recipe.id}))

        with self.subTest(
                "Posting a normal recipe with tags, cookware, and ingredients"
        ):
            self.assertEqual(response.context["object"].name, 'dummy')
            self.assertListEqual(
                list(recipe.tag_set.all()),
                list(response.context["object"].tag_set.all()))
            self.assertEqual(
                "Spatula",
                list(response.context["object"].equipment_set.all())[0].name)
            self.assertListEqual(
                list(recipe.recipe_ingredients),
                list(response.context["object"].recipe_ingredients))

        with self.subTest("Editing ingredients and tags"):
            ingredients = json.loads(response.context["ingredients"])
            tags = list(response.context["object"].tag_set.all())
            tags[0].name = "dummyTag2"
            recipe_body['tags'] = json.dumps(tags, cls=ModelEncoder)
            recipe_body['ingredients'] = json.dumps([{
                "id": ingredients[0]["id"],
                "quantity": {
                    "number": "3",
                    "unit": "tsp"
                },
                "name": "Salt"
            }])
            self.client.post(
                reverse("koocook_core:recipes:edit", kwargs={'pk': recipe.id}),
                recipe_body)
            response = self.client.get(
                reverse("koocook_core:recipes:edit", kwargs={'pk': recipe.id}))
            self.assertEqual(
                'Salt',
                response.context["object"].recipe_ingredients[0].meta.name)

        self.client.login(username=self.user2.username, password=self.password)
        with self.subTest("Posting with a different user"):
            response = self.client.post(
                reverse("koocook_core:recipes:edit", kwargs={'pk': recipe.id}),
                recipe_body)
            self.assertEqual(response.status_code, 403)
Пример #7
0
 def test_recipe_detail_view_context(self):
     recipe = create_dummy_recipe(self.author)
     response = self.client.get(
         reverse("koocook_core:recipes:detail",
                 kwargs={'recipe_id': recipe.id}))
     self.assertEqual(response.context["ingredients"], [])
Пример #8
0
 def setUp(self) -> None:
     super().setUp()
     self.recipe = create_dummy_recipe(self.author)
     self.visit = RecipeVisit.associate_recipe_with_user(
         self.kc_user, self.recipe)