|
1 import logging |
|
2 |
|
3 from django.contrib.auth import get_user_model |
|
4 from django.test import TestCase |
|
5 from protocols.models import (Metacategory, MetacategoryRevision, Protocol, |
|
6 ProtocolRevision) |
|
7 from protocols.serializers import (MetacategoryRevisionSerializer, |
|
8 ProtocolRevisionSerializer) |
|
9 |
|
10 # Create your tests here. |
|
11 |
|
12 logger = logging.getLogger(__name__) |
|
13 |
|
14 class SerializerTest(TestCase): |
|
15 |
|
16 maxDiff=None |
|
17 |
|
18 def setUp(self): |
|
19 User = get_user_model() |
|
20 self.user1 = User.objects.create_user( |
|
21 username='user1', |
|
22 email='user1@email.com', |
|
23 password='hiddenpassword' |
|
24 ) |
|
25 |
|
26 def test_protocol_simple_serialization(self): |
|
27 protocol = Protocol.objects.create(app=self.user1, owner='admin') |
|
28 protocol_revision = ProtocolRevision.objects.create(title="protocol1", description="Protocol nº1", protocol=protocol) |
|
29 serializer = ProtocolRevisionSerializer(protocol_revision) |
|
30 |
|
31 expected_data = { |
|
32 'id': str(protocol.ext_id), |
|
33 'version': 1, |
|
34 'title': 'protocol1', |
|
35 'description': 'Protocol nº1', |
|
36 'owner' : 'admin', |
|
37 'metacategories' : [] |
|
38 } |
|
39 |
|
40 self.assertDictEqual(expected_data, serializer.data) |
|
41 |
|
42 |
|
43 def test_protocol_complete_serialization(self): |
|
44 protocol = Protocol.objects.create(app=self.user1, owner='admin') |
|
45 protocol_revision = ProtocolRevision.objects.create(title="protocol2", description="Protocol nº2", protocol=protocol) |
|
46 |
|
47 metacategory1 = Metacategory.objects.create( |
|
48 app=self.user1, |
|
49 title="Important base", |
|
50 label="important-base", |
|
51 description="Important base.", |
|
52 color="#F1C41F", |
|
53 has_comment=False, |
|
54 is_default=False |
|
55 ) |
|
56 metacategory_revision1 = MetacategoryRevision.objects.create( |
|
57 base=metacategory1, |
|
58 title="Important", |
|
59 label="important", |
|
60 description="Important.", |
|
61 color="#F1C40F", |
|
62 has_comment=False |
|
63 ) |
|
64 |
|
65 metacategory2 = Metacategory.objects.create( |
|
66 app=self.user1, |
|
67 title="Mot-clé base", |
|
68 label="mot-cle-base", |
|
69 description="Mot-clé base.", |
|
70 color="#2ECC72", |
|
71 has_comment=False, |
|
72 is_default=False |
|
73 ) |
|
74 metacategory_revision2 = MetacategoryRevision.objects.create( |
|
75 base=metacategory2, |
|
76 title="Mot-clé", |
|
77 label="mot-cle", |
|
78 description="Mot-clé.", |
|
79 color="#2ECC71", |
|
80 has_comment=False |
|
81 ) |
|
82 |
|
83 metacategory3 = Metacategory.objects.create( |
|
84 app=self.user1, |
|
85 title="Commentaire base", |
|
86 label="commentaire-base", |
|
87 description="Commentaire base.", |
|
88 color="#3498DC", |
|
89 has_comment=False, |
|
90 is_default=False |
|
91 ) |
|
92 metacategory_revision3 = MetacategoryRevision.objects.create( |
|
93 base=metacategory3, |
|
94 title="Commentaire", |
|
95 label="commentaire", |
|
96 description="Commentaire.", |
|
97 color="#3498DB", |
|
98 has_comment=True |
|
99 ) |
|
100 |
|
101 protocol_revision.metacategories.add(metacategory_revision1, metacategory_revision2, metacategory_revision3) |
|
102 |
|
103 serializer = ProtocolRevisionSerializer(protocol_revision) |
|
104 |
|
105 expected_data = { |
|
106 'id': str(protocol.ext_id), |
|
107 'owner': 'admin', |
|
108 'version': 1, |
|
109 'title': 'protocol2', |
|
110 'description': 'Protocol nº2', |
|
111 'metacategories' : [{ |
|
112 'id': str(metacategory_revision1.ext_id), |
|
113 'base': str(metacategory1.ext_id), |
|
114 'title': 'Important', |
|
115 'label': 'important', |
|
116 'description': "Important.", |
|
117 'color': '#F1C40F', |
|
118 'version': 1, |
|
119 'has_comment': False |
|
120 }, { |
|
121 'id': str(metacategory_revision2.ext_id), |
|
122 'base': str(metacategory2.ext_id), |
|
123 'title': 'Mot-clé', |
|
124 'label': 'mot-cle', |
|
125 'description': "Mot-clé.", |
|
126 'color': '#2ECC71', |
|
127 'version': 1, |
|
128 'has_comment': False |
|
129 }, { |
|
130 'id': str(metacategory_revision3.ext_id), |
|
131 'base': str(metacategory3.ext_id), |
|
132 'title': 'Commentaire', |
|
133 'label': 'commentaire', |
|
134 'description': "Commentaire.", |
|
135 'color': '#3498DB', |
|
136 'version': 1, |
|
137 'has_comment': True |
|
138 }] |
|
139 } |
|
140 |
|
141 self.assertDictEqual(expected_data, serializer.data) |
|
142 |
|
143 |
|
144 def test_metacategory_simple_serialization(self): |
|
145 metacategory = Metacategory.objects.create( |
|
146 app=self.user1, |
|
147 title="Important", |
|
148 label="important", |
|
149 description="Important.", |
|
150 color="#F1C40F", |
|
151 has_comment=False, |
|
152 is_default=False |
|
153 ) |
|
154 metacategory_revision = MetacategoryRevision.objects.create( |
|
155 base=metacategory, |
|
156 title="Important", |
|
157 label="important", |
|
158 description="Important.", |
|
159 color="#F1C40F", |
|
160 has_comment=False |
|
161 ) |
|
162 serializer = MetacategoryRevisionSerializer(metacategory_revision) |
|
163 |
|
164 expected_data = { |
|
165 'id': str(metacategory_revision.ext_id), |
|
166 'base': str(metacategory.ext_id), |
|
167 'title': 'Important', |
|
168 'label': 'important', |
|
169 'description': "Important.", |
|
170 'color': '#F1C40F', |
|
171 'version': 1, |
|
172 'has_comment': False |
|
173 } |
|
174 |
|
175 self.assertDictEqual(expected_data, serializer.data) |
|
176 |