def test_load_templates( self, mocked_environment_class, mocked_file_system_loader_class, mocked_package_loader_class, mocked_choice_loader_class, mocked_check_template, ): """Test to load templates for text """ mocked_environment_class.return_value.filters = {} # create object text_generator = TextGenerator("package.templates", Path("directory"), filenames={"text": "text.ass"}) # pre assert there are no templates self.assertIsNone(text_generator.environment) # call the method text_generator.load_templates() # assert there are templates defined self.assertIsNotNone(text_generator.environment) mocked_file_system_loader_class.assert_called_with(Path("directory")) mocked_package_loader_class.assert_called_with("package", "templates") mocked_choice_loader_class.assert_called_with([ mocked_file_system_loader_class.return_value, mocked_package_loader_class.return_value, ]) mocked_environment_class.assert_called_with( loader=mocked_choice_loader_class.return_value) mocked_check_template.assert_called_once_with(text_generator, "text", "text.ass")
def test_load_templates_custom(self): """Test to load custom templates using an existing directory """ with TempDir() as temp: # prepare directory with path("dakara_player.resources.templates", "idle.ass") as file: Path(file).copy(temp) with path("dakara_player.resources.templates", "transition.ass") as file: Path(file).copy(temp) # create object text_generator = TextGenerator( package="dakara_player.resources.templates", directory=temp, filenames={ "idle": "idle.ass", "transition": "transition.ass" }, ) # call the method text_generator.load_templates() # assert there are templates defined loader_custom, loader_default = text_generator.environment.loader.loaders self.assertIn("idle.ass", loader_custom.list_templates()) self.assertIn("transition.ass", loader_custom.list_templates())
def test_check_template_default(self, mocked_get_environment_loaders): """Test to find a default template """ mocked_loader_custom = MagicMock() mocked_loader_custom.list_templates.return_value = [] mocked_loader_default = MagicMock() mocked_loader_default.list_templates.return_value = ["idle.ass"] mocked_get_environment_loaders.return_value = [ mocked_loader_custom, mocked_loader_default, ] with self.assertLogs("dakara_player.text_generator", "DEBUG") as logger: text_generator = TextGenerator("package", filenames={"idle": "idle.ass"}) text_generator.check_template("idle", "idle.ass") self.assertListEqual( logger.output, [ "DEBUG:dakara_player.text_generator:Loading default idle " "text template file 'idle.ass'" ], )
def test_load_templates_default(self): """Test to load default templates using an existing directory Integration test. """ with TempDir() as temp: # create object text_generator = TextGenerator( package="dakara_player.resources.templates", directory=temp, filenames={ "idle": "idle.ass", "transition": "transition.ass" }, ) # call the method text_generator.load_templates() # assert there are templates defined loader_custom, loader_default = text_generator.environment.loader.loaders self.assertNotIn("idle.ass", loader_custom.list_templates()) self.assertNotIn("transition.ass", loader_custom.list_templates()) self.assertIn("idle.ass", loader_default.list_templates()) self.assertIn("transition.ass", loader_default.list_templates())
def test_convert_icon_none(self): """Test the convertion of a null icon name is handled """ # create object text_generator = TextGenerator("package") self.assertEqual(text_generator.convert_icon(None), "")
def test_convert_icon_unavailable(self): """Test the convertion of an unavailable icon name to a generic code """ # create object text_generator = TextGenerator("package") self.assertEqual(text_generator.convert_icon("unavailable"), " ")
def test_convert_icon(self): """Test the convertion of an available icon name to its code """ # create object text_generator = TextGenerator("package") text_generator.icon_map = {"music": "0xf001"} self.assertEqual(text_generator.convert_icon("music"), "\uf001") self.assertEqual(text_generator.convert_icon("other"), " ")
def test_load(self, mocked_load_icon_map, mocked_load_templates): """Test the load method """ # create ojbect text_generator = TextGenerator("package") # call the method text_generator.load() # assert the call mocked_load_icon_map.assert_called_once_with() mocked_load_templates.assert_called_once_with()
def test_convert_link_type_name(self): """Test the convertion of a link type to its long name """ # create object text_generator = TextGenerator("package") self.assertEqual(text_generator.convert_link_type_name("OP"), "Opening") self.assertEqual(text_generator.convert_link_type_name("ED"), "Ending") self.assertEqual(text_generator.convert_link_type_name("IN"), "Insert song") self.assertEqual(text_generator.convert_link_type_name("IS"), "Image song")
def setUp(self): # create info dictionary self.idle_info = {"notes": ["VLC 0.0.0", "Dakara player 0.0.0"]} # create playlist entry self.playlist_entry = { "song": { "title": "Song title", "artists": [{ "name": "Artist name" }], "works": [{ "work": { "title": "Work title", "subtitle": "Subtitle of the work", "work_type": { "name": "Work type name", "icon_name": "music", }, }, "link_type": "OP", "link_type_number": 1, "episodes": "1, 2, 3", }], "file_path": "path/of/the/file", }, "owner": { "username": "******" }, "use_instrumental": True, "date_created": "1970-01-01T00:00:00.00", } # create text generator object self.text_generator = TextGenerator( package="dakara_player.resources.templates", filenames={ "idle": "idle.ass", "transition": "transition.ass" }, ) self.text_generator.load()
def test_check_template_not_found(self, mocked_get_environment_loaders): """Test to find an unaccessible template """ mocked_loader_custom = MagicMock() mocked_loader_custom.list_templates.return_value = [] mocked_loader_default = MagicMock() mocked_loader_default.list_templates.return_value = [] mocked_get_environment_loaders.return_value = [ mocked_loader_custom, mocked_loader_default, ] with self.assertRaisesRegex( TemplateNotFoundError, "No idle text template file found for 'idle.ass'"): text_generator = TextGenerator("package", filenames={"idle": "idle.ass"}) text_generator.check_template("idle", "idle.ass")
def test_load_icon_map(self, mocked_loads, mocked_read_text): """Test to load the icon map """ # create the mock mocked_loads.return_value = {"name": "value"} mocked_read_text.return_value = '{"name": "value"}' # create the object text_generator = TextGenerator("package") # pre assert there are not icon map self.assertDictEqual(text_generator.icon_map, {}) # call the method text_generator.load_icon_map() # assert there is an icon map self.assertDictEqual(text_generator.icon_map, {"name": "value"}) # assert the mock mocked_loads.assert_called_with(mocked_read_text.return_value)
def init_worker(self, config, tempdir, warn_long_exit=True): """Initialize the base objects of the media player. Actions performed in this method should not have any side effects (query file system, etc.). Args: config (dict): Dictionary of configuration. tempdir (path.Path): Path of the temporary directory. warn_long_exit (bool): If True, the class will display a warning message if the media player takes too long to stop. """ self.check_is_available() # karaoke parameters self.fullscreen = config.get("fullscreen", False) self.kara_folder_path = Path(config.get("kara_folder", "")) # inner objects self.playlist_entry = None self.callbacks = {} self.warn_long_exit = warn_long_exit # set durations config_durations = config.get("durations") or {} self.durations = { "idle": IDLE_DURATION, "transition": config_durations.get("transition_duration", TRANSITION_DURATION), } # set text paths self.text_paths = { "idle": tempdir / IDLE_TEXT_NAME, "transition": tempdir / TRANSITION_TEXT_NAME, } # set text generator config_texts = config.get("templates") or {} self.text_generator = TextGenerator( package="dakara_player.resources.templates", directory=get_user_directory().expand() / "templates", filenames={ "transition": config_texts.get("transition_template_name", TRANSITION_TEXT_NAME), "idle": config_texts.get("idle_template_name", IDLE_TEXT_NAME), }, ) # set background loader config_backgrounds = config.get("backgrounds") or {} self.background_loader = BackgroundLoader( destination=tempdir, package="dakara_player.resources.backgrounds", directory=get_user_directory().expand() / "backgrounds", filenames={ "transition": config_backgrounds.get("transition_background_name", TRANSITION_BG_NAME), "idle": config_backgrounds.get("idle_background_name", IDLE_BG_NAME), }, ) # set default callbacks self.set_default_callbacks() # call specialized constructor self.init_player(config, tempdir)