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 POST and PATCH user models at the api/users endpoint #340

Merged
merged 16 commits into from
Dec 7, 2023
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 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,50 @@ 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)
assert detail_response.status_code == 200

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

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)
self.assertEqual(User.objects.count(), 0)

def test_user_post(self):
user = {"name": "BillyBob", "email": "[email protected]"}

url = reverse("api-user-list")
response = self.client.post(url, data=user, format="json")

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!

self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(User.objects.count(), 1)
self.assertEqual(response.data["email"], user["email"])
self.assertEqual(response.data["name"], user["name"])

def test_user_patch(self):
user = UserFactory()
url = reverse("api-user-detail", kwargs={"pk": user.id})

updated_data = {"name": "Johnn"}

response = self.client.patch(url, data=updated_data, format="json")
Copy link
Contributor

Choose a reason for hiding this comment

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

Simple, clean, and I approve!


self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(User.objects.count(), 1)
self.assertEqual(response.data["name"], updated_data["name"])