Ejemplo n.º 1
0
 def test_create_output_directory(self):
     """Should create a folder with the expected name."""
     self.clean_test_output_folder = True
     shutil.rmtree(self.test_output_folder, ignore_errors=True)
     self.assertFalse(os.path.isdir(self.test_output_folder))
     Images._create_output_directory(self.xlsform1)
     self.assertTrue(os.path.isdir(self.test_output_folder))
Ejemplo n.º 2
0
 def test_prepare_base_image_no_logo(self):
     """Should return a base image with no logo."""
     settings = {'image_width': 500, 'image_height': 500,
                 'image_color': 'white', 'logo_image_path': ''}
     observed, _ = Images._prepare_base_image(settings=settings,
                                              xlsform_path="")
     expected = Images._create_blank_image(500, 500, 'white')
     self.assertEqual(list(expected.getdata()), list(observed.getdata()))
Ejemplo n.º 3
0
 def test_open_image_bad_path(self):
     """Should raise a FileNotFoundError if the file doesn't exist."""
     image_path = "some_image.png"
     xlsform_path = ""
     self.assertFalse(os.path.isfile(image_path))
     with self.assertRaises(FileNotFoundError):
         Images._locate_and_open_image(
             image_path=image_path, xlsform_path=xlsform_path)
Ejemplo n.º 4
0
 def test_write_single_language(self):
     """Should create the expected number of images."""
     self.clean_test_output_folder = True
     settings = ImageSettings.read(xlsform_workbook=self.xlsform1_workbook)
     add_content = ImageContent.read(
         xlsform_workbook=self.xlsform1_workbook, settings=settings[2])
     Images.write(self.xlsform1, add_content)
     output_files = os.listdir(self.test_output_folder)
     self.assertEqual(184, len(output_files))
Ejemplo n.º 5
0
 def test_create_output_directory_with_spaces(self):
     """Should create a folder with the expected name."""
     self.clean_test_output_folder = True
     self.test_output_folder = 'folder with spaces'
     test_file_path = 'folder with spaces/dummy_file.txt'
     shutil.rmtree(self.test_output_folder, ignore_errors=True)
     self.assertFalse(os.path.isdir(self.test_output_folder))
     Images._create_output_directory(test_file_path)
     self.assertTrue(os.path.isdir(self.test_output_folder))
Ejemplo n.º 6
0
 def test_doesnt_alter_input_paste_image(self):
     """Resizing should be done on a copy, not the original."""
     base_image = Images._create_blank_image(500, 500, 'red')
     paste_image = Images._create_blank_image(600, 600, 'blue')
     expected = (600, 600)
     Images._paste_image(
         base_image=base_image, pixels_from_top=20,
         paste_image=paste_image, pixels_before=20, max_height=50)
     observed = paste_image.size
     self.assertEqual(expected, observed)
Ejemplo n.º 7
0
 def test_create_output_directory_with_spaces_unc(self):
     """Should create a folder with the expected name."""
     self.clean_test_output_folder = True
     test_folder = os.path.join(self.cwd, 'folder with spaces')
     unc_path = '\\\\localhost\\c$' + test_folder[2:]
     self.test_output_folder = test_folder
     test_file_path = unc_path + '\\my_xlsform.xlsx'
     shutil.rmtree(self.test_output_folder, ignore_errors=True)
     self.assertFalse(os.path.isdir(self.test_output_folder))
     Images._create_output_directory(test_file_path)
     self.assertTrue(os.path.isdir(self.test_output_folder))
Ejemplo n.º 8
0
 def test_shrink_square_to_available(self):
     """Should resize to width."""
     pixels_from_top = 0
     base_image = Images._create_blank_image(500, 500, 'red')
     paste_image = Images._create_blank_image(600, 600, 'blue')
     pixels_before = 5
     expected = 485
     base_image, observed = Images._paste_image(
         base_image=base_image, pixels_from_top=pixels_from_top,
         paste_image=paste_image, pixels_before=pixels_before,
         max_height=None)
     self.assertEqual(expected, observed)
Ejemplo n.º 9
0
 def test_shrink_square_to_available_with_y_offset(self):
     """Should resize to fit available space."""
     base_image = Images._create_blank_image(500, 500, 'red')
     pixels_from_top = 50
     paste_image = Images._create_blank_image(600, 600, 'blue')
     pixels_before = 5
     expected = 490
     base_image, observed = Images._paste_image(
         base_image=base_image, pixels_from_top=pixels_from_top,
         paste_image=paste_image, pixels_before=pixels_before,
         max_height=None)
     self.assertEqual(expected, observed)
Ejemplo n.º 10
0
 def test_shrink_tall_to_max_height_with_y_offset(self):
     """Should resize to max_height."""
     base_image = Images._create_blank_image(500, 500, 'red')
     pixels_from_top = 100
     paste_image = Images._create_blank_image(750, 600, 'blue')
     pixels_before = 5
     max_height = 250
     expected = 355
     base_image, observed = Images._paste_image(
         base_image=base_image, pixels_from_top=pixels_from_top,
         paste_image=paste_image, pixels_before=pixels_before,
         max_height=max_height)
     self.assertEqual(expected, observed)
Ejemplo n.º 11
0
 def test_prepare_base_image_with_logo(self):
     """Should return a base image with a logo pasted onto it."""
     logo = os.path.join(self.cwd, 'nest_images/stopc-logo.png')
     settings = {'image_width': 500, 'image_height': 500,
                 'image_color': 'white',
                 'logo_image_path': logo,
                 'logo_image_pixels_before': 10,
                 'logo_image_height': 40}
     observed, _ = Images._prepare_base_image(settings=settings,
                                              xlsform_path="")
     base_logo = os.path.join(self.cwd, 'reference_images', 'base_logo.png')
     expected = Images._open_image(image_path=base_logo)
     self.assertEqual(list(expected.getdata()), list(observed.getdata()))
     expected.close()
Ejemplo n.º 12
0
 def test_all_content_types(self):
     """Should return image with label, hint and nested image."""
     settings = self.settings
     base_image, pixels_from_top = Images._prepare_base_image(
         settings=settings, xlsform_path=self.xlsform1)
     observed, _ = list(Images._prepare_question_images(
         base_image=base_image, pixels_from_top=pixels_from_top,
         settings=settings, output_path='', xlsform_path=self.xlsform1))[0]
     expected = self.ref_image_all
     diff_image = ImageChops.difference(observed, expected)
     pixel_count, diff_dict = self.quantify_image_difference(diff_image)
     self.assertTrue(diff_dict["tiny"] <= pixel_count * 0.01, diff_dict)
     self.assertTrue(diff_dict["minor"] <= pixel_count * 0.001, diff_dict)
     self.assertTrue(diff_dict["large"] == 0, diff_dict)
Ejemplo n.º 13
0
 def test_open_image_relative_path(self):
     """Should return Image if the path is relative to the xlsform."""
     xlsform_path = os.path.join(self.cwd, "reference_images/xlsform.xlsx")
     image_path = "open_image.png"
     observed = Images._locate_and_open_image(
         image_path=image_path, xlsform_path=xlsform_path)
     self.assertIsInstance(observed, Image.Image)
Ejemplo n.º 14
0
 def test_line_exceeds_width_and_height_multiple(self):
     """Should return an errors indicating which lines are too big."""
     base_image = Images._create_blank_image(50, 100, 'white')
     text = ['This text is too big', 'and so is this']
     settings = {'text_label_font_name': 'arialbd.ttf',
                 'text_label_font_size': 64,
                 'text_label_font_color': 'red'}
     font_kwargs = ImageSettings._get_font_kwargs(
         settings=settings, label_or_hint='label')
     image_log = logging.getLogger('odk_tools.question_images.images')
     with self.assertLogs(logger=image_log, level="WARN") as logs:
         Images._draw_text(
             base_image=base_image, pixels_from_top=0, pixels_before=10,
             pixels_between=5, **font_kwargs, text=text, image_name="img")
     expected = 3
     self.assertEqual(expected, len(logs.output))
Ejemplo n.º 15
0
 def test_label_only(self):
     """Should return image with label only."""
     settings = self.settings
     settings['image_content'][0]['text_hint_column'] = ''
     settings['image_content'][0]['nest_image_column'] = ''
     base_image, pixels_from_top = Images._prepare_base_image(
         settings=settings, xlsform_path=self.xlsform1)
     observed, _ = list(Images._prepare_question_images(
         base_image=base_image, pixels_from_top=pixels_from_top,
         settings=settings, output_path='', xlsform_path=self.xlsform1))[0]
     expected = self.ref_image_label
     diff_image = ImageChops.difference(observed, expected)
     pixel_count, diff_dict = self.quantify_image_difference(diff_image)
     self.assertTrue(diff_dict["tiny"] <= pixel_count * 0.01, diff_dict)
     self.assertTrue(diff_dict["minor"] <= pixel_count * 0.001, diff_dict)
     self.assertTrue(diff_dict["large"] == 0, diff_dict)
Ejemplo n.º 16
0
 def test_open_image_absolute_path(self):
     """Should return Image if the path is absolute."""
     image_path = os.path.join(
         self.cwd, "reference_images", "open_image.png")
     xlsform_path = "xlsform.xlsx"
     observed = Images._locate_and_open_image(
         image_path=image_path, xlsform_path=xlsform_path)
     self.assertIsInstance(observed, Image.Image)
Ejemplo n.º 17
0
 def setUpClass(cls):
     super().setUpClass()
     cls.ref_images = os.path.join(cls.cwd, "reference_images")
     cls.ref_image_all = Images._open_image(
         image_path=os.path.join(
             cls.ref_images, "da2d10ye_english_all.png"))
     cls.ref_image_label = Images._open_image(
         image_path=os.path.join(
             cls.ref_images, "da2d10ye_english_label.png"))
     cls.ref_image_label_hint = Images._open_image(
         image_path=os.path.join(
             cls.ref_images, "da2d10ye_english_label_hint.png"))
     cls.ref_image_label_image = Images._open_image(
         image_path=os.path.join(
             cls.ref_images, "da2d10ye_english_label_image.png"))
     cls.template = ImageSettings.read(
         xlsform_workbook=cls.xlsform1_workbook)
Ejemplo n.º 18
0
 def test_line_exceeds_height(self):
     """Should return an error indicating the line that was too tall."""
     base_image = Images._create_blank_image(200, 100, 'white')
     text = ['Big', 'Text']
     settings = {'text_label_font_name': 'arialbd.ttf',
                 'text_label_font_size': 64,
                 'text_label_font_color': 'red'}
     font_kwargs = ImageSettings._get_font_kwargs(
         settings=settings, label_or_hint='label')
     image_log = logging.getLogger('odk_tools.question_images.images')
     with self.assertLogs(logger=image_log, level="WARN") as logs:
         Images._draw_text(
             base_image=base_image, pixels_from_top=0, pixels_before=10,
             pixels_between=5, **font_kwargs, text=text, image_name="img")
     expected = "WARNING:odk_tools.question_images.images:" \
                "Text outside image margins. image name (img)," \
                " dim (height), pos (59), text ({0})".format(text[1])
     self.assertEqual(expected, logs.output[0])
Ejemplo n.º 19
0
 def test_line_exceeds_width_and_height(self):
     """Should return an errors indicating the line is too wide and tall."""
     base_image = Images._create_blank_image(50, 50, 'white')
     text = ['This is too big']
     settings = {'text_label_font_name': 'arialbd.ttf',
                 'text_label_font_size': 64,
                 'text_label_font_color': 'red'}
     font_kwargs = ImageSettings._get_font_kwargs(
         settings=settings, label_or_hint='label')
     image_log = logging.getLogger('odk_tools.question_images.images')
     with self.assertLogs(logger=image_log, level="WARN") as logs:
         Images._draw_text(
             base_image=base_image, pixels_from_top=0, pixels_before=10,
             pixels_between=5, **font_kwargs, text=text, image_name="img")
     self.assertEqual(2, len(logs.output))
     expected_width = "dim (width), pos (436), text ({0})".format(text[0])
     self.assertTrue(logs.output[0].endswith(expected_width))
     expected_height = "dim (height), pos (72), text ({0})".format(text[0])
     self.assertTrue(logs.output[1].endswith(expected_height))
Ejemplo n.º 20
0
 def test_ideal_input(self):
     """Should return a modified image with expected offset and no errors."""
     settings = ImageSettings.read(xlsform_workbook=self.xlsform1_workbook)
     settings = settings[2]
     base_image = Images._create_blank_image(
         settings['image_width'], settings['image_height'],
         settings['image_color'])
     original_image = base_image.copy()
     text = ['This is some text', 'that is for a question']
     font_kwargs = ImageSettings._get_font_kwargs(
         settings=settings, label_or_hint='label')
     with patch('logging.Logger.warn', MagicMock()) as log_warn:
         drawn_image, vertical = Images._draw_text(
             base_image=base_image, pixels_from_top=0, pixels_before=10,
             pixels_between=5, **font_kwargs, text=text, image_name='img')
     self.assertFalse(log_warn.called)
     self.assertNotEqual(list(original_image.getdata()),
                         list(drawn_image.getdata()))
     self.assertEqual(79, vertical)
Ejemplo n.º 21
0
 def test_image_diff_method_for_almost_identical_images(self):
     """Should pass for identical images."""
     ref_image1 = self.ref_image_all
     ref_image2 = Images._open_image(image_path=os.path.join(
         self.ref_images, "da2d10ye_english_all_almost_identical.png"))
     diff_image = ImageChops.difference(ref_image1, ref_image2)
     pixel_count, diff_dict = self.quantify_image_difference(diff_image)
     self.assertTrue(diff_dict["tiny"] <= pixel_count * 0.01, diff_dict)
     self.assertTrue(diff_dict["minor"] <= pixel_count * 0.001, diff_dict)
     self.assertTrue(diff_dict["large"] == 0, diff_dict)
Ejemplo n.º 22
0
 def test_create_blank_image(self):
     """Should create an image of the expected size and colour."""
     observed = Images._create_blank_image(50, 10, 'lavender')
     self.assertEqual((50, 10), observed.size)
     lavender_rgb_values = (230, 230, 250)
     self.assertEqual(lavender_rgb_values, observed.load()[5, 5])