Skip to content

Commit

Permalink
Merge branch 'dev' into apis/post-users
Browse files Browse the repository at this point in the history
  • Loading branch information
cuyakonwu authored Dec 7, 2023
2 parents 31d910c + 2af4411 commit d0726f7
Showing 1 changed file with 98 additions and 2 deletions.
100 changes: 98 additions & 2 deletions src/chigame/api/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# from django.test import TestCase

# Create your tests here.

# Compare this snippet from src/chigame/api/tests.py:
from django.urls import reverse

# Related third party imports
from rest_framework import status
from rest_framework.test import APITestCase
from rest_framework.utils.serializer_helpers import ReturnDict

# Local application/library specific imports
from chigame.api.serializers import GameSerializer
from chigame.api.tests.factories import ChatFactory, GameFactory, TournamentFactory, UserFactory
from chigame.games.models import Game, Message

Expand All @@ -17,7 +21,24 @@ def check_equal(self, obj, expected: dict):
Helper function to check that the object data matches the expected data.
"""
for key in expected:
self.assertEqual(getattr(obj, key), expected[key])
# Issue: Serialized data often converts numbers to strings for transport
# leading to a type mismatch when compared with their original Python types.

# Solution: The check on lines 35-36 converts the serialized data to
# the original Python type before comparing it with the expected data.
# This ensures that the comparison is done on the same type of data.

# For example, without this check, you might encounter issues
# when comparing Decimal('5') and '5.00',
# which would fail due to type mismatch despite representing the same value.

if isinstance(obj, (ReturnDict, dict)):
obj_value = obj[key]
else:
# Use getattr for object instances
obj_value = getattr(obj, key, None)

self.assertEqual(obj_value, expected[key])

# def test_get_game(self):
# """
Expand All @@ -36,6 +57,81 @@ def check_equal(self, obj, expected: dict):
# self.assertEqual(Game.objects.count(), 1)
# self.check_equal(Game.objects.get(), response.data)

def test_get_game(self):
"""
Ensure we can get a game object.
"""

# Create a game object
game = GameFactory()

# Get the game object
url = reverse("api-game-list")
response = self.client.get(url, format="json")
serialized_game = GameSerializer(game).data

# Check that the game object was retrieved correctly
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(Game.objects.count(), 1)
self.check_equal(serialized_game, response.data["results"][0])

def test_get_game_list1(self):
"""
Ensure we can get a list of game objects.
"""

# create three game objects
game1 = GameFactory()
game2 = GameFactory()
game3 = GameFactory()

# Get the game object list
url = reverse("api-game-list")
response = self.client.get(url, format="json")

# Check that the game object list was retrieved correctly
self.assertEqual(response.status_code, status.HTTP_200_OK)

# test that the game object list contains the two game objects we created
self.assertEqual(Game.objects.count(), 3)
serialized_game1 = GameSerializer(game1).data
serialized_game2 = GameSerializer(game2).data
serialized_game3 = GameSerializer(game3).data

self.check_equal(serialized_game1, response.data["results"][0])
self.check_equal(serialized_game2, response.data["results"][1])
self.check_equal(serialized_game3, response.data["results"][2])

def test_game_list2(self):
"""
Ensure we can get a list of game objects.
"""
url = reverse("api-game-list")

# create four game objects
game1 = GameFactory()
game2 = GameFactory()
game3 = GameFactory()
game4 = GameFactory()

# Get the game object list
url = reverse("api-game-list")
response = self.client.get(url, format="json")

# Check that the game object list was retrieved correctly
self.assertEqual(response.status_code, status.HTTP_200_OK)

serialized_game1 = GameSerializer(game1).data
serialized_game2 = GameSerializer(game2).data
serialized_game3 = GameSerializer(game3).data
serialized_game4 = GameSerializer(game4).data

# test that the game object list contains the two game objects we created
self.check_equal(serialized_game1, response.data["results"][0])
self.check_equal(serialized_game2, response.data["results"][1])
self.check_equal(serialized_game3, response.data["results"][2])
self.check_equal(serialized_game4, response.data["results"][3])

def test_update_game(self):
"""
Ensure we can update a game object.
Expand Down

0 comments on commit d0726f7

Please sign in to comment.