"Fossies" - the Fresh Open Source Software Archive

Member "reportlab-3.5.32/tests/test_graphics_barcode.py" (1 Oct 2019, 2853 Bytes) of package /linux/privat/reportlab-3.5.32.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 #Copyright ReportLab Europe Ltd. 2000-2017
    2 #see license.txt for license details
    3 """
    4 Tests for barcodes
    5 """
    6 from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation
    7 setOutDir(__name__)
    8 import unittest, os, sys, glob
    9 
   10 try:
   11     from reportlab.graphics import _renderPM
   12 except ImportError:
   13     _renderPM = None
   14 
   15 class BarcodeWidgetTestCase(unittest.TestCase):
   16     "Test barcode classes."
   17 
   18     @classmethod
   19     def setUpClass(cls):
   20         cls.outDir = outDir = outputfile('barcode-out')
   21         if not os.path.isdir(outDir):
   22             os.makedirs(outDir)
   23         for x in glob.glob(os.path.join(outDir,'*')):
   24             os.remove(x)
   25 
   26     @unittest.skipIf(not _renderPM,'no _renderPM')
   27     def test0(self):
   28         from reportlab.graphics.shapes import Drawing
   29         outDir = self.outDir
   30         html = ['<html><head></head><body>']
   31         a = html.append
   32         formats = ['gif','pict','pdf']
   33         from reportlab.graphics.barcode import getCodes
   34         CN = list(getCodes().items())
   35         for name,C in CN:
   36             i = C()
   37             x0,y0,x1,y1 = i.getBounds()
   38             D = Drawing(x1-x0,y1-y0)
   39             D.add(i)
   40             D.save(formats=formats,outDir=outDir,fnRoot=name)
   41             a('<h2>%s</h2><img src="%s.gif"><br>' % (name, name))
   42             for fmt in formats:
   43                 efn = os.path.join(outDir,'%s.%s' % (name,fmt))
   44                 self.assertTrue(os.path.isfile(efn),msg="Expected file %r was not created" % efn)
   45         a('</body></html>')
   46         open(os.path.join(outDir,'index.html'),'w').write('\n'.join(html))
   47 
   48     def test1(self):
   49         '''test createBarcodeDrawing'''
   50         from reportlab.graphics.barcode import createBarcodeDrawing
   51         from reportlab.graphics.barcode import getCodeNames
   52         for name in getCodeNames():
   53             d = createBarcodeDrawing(name)
   54             for t in getattr(d.__class__,'_tests',[]):
   55                 createBarcodeDrawing(name,value=t)
   56 
   57     def test_qr_code_with_comma(self):
   58         from reportlab.graphics.barcode.qr import QrCodeWidget
   59         from reportlab.graphics.shapes import Drawing
   60         i = QrCodeWidget("VALUE WITH A COMMA,")
   61         x0, y0, x1, y1 = i.getBounds()
   62         D = Drawing(x1-x0, y1-y0)
   63         D.add(i)
   64         D.save(['gif','pict','pdf'], outDir=self.outDir, fnRoot="QR_with_comma")
   65 
   66     def test_qr_character_set_valid(self):
   67         from reportlab.graphics.barcode.qrencoder import QRNumber, QRAlphaNum
   68         for klass in (QRNumber,QRAlphaNum):
   69             for c in klass.chars:
   70                 if not klass.valid(c):
   71                     raise ValueError('%s.valid(%r) does not match' % (klass.__name__,c))
   72 
   73 def makeSuite():
   74     return makeSuiteForClasses(BarcodeWidgetTestCase)
   75 
   76 #noruntests
   77 if __name__ == "__main__":
   78     unittest.TextTestRunner().run(makeSuite())
   79     printLocation()