def test_week_wrong(self): """construct_postscript raises ValueError if 0 < week < 54 ISO weeks must be between 1 and 53. """ weeks = [0, 54] for week in weeks: with self.subTest(week=week): with self.assertRaisesRegex(ValueError, str(week)): star_barcode.construct_postscript( week=week, bwipp_location=self.bwipp, issn=self.issn, sequence=21, header_line='')
def test_missing_bwipp(self): """construct_postscript raises ValueError if bwipp is missing If the location passed to construct_postscript for the location of the bwipp postscript library does not exist, then it should raise ValueError. """ seq = 21 week = 46 header = 'MSTAR 2016-11-14 MON 1.0' with self.assertRaisesRegex(ValueError, 'BWIPP'): star_barcode.construct_postscript( bwipp_location=Path('/fake-path/not-here.ps'), issn=self.issn, sequence=seq, week=week, header_line=header)
def test_issn_incorrect_length(self): """construct_postscript raises ValueError for ISSN of incorrect length ISSNs are either 7 or 8 digits long (8 being the optional check digit), with a mandatory (for BWIPP) hyphen in the fifth place. construct_postscript should raise a ValueError if the ISSN is not of the form \d{4}-\d{3,4}. """ issns = ['0307-15', '0307-15789', '03071758', '0307175'] for num in issns: with self.subTest(num=num): with self.assertRaisesRegex(ValueError, num): star_barcode.construct_postscript( issn=num, bwipp_location=self.bwipp, sequence=21, week=46, header_line='')
def test_sequence_outside_range(self): """construct_postscript raises ValueError if sequence outside range Sequence can be between 00 and 99. Although in our usage the second digit is the ISO weekday, so in practice limited 01-07 and 91-97, special sequences may require the extra numbers. Must not raise for sequences 00-09, represented by integers 0-9, as these are valid sequences (and should be padded as such), tested separately. """ seqs = [-1, 100] for seq in seqs: with self.subTest(seq=seq): with self.assertRaisesRegex(ValueError, str(seq)): star_barcode.construct_postscript( sequence=seq, bwipp_location=self.bwipp, issn=self.issn, week=46, header_line='')
def test_week_in_range(self): """construct_postscript accepts week >= 1, <= 53""" weeks = list(range(1, 54)) seq = 21 for week in weeks: with self.subTest(week=week): result = star_barcode.construct_postscript( week=week, bwipp_location=self.bwipp, issn=self.issn, sequence=seq, header_line='') self.assertGreater( result.find(f'{self.issn} {seq:02} {week:02}'), -1)
def test_typical(self): """construct_postscript correctly formats typical arguments""" seq = 21 week = 46 header = 'MSTAR 2016-11-14 MON 1.0' issn_args = ' '.join([self.issn, str(seq), str(week)]) result = star_barcode.construct_postscript(bwipp_location=self.bwipp, issn=self.issn, sequence=seq, week=week, header_line=header) self.assertGreater(result.find(str(self.bwipp)), -1) self.assertGreater(result.find(issn_args), -1) self.assertGreater(result.find(header), -1)
def test_sequence_0_to_9(self): """construct_postscript must not raise for sequences 00-09 As all possible sequences run 00-99, they can be adequately represented by an integer rather than a string. But the construct_postscript function must not raise for integers 0-9, even though as (unformatted) strings they are of length 1. """ seqs = list(range(10)) for seq in seqs: with self.subTest(seq=seq): result = star_barcode.construct_postscript( sequence=seq, bwipp_location=self.bwipp, issn=self.issn, week=20, header_line='') self.assertGreater(result.find(f'{self.issn} {seq:02}'), -1)
def test_sequence_specials(self): """construct_postscript must not raise for special sequences While the paper only uses sequences 01-07 through 91-97, the missing numbers are valid (00, 08, 09 … 90, 98, 99) and should be accepted by construct_postscript. """ special_digits = [0, 8, 9] for t in range(0, 10): for special in special_digits: seq = t * 10 + special with self.subTest(seq=seq): result = star_barcode.construct_postscript( sequence=seq, bwipp_location=self.bwipp, issn=self.issn, week=20, header_line='') self.assertGreater(result.find(f'{self.issn} {seq:02}'), -1)