def test_check_sound_files_do_exist(self, monkeypatch): """ Test that `check_sound_files_do_exist()` checks whether the sound files exist at their respective locations. """ get_sound_root_directory_mock = MagicMock(return_value="root") is_file_mock = MagicMock() monkeypatch.setattr("src.sound.sound_checker.utils.get_sound_root_directory", get_sound_root_directory_mock) monkeypatch.setattr("src.sound.sound_checker.os.path.isfile", is_file_mock) group_1 = SoundGroup( {"name": "Group 1", "sounds": [{"name": "Sound 1", "files": ["sound-1.wav", "sound-2.wav"]}]} ) group_2 = SoundGroup({"name": "Group 2", "sounds": [{"name": "Sound 2", "files": ["sound-3.wav"]}]}) SoundChecker([group_1, group_2], "default/dir").check_sound_files_do_exist() expected_get_root_calls = [ call(group_1, group_1.sounds[0], default_dir="default/dir"), call(group_2, group_2.sounds[0], default_dir="default/dir"), ] assert get_sound_root_directory_mock.call_count == 2 # There are two sounds in total get_sound_root_directory_mock.assert_has_calls(expected_get_root_calls, any_order=True) expected_is_file_calls = [ call(os.path.join("root", group_1.sounds[0].files[0].file)), call(os.path.join("root", group_1.sounds[0].files[1].file)), call(os.path.join("root", group_2.sounds[0].files[0].file)), ] assert is_file_mock.call_count == 3 # There are three sound files in total to check is_file_mock.assert_has_calls(expected_is_file_calls, any_order=True)
def test_groups_are_created(self, minimal_sound_manager_config): sound_group_1_config = {"name": "Some Sound Group 1", "sounds": []} sound_group_2_config = {"name": "Some Sound Group 2", "sounds": []} minimal_sound_manager_config["groups"] = [ sound_group_1_config, sound_group_2_config ] sound_manager = SoundManager(minimal_sound_manager_config) assert len(sound_manager.groups) == 2 assert sound_manager.groups[0] == SoundGroup(sound_group_1_config) assert sound_manager.groups[1] == SoundGroup(sound_group_2_config)
def test_not_equal_if_different_number_of_sounds(self): group_1 = SoundGroup({"name": "Group", "sounds": []}) group_2 = SoundGroup({ "name": "Group", "sounds": [{ "name": "Sound", "files": [] }] }) assert group_1 != group_2 assert group_2 != group_1
def test_sort_in_config(self, minimal_sound_manager_config): name_starts_with_n_config = { "name": "Not First In The Alphabet", "sounds": [] } name_starts_with_a_config = {"name": "Alphabet", "sounds": []} minimal_sound_manager_config["sort"] = False minimal_sound_manager_config["groups"] = [ name_starts_with_n_config, name_starts_with_a_config ] sound_manager = SoundManager(minimal_sound_manager_config) assert sound_manager.groups[0] == SoundGroup(name_starts_with_n_config) assert sound_manager.groups[1] == SoundGroup(name_starts_with_a_config)
def test_check_sound_files_do_exist_raises_exception_if_file_invalid(self): """ Test that `check_sound_files_do_exist()´ raises a `ValueError` if the file path of a sound is invalid. """ group = SoundGroup({"name": "Group 1", "sounds": [{"name": "Sound 1", "files": ["file-does-not-exist.wav"]}]}) with pytest.raises(ValueError): SoundChecker([group], "dir").check_sound_files_do_exist()
def test_sound_tuple_generator(self): """ Test that calling `sound_tuple_generator()` returns an iterator over all tuples of the form (group, sound, sound_file). """ group_config = { "name": "Group 1", "sounds": [ { "name": "Sound 1", "files": ["sound_file_1.wav"] }, { "name": "Sound 2", "files": ["sound_file_2.wav", "sound_file_3.wav"] }, ], } groups = [SoundGroup(group_config)] generator = utils.sound_tuple_generator(groups) first_tuple = (groups[0], groups[0].sounds[0], groups[0].sounds[0].files[0]) second_tuple = (groups[0], groups[0].sounds[1], groups[0].sounds[1].files[0]) third_tuple = (groups[0], groups[0].sounds[1], groups[0].sounds[1].files[1]) assert first_tuple == next(generator) assert second_tuple == next(generator) assert third_tuple == next(generator) with pytest.raises(StopIteration): next(generator)
def test_check_sound_files_do_exist_re_raises_exception(self): """ Test that `check_sound_files_do_exist()´ re-raises the `ValueError` raised by `utils.get_sound_root_directory()` if the sound has no root directory. """ group = SoundGroup({"name": "Group 1", "sounds": [{"name": "Sound 1", "files": ["no-directory.wav"]}]}) with pytest.raises(ValueError): SoundChecker([group], None).check_sound_files_do_exist()
def test_not_equal_if_different_sounds(self): group_1 = SoundGroup({ "name": "Group", "sounds": [{ "name": "Unique Sound", "files": [] }] }) group_2 = SoundGroup({ "name": "Group", "sounds": [{ "name": "Other Sound", "files": [] }] }) assert group_1 != group_2 assert group_2 != group_1
def test_sounds_are_created(self, minimal_sound_group_config): sound_1_config = {"name": "Some Sound 1", "files": []} sound_2_config = {"name": "Some Sound 2", "files": []} minimal_sound_group_config["sounds"] = [sound_1_config, sound_2_config] sound_group = SoundGroup(minimal_sound_group_config) assert len(sound_group.sounds) == 2 assert sound_group.sounds[0] == Sound(sound_1_config) assert sound_group.sounds[1] == Sound(sound_2_config)
def test_check_sound_files_can_be_played(self): """ Test that `check_sound_files_can_be_played()` checks that the sound files can be played with pygame. """ group_1 = SoundGroup( { "name": "Group 1", "sounds": [ {"name": "Supported Format 1", "files": ["supported_format.wav"]}, {"name": "Supported Format 2", "files": ["supported_format.wav"]}, ], } ) group_2 = SoundGroup( {"name": "Group 2", "sounds": [{"name": "Supported Format 3", "files": ["supported_format.ogg"]}]} ) SoundChecker([group_1, group_2], "tests/_resources").check_sound_files_can_be_played()
def example_group(self): group_config = { "name": "Group 1", "sounds": [{ "name": "Sound 1", "files": ["sound_1.wav"] }] } return SoundGroup(group_config)
def test_sort_in_config(self, minimal_sound_group_config): name_starts_with_n_config = { "name": "Not First In The Alphabet", "files": [] } name_starts_with_a_config = {"name": "Alphabet", "files": []} minimal_sound_group_config["sort"] = False minimal_sound_group_config["sounds"] = [ name_starts_with_n_config, name_starts_with_a_config ] sound_group = SoundGroup(minimal_sound_group_config) assert sound_group.sounds[0] == Sound(name_starts_with_n_config) assert sound_group.sounds[1] == Sound(name_starts_with_a_config)
def test_convert_incompatible_wav_files_converts_incompatible_file(self): """ Test that `convert_incompatible_wav_files()` will recognize incompatible wav files and convert them, storing the result in the cache directory with the file hash as the name. """ incompatible_file_path = "tests/_resources/unsupported_format.wav" incompatible_file_hash = cache.get_file_hash(incompatible_file_path) converted_file_path = os.path.join(cache.CONVERSION_CACHE_DIR, f"{incompatible_file_hash}.wav") if os.path.exists(converted_file_path): # Delete converted file if present os.remove(converted_file_path) group = SoundGroup({"name": "Group", "sounds": [{"name": "Sound", "files": ["unsupported_format.wav"]}]}) sound_file = group.sounds[0].files[0] assert sound_file.file == "unsupported_format.wav" SoundChecker([group], "tests/_resources").convert_incompatible_wav_files() assert os.path.exists(converted_file_path) assert sound_file.file == converted_file_path os.remove(converted_file_path)
def test_check_sound_files_can_be_played_raises_type_error_if_file_not_playable(self): """ Test that `check_sound_files_can_be_played()` raises a `TypeError` if a sound file cannot be played with pygame. """ # Unsupported WAV formats will cause a double free or corruption error on most OS, so ignore this test # For more information see issue #406 on the pygame repository if platform.system() != "Windows": return group = SoundGroup( { "name": "Group 1", "sounds": [ {"name": "Supported Format", "files": ["supported_format.wav"]}, {"name": "Unsupported Format", "files": ["unsupported_format.wav"]}, ], } ) with pytest.raises(TypeError): SoundChecker([group], "tests/_resources").check_sound_files_can_be_played()
def test_convert_incompatible_wav_files_does_not_convert_if_in_cache(self, monkeypatch): """ Test that `convert_incompatible_wav_files()` will not convert an incompatible file if the converted file is already present in the cache. In this case only the pointer to the file should be changed. """ incompatible_file_path = "tests/_resources/unsupported_format.wav" incompatible_file_hash = cache.get_file_hash(incompatible_file_path) converted_file_path = os.path.join(cache.CONVERSION_CACHE_DIR, f"{incompatible_file_hash}.wav") if os.path.exists(converted_file_path): # Delete converted file if present os.remove(converted_file_path) group = SoundGroup({"name": "Group", "sounds": [{"name": "Sound", "files": ["unsupported_format.wav"]}]}) sound_file = group.sounds[0].files[0] SoundChecker([group], "tests/_resources").convert_incompatible_wav_files() # Now converted file is in cache assert os.path.exists(converted_file_path) convert_file_mock = MagicMock() monkeypatch.setattr("src.sound.sound_checker.convert_file", convert_file_mock) SoundChecker([group], "tests/_resources").convert_incompatible_wav_files() # Should not convert convert_file_mock.assert_not_called() assert sound_file.file == converted_file_path os.remove(converted_file_path)
def test_minimal_dict_as_config(self, minimal_sound_group_config): sound_group = SoundGroup(minimal_sound_group_config) assert sound_group.name == "Some Name" assert len(sound_group.sounds) == 0
def test_not_equal_if_different_attributes(self): group_1 = SoundGroup({"name": "Unique Group", "sounds": []}) group_2 = SoundGroup({"name": "Normal Group", "sounds": []}) assert group_1 != group_2 assert group_2 != group_1
def test_equal_if_same_config(self): group_1 = SoundGroup({"name": "Group", "sounds": []}) group_2 = SoundGroup({"name": "Group", "sounds": []}) assert group_1 == group_2 assert group_2 == group_1
def test_sounds_use_tuple_instead_of_list(self, minimal_sound_group_config): sound_group = SoundGroup(minimal_sound_group_config) assert isinstance(sound_group.sounds, tuple)
def test_directory_in_config(self, minimal_sound_group_config): minimal_sound_group_config["directory"] = "/some/dir/" sound_group = SoundGroup(minimal_sound_group_config) assert sound_group.directory == "/some/dir/"
def test_directory_is_none_by_default(self, minimal_sound_group_config): sound_group = SoundGroup(minimal_sound_group_config) assert sound_group.directory is None
def test_not_equal_if_different_types(self): config = {"name": "Group", "sounds": [{"name": "Sound", "files": []}]} group = SoundGroup(config) assert config != group assert group != config