Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

整理: 辞書単語テストの形式を統一 #1443

Merged
merged 5 commits into from
Jun 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading