def test_save_theme_background_override(self, mocked_paths, mocked_delete, mocked_shutil): """ Test that we log a warning if the new background is missing """ # GIVEN: A new theme manager instance, with invalid files. Setup as if the user # has changed the background to a invalid path. # Not using resource dir because I could potentially copy a file folder_path = Path(mkdtemp()) theme_manager = ThemeManager(None) theme_manager.old_background_image_path = folder_path / 'old.png' theme_manager.update_preview_images = MagicMock() theme_manager.theme_path = MagicMock() mocked_theme = MagicMock() mocked_theme.theme_name = 'themename' mocked_theme.background_filename = folder_path / 'new_cached.png' # mocked_theme.background_source.exists() will return True mocked_theme.background_source = MagicMock() # override_background.exists() will return True override_background = MagicMock() # WHEN: Calling save_theme with a background override theme_manager.save_theme(mocked_theme, background_override=override_background) # THEN: The override_background should have been copied rather than the background_source mocked_shutil.copyfile.assert_called_once_with(override_background, mocked_theme.background_filename)
def test_save_theme_missing_original(self, mocked_paths, mocked_delete, mocked_log_exception): """ Test that we revert to the old theme background image if the source is missing when changing the theme. (User doesn't change background but the original is missing) """ # GIVEN: A new theme manager instance, with invalid files. Setup as if the user # has left the background the same, or reselected the same path. # Not using resource dir because I could potentially copy a file folder_path = Path(mkdtemp()) theme_manager = ThemeManager(None) theme_manager.old_background_image_path = folder_path / 'old.png' theme_manager.update_preview_images = MagicMock() theme_manager.theme_path = MagicMock() mocked_theme = MagicMock() mocked_theme.theme_name = 'themename' mocked_theme.background_filename = folder_path / 'old.png' mocked_theme.background_source = folder_path / 'non_existent_original.png' # WHEN: Calling save_theme with a invalid background_filename # Although all filenames are considered invalid in this test, # it is important it reverts to the old background path as this in reality is always # valid unless someone has messed with the cache. theme_manager.save_theme(mocked_theme) # THEN: The old background should not have bee deleted # AND the filename should have been replaced with the old cached background # AND there is no exception assert mocked_delete.called is False, 'delete_file should not be called' assert mocked_theme.background_filename == theme_manager.old_background_image_path, \ 'Background path should be reverted' assert mocked_log_exception.called is False, \ 'Should not have been an exception as the file wasn\'t changed'
def test_save_theme_special_char_name(self): """ Test that we can save themes with special characters in the name """ # GIVEN: A new theme manager instance, with mocked theme and thememanager-attributes. theme_manager = ThemeManager(None) theme_manager.old_background_image_path = None theme_manager.update_preview_images = MagicMock() theme_manager.theme_path = Path(self.temp_folder) mocked_theme = MagicMock() mocked_theme.theme_name = 'theme 愛 name' mocked_theme.export_theme.return_value = "{}" # WHEN: Calling save_theme with a theme with a name with special characters in it theme_manager.save_theme(mocked_theme) # THEN: It should have been created assert os.path.exists(os.path.join(self.temp_folder, 'theme 愛 name', 'theme 愛 name.json')) is True, \ 'Theme with special characters should have been created!'
def test_save_theme_delete_old_image(self, mocked_create_paths, mocked_delete_file, mocked_shutil): """ Test that we do delete a old theme background image when a new is submitted """ # GIVEN: A new theme manager instance, with mocked builtins.open, # theme, create_paths, thememanager-attributes and background # .filename path is the same as the source path. theme_manager = ThemeManager(None) theme_manager.old_background_image_path = RESOURCE_PATH / 'old_church.png' theme_manager.update_preview_images = MagicMock() theme_manager.theme_path = MagicMock() mocked_theme = MagicMock() mocked_theme.theme_name = 'themename' mocked_theme.background_filename = RESOURCE_PATH / 'church.jpg' mocked_theme.background_source = RESOURCE_PATH / 'church2.jpg' # WHEN: Calling save_theme with both background paths to different images theme_manager.save_theme(mocked_theme) # THEN: The mocked_delete_file should have been called to delete the old cached background assert mocked_delete_file.called is True, 'delete_file should be called'
def test_save_theme_missing_new(self, mocked_paths, mocked_delete, mocked_log_warning): """ Test that we log a warning if the new background is missing """ # GIVEN: A new theme manager instance, with invalid files. Setup as if the user # has changed the background to a invalid path. # Not using resource dir because I could potentially copy a file folder_path = Path(mkdtemp()) theme_manager = ThemeManager(None) theme_manager.old_background_image_path = folder_path / 'old.png' theme_manager.update_preview_images = MagicMock() theme_manager.theme_path = MagicMock() mocked_theme = MagicMock() mocked_theme.theme_name = 'themename' mocked_theme.background_filename = folder_path / 'new_cached.png' mocked_theme.background_source = folder_path / 'new_original.png' # WHEN: Calling save_theme with a invalid background_filename theme_manager.save_theme(mocked_theme) # THEN: A warning should have happened due to attempting to copy a missing file mocked_log_warning.assert_called_once_with('Background does not exist, retaining cached background')
def test_save_theme_same_image(self, mocked_create_paths, mocked_shutil): """ Test that we don't try to overwrite a theme background image with itself """ # GIVEN: A new theme manager instance, with mocked builtins.open, copyfile, # theme, create_paths, thememanager-attributes and background # .filename path is the same as the source path. theme_manager = ThemeManager(None) theme_manager.old_background_image_path = None theme_manager.update_preview_images = MagicMock() theme_manager.theme_path = MagicMock() mocked_theme = MagicMock() mocked_theme.theme_name = 'themename' mocked_theme.extract_formatted_xml = MagicMock() mocked_theme.extract_formatted_xml.return_value = 'fake_theme_xml'.encode() file_path_1 = RESOURCE_PATH / 'church.jpg' mocked_theme.background_filename = file_path_1 mocked_theme.background_source = file_path_1 # WHEN: Calling save_theme with both background paths to the same image theme_manager.save_theme(mocked_theme) # THEN: The mocked_copyfile should not have been called assert mocked_shutil.copyfile.called is False, 'copyfile should not be called'