Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing test cases to GET and DELETE user models at the api/users endpoint #335

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
26 changes: 25 additions & 1 deletion src/chigame/api/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from rest_framework.test import APITestCase

from chigame.api.tests.factories import ChatFactory, GameFactory, TournamentFactory, UserFactory
from chigame.games.models import Game, Message
from chigame.games.models import Game, Message, User


class GameTests(APITestCase):
Expand Down Expand Up @@ -282,3 +282,27 @@ def test_delete_message(self):
self.assertEqual(data2["update_on"], Message.objects.get(id=2).update_on)
self.assertEqual(data2["content"], Message.objects.get(id=2).content)
self.assertEqual(2, Message.objects.get(id=2).token_id)


class UserTests(APITestCase):
def test_user_get(self):
user = UserFactory()

list_url = reverse("api-user-list")
detail_url = reverse("api-user-detail", kwargs={"pk": user.id})

list_response = self.client.get(list_url)
assert list_response.status_code == 200

detail_response = self.client.get(detail_url)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps compare fields here to ensure that the response and the user (from the factory) match!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There would be no simple way to do that as the faker is random, I will fix that once I implement the patch test!

assert detail_response.status_code == 200

def test_user_delete(self):
user = UserFactory()
self.assertEqual(User.objects.count(), 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me!


url = reverse("api-user-detail", args=[user.id])
response = self.client.delete(url, format="json")

self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
giomhern marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(User.objects.count(), 0)