def test_to_str(): s = 'Märchen' ok_(str(pyqrcode.create(s))) s = '外来語' qr = pyqrcode.create(s) ok_(str(pyqrcode.create(s)))
def test_to_str(): s = 'Märchen' ok_(str(pyqrcode.create(s))) s = '外来語' qr = pyqrcode.create(s) ok_(str(pyqrcode.create(s)))
def test_kanji_enforce_binary(): data = '点' # 1. Try usual mode --> kanji qr = pyqrcode.create(data) eq_('kanji', qr.mode) # 2. Try another encoding --> binary qr = pyqrcode.create(data, mode='binary', encoding='utf-8') eq_('binary', qr.mode)
def test_kanji_enforce_binary(): data = '点' # 1. Try usual mode --> kanji qr = pyqrcode.create(data) eq_('kanji', qr.mode) # 2. Try another encoding --> binary qr = pyqrcode.create(data, mode='binary', encoding='utf-8') eq_('binary', qr.mode)
def test_unicode_utf8(): s = '\u263A' # ☺ (WHITE SMILING FACE) try: pyqrcode.create(s, encoding='latin1') raise Exception('Expected an error for \u263A and ISO-8859-1') except ValueError: pass qr = pyqrcode.create(s, encoding='utf-8') eq_('binary', qr.mode)
def test_unicode_utf8(): s = '\u263A' # ☺ (WHITE SMILING FACE) try: pyqrcode.create(s, encoding='latin1') raise Exception('Expected an error for \u263A and ISO-8859-1') except ValueError: pass qr = pyqrcode.create(s, encoding='utf-8') eq_('binary', qr.mode)
def test_custom_svg_class(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, svgclass='test-class') root = _parse_xml(out) ok_('class' in root.attrib) eq_('test-class', root.attrib.get('class'))
def test_no_line_class(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, lineclass=None) root = _parse_xml(out) path_el = _get_path(root) ok_('class' not in path_el.attrib)
def test_no_line_class(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, lineclass=None) root = _parse_xml(out) path_el = _get_path(root) ok_('class' not in path_el.attrib)
def test_custom_svg_class(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, svgclass='test-class') root = _parse_xml(out) ok_('class' in root.attrib) eq_('test-class', root.attrib.get('class'))
def test_viewbox(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, omithw=True) root = _parse_xml(out) ok_('viewBox' in root.attrib) ok_('height' not in root.attrib) ok_('width' not in root.attrib)
def test_custom_line_class(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, lineclass='test-class') root = _parse_xml(out) path_el = _get_path(root) ok_('class' in path_el.attrib) eq_('test-class', path_el.attrib.get('class'))
def test_title2(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, title='Määhhh') root = _parse_xml(out) title_el = _get_title(root) ok_(title_el is not None) eq_('Määhhh', title_el.text)
def test_scale(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, scale=2) root = _parse_xml(out) path = _get_path(root) ok_(path is not None) ok_('scale(2)' in path.attrib['transform'])
def test_scale(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, scale=2) root = _parse_xml(out) path = _get_path(root) ok_(path is not None) ok_('scale(2)' in path.attrib['transform'])
def test_custom_line_class(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, lineclass='test-class') root = _parse_xml(out) path_el = _get_path(root) ok_('class' in path_el.attrib) eq_('test-class', path_el.attrib.get('class'))
def test_title2(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, title='Määhhh') root = _parse_xml(out) title_el = _get_title(root) ok_(title_el is not None) eq_('Määhhh', title_el.text)
def test_viewbox(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, omithw=True) root = _parse_xml(out) ok_('viewBox' in root.attrib) ok_('height' not in root.attrib) ok_('width' not in root.attrib)
def test_scale_float(): qr = pyqrcode.create('test') out = io.BytesIO() scale = 2.13 qr.svg(out, scale=scale) root = _parse_xml(out) path = _get_path(root) ok_(path is not None) ok_('scale({0})'.format(scale) in path.attrib['transform'])
def test_scale_float(): qr = pyqrcode.create('test') out = io.BytesIO() scale = 2.13 qr.svg(out, scale=scale) root = _parse_xml(out) path = _get_path(root) ok_(path is not None) ok_('scale({0})'.format(scale) in path.attrib['transform'])
def test_module_color(): qr = pyqrcode.create('test') out = io.BytesIO() color = '#800080' qr.svg(out, module_color=color) root = _parse_xml(out) path = _get_path(root) ok_(path is not None) eq_(color, path.attrib['stroke'])
def test_omit_svgns(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, svgns=False) root = _parse_xml(out) path_el = _get_path(root) ok_(path_el is None) # (since _get_path uses the SVG namespace) path_el = root.find('path') # Query w/o namespace MUST find the path ok_(path_el is not None)
def test_omit_svgns(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, svgns=False) root = _parse_xml(out) path_el = _get_path(root) ok_(path_el is None) # (since _get_path uses the SVG namespace) path_el = root.find('path') # Query w/o namespace MUST find the path ok_(path_el is not None)
def test_module_color(): qr = pyqrcode.create('test') out = io.BytesIO() color = '#800080' qr.svg(out, module_color=color) root = _parse_xml(out) path = _get_path(root) ok_(path is not None) eq_(color, path.attrib['stroke'])
def test_background(): qr = pyqrcode.create('test') out = io.BytesIO() color = '#800080' qr.svg(out, background=color) root = _parse_xml(out) # Background should be the first path in the doc rect = _get_path(root) ok_(rect is not None) eq_(color, rect.attrib['fill'])
def test_background(): qr = pyqrcode.create('test') out = io.BytesIO() color = '#800080' qr.svg(out, background=color) root = _parse_xml(out) # Background should be the first path in the doc rect = _get_path(root) ok_(rect is not None) eq_(color, rect.attrib['fill'])
def test_get_png_size(): code = pyqrcode.create('Hello world') qr_size = 25 quiet_zone = 0 eq_(qr_size, code.get_png_size(1, quiet_zone=quiet_zone)) quiet_zone = 1 eq_((qr_size + 2 * quiet_zone) * quiet_zone, code.get_png_size(1, quiet_zone=quiet_zone)) quiet_zone = 4 # (default quiet_zone) eq_((qr_size + 2 * quiet_zone) * 1, code.get_png_size()) eq_((qr_size + 2 * quiet_zone) * 1, code.get_png_size(1)) eq_((qr_size + 2 * quiet_zone) * 4, code.get_png_size(4)) quiet_zone = 0 eq_((qr_size + 2 * quiet_zone) * 4, code.get_png_size(4, quiet_zone=quiet_zone))
def test_debug(): qr = pyqrcode.create('test') out = io.BytesIO() code = qr.code # Add some errors code[0][1] = ' ' code[0][2] = ' ' qr.svg(out, lineclass=None, quiet_zone=0, debug=True) root = _parse_xml(out) path = [path for path in root.findall('{%s}path' % _SVG_NS) if 'class' in path.attrib] eq_(1, len(path)) path = path[0] eq_('pyqrerr', path.attrib['class']) eq_('red', path.attrib['stroke']) ok_('M1' in path.attrib['d']) ok_('M2' in path.attrib['d'])
def test_kanji_tranform_encoding(): """Test the encoding can be set to shiftjis for utf-8 encoding. """ s = 'モンティ' s = '点茗' #Characters directly from the standard #Encode the string as utf-8 *not* shiftjis utf8 = s.encode('utf-8') qr = pyqrcode.create(utf8, encoding='utf-8') # PyQRCode: # eq_(qr.mode, 'kanji') eq_(qr.mode, 'binary') # PyQRCode: # eq_(qr.encoding, 'shiftjis') eq_(qr.encoding, 'utf-8')
def test_kanji_encoding(): s = '点茗' #Characters directly from the standard #These come from a reference image passed through the debugger codewords = [128,38,207,234,168,0,236,17,236,18,75,55,241,75,140,21, 117,174,242,221,243,87,199,123,50,169] qr = pyqrcode.create(s) #Get the binary representation of the data for the code bin_words = qr.builder.buffer.getvalue() #Convert the data into integer bytes b = [int(bin_words[i:i+8], base=2) for i in range(0, len(bin_words), 8)] #See if the calculated code matches the known code eq_(b, codewords)
def test_kanji_tranform_encoding(): """Test the encoding can be set to shiftjis for utf-8 encoding. """ s = 'モンティ' s = '点茗' #Characters directly from the standard #Encode the string as utf-8 *not* shiftjis utf8 = s.encode('utf-8') qr = pyqrcode.create(utf8, encoding='utf-8') # PyQRCode: # eq_(qr.mode, 'kanji') eq_(qr.mode, 'binary') # PyQRCode: # eq_(qr.encoding, 'shiftjis') eq_(qr.encoding, 'utf-8')
def test_kanji_encoding(): s = '点茗' #Characters directly from the standard #These come from a reference image passed through the debugger codewords = [ 128, 38, 207, 234, 168, 0, 236, 17, 236, 18, 75, 55, 241, 75, 140, 21, 117, 174, 242, 221, 243, 87, 199, 123, 50, 169 ] qr = pyqrcode.create(s) #Get the binary representation of the data for the code bin_words = qr.builder.buffer.getvalue() #Convert the data into integer bytes b = [int(bin_words[i:i + 8], base=2) for i in range(0, len(bin_words), 8)] #See if the calculated code matches the known code eq_(b, codewords)
def test_debug(): qr = pyqrcode.create('test') out = io.BytesIO() code = qr.code # Add some errors code[0][1] = ' ' code[0][2] = ' ' qr.svg(out, lineclass=None, quiet_zone=0, debug=True) root = _parse_xml(out) path = [ path for path in root.findall('{%s}path' % _SVG_NS) if 'class' in path.attrib ] eq_(1, len(path)) path = path[0] eq_('pyqrerr', path.attrib['class']) eq_('red', path.attrib['stroke']) ok_('M1' in path.attrib['d']) ok_('M2' in path.attrib['d'])
def check(s, error_level, encoding, expected_mode, reference): qr = pyqrcode.create(s, error=err, encoding=encoding) eq_(error_level, qr.error) eq_(expected_mode, qr.mode) scale, quiet_zone = 6, 4 # Read reference image ref_width, ref_height, ref_pixels = _get_png_info(filename=_get_reference_filename(reference)) # Create our image out = io.BytesIO() qr.png(out, scale=scale, quiet_zone=quiet_zone) out.seek(0) # Excpected width/height expected_width = qr.get_png_size(scale, quiet_zone) # Read created image width, height, pixels = _get_png_info(file=out) eq_(expected_width, ref_width) eq_(expected_width, ref_height) eq_(ref_width, width) eq_(ref_height, height) eq_(len(ref_pixels), len(pixels)) eq_(ref_pixels, pixels)
def test_write_svg(): # Test with default options qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out) xml_str = out.getvalue().decode('utf-8') ok_(xml_str.startswith('<?xml')) root = _parse_xml(out) ok_('viewBox' not in root.attrib) ok_('height' in root.attrib) ok_('width' in root.attrib) css_class = root.attrib.get('class') ok_(css_class) eq_('pyqrcode', css_class) path_el = _get_path(root) ok_(path_el is not None) path_class = path_el.get('class') eq_('pyqrline', path_class) stroke = path_el.get('stroke') eq_('#000', stroke) title_el = _get_title(root) ok_(title_el is None)
def check(serializer_name, buffer_factory, to_matrix_func, data, error, quiet_zone): """\ `serializer_name` Method name to serialize the QR code `buffer_factory` Callable to construct the buffer. `to_matrix_func` Function to convert the buffer back to a matrix. `data` The input to construct the QR code. `error` ECC level `quiet_zone` quiet_zone size. """ qr = pyqrcode.create(data, error=error) out = buffer_factory() meth = getattr(qr, serializer_name) meth(out, quiet_zone=quiet_zone) matrix = to_matrix_func(out, quiet_zone) eq_(qr.code, matrix)
def test_write_svg(): # Test with default options qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out) xml_str = out.getvalue().decode('utf-8') ok_(xml_str.startswith('<?xml')) root = _parse_xml(out) ok_('viewBox' not in root.attrib) ok_('height' in root.attrib) ok_('width' in root.attrib) css_class = root.attrib.get('class') ok_(css_class) eq_('pyqrcode', css_class) path_el = _get_path(root) ok_(path_el is not None) path_class = path_el.get('class') eq_('pyqrline', path_class) stroke = path_el.get('stroke') eq_('#000', stroke) title_el = _get_title(root) ok_(title_el is None)
def test_png_as_base64_str(): """\ Test PNG to Base64 converions using a known Base64 string. """ expected_str = "iVBORw0KGgoAAAANSUhEUgAAAOEAAADhAQAAAAAWyO/XAAACIklEQVR4n"\ "O2YMXKrQBBER6WAkCPsTaSLUbVU6WLmJhyBkIDSuLtXxtgu/8DBbwUiQMB"\ "TMMzsdM8S+Y9jjBd90b/SJSJOS831upyzu/e3zDc86sy0Zk7LUPKtHyMu/"\ "RbdhIDddMAtws17REH0Q8FtlGeguZX10ufcMYn5PDSQSSRR9X0Gqvqec2U"\ "S1xOe/ay+gaoXVNWP049O+f9UB9KJ0znjmre5+64qDsqWREERZzCTG35Za"\ "TNlkE3IkkJWZ572mE2U637iWuMDLL06U0bmzkwRZFJj78gf6ptzC9xLkUk"\ "2J8uLSCG0sbLSnZmOAYOsLYkSD5jSba++jZZ81PeW6wVJXIMeYKZb7AoyB"\ "00JHpB7fU2Ui58tidOAnDKxgavOS9mXV7mQTAnplIKYKTQCLSkrOiOTqu/"\ "hjVwULXnpR81csTa/zKN3eyi88bRoHAzOOePjyk4RMyRN4oHmpIHv/eGi1"\ "A32AiQjPlrj4N0mSpdsVsQ5DMMEZotPNXPRFnM0jaWaJSW3mCmnB6w1Chk"\ "kg9NhnKQmXspZXt7N/E34C3Zodc+zi3LBAdAg24ZDs0WaKfSU9dXmJx+T9"\ "BBmqkPPtIdVc3Kj7aXal2lnzcD1gWRavu7aHLS2lsR0gw5tBrkcV52H6tu"\ "IbrfHK8Rhh2ulQxtXIR7JwOunbngpp5vCj0mRx+nLRVVfDcz6YqNeqHaqX"\ "hBN+tFYvs2EFvr78aIv+kf6Dm6Pdk09JdaJAAAAAElFTkSuQmCC" qr = pyqrcode.create('Are you suggesting coconuts migrate?') generated_str = qr.png_as_base64_str(scale=5) eq_(generated_str, expected_str)
def test_xbm(): """Test the xbm render against a known good. This test checks the *values* contained in the XBM, not the text. """ c = pyqrcode.create('Test').xbm(scale=1) #Testing number-by-number to get more useful failure message c_width, c_height, c_bits = decompose_xbm(c) e_width, e_height, e_bits = decompose_xbm(expected) #Check the there is the same width and height eq_(c_width, e_width) eq_(c_height, e_height) #Check that there is the same number of bits eq_(len(c_bits), len(e_bits)) #Check the bit values for i in range(len(e_bits)): eq_(c_bits[i], e_bits[i], "Wrong value at {0}: {1} != {2}".format(i, c_bits[i], e_bits[i]))
def test_xbm_with_tkinter(): """Test XBM renderer is compatible with Tkinter """ #Under TOX tkinter testing does not work, skip if tox environemnt if not os.getenv('DISPLAY'): raise nose.SkipTest() #Python 2 vs 3 try: import Tkinter as tkinter except: import tkinter code = pyqrcode.create('Test') code_size = code.get_png_size(scale=1) code_xbm = code.xbm(scale=1) top = tkinter.Tk() bitmap = tkinter.BitmapImage(data=code_xbm) eq_(bitmap.width(), code_size) eq_(bitmap.height(), code_size)
def test_write_no_xmldecl(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, xmldecl=False) xml_str = out.getvalue().decode('utf-8') ok_(xml_str.startswith('<svg'))
def test_create_numeric(): code = pyqrcode.create(666) eq_('numeric', code.mode)
def test_create_numeric_from_string(): code = pyqrcode.create('666') eq_('numeric', code.mode)
def test_version_1_max_binary(): code = pyqrcode.create('ABCDEFG', error="H", mode='binary') eq_(code.version, 1)
def test_no_svg_class(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, svgclass=None) root = _parse_xml(out) ok_('class' not in root.attrib)
def test_binary_detection(): code = pyqrcode.create('Hello world') eq_('binary', code.mode)
def test_ascii2(): s = 'MAERCHENBUCH' code = pyqrcode.create(s, error='M', encoding=None) eq_('alphanumeric', code.mode) eq_(s, code.data.decode('iso-8859-1'))
def test_version_1_max_kanji(): code = pyqrcode.create('点点点点', error="H", mode='kanji') eq_(code.mode, 'kanji') eq_(code.version, 1)
def test_donot_close_svg(): code = pyqrcode.create('a') out = KeepTrackOfClose() code.svg(out) ok_(not out.is_closed)
def test_version_1_max_numeric(): code = pyqrcode.create("11111111111111111", error="H") eq_(code.version, 1)
def test_version_1_max_alphanumeric(): code = pyqrcode.create('ABCDEFGJKL', error="H") eq_(code.version, 1)
def test_umlaut(): s = 'Märchenbuch' code = pyqrcode.create(s, error='M') eq_('binary', code.mode) eq_(s.encode('iso-8859-1'), code.data)
def test_long_number_gives_version2(): code = pyqrcode.create("6010102401", error="H") eq_(code.version, 1)