| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 
 | test_no_fodselsnummer.doctest - more detailed doctests for stdnum.no.fodselsnummer module
Copyright (C) 2018 Ilya Vihtinsky
Copyright (C) 2018 Arthur de Jong
Copyright (C) 2020 Leon Sandøy
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
This file contains more detailed doctests for the stdnum.no.fodselsnummer
module. It tries to cover more corner cases and detailed functionality that
is not really useful as module documentation.
>>> from stdnum.no import fodselsnummer
These numbers should be detected as male or female.
>>> fodselsnummer.get_gender('70624830529')
'M'
>>> fodselsnummer.get_gender('56403643756')
'M'
>>> fodselsnummer.get_gender('70341666064')
'F'
>>> fodselsnummer.get_gender('82938389280')
'F'
The last two check digits are validated independently of each other.
>>> fodselsnummer.is_valid('26111593816')
True
>>> fodselsnummer.is_valid('26111593817')  # only change last digit
False
>>> fodselsnummer.is_valid('26111593826')  # only change first digit
False
>>> fodselsnummer.is_valid('26111593875')  # change first, correct second
False
Length and format are also validated.
>>> fodselsnummer.validate('42485176432123')
Traceback (most recent call last):
    ...
InvalidLength: ...
>>> fodselsnummer.validate('a0gzaB30529')
Traceback (most recent call last):
    ...
InvalidFormat: ...
The birth date can be extracted from the number:
>>> fodselsnummer.get_birth_date('11111598403')
datetime.date(2015, 11, 11)
>>> fodselsnummer.get_birth_date('151086-95088')
datetime.date(1986, 10, 15)
>>> fodselsnummer.get_birth_date('180615 92527')
datetime.date(2015, 6, 18)
>>> fodselsnummer.get_birth_date('10 04 87 44 732')
datetime.date(1987, 4, 10)
>>> fodselsnummer.get_birth_date('13-04-99-58441')
datetime.date(1899, 4, 13)
Invalid dates are rejected:
>>> fodselsnummer.validate('19575770838')
Traceback (most recent call last):
  ...
InvalidComponent: The number does not contain valid birth date information.
>>> fodselsnummer.get_birth_date('45014054018')
Traceback (most recent call last):
  ...
InvalidComponent: The birthdate century cannot be determined
>>> fodselsnummer.get_birth_date('82314251342')
Traceback (most recent call last):
  ...
InvalidComponent: This number is an FH-number, and does not contain birth date information by design.
>>> fodselsnummer.validate('18103970861')
Traceback (most recent call last):
  ...
InvalidComponent: The birth date information is valid, but this person has not been born yet.
These should all be valid numbers.
>>> numbers = '''
...
... 100487 45526
... 10048744732
... 10048745364
... 111115984-03
... 151086-95088
... 18-06-15-92527
... 231140-38547
... 23114048690
... 26 11 15 940 14
... 26111593816
... 26111594286
... 26111594448
...
... '''
>>> [x for x in numbers.splitlines() if x and not fodselsnummer.is_valid(x)]
[]
 |