def test_error_correction(self): file_base_name = 'qrfromtext_error_correction' tests_data = [] for correction_level in ERROR_CORRECTION_DICT.keys(): ref_file_name = '%s_%s' % (file_base_name, correction_level) tests_data.append( dict(source='{% qr_from_text "' + COMPLEX_TEST_TEXT + '" image_format="png" error_correction="' + correction_level + '" %}', ref_file_name=ref_file_name.lower())) for test_data in tests_data: print('Testing template: %s' % test_data['source']) html_source = mark_safe('{% load qr_code %}' + test_data['source']) template = Template(html_source) context = Context() source_image = template.render(context).strip() source_image_data = source_image[ 32:-len('" alt="%s"' % escape(COMPLEX_TEST_TEXT))] source_image_data = base64.b64decode(source_image_data) if REFRESH_REFERENCE_IMAGES: write_png_content_to_file(test_data['ref_file_name'], source_image_data) ref_image_data = get_png_content_from_file_name( test_data['ref_file_name']) self.assertEqual(source_image_data, ref_image_data)
def test_error_correction(self): file_base_name = 'qrfromtext_error_correction' tests_data = [] for correction_level in ERROR_CORRECTION_DICT.keys(): ref_file_name = '%s_%s' % (file_base_name, correction_level) tests_data.append( dict( source= f'{{% qr_from_text "{COMPLEX_TEST_TEXT}" image_format="svg" error_correction="{correction_level}" %}}', ref_file_name=ref_file_name.lower())) for test_data in tests_data: print('Testing template: %s' % test_data['source']) html_source = mark_safe('{% load qr_code %}' + test_data['source']) template = Template(html_source) context = Context() source_image_data = template.render(context) if REFRESH_REFERENCE_IMAGES: write_svg_content_to_file(test_data['ref_file_name'], source_image_data) ref_image_data = get_svg_content_from_file_name( test_data['ref_file_name']) self.assertEqual(minimal_svg(source_image_data), minimal_svg(ref_image_data))
def _get_valid_error_correction_or_default(error_correction): return ERROR_CORRECTION_DICT.get(error_correction.upper(), ERROR_CORRECTION_DICT[ DEFAULT_ERROR_CORRECTION])