Skip to content

Commit 276b709

Browse files
committed
Contributor tests
1 parent c040e51 commit 276b709

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

api/serializers/contributor_test.py

+54-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class TestContributorSerializer(ModelSerializerTestCase[User, Contributor]):
1616
model_serializer_class = ContributorSerializer
1717

1818
def setUp(self):
19+
"""Set up data to be used for testing"""
1920
self.data1 = {
2021
"id": 1,
2122
"email": "[email protected]",
@@ -40,6 +41,58 @@ def test_create(self):
4041

4142
def test_create_list(self):
4243
"""List all contributor"""
43-
# expected = [self.data1, self.data2]
44+
# Create multiple Contributors
4445
self.assert_create(validated_data=self.data1, new_data=self.data1)
4546
self.assert_create(validated_data=self.data2, new_data=self.data2)
47+
48+
# Compare results
49+
queryset = Contributor.objects.all()
50+
assert len(queryset) == 2
51+
52+
def test_get_first(self):
53+
"""Retrieve the first object"""
54+
# Create multiple Contributors
55+
self.assert_create(validated_data=self.data1, new_data=self.data1)
56+
self.assert_create(validated_data=self.data2, new_data=self.data2)
57+
58+
# Retrieve the first and compare
59+
cont = Contributor.objects.first()
60+
serializer = ContributorSerializer(cont)
61+
assert serializer.data["id"] == self.data1["id"]
62+
63+
def test_get_any(self):
64+
""" " Retrieve any object using its id"""
65+
# Create multiple Contributors
66+
self.assert_create(validated_data=self.data1, new_data=self.data1)
67+
self.assert_create(validated_data=self.data2, new_data=self.data2)
68+
69+
# Retrieve by id and compare
70+
cont = Contributor.objects.get(id=2)
71+
serializer = ContributorSerializer(cont)
72+
assert serializer.data["id"] == self.data2["id"]
73+
74+
def test_update(self):
75+
"""Updating a single contributor"""
76+
# Create a new contributor
77+
cont = Contributor.objects.create(
78+
id=1,
79+
80+
name="Cont One",
81+
location="London",
82+
html_url="http://github.com/cont1",
83+
avatar_url="https://testcont.github.io/gravatar-url-generator/",
84+
)
85+
86+
# Expected Results
87+
new_data = {"email": "[email protected]", "name": "New Name"}
88+
expected = {
89+
"id": 1,
90+
"email": "[email protected]",
91+
"name": "New Name",
92+
"location": "London",
93+
"html_url": "http://github.com/cont1",
94+
"avatar_url": "https://testcont.github.io/gravatar-url-generator/",
95+
}
96+
self.assert_update(
97+
instance=cont, validated_data=new_data, new_data=expected
98+
)

0 commit comments

Comments
 (0)