Skip to content

Commit

Permalink
整理: テスト指摘箇所を改善 (#1315)
Browse files Browse the repository at this point in the history
* refactor: 型チェックで保証済み事項の動的テストを削除

* refactor: テスト用プリセットパスの変数名を明確化

* fix: テスト名を明確化

* fix: テスト意図と実装の乖離を修正
  • Loading branch information
tarepan authored May 28, 2024
1 parent d86007e commit fc73575
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 59 deletions.
80 changes: 40 additions & 40 deletions test/preset/test_preset.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ def test_not_exist_file() -> None:


def test_add_preset(tmp_path: Path) -> None:
temp_path = tmp_path / "presets-test-temp.yaml"
copyfile(presets_test_1_yaml_path, temp_path)
preset_manager = PresetManager(preset_path=temp_path)
preset_path = tmp_path / "presets.yaml"
copyfile(presets_test_1_yaml_path, preset_path)
preset_manager = PresetManager(preset_path=preset_path)
preset = Preset(
**{
"id": 10,
Expand All @@ -83,7 +83,7 @@ def test_add_preset(tmp_path: Path) -> None:
for _preset in preset_manager.presets:
if _preset.id == id:
assert _preset == preset
remove(temp_path)
remove(preset_path)


def test_add_preset_load_failure() -> None:
Expand All @@ -109,9 +109,9 @@ def test_add_preset_load_failure() -> None:


def test_add_preset_conflict_id(tmp_path: Path) -> None:
temp_path = tmp_path / "presets-test-temp.yaml"
copyfile(presets_test_1_yaml_path, temp_path)
preset_manager = PresetManager(preset_path=temp_path)
preset_path = tmp_path / "presets.yaml"
copyfile(presets_test_1_yaml_path, preset_path)
preset_manager = PresetManager(preset_path=preset_path)
preset = Preset(
**{
"id": 2,
Expand All @@ -132,13 +132,13 @@ def test_add_preset_conflict_id(tmp_path: Path) -> None:
for _preset in preset_manager.presets:
if _preset.id == id:
assert _preset == preset
remove(temp_path)
remove(preset_path)


def test_add_preset_conflict_id2(tmp_path: Path) -> None:
temp_path = tmp_path / "presets-test-temp.yaml"
copyfile(presets_test_1_yaml_path, temp_path)
preset_manager = PresetManager(preset_path=temp_path)
preset_path = tmp_path / "presets.yaml"
copyfile(presets_test_1_yaml_path, preset_path)
preset_manager = PresetManager(preset_path=preset_path)
preset = Preset(
**{
"id": -1,
Expand All @@ -159,13 +159,13 @@ def test_add_preset_conflict_id2(tmp_path: Path) -> None:
for _preset in preset_manager.presets:
if _preset.id == id:
assert _preset == preset
remove(temp_path)
remove(preset_path)


def test_add_preset_write_failure(tmp_path: Path) -> None:
temp_path = tmp_path / "presets-test-temp.yaml"
copyfile(presets_test_1_yaml_path, temp_path)
preset_manager = PresetManager(preset_path=temp_path)
preset_path = tmp_path / "presets.yaml"
copyfile(presets_test_1_yaml_path, preset_path)
preset_manager = PresetManager(preset_path=preset_path)
preset = Preset(
**{
"id": 10,
Expand All @@ -187,13 +187,13 @@ def test_add_preset_write_failure(tmp_path: Path) -> None:
with pytest.raises(PresetInternalError, match=true_msg):
preset_manager.add_preset(preset)
assert len(preset_manager.presets) == 2
remove(temp_path)
remove(preset_path)


def test_update_preset(tmp_path: Path) -> None:
temp_path = tmp_path / "presets-test-temp.yaml"
copyfile(presets_test_1_yaml_path, temp_path)
preset_manager = PresetManager(preset_path=temp_path)
preset_path = tmp_path / "presets.yaml"
copyfile(presets_test_1_yaml_path, preset_path)
preset_manager = PresetManager(preset_path=preset_path)
preset = Preset(
**{
"id": 1,
Expand All @@ -214,7 +214,7 @@ def test_update_preset(tmp_path: Path) -> None:
for _preset in preset_manager.presets:
if _preset.id == id:
assert _preset == preset
remove(temp_path)
remove(preset_path)


def test_update_preset_load_failure() -> None:
Expand All @@ -240,9 +240,9 @@ def test_update_preset_load_failure() -> None:


def test_update_preset_not_found(tmp_path: Path) -> None:
temp_path = tmp_path / "presets-test-temp.yaml"
copyfile(presets_test_1_yaml_path, temp_path)
preset_manager = PresetManager(preset_path=temp_path)
preset_path = tmp_path / "presets.yaml"
copyfile(presets_test_1_yaml_path, preset_path)
preset_manager = PresetManager(preset_path=preset_path)
preset = Preset(
**{
"id": 10,
Expand All @@ -261,13 +261,13 @@ def test_update_preset_not_found(tmp_path: Path) -> None:
with pytest.raises(PresetInputError, match=true_msg):
preset_manager.update_preset(preset)
assert len(preset_manager.presets) == 2
remove(temp_path)
remove(preset_path)


def test_update_preset_write_failure(tmp_path: Path) -> None:
temp_path = tmp_path / "presets-test-temp.yaml"
copyfile(presets_test_1_yaml_path, temp_path)
preset_manager = PresetManager(preset_path=temp_path)
preset_path = tmp_path / "presets.yaml"
copyfile(presets_test_1_yaml_path, preset_path)
preset_manager = PresetManager(preset_path=preset_path)
preset = Preset(
**{
"id": 1,
Expand All @@ -290,17 +290,17 @@ def test_update_preset_write_failure(tmp_path: Path) -> None:
preset_manager.update_preset(preset)
assert len(preset_manager.presets) == 2
assert preset_manager.presets[0].name == "test"
remove(temp_path)
remove(preset_path)


def test_delete_preset(tmp_path: Path) -> None:
temp_path = tmp_path / "presets-test-temp.yaml"
copyfile(presets_test_1_yaml_path, temp_path)
preset_manager = PresetManager(preset_path=temp_path)
preset_path = tmp_path / "presets.yaml"
copyfile(presets_test_1_yaml_path, preset_path)
preset_manager = PresetManager(preset_path=preset_path)
id = preset_manager.delete_preset(1)
assert id == 1
assert len(preset_manager.presets) == 1
remove(temp_path)
remove(preset_path)


def test_delete_preset_load_failure() -> None:
Expand All @@ -311,25 +311,25 @@ def test_delete_preset_load_failure() -> None:


def test_delete_preset_not_found(tmp_path: Path) -> None:
temp_path = tmp_path / "presets-test-temp.yaml"
copyfile(presets_test_1_yaml_path, temp_path)
preset_manager = PresetManager(preset_path=temp_path)
preset_path = tmp_path / "presets.yaml"
copyfile(presets_test_1_yaml_path, preset_path)
preset_manager = PresetManager(preset_path=preset_path)
true_msg = "削除対象のプリセットが存在しません"
with pytest.raises(PresetInputError, match=true_msg):
preset_manager.delete_preset(10)
assert len(preset_manager.presets) == 2
remove(temp_path)
remove(preset_path)


def test_delete_preset_write_failure(tmp_path: Path) -> None:
temp_path = tmp_path / "presets-test-temp.yaml"
copyfile(presets_test_1_yaml_path, temp_path)
preset_manager = PresetManager(preset_path=temp_path)
preset_path = tmp_path / "presets.yaml"
copyfile(presets_test_1_yaml_path, preset_path)
preset_manager = PresetManager(preset_path=preset_path)
preset_manager.load_presets()
preset_manager._refresh_cache = lambda: None # type:ignore[method-assign]
preset_manager.preset_path = "" # type: ignore[assignment]
true_msg = "プリセットの設定ファイルが見つかりません"
with pytest.raises(PresetInternalError, match=true_msg):
preset_manager.delete_preset(1)
assert len(preset_manager.presets) == 2
remove(temp_path)
remove(preset_path)
11 changes: 0 additions & 11 deletions test/setting/test_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,6 @@ def test_setting_handler_save(tmp_path: Path) -> None:
assert true_setting == setting


def test_setting_handler_load_cors_policy_mode_type() -> None:
"""`SettingHandler.load()` で正しい型の値を得られる。"""
# Inputs
setting_path = Path("test/setting/setting-test-load-1.yaml")
setting_loader = SettingHandler(setting_path)
# Outputs
setting = setting_loader.load()
# Test
assert isinstance(setting.cors_policy_mode, CorsPolicyMode)


def test_setting_invalid_input() -> None:
"""`Setting` は不正な入力に対してエラーを送出する。"""
# Test
Expand Down
6 changes: 3 additions & 3 deletions test/tts_pipeline/test_mora_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ def test_mora2text() -> None:

def test_mora2text_injective() -> None:
"""異なるモーラが同じ読みがなに対応しないか確認する"""
values = list(mora_phonemes_to_mora_kana.values())
uniq_values = list(set(values))
assert sorted(values) == sorted(uniq_values)
mora_kanas = list(mora_phonemes_to_mora_kana.values())
# NOTE: 同じ読みがなが複数回登場すると set で非重複化して全長が短くなる
assert len(mora_kanas) == len(set(mora_kanas))
10 changes: 5 additions & 5 deletions test/tts_pipeline/test_text_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,21 +229,21 @@ def test_mora_label_features() -> None:
accent_phrase_hiho = AccentPhraseLabel.from_labels(labels_hello_hiho[11:19])


def test_accentphrase_accent() -> None:
def test_accent_phrase_accent() -> None:
"""AccentPhraseLabel に含まれるアクセント位置をテスト"""
assert accent_phrase_hello.accent == 5
assert accent_phrase_hiho.accent == 1


def test_accentphrase_phonemes() -> None:
def test_accent_phrase_phonemes() -> None:
"""AccentPhraseLabel に含まれる音素系列をテスト"""
outputs_hello = space_jointed_phonemes(accent_phrase_hello)
outputs_hiho = space_jointed_phonemes(accent_phrase_hiho)
assert outputs_hello == "k o N n i ch i w a"
assert outputs_hiho == "h i h o d e s U"


def test_accentphrase_features() -> None:
def test_accent_phrase_features() -> None:
"""AccentPhraseLabel に含まれる features をテスト"""
expects = test_case_hello_hiho
assert features(accent_phrase_hello) == expects[1:10]
Expand All @@ -254,15 +254,15 @@ def test_accentphrase_features() -> None:
breath_group_hiho = BreathGroupLabel.from_labels(labels_hello_hiho[11:19])


def test_breathgroup_phonemes() -> None:
def test_breath_group_phonemes() -> None:
"""BreathGroupLabel に含まれる音素系列をテスト"""
outputs_hello = space_jointed_phonemes(breath_group_hello)
outputs_hiho = space_jointed_phonemes(breath_group_hiho)
assert outputs_hello == "k o N n i ch i w a"
assert outputs_hiho == "h i h o d e s U"


def test_breathgroup_features() -> None:
def test_breath_group_features() -> None:
"""BreathGroupLabel に含まれる features をテスト"""
expects = test_case_hello_hiho
assert features(breath_group_hello) == expects[1:10]
Expand Down

0 comments on commit fc73575

Please sign in to comment.