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

Added tests for getting game lists using serializers and the api-game… #336

Merged
merged 12 commits into from
Dec 7, 2023
Merged
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.
Copy link
Contributor

Choose a reason for hiding this comment

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

Absolutely fantastic code comment.
Clear, direct, justified, with an example to boot!

Thank you for your work


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