7

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!

3
  • 1
    You do not have to test every ordinary serializer, I would rather test the whole API endpoint which can be achieved with self.client.[HTTP_METHOD](url, data=data) method. Check this out: docs.djangoproject.com/en/1.10/topics/testing/tools Commented Jan 19, 2017 at 18:49
  • Thanks for your thought. So I'm currently thinking of this approach: 1) Testing the api like this:"After making the request 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? Commented Jan 19, 2017 at 19:24
  • 1
    Yeap, that sounds much better. Please keep in mind, you are not testing Django or DRF, so if your CRUD actions are default one - no need to test the as well :) Only your custom and business logic have to be tested. Commented Jan 20, 2017 at 10:04

1 Answer 1

11

Just test your business logic. I know I was testing model serializers myself but in truth we don't need to. Django has tested them already.

Just do an integration test in the view to see that your CRUD operations are working correctly.

Sign up to request clarification or add additional context in comments.

2 Comments

Isn't it important that you test that your models are defined the way you want them to be defined? That you can join foreign keys and that kind of stuff? Not that Django might break down, but rather that you understood Django properly and wrote the right code so that joins occur correctly and so on.
Yes, i think so, but the test coverage require me to write test cases for all functions in all classes 🤦‍♂️

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.