From 276b70936c80ce302e4c626971cc669bff46ed7d Mon Sep 17 00:00:00 2001 From: SalmanAsh Date: Fri, 12 Jul 2024 14:01:40 +0000 Subject: [PATCH] Contributor tests --- api/serializers/contributor_test.py | 55 ++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/api/serializers/contributor_test.py b/api/serializers/contributor_test.py index e31299d..4795bd4 100644 --- a/api/serializers/contributor_test.py +++ b/api/serializers/contributor_test.py @@ -16,6 +16,7 @@ class TestContributorSerializer(ModelSerializerTestCase[User, Contributor]): model_serializer_class = ContributorSerializer def setUp(self): + """Set up data to be used for testing""" self.data1 = { "id": 1, "email": "cont1@gmail.com", @@ -40,6 +41,58 @@ def test_create(self): def test_create_list(self): """List all contributor""" - # expected = [self.data1, self.data2] + # Create multiple Contributors self.assert_create(validated_data=self.data1, new_data=self.data1) self.assert_create(validated_data=self.data2, new_data=self.data2) + + # Compare results + queryset = Contributor.objects.all() + assert len(queryset) == 2 + + def test_get_first(self): + """Retrieve the first object""" + # Create multiple Contributors + self.assert_create(validated_data=self.data1, new_data=self.data1) + self.assert_create(validated_data=self.data2, new_data=self.data2) + + # Retrieve the first and compare + cont = Contributor.objects.first() + serializer = ContributorSerializer(cont) + assert serializer.data["id"] == self.data1["id"] + + def test_get_any(self): + """ " Retrieve any object using its id""" + # Create multiple Contributors + self.assert_create(validated_data=self.data1, new_data=self.data1) + self.assert_create(validated_data=self.data2, new_data=self.data2) + + # Retrieve by id and compare + cont = Contributor.objects.get(id=2) + serializer = ContributorSerializer(cont) + assert serializer.data["id"] == self.data2["id"] + + def test_update(self): + """Updating a single contributor""" + # Create a new contributor + cont = Contributor.objects.create( + id=1, + email="cont1@gmail.com", + name="Cont One", + location="London", + html_url="http://github.com/cont1", + avatar_url="https://testcont.github.io/gravatar-url-generator/", + ) + + # Expected Results + new_data = {"email": "new_email@gmail.com", "name": "New Name"} + expected = { + "id": 1, + "email": "new_email@gmail.com", + "name": "New Name", + "location": "London", + "html_url": "http://github.com/cont1", + "avatar_url": "https://testcont.github.io/gravatar-url-generator/", + } + self.assert_update( + instance=cont, validated_data=new_data, new_data=expected + )