Skip to content

Commit

Permalink
Tag API detail implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
kolenkoal committed Aug 31, 2023
1 parent e186b5b commit 6aa2ff2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
23 changes: 23 additions & 0 deletions app/recipe/tests/test_tags_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,26 @@ def test_tags_limited_to_user(self):
self.assertEqual(len(res.data), 1)
self.assertEqual(res.data[0]['name'], tag.name)
self.assertEqual(res.data[0]['id'], tag.id)

def test_update_tag(self):
"""Test updating a tag."""
tag = Tag.objects.create(user=self.user, name="After Dinner")
payload = {'name': 'Dessert'}

url = detail_url(tag.id)
res = self.client.patch(url, payload)

self.assertEqual(res.status_code, status.HTTP_200_OK)
tag.refresh_from_db()
self.assertEqual(tag.name, payload['name'])

def test_delete_tag(self):
"""Test deleting a tag."""
tag = Tag.objects.create(user=self.user, name='Morning')

url = detail_url(tag.id)
res = self.client.delete(url)

self.assertEqual(res.status_code, status.HTTP_204_NO_CONTENT)
self.assertFalse(Tag.objects.filter(id=tag.id).exists())
# self.assertFalse(Tag.objects.filter(user=self.user).exists())
5 changes: 4 additions & 1 deletion app/recipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ def perform_create(self, serializer):
serializer.save(user=self.request.user)


class TagViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
class TagViewSet(mixins.DestroyModelMixin,
mixins.UpdateModelMixin,
mixins.ListModelMixin,
viewsets.GenericViewSet):
"""Manage tags in the database"""
serializer_class = serializers.TagSerializer
authentication_classes = [TokenAuthentication]
Expand Down

0 comments on commit 6aa2ff2

Please sign in to comment.