def test_read_language_settings_values(self): """Should return a dict with a key for all supported settings.""" sheet = self.xlsform1_workbook.sheet_by_name( sheet_name='image_settings') settings = ImageSettings._read_language_settings_values( image_settings_sheet=sheet, column_index=2, settings=dict()) expected = set(ImageSettings._supported_settings()) observed = set(settings.keys()) | {'language'} self.assertEqual(expected, observed)
def test_read_image_settings(self): """Should return expected sample settings values.""" settings = ImageSettings.read(self.xlsform1_workbook) observed = settings.get(2) self.assertEqual(observed['language'], 'english') self.assertEqual(observed['text_label_font_name'], 'arialbd.ttf') self.assertIsInstance(observed['text_hint_pixels_line'], int) self.assertIsInstance(observed['nest_image_column'], str)
def test_locate_language_settings_columns(self): """Should return the column position and name of language settings.""" expected = {2: {'language': 'english'}} sheet = self.xlsform1_workbook.sheet_by_name( sheet_name='image_settings') observed = ImageSettings._locate_language_settings_columns( image_settings_sheet=sheet) self.assertEqual(expected, observed)
def test_read_from_image_settings(self): """Should return expected sample image content values.""" settings = ImageSettings.read( xlsform_workbook=self.xlsform1_workbook) observed = ImageContent.read( xlsform_workbook=self.xlsform1_workbook, settings=settings[2]) image_content = observed['image_content'] self.assertEqual('nl_visit', image_content[0]['file_name_column']) self.assertEqual(['Subject ID'], image_content[2]['text_label_column'])
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))
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)
def test_get_font_kwargs_hint(self): """Should return hint font kwargs dict with expected values.""" settings = { 'text_hint_font_name': 'arial.ttf', 'text_hint_font_size': 28, 'text_hint_font_color': 'black' } font_kwargs = ImageSettings._get_font_kwargs( settings=settings, label_or_hint='hint') self.assertEqual('black', font_kwargs['font_color']) self.assertEqual('Arial', font_kwargs['font'].font.family) self.assertEqual('Regular', font_kwargs['font'].font.style) self.assertEqual(28, font_kwargs['font'].size)
def test_get_font_kwargs_label(self): """Should return label font kwargs dict with expected values.""" settings = { 'text_label_font_name': 'arialbd.ttf', 'text_label_font_size': 32, 'text_label_font_color': 'black' } font_kwargs = ImageSettings._get_font_kwargs( settings=settings, label_or_hint='label') self.assertEqual('black', font_kwargs['font_color']) self.assertEqual('Arial', font_kwargs['font'].font.family) self.assertEqual('Bold', font_kwargs['font'].font.style) self.assertEqual(32, font_kwargs['font'].size)
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))
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)
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])
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))
def test_csv_to_list(self): """Should parse the csv into a trimmed value list.""" csv = 'start,end, deviceid, begin group,end group ' expected = ['start', 'end', 'deviceid', 'begin group', 'end group'] observed = ImageSettings._csv_to_list(csv) self.assertEqual(expected, observed)