From 57c12d816bcf324b14cb2dc7f8b284400c075591 Mon Sep 17 00:00:00 2001 From: Arthur de Jong Date: Sat, 15 Apr 2017 21:20:05 +0200 Subject: An ISMN can only be 10 or 13 digits This also adds the test that an ISMN should start with 9790. --- stdnum/ismn.py | 8 ++++++-- tests/test_ismn.doctest | 20 +++++++++++++++++--- 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: ... -- cgit v1.2.3