Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2017-04-15 21:20:05 +0200
committerArthur de Jong <arthur@arthurdejong.org>2017-04-15 21:20:51 +0200
commit57c12d816bcf324b14cb2dc7f8b284400c075591 (patch)
tree26b40526023bec45ba3253a9a52963622e2bfa45
parent6fb2e899537564194bb44e71f96505b7eea903e4 (diff)
An ISMN can only be 10 or 13 digits
This also adds the test that an ISMN should start with 9790.
-rw-r--r--stdnum/ismn.py8
-rw-r--r--tests/test_ismn.doctest20
2 files changed, 23 insertions, 5 deletions
diff --git a/stdnum/ismn.py b/stdnum/ismn.py
index 3de83f1..b04c763 100644
--- a/stdnum/ismn.py
+++ b/stdnum/ismn.py
@@ -1,6 +1,6 @@
# ismn.py - functions for handling ISMNs
#
-# Copyright (C) 2010, 2011, 2012, 2013 Arthur de Jong
+# Copyright (C) 2010-2017 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
@@ -62,8 +62,12 @@ def validate(number):
if number[0] != 'M':
raise InvalidFormat()
ean.validate('9790' + number[1:])
- else:
+ elif len(number) == 13:
+ if not number.startswith('9790'):
+ raise InvalidComponent()
ean.validate(number)
+ else:
+ raise InvalidLength()
return number
diff --git a/tests/test_ismn.doctest b/tests/test_ismn.doctest
index c3d46b6..23eb5f2 100644
--- a/tests/test_ismn.doctest
+++ b/tests/test_ismn.doctest
@@ -1,6 +1,6 @@
test_ismn.doctest - more detailed doctests for stdnum.ismn module
-Copyright (C) 2010, 2011, 2012, 2013 Arthur de Jong
+Copyright (C) 2010-2017 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
@@ -41,7 +41,7 @@ These are normal variations that should just work.
'9790260000438'
-Tests for mangling and incorect check digits.
+Tests for mangling and incorrect check digits.
>>> ismn.validate('979-0-3217-6543-x')
Traceback (most recent call last):
@@ -54,7 +54,7 @@ InvalidChecksum: ...
>>> ismn.validate('979M321765450')
Traceback (most recent call last):
...
-InvalidFormat: ...
+InvalidComponent: ...
>>> ismn.validate('Z-3217-6546-8')
Traceback (most recent call last):
...
@@ -72,6 +72,7 @@ See if 10 to 13 digit conversion works.
Test the ismn_type() function
+
>>> ismn.ismn_type('M-3217-6546-7')
'ISMN10'
>>> ismn.ismn_type('BAD')
@@ -85,3 +86,16 @@ Regrouping tests.
'979-0-3217-6546-7'
>>> ismn.format('9790321765450')
'979-0-3217-6545-0'
+
+
+While an EAN can also be less than 13 digits and ISMN should always be
+13 digits (when not 10 digits) and start with 9790.
+
+>>> ismn.validate('979023456784')
+Traceback (most recent call last):
+ ...
+InvalidLength: ...
+>>> ismn.validate('9781234567866')
+Traceback (most recent call last):
+ ...
+InvalidComponent: ...