Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/tests/test_isan.doctest
blob: 7984e48afafaf5c8f21586753f43b5c247f244b3 (plain)
1
2
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
test_isan.doctest - more detailed doctests for stdnum.isan module

Copyright (C) 2010, 2012, 2013 Arthur de Jong

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.isan module. It
tries to test more corner cases and detailed functionality that is not
really useful as module documentation.

>>> from stdnum import isan


This is how numbers should be split (possible with and without
episode number):

>>> isan.split('000000018947000000000000')
('000000018947', '0000', '', '00000000', '')
>>> isan.split('0000-0000-D07A-0090-Q-0000-0000-X')
('00000000D07A', '0090', 'Q', '00000000', 'X')
>>> isan.split('000000018947000000000000')
('000000018947', '0000', '', '00000000', '')
>>> isan.split('1881-66C7-3420-6541-Y')
('188166C73420', '6541', 'Y', '', '')
>>> isan.split('1881-66C7-3420-6541')
('188166C73420', '6541', '', '', '')

Compacting should also work:

>>> isan.compact('000000018947000000000000')
'000000018947000000000000'
>>> isan.compact('0000-0000-D07A-0090-Q-0000-0000-X')
'00000000D07A009000000000'
>>> isan.compact('000000018947000000000000')
'000000018947000000000000'
>>> isan.compact('1881-66C7-3420-6541-Y')
'188166C734206541'
>>> isan.compact('1881-66C7-3420-6541')
'188166C734206541'


These should be valid numbers:

>>> isan.validate('0000-0000-D07A-0090-Q-0000-0000-X')
'00000000D07A0090Q00000000X'
>>> isan.validate('1881-66C7-3420-6541-Y-9F3A-0245-O')
'188166C734206541Y9F3A0245O'
>>> isan.validate('0000-0000-D07A-0090-Q-0000-0000-X')
'00000000D07A0090Q00000000X'
>>> isan.validate('0000-0000-D07A-0090-Q')
'00000000D07A0090Q'


And these should be invalid:

>>> isan.validate('00000000D07A0090-Z')
Traceback (most recent call last):
    ...
InvalidChecksum: ...
>>> isan.validate('3abc6800-0041X1-20')
Traceback (most recent call last):
    ...
InvalidFormat: ...
>>> isan.validate('3abc6800-0041')
Traceback (most recent call last):
    ...
InvalidLength: ...


The format() function can add the check digit if needed. It should leave
alone existing check digits (even invalid ones) and respect the presence
or absence of a version number.

>>> isan.format('00000000D07A0090')
'0000-0000-D07A-0090-Q'
>>> isan.format('1881-66C734206541-9F3A-0245')
'1881-66C7-3420-6541-Y-9F3A-0245-O'
>>> isan.format('1881-66C7-3420-6541-?-9F3A-0245-?', strip_check_digits=True, add_check_digits=True)
'1881-66C7-3420-6541-Y-9F3A-0245-O'
>>> isan.validate('1881-66C7-3420-6541-Y-9F3A-0245-O', strip_check_digits=True)
'188166C7342065419F3A0245'
>>> isan.validate('188166C7342065419F3A0245', add_check_digits=True)
'188166C734206541Y9F3A0245O'


A simple test for the to_binary() function.

>>> import binascii
>>> import sys
>>> x = binascii.b2a_hex(isan.to_binary('0000-0000-D07A-0090-Q'))
>>> if sys.version > '3':
...     x = str(x, encoding='ascii')
>>> x
'00000000d07a0090'