From c319122b9d5184946c9d104c9f90509a1f66596b Mon Sep 17 00:00:00 2001 From: cuyakonwu Date: Thu, 30 Nov 2023 13:41:05 -0600 Subject: [PATCH 1/5] Created a test for GET at the api/users endpoint --- src/chigame/api/tests/test_api.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/chigame/api/tests/test_api.py b/src/chigame/api/tests/test_api.py index 5ce0956f..aac96bb0 100644 --- a/src/chigame/api/tests/test_api.py +++ b/src/chigame/api/tests/test_api.py @@ -282,3 +282,13 @@ 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() + + url = reverse("users", kwargs={"pk": user.id}) + + response = self.client.get(url) + assert response.status_code == 200 From 0a685e36ae197588ac213c1b74610f54aedd1494 Mon Sep 17 00:00:00 2001 From: cuyakonwu Date: Thu, 30 Nov 2023 13:53:21 -0600 Subject: [PATCH 2/5] Appopriately implemented the user list and detail tests --- src/chigame/api/tests/test_api.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/chigame/api/tests/test_api.py b/src/chigame/api/tests/test_api.py index aac96bb0..2e206ae5 100644 --- a/src/chigame/api/tests/test_api.py +++ b/src/chigame/api/tests/test_api.py @@ -288,7 +288,13 @@ class UserTests(APITestCase): def test_user_get(self): user = UserFactory() - url = reverse("users", kwargs={"pk": user.id}) + list_url = reverse("api-user-list") + detail_url = reverse("api-user-detail", kwargs={"pk": user.id}) - response = self.client.get(url) - assert response.status_code == 200 + # Your list view test + list_response = self.client.get(list_url) + assert list_response.status_code == 200 + + # Your detail view test + detail_response = self.client.get(detail_url) + assert detail_response.status_code == 200 From 8a1f341afabdd0a42b00c10bfbd0fa7ee13225b0 Mon Sep 17 00:00:00 2001 From: cuyakonwu Date: Thu, 30 Nov 2023 14:04:58 -0600 Subject: [PATCH 3/5] Removed comments --- src/chigame/api/tests/test_api.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/chigame/api/tests/test_api.py b/src/chigame/api/tests/test_api.py index 2e206ae5..3721315c 100644 --- a/src/chigame/api/tests/test_api.py +++ b/src/chigame/api/tests/test_api.py @@ -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): @@ -291,10 +291,17 @@ def test_user_get(self): list_url = reverse("api-user-list") detail_url = reverse("api-user-detail", kwargs={"pk": user.id}) - # Your list view test list_response = self.client.get(list_url) assert list_response.status_code == 200 - # Your detail view test detail_response = self.client.get(detail_url) assert detail_response.status_code == 200 + + def test_user_delete(self): + user = UserFactory() + + 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) From 4c201a01c32f54ccd0c5738f7f528c71c07d17f5 Mon Sep 17 00:00:00 2001 From: cuyakonwu Date: Thu, 30 Nov 2023 15:58:40 -0600 Subject: [PATCH 4/5] adjusted the delete function to ensuerr a new user was created to begin with --- src/chigame/api/tests/test_api.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/chigame/api/tests/test_api.py b/src/chigame/api/tests/test_api.py index 3721315c..9b777fe8 100644 --- a/src/chigame/api/tests/test_api.py +++ b/src/chigame/api/tests/test_api.py @@ -299,6 +299,7 @@ def test_user_get(self): 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") From 3189394817c6e327d261c91d1040f79e9f125c5f Mon Sep 17 00:00:00 2001 From: cuyakonwu Date: Thu, 7 Dec 2023 15:17:36 -0600 Subject: [PATCH 5/5] Added a username faker to support better lookups --- src/chigame/api/tests/factories.py | 2 ++ src/chigame/api/tests/test_api.py | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/chigame/api/tests/factories.py b/src/chigame/api/tests/factories.py index f4d1b5c7..a2e30108 100644 --- a/src/chigame/api/tests/factories.py +++ b/src/chigame/api/tests/factories.py @@ -82,7 +82,9 @@ class Meta: name = Faker("name") email = Faker("email") + username = Faker("user_name") password = Faker("password") + tokens = Faker("random_int", min=1, max=3) class TournamentFactory(DjangoModelFactory): diff --git a/src/chigame/api/tests/test_api.py b/src/chigame/api/tests/test_api.py index fdbb492d..37020fd7 100644 --- a/src/chigame/api/tests/test_api.py +++ b/src/chigame/api/tests/test_api.py @@ -289,7 +289,7 @@ def test_user_get(self): user = UserFactory() list_url = reverse("api-user-list") - detail_url = reverse("api-user-detail", kwargs={"pk": user.id}) + detail_url = reverse("api-user-detail", kwargs={"slug": user.username}) list_response = self.client.get(list_url) assert list_response.status_code == 200 @@ -306,7 +306,7 @@ def test_user_delete(self): self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(User.objects.count(), 0) - + def test_feed_message(self): self.user1 = UserFactory() self.user2 = UserFactory()