1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
from codicefiscale import codicefiscale |
4
|
|
|
from codicefiscale.version import __version__ |
5
|
|
|
from datetime import datetime |
6
|
|
|
import re |
7
|
|
|
import unittest |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class CodiceFiscaleTestCase(unittest.TestCase): |
11
|
|
|
|
12
|
|
|
def test_encode_surname(self): |
13
|
|
|
|
14
|
|
|
data = [ |
15
|
|
|
{ |
16
|
|
|
'input':'', |
17
|
|
|
'result':'XXX', |
18
|
|
|
}, |
19
|
|
|
{ |
20
|
|
|
'input':'Caccamo', |
21
|
|
|
'result':'CCC', |
22
|
|
|
}, |
23
|
|
|
{ |
24
|
|
|
'input':'Fò', |
25
|
|
|
'result':'FOX', |
26
|
|
|
} |
27
|
|
|
] |
28
|
|
|
|
29
|
|
|
for obj in data: |
30
|
|
|
# with self.subTest(obj=obj): |
31
|
|
|
self.assertEqual( |
32
|
|
|
codicefiscale.encode_surname(obj['input']), |
33
|
|
|
obj['result']) |
34
|
|
|
|
35
|
|
|
def test_encode_name(self): |
36
|
|
|
|
37
|
|
|
data = [ |
38
|
|
|
{ |
39
|
|
|
'input':'', |
40
|
|
|
'result':'XXX', |
41
|
|
|
}, |
42
|
|
|
{ |
43
|
|
|
'input':'Alessandro', |
44
|
|
|
'result':'LSN', |
45
|
|
|
}, |
46
|
|
|
{ |
47
|
|
|
'input':'Dario', |
48
|
|
|
'result':'DRA', |
49
|
|
|
}, |
50
|
|
|
{ |
51
|
|
|
'input':'Fabio', |
52
|
|
|
'result':'FBA', |
53
|
|
|
}, |
54
|
|
|
{ |
55
|
|
|
'input':'Giovanni', |
56
|
|
|
'result':'GNN', |
57
|
|
|
}, |
58
|
|
|
{ |
59
|
|
|
'input':'Hu', |
60
|
|
|
'result':'HUX', |
61
|
|
|
}, |
62
|
|
|
{ |
63
|
|
|
'input':'Maria', |
64
|
|
|
'result':'MRA', |
65
|
|
|
}, |
66
|
|
|
{ |
67
|
|
|
'input':'Michele', |
68
|
|
|
'result':'MHL', |
69
|
|
|
} |
70
|
|
|
] |
71
|
|
|
|
72
|
|
|
for obj in data: |
73
|
|
|
# with self.subTest(obj=obj): |
74
|
|
|
self.assertEqual( |
75
|
|
|
codicefiscale.encode_name(obj['input']), |
76
|
|
|
obj['result']) |
77
|
|
|
|
78
|
|
|
def test_encode_birthdate_formats(self): |
79
|
|
|
|
80
|
|
|
data = [ |
81
|
|
|
{ |
82
|
|
|
'input':datetime(1985, 4, 3), |
83
|
|
|
'result':'85D03', |
84
|
|
|
}, |
85
|
|
|
{ |
86
|
|
|
'input':'03 04 1985', |
87
|
|
|
'result':'85D03', |
88
|
|
|
}, |
89
|
|
|
{ |
90
|
|
|
'input':'03/04/1985', |
91
|
|
|
'result':'85D03', |
92
|
|
|
}, |
93
|
|
|
{ |
94
|
|
|
'input':'03-04-1985', |
95
|
|
|
'result':'85D03', |
96
|
|
|
}, |
97
|
|
|
{ |
98
|
|
|
'input':'03.04.1985', |
99
|
|
|
'result':'85D03', |
100
|
|
|
}, |
101
|
|
|
{ |
102
|
|
|
'input':'3/4/1985', |
103
|
|
|
'result':'85D03', |
104
|
|
|
}, |
105
|
|
|
{ |
106
|
|
|
'input':'3-4-1985', |
107
|
|
|
'result':'85D03', |
108
|
|
|
}, |
109
|
|
|
{ |
110
|
|
|
'input':'3.4.1985', |
111
|
|
|
'result':'85D03', |
112
|
|
|
}, |
113
|
|
|
{ |
114
|
|
|
'input':'1985 04 03', |
115
|
|
|
'result':'85D03', |
116
|
|
|
}, |
117
|
|
|
{ |
118
|
|
|
'input':'1985/04/03', |
119
|
|
|
'result':'85D03', |
120
|
|
|
}, |
121
|
|
|
{ |
122
|
|
|
'input':'1985-04-03', |
123
|
|
|
'result':'85D03', |
124
|
|
|
}, |
125
|
|
|
{ |
126
|
|
|
'input':'1985.04.03', |
127
|
|
|
'result':'85D03', |
128
|
|
|
}, |
129
|
|
|
{ |
130
|
|
|
'input':'1985/4/3', |
131
|
|
|
'result':'85D03', |
132
|
|
|
}, |
133
|
|
|
{ |
134
|
|
|
'input':'1985-4-3', |
135
|
|
|
'result':'85D03', |
136
|
|
|
}, |
137
|
|
|
{ |
138
|
|
|
'input':'1985.4.3', |
139
|
|
|
'result':'85D03', |
140
|
|
|
}, |
141
|
|
|
] |
142
|
|
|
|
143
|
|
|
for obj in data: |
144
|
|
|
# with self.subTest(obj=obj): |
145
|
|
|
self.assertEqual( |
146
|
|
|
codicefiscale.encode_birthdate(obj['input'], 'M'), |
147
|
|
|
obj['result']) |
148
|
|
|
|
149
|
|
|
def test_encode_birthdate_invalid_arguments(self): |
150
|
|
|
|
151
|
|
|
with self.assertRaises(ValueError): |
152
|
|
|
codicefiscale.encode_birthdate(None, 'M') |
153
|
|
|
|
154
|
|
|
with self.assertRaises(ValueError): |
155
|
|
|
codicefiscale.encode_birthdate('03/04/1985', None) |
156
|
|
|
|
157
|
|
|
with self.assertRaises(ValueError): |
158
|
|
|
codicefiscale.encode_birthdate('03/04/1985', 'X') |
159
|
|
|
|
160
|
|
|
with self.assertRaises(ValueError): |
161
|
|
|
codicefiscale.encode_birthdate('1985/1985/1985', 'M') |
162
|
|
|
|
163
|
|
|
def test_encode_birthdate_sex(self): |
164
|
|
|
|
165
|
|
|
data = [ |
166
|
|
|
{ |
167
|
|
|
'input':['03/04/1985', 'M'], |
168
|
|
|
'result':'85D03', |
169
|
|
|
}, |
170
|
|
|
{ |
171
|
|
|
'input':['03/04/1985', 'F'], |
172
|
|
|
'result':'85D43', |
173
|
|
|
}, |
174
|
|
|
] |
175
|
|
|
|
176
|
|
|
for obj in data: |
177
|
|
|
# with self.subTest(obj=obj): |
178
|
|
|
self.assertEqual( |
179
|
|
|
codicefiscale.encode_birthdate(*obj['input']), |
180
|
|
|
obj['result']) |
181
|
|
|
|
182
|
|
|
def test_encode_birthplace_italy(self): |
183
|
|
|
|
184
|
|
|
data = [ |
185
|
|
|
{ |
186
|
|
|
'input':'Torino, Italy', |
187
|
|
|
'result':'L219', |
188
|
|
|
}, |
189
|
|
|
{ |
190
|
|
|
'input':'Torino (TO), Italy', |
191
|
|
|
'result':'L219', |
192
|
|
|
}, |
193
|
|
|
{ |
194
|
|
|
'input':'Torino (TO)', |
195
|
|
|
'result':'L219', |
196
|
|
|
}, |
197
|
|
|
{ |
198
|
|
|
'input':'Torino', |
199
|
|
|
'result':'L219', |
200
|
|
|
}, |
201
|
|
|
{ |
202
|
|
|
'input':'L219', |
203
|
|
|
'result':'L219', |
204
|
|
|
}, |
205
|
|
|
] |
206
|
|
|
|
207
|
|
|
for obj in data: |
208
|
|
|
# with self.subTest(obj=obj): |
209
|
|
|
self.assertEqual( |
210
|
|
|
codicefiscale.encode_birthplace(obj['input']), |
211
|
|
|
obj['result']) |
212
|
|
|
|
213
|
|
|
def test_encode_birthplace_foreign_country(self): |
214
|
|
|
|
215
|
|
|
data = [ |
216
|
|
|
{ |
217
|
|
|
'input':'Lettonia', |
218
|
|
|
'result':'Z145', |
219
|
|
|
}, |
220
|
|
|
{ |
221
|
|
|
'input':'Giappone', |
222
|
|
|
'result':'Z219', |
223
|
|
|
}, |
224
|
|
|
{ |
225
|
|
|
'input':'Marocco', |
226
|
|
|
'result':'Z330', |
227
|
|
|
}, |
228
|
|
|
] |
229
|
|
|
|
230
|
|
|
for obj in data: |
231
|
|
|
# with self.subTest(obj=obj): |
232
|
|
|
self.assertEqual( |
233
|
|
|
codicefiscale.encode_birthplace(obj['input']), |
234
|
|
|
obj['result']) |
235
|
|
|
|
236
|
|
|
def test_encode_birthplace_invalid_arguments(self): |
237
|
|
|
|
238
|
|
|
with self.assertRaises(ValueError): |
239
|
|
|
codicefiscale.encode_birthplace(None) |
240
|
|
|
|
241
|
|
|
with self.assertRaises(ValueError): |
242
|
|
|
codicefiscale.encode_birthplace('Area 51') |
243
|
|
|
|
244
|
|
|
def test_encode_cin(self): |
245
|
|
|
|
246
|
|
|
data = [ |
247
|
|
|
{ |
248
|
|
|
'input':'CCCFBA85D03L219', |
249
|
|
|
'result':'P', |
250
|
|
|
}, |
251
|
|
|
] |
252
|
|
|
|
253
|
|
|
for obj in data: |
254
|
|
|
# with self.subTest(obj=obj): |
255
|
|
|
self.assertEqual( |
256
|
|
|
codicefiscale.encode_cin(obj['input']), |
257
|
|
|
obj['result']) |
258
|
|
|
|
259
|
|
|
def test_encode_cin_invalid_arguments(self): |
260
|
|
|
|
261
|
|
|
with self.assertRaises(ValueError): |
262
|
|
|
codicefiscale.encode_cin(None) |
263
|
|
|
|
264
|
|
|
with self.assertRaises(ValueError): |
265
|
|
|
codicefiscale.encode_cin('CCCFBA85D03') |
266
|
|
|
|
267
|
|
|
def test_encode(self): |
268
|
|
|
|
269
|
|
|
data = [ |
270
|
|
|
{ |
271
|
|
|
'input': { 'surname':'Ait Hadda', 'name':'Saad', 'sex':'M', 'birthdate':'08/09/1995', 'birthplace':'Marocco' }, |
272
|
|
|
'result':'THDSDA95P08Z330H', |
273
|
|
|
}, |
274
|
|
|
{ |
275
|
|
|
'input': { 'surname':'Belousovs', 'name':'Olegs', 'sex':'M', 'birthdate':'22/03/1984', 'birthplace':'Lettonia' }, |
276
|
|
|
'result':'BLSLGS84C22Z145O', |
277
|
|
|
}, |
278
|
|
|
{ |
279
|
|
|
'input': { 'surname':'Bruno', 'name':'Giovanni', 'sex':'M', 'birthdate':'26/02/1971', 'birthplace':'Torino' }, |
280
|
|
|
'result':'BRNGNN71B26L219T', |
281
|
|
|
}, |
282
|
|
|
{ |
283
|
|
|
'input': { 'surname':'Caccamo', 'name':'Fabio', 'sex':'M', 'birthdate':'03/04/1985', 'birthplace':'Torino' }, |
284
|
|
|
'result':'CCCFBA85D03L219P', |
285
|
|
|
}, |
286
|
|
|
{ |
287
|
|
|
'input': { 'surname':'Gomba', 'name':'Alessandro', 'sex':'M', 'birthdate':'05/01/1984', 'birthplace':'Pinerolo' }, |
288
|
|
|
'result':'GMBLSN84A05G674H', |
289
|
|
|
}, |
290
|
|
|
{ |
291
|
|
|
'input': { 'surname':'Martini', 'name':'Maria', 'sex':'F', 'birthdate':'16/12/1983', 'birthplace':'Anagni' }, |
292
|
|
|
'result':'MRTMRA83T56A269B', |
293
|
|
|
}, |
294
|
|
|
{ |
295
|
|
|
'input': { 'surname':'Panella', 'name':'Michele', 'sex':'M', 'birthdate':'27/10/1979', 'birthplace':'San Severo (FG)' }, |
296
|
|
|
'result':'PNLMHL79R27I158P', |
297
|
|
|
}, |
298
|
|
|
{ |
299
|
|
|
'input': { 'surname':'Quatrini', 'name':'Dario', 'sex':'M', 'birthdate':'13/09/1971', 'birthplace':'Pavia' }, |
300
|
|
|
'result':'QTRDRA71P13G388J', |
301
|
|
|
}, |
302
|
|
|
{ |
303
|
|
|
'input': { 'surname':'Takakura', 'name':'Yuuki', 'sex':'F', 'birthdate':'28/02/1987', 'birthplace':'Torino' }, |
304
|
|
|
'result':'TKKYKU87B68L219F', |
305
|
|
|
}, |
306
|
|
|
] |
307
|
|
|
|
308
|
|
|
for obj in data: |
309
|
|
|
# with self.subTest(obj=obj): |
310
|
|
|
code = codicefiscale.encode(**obj['input']) |
311
|
|
|
self.assertEqual(code, obj['result']) |
312
|
|
|
|
313
|
|
|
def test_decode(self): |
314
|
|
|
|
315
|
|
|
data = [ |
316
|
|
|
{ |
317
|
|
|
'input':'THDSDA95P08Z330H', |
318
|
|
|
'result': { 'sex':'M', 'birthdate':'08/09/1995', 'birthplace':'Marocco' }, |
319
|
|
|
}, |
320
|
|
|
{ |
321
|
|
|
'input':'BLSLGS84C22Z145O', |
322
|
|
|
'result': { 'sex':'M', 'birthdate':'22/03/1984', 'birthplace':'Lettonia' }, |
323
|
|
|
}, |
324
|
|
|
{ |
325
|
|
|
'input':'BRNGNN71B26L219T', |
326
|
|
|
'result': { 'sex':'M', 'birthdate':'26/02/1971', 'birthplace':'Torino' }, |
327
|
|
|
}, |
328
|
|
|
{ |
329
|
|
|
'input':'CCCFBA85D03L219P', |
330
|
|
|
'result': { 'sex':'M', 'birthdate':'03/04/1985', 'birthplace':'Torino' }, |
331
|
|
|
}, |
332
|
|
|
{ |
333
|
|
|
'input':'GMBLSN84A05G674H', |
334
|
|
|
'result': { 'sex':'M', 'birthdate':'05/01/1984', 'birthplace':'Pinerolo' }, |
335
|
|
|
}, |
336
|
|
|
{ |
337
|
|
|
'input':'MRTMRA83T56A269B', |
338
|
|
|
'result': { 'sex':'F', 'birthdate':'16/12/1983', 'birthplace':'Anagni' }, |
339
|
|
|
}, |
340
|
|
|
{ |
341
|
|
|
'input':'PNLMHL79R27I158P', |
342
|
|
|
'result': { 'sex':'M', 'birthdate':'27/10/1979', 'birthplace':'San Severo' }, |
343
|
|
|
}, |
344
|
|
|
{ |
345
|
|
|
'input':'QTRDRA71P13G388J', |
346
|
|
|
'result': { 'sex':'M', 'birthdate':'13/09/1971', 'birthplace':'Pavia' }, |
347
|
|
|
}, |
348
|
|
|
{ |
349
|
|
|
'input':'TKKYKU87B68L219F', |
350
|
|
|
'result': { 'sex':'F', 'birthdate':'28/02/1987', 'birthplace':'Torino' }, |
351
|
|
|
}, |
352
|
|
|
{ |
353
|
|
|
'input':'RSSMRA68A01H501Y', |
354
|
|
|
'result': { 'sex':'M', 'birthdate':'01/01/1968', 'birthplace':'Roma' }, |
355
|
|
|
}, |
356
|
|
|
] |
357
|
|
|
|
358
|
|
|
for obj in data: |
359
|
|
|
# with self.subTest(obj=obj): |
360
|
|
|
|
361
|
|
|
result = obj['result'] |
362
|
|
|
obj_decoded = codicefiscale.decode(obj['input']) |
363
|
|
|
# print(obj_decoded) |
364
|
|
|
|
365
|
|
|
sex = obj_decoded.get('sex') |
366
|
|
|
self.assertFalse(sex is None) |
367
|
|
|
self.assertEqual(sex, result['sex']) |
368
|
|
|
|
369
|
|
|
birthdate = obj_decoded.get('birthdate') |
370
|
|
|
self.assertFalse(birthdate is None) |
371
|
|
|
self.assertEqual(birthdate, |
372
|
|
|
datetime.strptime(result['birthdate'], '%d/%m/%Y')) |
373
|
|
|
|
374
|
|
|
birthplace = obj_decoded.get('birthplace') |
375
|
|
|
self.assertFalse(birthplace is None) |
376
|
|
|
self.assertEqual(birthplace['name'].upper(), |
377
|
|
|
result['birthplace'].upper()) |
378
|
|
|
|
379
|
|
|
def test_decode_invalid_syntax(self): |
380
|
|
|
|
381
|
|
|
# invalid surname |
382
|
|
|
with self.assertRaises(ValueError): |
383
|
|
|
codicefiscale.decode('CC0FBA85X03L219P') |
384
|
|
|
|
385
|
|
|
# invalid name |
386
|
|
|
with self.assertRaises(ValueError): |
387
|
|
|
codicefiscale.decode('CCCFB085X03L219P') |
388
|
|
|
|
389
|
|
|
# invalid date-year |
390
|
|
|
with self.assertRaises(ValueError): |
391
|
|
|
codicefiscale.decode('CCCFBA8XD03L219S') |
392
|
|
|
|
393
|
|
|
# invalid date-month |
394
|
|
|
with self.assertRaises(ValueError): |
395
|
|
|
codicefiscale.decode('CCCFBA85X03L219P') |
396
|
|
|
|
397
|
|
|
# invalid date-day |
398
|
|
|
with self.assertRaises(ValueError): |
399
|
|
|
codicefiscale.decode('CCCFBA85D00L219P') |
400
|
|
|
|
401
|
|
|
def test_decode_omocodia(self): |
402
|
|
|
data = [ |
403
|
|
|
{ |
404
|
|
|
'input':'CCCFBA85D03L219P', |
405
|
|
|
'result': { 'sex':'M', 'birthdate':'03/04/1985', 'birthplace':'Torino' }, |
406
|
|
|
}, |
407
|
|
|
{ |
408
|
|
|
'input':'CCCFBA85D03L21VE', |
409
|
|
|
'result': { 'sex':'M', 'birthdate':'03/04/1985', 'birthplace':'Torino' }, |
410
|
|
|
}, |
411
|
|
|
{ |
412
|
|
|
'input':'CCCFBA85D03L2MVP', |
413
|
|
|
'result': { 'sex':'M', 'birthdate':'03/04/1985', 'birthplace':'Torino' }, |
414
|
|
|
}, |
415
|
|
|
{ |
416
|
|
|
'input':'CCCFBA85D03LNMVE', |
417
|
|
|
'result': { 'sex':'M', 'birthdate':'03/04/1985', 'birthplace':'Torino' }, |
418
|
|
|
}, |
419
|
|
|
{ |
420
|
|
|
'input':'CCCFBA85D0PLNMVA', |
421
|
|
|
'result': { 'sex':'M', 'birthdate':'03/04/1985', 'birthplace':'Torino' }, |
422
|
|
|
}, |
423
|
|
|
{ |
424
|
|
|
'input':'CCCFBA85DLPLNMVL', |
425
|
|
|
'result': { 'sex':'M', 'birthdate':'03/04/1985', 'birthplace':'Torino' }, |
426
|
|
|
}, |
427
|
|
|
{ |
428
|
|
|
'input':'CCCFBA8RDLPLNMVX', |
429
|
|
|
'result': { 'sex':'M', 'birthdate':'03/04/1985', 'birthplace':'Torino' }, |
430
|
|
|
}, |
431
|
|
|
{ |
432
|
|
|
'input':'CCCFBAURDLPLNMVU', |
433
|
|
|
'result': { 'sex':'M', 'birthdate':'03/04/1985', 'birthplace':'Torino' }, |
434
|
|
|
}, |
435
|
|
|
] |
436
|
|
|
|
437
|
|
|
codes = [obj['input'] for obj in data] |
438
|
|
|
|
439
|
|
|
for obj in data: |
440
|
|
|
# with self.subTest(obj=obj): |
441
|
|
|
|
442
|
|
|
code = obj['input'] |
443
|
|
|
result = obj['result'] |
444
|
|
|
obj_decoded = codicefiscale.decode(code) |
445
|
|
|
|
446
|
|
|
sex = obj_decoded.get('sex') |
447
|
|
|
self.assertFalse(sex is None) |
448
|
|
|
self.assertEqual(sex, result['sex']) |
449
|
|
|
|
450
|
|
|
birthdate = obj_decoded.get('birthdate') |
451
|
|
|
self.assertFalse(birthdate is None) |
452
|
|
|
self.assertEqual(birthdate, |
453
|
|
|
datetime.strptime(result['birthdate'], '%d/%m/%Y')) |
454
|
|
|
|
455
|
|
|
birthplace = obj_decoded.get('birthplace') |
456
|
|
|
self.assertFalse(birthplace is None) |
457
|
|
|
self.assertEqual(birthplace['name'].upper(), |
458
|
|
|
result['birthplace'].upper()) |
459
|
|
|
|
460
|
|
|
omocodes = obj_decoded.get('omocodes', []) |
461
|
|
|
self.assertEqual(8, len(omocodes)) |
462
|
|
|
self.assertEqual(omocodes, codes) |
463
|
|
|
|
464
|
|
|
def test_is_omocode(self): |
465
|
|
|
|
466
|
|
|
self.assertFalse(codicefiscale.is_omocode('CCCFBA85D03L219P')) |
467
|
|
|
self.assertTrue(codicefiscale.is_omocode('CCCFBA85D03L21VE')) |
468
|
|
|
self.assertTrue(codicefiscale.is_omocode('CCCFBA85D03L2MVP')) |
469
|
|
|
self.assertTrue(codicefiscale.is_omocode('CCCFBA85D03LNMVE')) |
470
|
|
|
self.assertTrue(codicefiscale.is_omocode('CCCFBA85D0PLNMVA')) |
471
|
|
|
self.assertTrue(codicefiscale.is_omocode('CCCFBA85DLPLNMVL')) |
472
|
|
|
self.assertTrue(codicefiscale.is_omocode('CCCFBA8RDLPLNMVX')) |
473
|
|
|
self.assertTrue(codicefiscale.is_omocode('CCCFBAURDLPLNMVU')) |
474
|
|
|
|
475
|
|
|
def test_is_valid(self): |
476
|
|
|
|
477
|
|
|
self.assertTrue(codicefiscale.is_valid('CCCFBA85D03L219P')) |
478
|
|
|
self.assertTrue(codicefiscale.is_valid('CCC FBA 85 D03 L219 P')) |
479
|
|
|
self.assertTrue(codicefiscale.is_valid('CCC-FBA-85-D03-L219-P')) |
480
|
|
|
|
481
|
|
|
self.assertFalse(codicefiscale.is_valid('CCCFBA85D03L219PP')) # too long |
482
|
|
|
self.assertFalse(codicefiscale.is_valid('CCCFBA85D03L219B')) # wrong CIN |
483
|
|
|
self.assertFalse(codicefiscale.is_valid('CCCFBA85D03L219')) # too short |
484
|
|
|
self.assertFalse(codicefiscale.is_valid('CCCFBA85D00L219')) # wrong birthdate day |
485
|
|
|
self.assertFalse(codicefiscale.is_valid('CCCFBA85D99L219')) # wrong birthdate day |
486
|
|
|
|
487
|
|
|
def test_version(self): |
488
|
|
|
version_pattern = re.compile('^(([\d]+)\.([\d]+)\.([\d]+))$') |
489
|
|
|
self.assertTrue(version_pattern.match(__version__)) |
490
|
|
|
|
491
|
|
|
if __name__ == '__main__': |
492
|
|
|
unittest.main() |
493
|
|
|
|