Skip to content

Commit dff7103

Browse files
author
whitie
committed
Added tests.
1 parent 4102647 commit dff7103

5 files changed

Lines changed: 58 additions & 6 deletions

File tree

barcode/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
__description__ = ('Create standard barcodes with Python. No external '
2121
'modules needed (optional PIL support included).')
2222
__version__ = '0.8'
23-
__release__ = '{version}beta1'.format(version=__version__)
23+
__release__ = '{version}'.format(version=__version__)
2424
__license__ = 'MIT'
2525
__url__ = 'https://bitbucket.org/whitie/python-barcode/'
2626
__classifiers__ = [
@@ -88,9 +88,11 @@ def get_class(name):
8888

8989

9090
def generate(name, code, writer=None, output=None, writer_options=None,
91-
text=None):
91+
text=None, pil=False):
9292
options = writer_options or {}
93-
barcode = get_barcode(name, code, writer)
93+
barcode = get(name, code, writer)
94+
if pil:
95+
return barcode.render(writer_options, text)
9496
if isinstance(output, _strbase):
9597
fullname = barcode.save(output, options, text)
9698
return fullname

barcode/codex.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def render(self, writer_options, text=None):
7979
return Barcode.render(self, options, text)
8080

8181

82-
class PZN(Code39):
82+
class PZN7(Code39):
8383
"""Initializes new German number for pharmaceutical products.
8484
8585
:parameters:
@@ -117,6 +117,12 @@ def calculate_checksum(self):
117117
return checksum
118118

119119

120+
class PZN8(PZN7):
121+
"""Will be fully added in v0.9."""
122+
123+
digits = 7
124+
125+
120126
class Code128(Barcode):
121127
"""Initializes a new Code128 instance. The checksum is added automatically
122128
when building the bars.
@@ -250,3 +256,7 @@ def render(self, writer_options, text=None):
250256
options = dict(module_width=MIN_SIZE, quiet_zone=MIN_QUIET_ZONE)
251257
options.update(writer_options or {})
252258
return Barcode.render(self, options, text)
259+
260+
261+
# For pre 0.8 compatibility
262+
PZN = PZN7

barcode/ean.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ def calculate_checksum(self):
7575
:rtype: Integer
7676
"""
7777
def sum_(x, y): return int(x) + int(y)
78-
evensum = reduce(sum_, self.ean[::2])
79-
oddsum = reduce(sum_, self.ean[1::2])
78+
evensum = reduce(sum_, self.ean[-2::-2])
79+
oddsum = reduce(sum_, self.ean[-1::-2])
8080
return (10 - ((evensum + oddsum * 3) % 10)) % 10
8181

8282
def build(self):

barcode/isxn.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ def __init__(self, isbn, writer=None):
4848
self.isbn13 = isbn
4949
if isbn[:3] not in ('978', '979'):
5050
raise WrongCountryCodeError('ISBN must start with 978 or 979.')
51+
if isbn[:3] == '979':
52+
if isbn[3:5] not in ('10', '11'):
53+
raise BarcodeError('ISBN must start with 97910 or 97911.')
5154
EuropeanArticleNumber13.__init__(self, isbn, writer)
5255

5356

test.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,45 @@ def test_ean8(self):
115115
self.assertEqual(ref, bc[0])
116116

117117

118+
class TestChecksums(unittest.TestCase):
119+
120+
def test_code39(self):
121+
code39 = get_barcode('code39', 'Code39')
122+
self.assertEqual('CODE39W', code39.get_fullcode())
123+
124+
def test_pzn(self):
125+
pzn = get_barcode('pzn', '103940')
126+
self.assertEqual('PZN-1039406', pzn.get_fullcode())
127+
128+
def test_ean13(self):
129+
ean = get_barcode('ean13', '400614457735')
130+
self.assertEqual('4006144577350', ean.get_fullcode())
131+
132+
def test_ean8(self):
133+
ean = get_barcode('ean8', '6032299')
134+
self.assertEqual('60322999', ean.get_fullcode())
135+
136+
def test_jan(self):
137+
jan = get_barcode('jan', '491400614457')
138+
self.assertEqual('4914006144575', jan.get_fullcode())
139+
140+
def test_ean14(self):
141+
ean = get_barcode('ean14', '1234567891258')
142+
self.assertEqual('12345678912589', ean.get_fullcode())
143+
144+
def test_isbn10(self):
145+
isbn = get_barcode('isbn10', '376926085')
146+
self.assertEqual('3769260856', isbn.isbn10)
147+
148+
def test_isbn13(self):
149+
isbn = get_barcode('isbn13', '978376926085')
150+
self.assertEqual('9783769260854', isbn.get_fullcode())
151+
152+
118153
if __name__ == '__main__':
119154
test()
120155
print('\nNow open {htmlfile} in your browser.'.format(htmlfile=HTMLFILE))
156+
if '-v' not in sys.argv:
157+
sys.argv.append('-v')
121158
unittest.main()
122159

0 commit comments

Comments
 (0)