Skip to content

Commit

Permalink
整理: 辞書単語テストの形式を統一 (#1443)
Browse files Browse the repository at this point in the history
* refactor: 辞書単語テストの形式を整理

* refactor: ユーザー辞書テストの docstring を明確化

* refactor: ユーザー辞書テストの docstring を明確化

* Apply suggestions from code review

---------

Co-authored-by: Hiroshiba <[email protected]>
  • Loading branch information
tarepan and Hiroshiba authored Jun 28, 2024
1 parent f3e449f commit 925c4d5
Showing 1 changed file with 70 additions and 12 deletions.
82 changes: 70 additions & 12 deletions test/unit/user_dict/test_user_dict_model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""UserDictWord のテスト"""

from typing import TypedDict

import pytest
Expand All @@ -7,7 +9,7 @@
from voicevox_engine.user_dict.model import UserDictWord


class _TestModel(TypedDict):
class UserDictWordInputs(TypedDict):
surface: str
priority: int
part_of_speech: str
Expand All @@ -24,7 +26,8 @@ class _TestModel(TypedDict):
accent_associative_rule: str


def generate_model() -> _TestModel:
def generate_model() -> UserDictWordInputs:
"""テスト用に UserDictWord の要素を生成する。"""
return {
"surface": "テスト",
"priority": 0,
Expand All @@ -44,19 +47,39 @@ def generate_model() -> _TestModel:


def test_valid_word() -> None:
test_value = generate_model()
UserDictWord(**test_value)
"""generate_model 関数は UserDictWord の要素を生成する。"""
# Outputs
args = generate_model()

# Test
UserDictWord(**args)


def test_convert_to_zenkaku() -> None:
"""UserDictWord は surface を全角にする。"""
# Inputs
test_value = generate_model()
test_value["surface"] = "test"
assert UserDictWord(**test_value).surface == "test"
# Expects
true_surface = "test"
# Outputs
surface = UserDictWord(**test_value).surface

# Test
assert surface == true_surface


def test_count_mora() -> None:
"""UserDictWord は mora_count=None を上書きする。"""
# Inputs
test_value = generate_model()
assert UserDictWord(**test_value).mora_count == 3
# Expects
true_mora_count = 3
# Outputs
mora_count = UserDictWord(**test_value).mora_count

# Test
assert mora_count == true_mora_count


def test_count_mora_x() -> None:
Expand All @@ -75,52 +98,87 @@ def test_count_mora_x() -> None:


def test_count_mora_xwa() -> None:
"""「ヮ」を含む発音のモーラ数が適切にカウントされる。"""
# Inputs
test_value = generate_model()
test_value["pronunciation"] = "クヮンセイ"
expected_count = 0
# Expects
true_mora_count = 0
for accent_phrase in parse_kana(
test_value["pronunciation"] + "'",
):
expected_count += len(accent_phrase.moras)
assert UserDictWord(**test_value).mora_count == expected_count
true_mora_count += len(accent_phrase.moras)
# Outputs
mora_rount = UserDictWord(**test_value).mora_count

# Test
assert mora_rount == true_mora_count


def test_invalid_pronunciation_not_katakana() -> None:
"""UserDictWord はカタカナでない pronunciation をエラーとする。"""
# Inputs
test_value = generate_model()
test_value["pronunciation"] = "ぼいぼ"

# Test
with pytest.raises(ValidationError):
UserDictWord(**test_value)


def test_invalid_pronunciation_invalid_sutegana() -> None:
"""UserDictWord は無効な pronunciation をエラーとする。"""
# Inputs
test_value = generate_model()
test_value["pronunciation"] = "アィウェォ"

# Test
with pytest.raises(ValidationError):
UserDictWord(**test_value)


def test_invalid_pronunciation_invalid_xwa() -> None:
"""UserDictWord は無効な pronunciation をエラーとする。"""
# Inputs
test_value = generate_model()
test_value["pronunciation"] = "アヮ"

# Test
with pytest.raises(ValidationError):
UserDictWord(**test_value)


def test_count_mora_voiced_sound() -> None:
"""UserDictWord はモーラ数を正しくカウントして上書きする。"""
# Inputs
test_value = generate_model()
test_value["pronunciation"] = "ボイボ"
assert UserDictWord(**test_value).mora_count == 3
# Expects
true_mora_count = 3
# Outputs
mora_count = UserDictWord(**test_value).mora_count

# Test
assert mora_count == true_mora_count

def test_invalid_accent_type() -> None:

def test_word_accent_type_too_big() -> None:
"""UserDictWord はモーラ数を超えた accent_type をエラーとする。"""
# Inputs
test_value = generate_model()
test_value["accent_type"] = 4

# Test
with pytest.raises(ValidationError):
UserDictWord(**test_value)


def test_invalid_accent_type_2() -> None:
def test_word_accent_type_negative() -> None:
"""UserDictWord は負の accent_type をエラーとする。"""
# Inputs
test_value = generate_model()
test_value["accent_type"] = -1

# Test
with pytest.raises(ValidationError):
UserDictWord(**test_value)

0 comments on commit 925c4d5

Please sign in to comment.