|
11 | 11 |
|
12 | 12 |
|
13 | 13 | class TestContributorSerializer(ModelSerializerTestCase[User, Contributor]):
|
14 |
| - """Test the functionality of the serializers""" |
| 14 | + """Test the Contributor serializers""" |
15 | 15 |
|
16 | 16 | model_serializer_class = ContributorSerializer
|
17 |
| - |
18 |
| - def setUp(self): |
19 |
| - """Set up data to be used for testing""" |
20 |
| - self.data1 = { |
21 |
| - "id": 1, |
22 |
| - |
23 |
| - "name": "Cont One", |
24 |
| - "location": "London", |
25 |
| - "html_url": "http://github.com/cont1", |
26 |
| - "avatar_url": "https://testcont.github.io/gravatar-url-generator/", |
27 |
| - } |
28 |
| - |
29 |
| - self.data2 = { |
30 |
| - "id": 2, |
31 |
| - |
32 |
| - "name": "Cont Two", |
33 |
| - "location": "London", |
34 |
| - "html_url": "http://github.com/cont2", |
35 |
| - "avatar_url": "https://cont2.github.io/gravatar-url-generator/", |
36 |
| - } |
37 |
| - |
38 |
| - def test_create(self): |
39 |
| - """Create a contributor""" |
40 |
| - self.assert_create(validated_data=self.data1, new_data=self.data1) |
41 |
| - |
42 |
| - def test_create_list(self): |
43 |
| - """List all contributor""" |
44 |
| - # Create multiple Contributors |
45 |
| - self.assert_create(validated_data=self.data1, new_data=self.data1) |
46 |
| - 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 |
| - |
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