In my project coach I have a problem with testing the Information serializer. I have the following serializer classes in the file running/serializes.py:
class Velocity(serializers.ModelSerializer):
class Meta:
model = VelocityModel
fields = ("id", "minimum", "average", "maximum")
class Information(serializers.ModelSerializer):
heart_beat = HeartBeat(read_only=True)
velocity = Velocity(read_only=True)
class Meta:
model = InformationModel
fields = ("id", "distance", "velocity", "heart_beat", "calories")
In my test I have this:
from running import models, serializers
@patch("running.serializers.Velocity")
def test_contains_id(self, mock_velocity):
# mocking stuff
returned_data = {}
mock_velocity.data = PropertyMock(return_value=returned_data)
# creating instances of the models
self.velocity = models.Velocity(minimum=8, average=10, maximum=12)
self.velocity.save()
self.heart_beat = models.HeartBeat(minimum=120, average=130, maximum=140)
self.heart_beat.save()
self.information = models.Information(distance=3.7, velocity=self.velocity, heart_beat=self.heart_beat, calories=132)
self.information.save()
# create the actual serializer
self.information_serializer = serializers.Information(self.information)
self.assertEqual(self.information_serializer.data["velocity"], returned_data)
So I want to test, that the data returned by the InformationSerializer (self.information_serializer.data), has a key "velocity", which points on the data returned by the VelocitySerializer (mock_velocity.data).
But self.information_serializer.data["velocity"] just contains the data, saved in the models (OrderedDict([('id', 1), ('minimum', 8.0), ('average', 10.0), ('maximum', 12.0)]). I don't know where is my fault...
Also another question would be, do I really need to test this? Because I'm questioning myself, if I'm testing more the Django Rest Framework than my serializers?!
So how to go on? Thanks in advance!
DELETE /running/run/1, no Run model with ID 1 should exist anymore." 2) and unit testing my own little classes/methods/properties like "self.assertEqual(person.name, "Peter")" What are you (@Taras) thinking of this?