From 84bd33f779d4007bbdeb65e56f787152332f83eb Mon Sep 17 00:00:00 2001 From: tarepan Date: Fri, 24 May 2024 08:31:05 +0900 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86:=20preset=20=E3=83=86?= =?UTF-8?q?=E3=82=B9=E3=83=88=E3=82=92=20pytest=20=E5=8C=96=20(#1273)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refactor: preset テストを pytest 化 --- test/preset/test_preset.py | 628 ++++++++++++++++++------------------- 1 file changed, 313 insertions(+), 315 deletions(-) diff --git a/test/preset/test_preset.py b/test/preset/test_preset.py index d7600189f..17c66fe16 100644 --- a/test/preset/test_preset.py +++ b/test/preset/test_preset.py @@ -1,8 +1,8 @@ from os import remove from pathlib import Path from shutil import copyfile -from tempfile import TemporaryDirectory -from unittest import TestCase + +import pytest from voicevox_engine.preset.Preset import Preset from voicevox_engine.preset.PresetError import PresetInputError, PresetInternalError @@ -14,321 +14,319 @@ presets_test_4_yaml_path = Path("test/preset/presets-test-4.yaml") -class TestPresetManager(TestCase): - def setUp(self) -> None: - self.tmp_dir = TemporaryDirectory() - self.tmp_dir_path = Path(self.tmp_dir.name) - - def tearDown(self) -> None: - self.tmp_dir.cleanup() - - def test_validation(self) -> None: - preset_manager = PresetManager(preset_path=presets_test_1_yaml_path) - presets = preset_manager.load_presets() - self.assertFalse(presets is None) - - def test_validation_same(self) -> None: - preset_manager = PresetManager(preset_path=presets_test_1_yaml_path) - presets = preset_manager.load_presets() - presets2 = preset_manager.load_presets() - self.assertFalse(presets is None) - self.assertEqual(presets, presets2) - - def test_validation_2(self) -> None: - preset_manager = PresetManager(preset_path=presets_test_2_yaml_path) - with self.assertRaises( - PresetInternalError, msg="プリセットの設定ファイルにミスがあります" - ): - preset_manager.load_presets() - - def test_preset_id(self) -> None: - preset_manager = PresetManager(preset_path=presets_test_3_yaml_path) - with self.assertRaises( - PresetInternalError, msg="プリセットのidに重複があります" - ): - preset_manager.load_presets() - - def test_empty_file(self) -> None: - preset_manager = PresetManager(preset_path=presets_test_4_yaml_path) - with self.assertRaises( - PresetInternalError, msg="プリセットの設定ファイルが空の内容です" - ): - preset_manager.load_presets() - - def test_not_exist_file(self) -> None: - preset_manager = PresetManager(preset_path=Path("test/presets-dummy.yaml")) - with self.assertRaises( - PresetInternalError, msg="プリセットの設定ファイルが見つかりません" - ): - preset_manager.load_presets() - - def test_add_preset(self) -> None: - temp_path = self.tmp_dir_path / "presets-test-temp.yaml" - copyfile(presets_test_1_yaml_path, temp_path) - preset_manager = PresetManager(preset_path=temp_path) - preset = Preset( - **{ - "id": 10, - "name": "test10", - "speaker_uuid": "7ffcb7ce-00ec-4bdc-82cd-45a8889e43ff", - "style_id": 2, - "speedScale": 1, - "pitchScale": 1, - "intonationScale": 0.5, - "volumeScale": 1, - "prePhonemeLength": 0.1, - "postPhonemeLength": 0.1, - } - ) - id = preset_manager.add_preset(preset) - self.assertEqual(id, 10) - self.assertEqual(len(preset_manager.presets), 3) - for _preset in preset_manager.presets: - if _preset.id == id: - self.assertEqual(_preset, preset) - remove(temp_path) - - def test_add_preset_load_failure(self) -> None: - preset_manager = PresetManager(preset_path=presets_test_2_yaml_path) - with self.assertRaises( - PresetInternalError, msg="プリセットの設定ファイルにミスがあります" - ): - preset_manager.add_preset( - Preset( - **{ - "id": 1, - "name": "", - "speaker_uuid": "", - "style_id": 0, - "speedScale": 0, - "pitchScale": 0, - "intonationScale": 0, - "volumeScale": 0, - "prePhonemeLength": 0, - "postPhonemeLength": 0, - } - ) - ) +def test_validation() -> None: + preset_manager = PresetManager(preset_path=presets_test_1_yaml_path) + presets = preset_manager.load_presets() + assert presets is not None - def test_add_preset_conflict_id(self) -> None: - temp_path = self.tmp_dir_path / "presets-test-temp.yaml" - copyfile(presets_test_1_yaml_path, temp_path) - preset_manager = PresetManager(preset_path=temp_path) - preset = Preset( - **{ - "id": 2, - "name": "test3", - "speaker_uuid": "7ffcb7ce-00ec-4bdc-82cd-45a8889e43ff", - "style_id": 2, - "speedScale": 1, - "pitchScale": 1, - "intonationScale": 0.5, - "volumeScale": 1, - "prePhonemeLength": 0.1, - "postPhonemeLength": 0.1, - } - ) - id = preset_manager.add_preset(preset) - self.assertEqual(id, 3) - self.assertEqual(len(preset_manager.presets), 3) - for _preset in preset_manager.presets: - if _preset.id == id: - self.assertEqual(_preset, preset) - remove(temp_path) - - def test_add_preset_conflict_id2(self) -> None: - temp_path = self.tmp_dir_path / "presets-test-temp.yaml" - copyfile(presets_test_1_yaml_path, temp_path) - preset_manager = PresetManager(preset_path=temp_path) - preset = Preset( - **{ - "id": -1, - "name": "test3", - "speaker_uuid": "7ffcb7ce-00ec-4bdc-82cd-45a8889e43ff", - "style_id": 2, - "speedScale": 1, - "pitchScale": 1, - "intonationScale": 0.5, - "volumeScale": 1, - "prePhonemeLength": 0.1, - "postPhonemeLength": 0.1, - } - ) - id = preset_manager.add_preset(preset) - self.assertEqual(id, 3) - self.assertEqual(len(preset_manager.presets), 3) - for _preset in preset_manager.presets: - if _preset.id == id: - self.assertEqual(_preset, preset) - remove(temp_path) - - def test_add_preset_write_failure(self) -> None: - temp_path = self.tmp_dir_path / "presets-test-temp.yaml" - copyfile(presets_test_1_yaml_path, temp_path) - preset_manager = PresetManager(preset_path=temp_path) - preset = Preset( - **{ - "id": 10, - "name": "test10", - "speaker_uuid": "7ffcb7ce-00ec-4bdc-82cd-45a8889e43ff", - "style_id": 2, - "speedScale": 1, - "pitchScale": 1, - "intonationScale": 0.5, - "volumeScale": 1, - "prePhonemeLength": 0.1, - "postPhonemeLength": 0.1, - } - ) + +def test_validation_same() -> None: + preset_manager = PresetManager(preset_path=presets_test_1_yaml_path) + presets = preset_manager.load_presets() + presets2 = preset_manager.load_presets() + assert presets is not None + assert presets == presets2 + + +def test_validation_2() -> None: + preset_manager = PresetManager(preset_path=presets_test_2_yaml_path) + true_msg = "プリセットの設定ファイルにミスがあります" + with pytest.raises(PresetInternalError, match=true_msg): preset_manager.load_presets() - preset_manager._refresh_cache = lambda: None # type:ignore[method-assign] - preset_manager.preset_path = "" # type: ignore[assignment] - with self.assertRaises( - PresetInternalError, msg="プリセットの設定ファイルが見つかりません" - ): - preset_manager.add_preset(preset) - self.assertEqual(len(preset_manager.presets), 2) - remove(temp_path) - - def test_update_preset(self) -> None: - temp_path = self.tmp_dir_path / "presets-test-temp.yaml" - copyfile(presets_test_1_yaml_path, temp_path) - preset_manager = PresetManager(preset_path=temp_path) - preset = Preset( - **{ - "id": 1, - "name": "test1 new", - "speaker_uuid": "7ffcb7ce-00ec-4bdc-82cd-45a8889e43ff", - "style_id": 2, - "speedScale": 1, - "pitchScale": 1, - "intonationScale": 0.5, - "volumeScale": 1, - "prePhonemeLength": 0.1, - "postPhonemeLength": 0.1, - } - ) - id = preset_manager.update_preset(preset) - self.assertEqual(id, 1) - self.assertEqual(len(preset_manager.presets), 2) - for _preset in preset_manager.presets: - if _preset.id == id: - self.assertEqual(_preset, preset) - remove(temp_path) - - def test_update_preset_load_failure(self) -> None: - preset_manager = PresetManager(preset_path=presets_test_2_yaml_path) - with self.assertRaises( - PresetInternalError, msg="プリセットの設定ファイルにミスがあります" - ): - preset_manager.update_preset( - Preset( - **{ - "id": 1, - "name": "", - "speaker_uuid": "", - "style_id": 0, - "speedScale": 0, - "pitchScale": 0, - "intonationScale": 0, - "volumeScale": 0, - "prePhonemeLength": 0, - "postPhonemeLength": 0, - } - ) - ) - def test_update_preset_not_found(self) -> None: - temp_path = self.tmp_dir_path / "presets-test-temp.yaml" - copyfile(presets_test_1_yaml_path, temp_path) - preset_manager = PresetManager(preset_path=temp_path) - preset = Preset( - **{ - "id": 10, - "name": "test1 new", - "speaker_uuid": "7ffcb7ce-00ec-4bdc-82cd-45a8889e43ff", - "style_id": 2, - "speedScale": 1, - "pitchScale": 1, - "intonationScale": 0.5, - "volumeScale": 1, - "prePhonemeLength": 0.1, - "postPhonemeLength": 0.1, - } - ) - with self.assertRaises( - PresetInputError, msg="更新先のプリセットが存在しません" - ): - preset_manager.update_preset(preset) - self.assertEqual(len(preset_manager.presets), 2) - remove(temp_path) - - def test_update_preset_write_failure(self) -> None: - temp_path = self.tmp_dir_path / "presets-test-temp.yaml" - copyfile(presets_test_1_yaml_path, temp_path) - preset_manager = PresetManager(preset_path=temp_path) - preset = Preset( - **{ - "id": 1, - "name": "test1 new", - "speaker_uuid": "7ffcb7ce-00ec-4bdc-82cd-45a8889e43ff", - "style_id": 2, - "speedScale": 1, - "pitchScale": 1, - "intonationScale": 0.5, - "volumeScale": 1, - "prePhonemeLength": 0.1, - "postPhonemeLength": 0.1, - } - ) + +def test_preset_id() -> None: + preset_manager = PresetManager(preset_path=presets_test_3_yaml_path) + true_msg = "プリセットのidに重複があります" + with pytest.raises(PresetInternalError, match=true_msg): + preset_manager.load_presets() + + +def test_empty_file() -> None: + preset_manager = PresetManager(preset_path=presets_test_4_yaml_path) + true_msg = "プリセットの設定ファイルが空の内容です" + with pytest.raises(PresetInternalError, match=true_msg): preset_manager.load_presets() - preset_manager._refresh_cache = lambda: None # type:ignore[method-assign] - preset_manager.preset_path = "" # type: ignore[assignment] - with self.assertRaises( - PresetInternalError, msg="プリセットの設定ファイルが見つかりません" - ): - preset_manager.update_preset(preset) - self.assertEqual(len(preset_manager.presets), 2) - self.assertEqual(preset_manager.presets[0].name, "test") - remove(temp_path) - - def test_delete_preset(self) -> None: - temp_path = self.tmp_dir_path / "presets-test-temp.yaml" - copyfile(presets_test_1_yaml_path, temp_path) - preset_manager = PresetManager(preset_path=temp_path) - id = preset_manager.delete_preset(1) - self.assertEqual(id, 1) - self.assertEqual(len(preset_manager.presets), 1) - remove(temp_path) - - def test_delete_preset_load_failure(self) -> None: - preset_manager = PresetManager(preset_path=presets_test_2_yaml_path) - with self.assertRaises( - PresetInternalError, msg="プリセットの設定ファイルにミスがあります" - ): - preset_manager.delete_preset(10) - - def test_delete_preset_not_found(self) -> None: - temp_path = self.tmp_dir_path / "presets-test-temp.yaml" - copyfile(presets_test_1_yaml_path, temp_path) - preset_manager = PresetManager(preset_path=temp_path) - with self.assertRaises( - PresetInputError, msg="削除対象のプリセットが存在しません" - ): - preset_manager.delete_preset(10) - self.assertEqual(len(preset_manager.presets), 2) - remove(temp_path) - - def test_delete_preset_write_failure(self) -> None: - temp_path = self.tmp_dir_path / "presets-test-temp.yaml" - copyfile(presets_test_1_yaml_path, temp_path) - preset_manager = PresetManager(preset_path=temp_path) + + +def test_not_exist_file() -> None: + preset_manager = PresetManager(preset_path=Path("test/presets-dummy.yaml")) + true_msg = "プリセットの設定ファイルが見つかりません" + with pytest.raises(PresetInternalError, match=true_msg): preset_manager.load_presets() - preset_manager._refresh_cache = lambda: None # type:ignore[method-assign] - preset_manager.preset_path = "" # type: ignore[assignment] - with self.assertRaises( - PresetInternalError, msg="プリセットの設定ファイルが見つかりません" - ): - preset_manager.delete_preset(1) - self.assertEqual(len(preset_manager.presets), 2) - remove(temp_path) + + +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 = Preset( + **{ + "id": 10, + "name": "test10", + "speaker_uuid": "7ffcb7ce-00ec-4bdc-82cd-45a8889e43ff", + "style_id": 2, + "speedScale": 1, + "pitchScale": 1, + "intonationScale": 0.5, + "volumeScale": 1, + "prePhonemeLength": 0.1, + "postPhonemeLength": 0.1, + } + ) + id = preset_manager.add_preset(preset) + assert id == 10 + assert len(preset_manager.presets) == 3 + for _preset in preset_manager.presets: + if _preset.id == id: + assert _preset == preset + remove(temp_path) + + +def test_add_preset_load_failure() -> None: + preset_manager = PresetManager(preset_path=presets_test_2_yaml_path) + true_msg = "プリセットの設定ファイルにミスがあります" + with pytest.raises(PresetInternalError, match=true_msg): + preset_manager.add_preset( + Preset( + **{ + "id": 1, + "name": "", + "speaker_uuid": "", + "style_id": 0, + "speedScale": 0, + "pitchScale": 0, + "intonationScale": 0, + "volumeScale": 0, + "prePhonemeLength": 0, + "postPhonemeLength": 0, + } + ) + ) + + +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 = Preset( + **{ + "id": 2, + "name": "test3", + "speaker_uuid": "7ffcb7ce-00ec-4bdc-82cd-45a8889e43ff", + "style_id": 2, + "speedScale": 1, + "pitchScale": 1, + "intonationScale": 0.5, + "volumeScale": 1, + "prePhonemeLength": 0.1, + "postPhonemeLength": 0.1, + } + ) + id = preset_manager.add_preset(preset) + assert id == 3 + assert len(preset_manager.presets) == 3 + for _preset in preset_manager.presets: + if _preset.id == id: + assert _preset == preset + remove(temp_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 = Preset( + **{ + "id": -1, + "name": "test3", + "speaker_uuid": "7ffcb7ce-00ec-4bdc-82cd-45a8889e43ff", + "style_id": 2, + "speedScale": 1, + "pitchScale": 1, + "intonationScale": 0.5, + "volumeScale": 1, + "prePhonemeLength": 0.1, + "postPhonemeLength": 0.1, + } + ) + id = preset_manager.add_preset(preset) + assert id == 3 + assert len(preset_manager.presets) == 3 + for _preset in preset_manager.presets: + if _preset.id == id: + assert _preset == preset + remove(temp_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 = Preset( + **{ + "id": 10, + "name": "test10", + "speaker_uuid": "7ffcb7ce-00ec-4bdc-82cd-45a8889e43ff", + "style_id": 2, + "speedScale": 1, + "pitchScale": 1, + "intonationScale": 0.5, + "volumeScale": 1, + "prePhonemeLength": 0.1, + "postPhonemeLength": 0.1, + } + ) + 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.add_preset(preset) + assert len(preset_manager.presets) == 2 + remove(temp_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 = Preset( + **{ + "id": 1, + "name": "test1 new", + "speaker_uuid": "7ffcb7ce-00ec-4bdc-82cd-45a8889e43ff", + "style_id": 2, + "speedScale": 1, + "pitchScale": 1, + "intonationScale": 0.5, + "volumeScale": 1, + "prePhonemeLength": 0.1, + "postPhonemeLength": 0.1, + } + ) + id = preset_manager.update_preset(preset) + assert id == 1 + assert len(preset_manager.presets) == 2 + for _preset in preset_manager.presets: + if _preset.id == id: + assert _preset == preset + remove(temp_path) + + +def test_update_preset_load_failure() -> None: + preset_manager = PresetManager(preset_path=presets_test_2_yaml_path) + true_msg = "プリセットの設定ファイルにミスがあります" + with pytest.raises(PresetInternalError, match=true_msg): + preset_manager.update_preset( + Preset( + **{ + "id": 1, + "name": "", + "speaker_uuid": "", + "style_id": 0, + "speedScale": 0, + "pitchScale": 0, + "intonationScale": 0, + "volumeScale": 0, + "prePhonemeLength": 0, + "postPhonemeLength": 0, + } + ) + ) + + +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 = Preset( + **{ + "id": 10, + "name": "test1 new", + "speaker_uuid": "7ffcb7ce-00ec-4bdc-82cd-45a8889e43ff", + "style_id": 2, + "speedScale": 1, + "pitchScale": 1, + "intonationScale": 0.5, + "volumeScale": 1, + "prePhonemeLength": 0.1, + "postPhonemeLength": 0.1, + } + ) + true_msg = "更新先のプリセットが存在しません" + with pytest.raises(PresetInputError, match=true_msg): + preset_manager.update_preset(preset) + assert len(preset_manager.presets) == 2 + remove(temp_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 = Preset( + **{ + "id": 1, + "name": "test1 new", + "speaker_uuid": "7ffcb7ce-00ec-4bdc-82cd-45a8889e43ff", + "style_id": 2, + "speedScale": 1, + "pitchScale": 1, + "intonationScale": 0.5, + "volumeScale": 1, + "prePhonemeLength": 0.1, + "postPhonemeLength": 0.1, + } + ) + 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.update_preset(preset) + assert len(preset_manager.presets) == 2 + assert preset_manager.presets[0].name == "test" + remove(temp_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) + id = preset_manager.delete_preset(1) + assert id == 1 + assert len(preset_manager.presets) == 1 + remove(temp_path) + + +def test_delete_preset_load_failure() -> None: + preset_manager = PresetManager(preset_path=presets_test_2_yaml_path) + true_msg = "プリセットの設定ファイルにミスがあります" + with pytest.raises(PresetInternalError, match=true_msg): + preset_manager.delete_preset(10) + + +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) + true_msg = "削除対象のプリセットが存在しません" + with pytest.raises(PresetInputError, match=true_msg): + preset_manager.delete_preset(10) + assert len(preset_manager.presets) == 2 + remove(temp_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_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)