Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/tests/test_isbn.doctest
blob: 26ccdab0a3878b082ba65e951e279c0e2d90cc9e (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
test_isbn.doctest - more detailed doctests for stdnum.isbn module

Copyright (C) 2010-2019 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.isbn module. It
tries to test more corner cases and detailed functionality that is not
really useful as module documentation.

>>> from stdnum import isbn


Tests for mangling and incorrect check digits.

>>> isbn.validate('08515x-629-2')  # added X in the middle
Traceback (most recent call last):
    ...
InvalidFormat: ...
>>> isbn.validate('85152-629-1')  # incorrect check digit
Traceback (most recent call last):
    ...
InvalidChecksum: ...
>>> isbn.validate('978-902453827X')  # ISBN13 with X check digit
Traceback (most recent call last):
    ...
InvalidFormat: ...
>>> isbn.validate('978-902453827')  # invalid length
Traceback (most recent call last):
    ...
InvalidLength: ...
>>> isbn.validate('7501031311309')  # valid EAN, unknown bookland code
Traceback (most recent call last):
    ...
InvalidComponent: ...


See if ISBN10 to 13 conversion works.

>>> isbn.to_isbn13('978-9024538270')  # ISBN13 should stay ISBN13
'978-9024538270'
>>> isbn.to_isbn13('1 85798218 5')
'978 1 85798218 3'
>>> isbn.to_isbn13('1857982185')
'9781857982183'
>>> isbn.to_isbn13('1-85798-218-5')
'978-1-85798-218-3'
>>> isbn.validate(isbn.to_isbn13('1 85798218 5'))
'9781857982183'
>>> isbn.compact('1 85798218 5', convert=True)
'9781857982183'
>>> isbn.validate('1 85798218 5', convert=True)
'9781857982183'


Conversion of 9 digit ISBN to ISBN13 also works.

>>> isbn.is_valid('12-345678-9')
True
>>> isbn.validate(isbn.to_isbn13('12-345678-9'))
'9780123456786'


See if ISBN13 to 10 conversion works.

>>> isbn.to_isbn10('1-85798-218-5')  # ISBN10 should stay ISBN10
'1-85798-218-5'
>>> isbn.to_isbn10('978 1 85798218 3')
'1 85798218 5'
>>> isbn.to_isbn10('9781857982183')
'1857982185'
>>> isbn.to_isbn10('978-1-85798-218-3')
'1-85798-218-5'
>>> isbn.to_isbn10('979-20-1234567-8')  # incorrect check digit
Traceback (most recent call last):
    ...
InvalidFormat: ...
>>> isbn.to_isbn10('9791843123391')
Traceback (most recent call last):
    ...
InvalidComponent: ...


Regrouping tests.

>>> isbn.split('9024538270')  # normal ISBN10
('', '90', '245', '3827', '0')
>>> isbn.split('9999678270')  # ISBN10, unknown publisher in group
('', '99996', '', '7827', '0')
>>> isbn.split('979-20-1234567-8')
('979', '', '', '201234567', '8')
>>> isbn.split('5413170121522')  # valid check digit, unknown prefix
('', '', '', '541317012152', '2')


Explicit tests for invalid characters. The first number is 978-90245᭓8270,
the second is 978-9024538²70.

>>> isbn.validate(b'978-90245\xe1\xad\x938270'.decode('utf-8'))
Traceback (most recent call last):
    ...
InvalidFormat: ...
>>> isbn.validate(b'978-9024538\xc2\xb270'.decode('utf-8'))
Traceback (most recent call last):
    ...
InvalidFormat: ...


These have been found online and should all be valid numbers.

>>> numbers = '''
...
... 0-19-963209-X
... 0-201-75041-4
... 0-7503-0197-X
... 0-7506-9275-8
... 0-8412-0473-X
... 0-8493-0464-4
... 0-8493-0504-7
... 0080423906
... 013036004X
... 2-225-30547-1
... 9284401348
... 978-1-4200-4561-1
... 978-1-4200-8032-2
... 978-1-4397-3244-1
... 978-1-4398-2494-8
... 978-1-4398-8929-9
... 978-1-55156-438-8
... 978-90-208-3866-4
... 978-90-208-5330-8
... 978-90-345-0790-7
... 978-90-345-1183-6
... 978-90-345-1341-0
... 978-90-345-2135-4
... 978-90-345-2248-1
... 978-90-345-3025-7
... 978-90-345-3239-8
... 978-90-345-3629-7
... 978-90-345-4767-5
... 978-90-345-4770-5
... 978-90-345-4934-1
... 978-90-345-5216-7
... 978-90-345-5632-5
... 978-90-345-6767-3
... 978-963-88880-5-1
... 9780 8872 76743
... 9780231501132
... 9780231506236
... 9780231507387
... 9780231508360
... 9780231527682
... 9780252092022
... 9780271055206
... 9780271055480
... 9780271056289
... 9780292717664
... 9780292752252
... 9780300058024
... 9780300091960
... 9780300097474
... 9780300110654
... 9780300115406
... 9780300133523
... 9780300156263
... 9780470010037
... 9780470012536
... 9780470026977
... 9780470027257
... 9780470027424
... 9780470058084
... 9780470149676
... 9780470180907
... 9780470317372
... 9780470358139
... 9780470358184
... 9780470358849
... 9780470519578
... 9780470706459
... 9780470707296
... 9780470723807
... 9780470757659
... 9780470760123
... 9780470773154
... 9780470774618
... 9780470841785
... 9780470863343
... 9780470869697
... 9780470987056
... 9780470996027
... 9780471024033
... 9780471206910
... 9780471221951
... 9780471444541
... 9780471445340
... 9780471619772
... 9780471623793
... 9780471713425
... 9780471718161
... 9780471723950
... 9780471740452
... 9780471790594
... 9780520205802
... 9780520217676
... 9780520230453
... 9780520238879
... 9780520238961
... 9780520244306
... 9780520255791
... 9780520911543
... 9780520928572
... 9780520937529
... 9780520939431
... 9780520939769
... 9780520945227
... 9780520945821
... 9780631211532
... 9780631224068
... 9780631225799
... 9780632038961
... 9780674061910
... 9780727917867
... 9780748621651
... 9780748625208
... 9780748633340
... 9780773506688
... 9780773528499
... 9780773528673
... 9780773530843
... 9780773563438
... 9780773563629
... 9780773566217
... 9780773567221
... 9780773570498
... 9780773572966
... 9780773574533
... 9780773582712
... 9780773587861
... 9780801447518
... 9780801467677
... 9780802009685
... 9780802043108
... 9780802078636
... 9780802081544
... 9780802088161
... 9780802096456
... 9780807845837
... 9780807871584
... 9780807871935
... 9780807872192
... 9780815730347
... 9780816604777
... 9780816606597
... 9780816647774
... 9780816655281
... 9780816659760
... 9780816665976
... 9780816681600
... 9780833035202
... 9780833048646
... 9780833059949
... 9780833077851
... 9780871545183
... 9781111525545
... 9781400821600
... 9781400822232
... 9781400826414
... 9781400828623
... 9781400828647
... 9781400840861
... 9781400842483
... 9781400845934
... 9781405131902
... 9781405175791
... 9781442602380
... 9781442612877
... 9781442613140
... 9781442641198
... 9781442644335
... 9781442670174
... 9781442670747
... 9781442674202
... 9781442685376
... 9781442688636
... 9781442693517
... 9781442696716
... 9781469606309
... 9781571136329
... 9781571138392
... 9781610447775
... 9781846156243
... 9783527283217
... 9783527298365
... 9783527305797
... 9783527307258
... 9783527311125
... 9783527406371
... 9783527602803
... 9783527604296
... 9783527606757
... 9783527615186
... 9783527615759
... 9783527619771
... 9783527621781
... 9783826334054
... 9789622091252
... 9789622099760
... 9789814319089
... 9789888052509
... 9789888139590
... 981-02-1046-9
... 9814 253065
...
... '''
>>> [x for x in numbers.splitlines() if x and not isbn.is_valid(x)]
[]