Skip to content

Commit

Permalink
整理: CoreMockMockCoreWrapper で置き換え (#1434)
Browse files Browse the repository at this point in the history
* add: dev dependency として `pytest-mock` を導入

* refactor: `CoreMock` を `MockCoreWrapper` と pytest-mock で置き換え

* refactor: `pytest-mock` を `unittest.mock.MagicMock` で置き換え

* fix: lint

* fix: spy に関する型チェックを回避
  • Loading branch information
tarepan authored Jun 27, 2024
1 parent 3a666dd commit 09df944
Showing 1 changed file with 9 additions and 82 deletions.
91 changes: 9 additions & 82 deletions test/unit/tts_pipeline/test_tts_engine.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json
"""TTSEngine のテスト"""

from test.utility import pydantic_to_native_type, round_floats, summarize_big_ndarray
from unittest.mock import Mock
from unittest.mock import MagicMock

import numpy as np
import pytest
from numpy.typing import NDArray
from syrupy.assertion import SnapshotAssertion

from voicevox_engine.dev.core.mock import MockCoreWrapper
Expand All @@ -29,81 +29,6 @@
from .tts_utils import gen_mora


def yukarin_s_mock(
length: int, phoneme_list: NDArray[np.int64], style_id: NDArray[np.int64]
) -> NDArray[np.float32]:
result = []
# mockとしての適当な処理、特に意味はない
for i in range(length):
result.append(round((phoneme_list[i] * 0.0625 + style_id).item(), 2))
return np.array(result, dtype=np.float32)


def yukarin_sa_mock(
length: int,
vowel_phoneme_list: NDArray[np.int64],
consonant_phoneme_list: NDArray[np.int64],
start_accent_list: NDArray[np.int64],
end_accent_list: NDArray[np.int64],
start_accent_phrase_list: NDArray[np.int64],
end_accent_phrase_list: NDArray[np.int64],
style_id: NDArray[np.int64],
) -> NDArray[np.float32]:
result = []
# mockとしての適当な処理、特に意味はない
for i in range(length):
result.append(
round(
(
(
vowel_phoneme_list[0][i]
+ consonant_phoneme_list[0][i]
+ start_accent_list[0][i]
+ end_accent_list[0][i]
+ start_accent_phrase_list[0][i]
+ end_accent_phrase_list[0][i]
)
* 0.0625
+ style_id
).item(),
2,
)
)
return np.array(result, dtype=np.float32)[np.newaxis]


def decode_mock(
length: int,
phoneme_size: int,
f0: NDArray[np.float32],
phoneme: NDArray[np.float32],
style_id: NDArray[np.int64],
) -> NDArray[np.float32]:
result = []
# mockとしての適当な処理、特に意味はない
for i in range(length):
result += [
(f0[i, 0] * (np.where(phoneme[i] == 1)[0] / phoneme_size) + style_id)
] * 256
return np.array(result, dtype=np.float32)


class MockCore:
default_sampling_rate = 24000
yukarin_s_forward = Mock(side_effect=yukarin_s_mock)
yukarin_sa_forward = Mock(side_effect=yukarin_sa_mock)
decode_forward = Mock(side_effect=decode_mock)

def metas(self) -> str:
return ""

def supported_devices(self) -> str:
return json.dumps({"cpu": True, "cuda": False, "dml": False})

def is_model_loaded(self, style_id: str) -> bool:
return True


def test_to_flatten_phonemes() -> None:
"""Test `to_flatten_phonemes`."""
# Inputs
Expand Down Expand Up @@ -192,9 +117,10 @@ def test_to_flatten_moras() -> None:


def test_update_length() -> None:
core = MockCore()
core = MockCoreWrapper()
core.yukarin_s_forward = MagicMock(wraps=core.yukarin_s_forward) # type: ignore[method-assign]
_yukarin_s_mock = core.yukarin_s_forward
tts_engine = TTSEngine(core=core) # type: ignore[arg-type]
tts_engine = TTSEngine(core=core)
# Inputs
hello_hiho = _gen_hello_hiho_accent_phrases()
# Indirect Outputs(yukarin_sに渡される値)
Expand All @@ -220,9 +146,10 @@ def test_update_length() -> None:


def test_update_pitch() -> None:
core = MockCore()
core = MockCoreWrapper()
core.yukarin_sa_forward = MagicMock(wraps=core.yukarin_sa_forward) # type: ignore[method-assign]
_yukarin_sa_mock = core.yukarin_sa_forward
tts_engine = TTSEngine(core=core) # type: ignore[arg-type]
tts_engine = TTSEngine(core=core)

# 空のリストでエラーを吐かないか
# Inputs
Expand Down

0 comments on commit 09df944

Please sign in to comment.