Permalink
Browse files

Virtual barcode errors are fatal

Raise an exception if something bad happens
instead of hiding the error. This may crash
makebills if there's an error in the code (which
is actually a good thing) instead of sending
invalid bills.
  • Loading branch information...
joneskoo committed Mar 14, 2011
1 parent d54a886 commit 2f96fb40f44447d3ca1b19d976b81f8c937b9489
Showing with 36 additions and 23 deletions.
  1. +22 −14 membership/reference_numbers.py
  2. +14 −9 membership/tests.py
@@ -2,6 +2,12 @@
from decimal import Decimal
+class ReferenceNumberException(Exception): pass
+class ReferenceNumberFormatException(ReferenceNumberException): pass
+class IBANFormatException(ReferenceNumberException): pass
+class InvalidAmountException(ReferenceNumberException): pass
+class DueDateFormatException(ReferenceNumberException): pass
+
def generate_membership_bill_reference_number(membership_id, bill_year):
# [jäsennumero] yyxxz
# jossa yy=vuosi kahdella numerolla, xx=maksutapahtumakoodi ja z tarkistenumero
@@ -57,20 +63,20 @@ def canonize_iban(iban):
iban = iban[2:]
if iban.isdigit() and len(iban) == 16:
return iban
- # TODO: Raise exception instead
- return 'X' * 16
+ raise IBANFormatException("IBAN format is invalid")
def canonize_refnum(refnum, digits = 20):
"""Removes any whitespace makes sure the number is 20 digits long"""
+ if refnum == None:
+ return '0' * digits
refnum = refnum.replace(' ', '')
if len(refnum) < digits:
- # zero pad to enough digits
- refnum = ("0" * digits) + refnum
- refnum = refnum [-digits:]
+ refnum = refnum.zfill(digits) # zero pad
if refnum.isdigit() and len(refnum) == digits and check_checknumber(refnum):
return refnum
- # TODO: Raise exception instead
- return '0' * 20
+ else:
+ raise ReferenceNumberFormatException("Reference number '%s' invalid" %
+ refnum)
def canonize_sum(euros, cents=0):
if cents == 0:
@@ -84,13 +90,15 @@ def canonize_sum(euros, cents=0):
# Amount too big, return 0
return '000000' + '00'
if cents > 99 or euros < 0 or cents < 0:
- # Invalid amount
- # TODO: Raise exception instead
- return '000000' + '00'
+ raise InvalidAmountException("Amount %s euros %s cents invalid" % (
+ euros, cents))
return '%06u%02u' % (euros, cents)
def canonize_duedate(duedate):
- if hasattr(duedate, 'strftime'):
- return duedate.strftime('%y%m%d')
- # TODO: Raise exception instead
- return '000000'
+ if duedate == None:
+ return '000000'
+ else:
+ try:
+ return duedate.strftime('%y%m%d')
+ except AttributeError:
+ raise DueDateFormatException("Invalid type for canonize_duedate")
View
@@ -29,6 +29,10 @@
from reference_numbers import generate_membership_bill_reference_number
from reference_numbers import generate_checknumber, add_checknumber, check_checknumber, group_right
from reference_numbers import barcode_4, canonize_iban, canonize_refnum, canonize_sum, canonize_duedate
+from reference_numbers import ReferenceNumberException
+from reference_numbers import ReferenceNumberFormatException
+from reference_numbers import IBANFormatException, InvalidAmountException
+from reference_numbers import DueDateFormatException
from management.commands.makebills import logger as makebills_logger
from management.commands.makebills import makebills
@@ -84,35 +88,36 @@ def test_canonize_iban(self):
self.assertEqual(canonize_iban('FI16 5741 3620 4069 56'), '1657413620406956')
self.assertEqual(canonize_iban(' 16 5741 3620 4069 56 '), '1657413620406956')
self.assertEqual(canonize_iban('1657413620406956'), '1657413620406956')
- self.assertEqual(canonize_iban('31231231657413620406956'),'XXXXXXXXXXXXXXXX')
- self.assertEqual(canonize_iban('SE16 5741 3620 4069 56'), 'XXXXXXXXXXXXXXXX')
- self.assertEqual(canonize_iban('foobar?'), 'XXXXXXXXXXXXXXXX')
+ self.assertRaises(IBANFormatException, canonize_iban, '31231231657413620406956')
+ self.assertRaises(IBANFormatException, canonize_iban, 'SE16 5741 3620 4069 56')
+ self.assertRaises(IBANFormatException, canonize_iban, 'foobar?')
def test_canonize_refnum(self):
self.assertEqual(canonize_refnum('42'), '00000000000000000042')
self.assertEqual(canonize_refnum('32287 22205 1'), '00000000032287222051')
- self.assertEqual(canonize_refnum('32287 22205 0'), '00000000000000000000')
self.assertEqual(canonize_refnum('86851 62596 19897'), '00000868516259619897')
self.assertEqual(canonize_refnum('559582243294671'), '00000559582243294671')
self.assertEqual(canonize_refnum('69 87567 20834 35364'), '00069875672083435364')
self.assertEqual(canonize_refnum('7 75847 47906 47489'), '00007758474790647489')
self.assertEqual(canonize_refnum('78 77767 96566 28687'), '00078777679656628687')
- self.assertEqual(canonize_refnum('000000000078777679656628687'), '00078777679656628687')
self.assertEqual(canonize_refnum('8 68624'), '00000000000000868624')
- self.assertEqual(canonize_refnum('not a ref num'), '00000000000000000000')
+ self.assertEqual(canonize_refnum(None), '00000000000000000000')
+ self.assertRaises(ReferenceNumberException, canonize_refnum, '32287 22205 0')
+ self.assertRaises(ReferenceNumberFormatException, canonize_refnum, '000000000078777679656628687')
+ self.assertRaises(ReferenceNumberFormatException, canonize_refnum, 'not refnum')
def test_canonize_sum(self):
self.assertEquals(canonize_sum(euros=35, cents=00), '00003500')
self.assertEquals(canonize_sum(euros=35, cents=15), '00003515')
self.assertEquals(canonize_sum(30), '00003000')
self.assertEquals(canonize_sum(123456), '12345600')
- self.assertEquals(canonize_sum(123456, 101), '00000000')
self.assertEquals(canonize_sum(1000000), '00000000')
self.assertEquals(canonize_sum("35"), '00003500')
self.assertEquals(canonize_sum("250", "99"), '00025099')
- self.assertEquals(canonize_sum(-1), '00000000')
self.assertEquals(canonize_sum("2.10"), '00000210')
self.assertEquals(canonize_sum(Decimal("150.99")), '00015099')
+ self.assertRaises(InvalidAmountException, canonize_sum, -1)
+ self.assertRaises(InvalidAmountException, canonize_sum, 123456, 101)
def test_canonize_duedate(self):
self.assertEquals(canonize_duedate(datetime(2011, 03 ,20)), '110320')
@@ -122,7 +127,7 @@ def test_canonize_duedate(self):
self.assertEquals(canonize_duedate(datetime(2010, 10, 10, 23, 59, 59)), '101010')
self.assertEquals(canonize_duedate(datetime(2010, 10, 10, 0, 1, 0)), '101010')
self.assertEquals(canonize_duedate(None), '000000')
- self.assertEquals(canonize_duedate('HETI'), '000000')
+ self.assertRaises(ReferenceNumberException, canonize_duedate, ('HETI'))
# http://www.fkl.fi/www/page/fk_www_1293
def test_barcode_4(self):

0 comments on commit 2f96fb4

Please sign in to comment.