55from constance .test import override_config
66from django .contrib .contenttypes .models import ContentType
77
8+ from app .models import ElectionDistrict
89from app .models import Municipality
910from app .models import Person
1011from app .serializers .person import PersonMappingSerializer
1112from app .tests import APITestCase
1213from app .tests .custom_fields .factories import CustomFieldFactory
1314from app .tests .custom_fields .factories import CustomValueFactory
1415from app .tests .factories import AddressFactory
16+ from app .tests .factories import ElectionDistrictFactory
1517from app .tests .factories import PersonFactory
1618from django_features .custom_fields .models import CustomField
19+ from django_features .custom_fields .models import CustomValue
1720
1821
1922MODEL_MAPPING_FIELD = {
3033 "external_choice_field" : "choice_value" ,
3134 "external_multiple_choice_field" : "multiple_choice_value" ,
3235 "external_municipality_title" : "place_of_residence.title" ,
36+ "external_election_district_title" : "election_district" ,
3337 "external_addresses" : "addresses" ,
3438 }
3539}
@@ -42,66 +46,68 @@ class MappingSerializerTestCase(APITestCase):
4246 def setUp (self ) -> None :
4347 self .person_ct = ContentType .objects .get_for_model (Person )
4448
45- self .char_field = CustomFieldFactory (
49+ self .char_field : CustomField = CustomFieldFactory ( # type: ignore
4650 identifier = "char_value" ,
4751 content_type = self .person_ct ,
4852 field_type = CustomField .FIELD_TYPES .CHAR ,
4953 )
50- self .text_field = CustomFieldFactory (
54+ self .text_field : CustomField = CustomFieldFactory ( # type: ignore
5155 identifier = "text_value" ,
5256 content_type = self .person_ct ,
5357 field_type = CustomField .FIELD_TYPES .TEXT ,
5458 )
55- self .date_field = CustomFieldFactory (
59+ self .date_field : CustomField = CustomFieldFactory ( # type: ignore
5660 identifier = "date_value" ,
5761 content_type = self .person_ct ,
5862 field_type = CustomField .FIELD_TYPES .DATE ,
5963 )
60- self .datetime_field = CustomFieldFactory (
64+ self .datetime_field : CustomField = CustomFieldFactory ( # type: ignore
6165 identifier = "datetime_value" ,
6266 content_type = self .person_ct ,
6367 field_type = CustomField .FIELD_TYPES .DATETIME ,
6468 )
65- self .integer_field = CustomFieldFactory (
69+ self .integer_field : CustomField = CustomFieldFactory ( # type: ignore
6670 identifier = "integer_value" ,
6771 content_type = self .person_ct ,
6872 field_type = CustomField .FIELD_TYPES .INTEGER ,
6973 )
70- self .boolean_field = CustomFieldFactory (
74+ self .boolean_field : CustomField = CustomFieldFactory ( # type: ignore
7175 identifier = "boolean_value" ,
7276 content_type = self .person_ct ,
7377 field_type = CustomField .FIELD_TYPES .BOOLEAN ,
7478 )
75- self .multiple_date_field = CustomFieldFactory (
79+ self .multiple_date_field : CustomField = CustomFieldFactory ( # type: ignore
7680 identifier = "multiple_date_value" ,
7781 content_type = self .person_ct ,
7882 field_type = CustomField .FIELD_TYPES .DATE ,
7983 multiple = True ,
8084 )
81- self .choice_field = CustomFieldFactory (
85+ self .choice_field : CustomField = CustomFieldFactory ( # type: ignore
8286 identifier = "choice_value" ,
8387 content_type = self .person_ct ,
8488 field_type = CustomField .FIELD_TYPES .DATE ,
8589 choice_field = True ,
8690 )
87- self .multiple_choice_field = CustomFieldFactory (
91+ self .multiple_choice_field : CustomField = CustomFieldFactory ( # type: ignore
8892 identifier = "multiple_choice_value" ,
8993 content_type = self .person_ct ,
9094 field_type = CustomField .FIELD_TYPES .DATE ,
9195 choice_field = True ,
9296 multiple = True ,
9397 )
9498
95- self .choice_1 = CustomValueFactory (field = self .choice_field , value = "2000-01-01" )
96- self .choice_2 = CustomValueFactory (field = self .choice_field , value = "2001-01-01" )
99+ self .choice_1 : CustomValue = CustomValueFactory (field = self .choice_field , value = "2000-01-01" ) # type: ignore
100+ self .choice_2 : CustomValue = CustomValueFactory ( # type: ignore
101+ field = self .choice_field , value = "2001-01-01"
102+ )
97103
98- self .multiple_choice_1 = CustomValueFactory (
104+ self .multiple_choice_1 : CustomValue = CustomValueFactory ( # type: ignore
99105 field = self .multiple_choice_field , value = "2000-01-01"
100106 )
101- self .multiple_choice_2 = CustomValueFactory (
107+ self .multiple_choice_2 : CustomValue = CustomValueFactory ( # type: ignore
102108 field = self .multiple_choice_field , value = "2001-01-01"
103109 )
104- self .multiple_choice_3 = CustomValueFactory (
110+ self .multiple_choice_3 : CustomValue = CustomValueFactory ( # type: ignore
105111 field = self .multiple_choice_field , value = "2002-01-01"
106112 )
107113
@@ -111,6 +117,8 @@ def setUp(self) -> None:
111117
112118 @override_config (MODEL_MAPPING_FIELD = MODEL_MAPPING_FIELD )
113119 def test_mapping_serializer_create (self ) -> None :
120+ election_district = ElectionDistrictFactory (title = "Koeniz" )
121+
114122 data = {
115123 "external_firstname" : "Hugo" ,
116124 "external_lastname" : "Boss" ,
@@ -124,6 +132,7 @@ def test_mapping_serializer_create(self) -> None:
124132 "external_choice_field" : "2000-01-01" ,
125133 "external_multiple_choice_field" : ["2000-01-01" , "2001-01-01" ],
126134 "external_municipality_title" : "Muri" ,
135+ "external_election_district_title" : "Koeniz" ,
127136 "external_addresses" : [
128137 self .address_1 .external_uid ,
129138 self .address_2 .external_uid ,
@@ -164,13 +173,21 @@ def test_mapping_serializer_create(self) -> None:
164173
165174 self .assertEqual ("Muri" , instance .place_of_residence .title )
166175 self .assertEqual (1 , Municipality .objects .count ())
176+
177+ self .assertEqual (election_district , instance .election_district )
178+ self .assertEqual (1 , ElectionDistrict .objects .count ())
179+
167180 self .assertEqual (2 , instance .addresses .count ())
168181 self .assertEqual (instance , instance .addresses .first ().target )
169182 self .assertEqual (instance , instance .addresses .last ().target )
170183
171184 @override_config (MODEL_MAPPING_FIELD = MODEL_MAPPING_FIELD )
172185 def test_mapping_serializer_update (self ) -> None :
173- person : Person = PersonFactory (firstname = "old" , lastname = "old" ) # type: ignore
186+ old_election_district = ElectionDistrictFactory (title = "Old" )
187+ election_district = ElectionDistrictFactory (title = "Koeniz" )
188+ person : Person = PersonFactory ( # type: ignore
189+ firstname = "old" , lastname = "old" , election_district = old_election_district
190+ )
174191
175192 person .refresh_with_custom_fields ()
176193
@@ -187,6 +204,7 @@ def test_mapping_serializer_update(self) -> None:
187204 "external_choice_field" : "2001-01-01" ,
188205 "external_multiple_choice_field" : ["2000-01-01" , "2002-01-01" ],
189206 "external_municipality_title" : "Koeniz" ,
207+ "external_election_district_title" : "Koeniz" ,
190208 "external_addresses" : [
191209 self .address_1 .external_uid ,
192210 self .address_3 .external_uid ,
@@ -227,6 +245,10 @@ def test_mapping_serializer_update(self) -> None:
227245
228246 self .assertEqual ("Koeniz" , instance .place_of_residence .title )
229247 self .assertEqual (1 , Municipality .objects .count ())
248+
249+ self .assertEqual (election_district , instance .election_district )
250+ self .assertEqual (2 , ElectionDistrict .objects .count ())
251+
230252 self .assertEqual (2 , instance .addresses .count ())
231253 self .assertEqual (person , instance .addresses .first ().target )
232254 self .assertEqual (person , instance .addresses .last ().target )
0 commit comments