Skip to content

Commit

Permalink
整理: ライブラリテストのユーティリティメソッドを関数へ切り出し (#1382)
Browse files Browse the repository at this point in the history
* refactor: `create_vvlib_without_manifest()` を切り出し

* refactor: マニフェスト追加を共通化

* refactor: 定数名を大文字へ修正

* refactor: 不正なマニフェストを生成するコードの形式を統一

* refactor: `create_vvlib_manifest()` を切り出し

* fix: 移動と変数リネームの同時編集をリバート
  • Loading branch information
tarepan authored Jun 2, 2024
1 parent ecbff34 commit 63588d5
Showing 1 changed file with 75 additions and 45 deletions.
120 changes: 75 additions & 45 deletions test/test_library_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,35 @@

from voicevox_engine.library.library_manager import LibraryManager

vvlib_manifest_name = "vvlib_manifest.json"
VVLIB_MANIFEST_NAME = "vvlib_manifest.json"


def create_vvlib_manifest(template_vvlib: Any, **kwargs: Any) -> dict[str, Any]:
"""テンプレートの vvlib から指定の属性を追加・上書きした、新たな vvlib オブジェクトを生成する。"""
vvlib_manifest = copy.deepcopy(template_vvlib)
return {**vvlib_manifest, **kwargs}


def create_vvlib_without_manifest(filename: str, template_vvlib_path: Path) -> None:
"""テンプレートの vvlib からマニフェストファイルを削除した、新たな vvlib ファイルを指定パスに生成する。"""
with (
ZipFile(filename, "w") as zf_out,
ZipFile(template_vvlib_path, "r") as zf_in,
):
for file in zf_in.infolist():
buffer = zf_in.read(file.filename)
if file.filename != VVLIB_MANIFEST_NAME:
zf_out.writestr(file, buffer)


def append_any_as_manifest_to_vvlib(obj: Any, vvlib_path: str) -> None:
"""指定 vvlib へ任意の Python オブジェクトをマニフェストファイルとして追加する。"""
with ZipFile(vvlib_path, "a") as zf:
if isinstance(obj, str):
obj_str = obj
else:
obj_str = json.dumps(obj)
zf.writestr(VVLIB_MANIFEST_NAME, obj_str)


class TestLibraryManager(TestCase):
Expand All @@ -37,28 +65,14 @@ def setUp(self) -> None:
speaker_infos = glob.glob("resources/character_info/**", recursive=True)
for info in speaker_infos:
zf.write(info)
zf.writestr(vvlib_manifest_name, json.dumps(self.vvlib_manifest))
zf.writestr(VVLIB_MANIFEST_NAME, json.dumps(self.vvlib_manifest))
self.library_file = open(self.library_filename, "br")

def tearDown(self) -> None:
self.tmp_dir.cleanup()
self.library_file.close()
self.library_filename.unlink()

def create_vvlib_without_manifest(self, filename: str) -> None:
with (
ZipFile(filename, "w") as zf_out,
ZipFile(self.library_filename, "r") as zf_in,
):
for file in zf_in.infolist():
buffer = zf_in.read(file.filename)
if file.filename != vvlib_manifest_name:
zf_out.writestr(file, buffer)

def create_vvlib_manifest(self, **kwargs: Any) -> dict[str, Any]:
vvlib_manifest = copy.deepcopy(self.vvlib_manifest)
return {**vvlib_manifest, **kwargs}

def test_installed_libraries(self) -> None:
self.assertEqual(self.library_manger.installed_libraries(), {})

Expand Down Expand Up @@ -96,7 +110,7 @@ def test_install_non_zip_file(self) -> None:
def test_install_manifest_less_library(self) -> None:
"""マニフェストの無い ZIP ファイルは音声ライブラリとしてインストールできない。"""
invalid_vvlib_name = "test/invalid.vvlib"
self.create_vvlib_without_manifest(invalid_vvlib_name)
create_vvlib_without_manifest(invalid_vvlib_name, self.library_filename)
with open(invalid_vvlib_name, "br") as f, self.assertRaises(HTTPException) as e:
self.library_manger.install_library(self.library_uuid, f)
self.assertEqual(
Expand All @@ -109,13 +123,13 @@ def test_install_manifest_less_library(self) -> None:
def test_install_broken_manifest_library(self) -> None:
"""不正な形式の vvlib_manifest.json をもつ ZIP ファイルは音声ライブラリとしてインストールできない。"""

# vvlib_manifestのパースのテスト
# Duplicate name: 'vvlib_manifest.json'とWarningを吐かれるので、毎回作り直す
# Inputs
invalid_vvlib_name = "test/invalid.vvlib"
self.create_vvlib_without_manifest(invalid_vvlib_name)
with ZipFile(invalid_vvlib_name, "a") as zf:
zf.writestr(vvlib_manifest_name, "test")
invalid_vvlib_manifest = "test"
create_vvlib_without_manifest(invalid_vvlib_name, self.library_filename)
append_any_as_manifest_to_vvlib(invalid_vvlib_manifest, invalid_vvlib_name)

# Tests
with open(invalid_vvlib_name, "br") as f, self.assertRaises(HTTPException) as e:
self.library_manger.install_library(self.library_uuid, f)
self.assertEqual(
Expand All @@ -126,13 +140,16 @@ def test_install_broken_manifest_library(self) -> None:

def test_install_invalid_type_manifest_library(self) -> None:
"""不正な形式の vvlib_manifest.json をもつ ZIP ファイルは音声ライブラリとしてインストールできない。"""
# vvlib_manifestのパースのテスト

# Inputs
invalid_vvlib_name = "test/invalid.vvlib"
invalid_vvlib_manifest = self.create_vvlib_manifest(version=10)
self.create_vvlib_without_manifest(invalid_vvlib_name)
with ZipFile(invalid_vvlib_name, "a") as zf:
zf.writestr(vvlib_manifest_name, json.dumps(invalid_vvlib_manifest))
invalid_vvlib_manifest = create_vvlib_manifest(
template_vvlib=self.vvlib_manifest, version=10
)
create_vvlib_without_manifest(invalid_vvlib_name, self.library_filename)
append_any_as_manifest_to_vvlib(invalid_vvlib_manifest, invalid_vvlib_name)

# Tests
with open(invalid_vvlib_name, "br") as f, self.assertRaises(HTTPException) as e:
self.library_manger.install_library(self.library_uuid, f)
self.assertEqual(
Expand All @@ -143,12 +160,16 @@ def test_install_invalid_type_manifest_library(self) -> None:

def test_install_invalid_version_manifest_library(self) -> None:
# vvlib_manifestの不正なversionのテスト

# Inputs
invalid_vvlib_name = "test/invalid.vvlib"
invalid_vvlib_manifest = self.create_vvlib_manifest(version="10")
self.create_vvlib_without_manifest(invalid_vvlib_name)
with ZipFile(invalid_vvlib_name, "a") as zf:
zf.writestr(vvlib_manifest_name, json.dumps(invalid_vvlib_manifest))
invalid_vvlib_manifest = create_vvlib_manifest(
template_vvlib=self.vvlib_manifest, version="10"
)
create_vvlib_without_manifest(invalid_vvlib_name, self.library_filename)
append_any_as_manifest_to_vvlib(invalid_vvlib_manifest, invalid_vvlib_name)

# Tests
with open(invalid_vvlib_name, "br") as f, self.assertRaises(HTTPException) as e:
self.library_manger.install_library(self.library_uuid, f)
self.assertEqual(
Expand All @@ -159,12 +180,16 @@ def test_install_invalid_version_manifest_library(self) -> None:

def test_install_invalid_manifest_version_library(self) -> None:
# vvlib_manifestの不正なmanifest_versionのテスト

# Inputs
invalid_vvlib_name = "test/invalid.vvlib"
invalid_vvlib_manifest = self.create_vvlib_manifest(manifest_version="10")
self.create_vvlib_without_manifest(invalid_vvlib_name)
with ZipFile(invalid_vvlib_name, "a") as zf:
zf.writestr(vvlib_manifest_name, json.dumps(invalid_vvlib_manifest))
invalid_vvlib_manifest = create_vvlib_manifest(
template_vvlib=self.vvlib_manifest, manifest_version="10"
)
create_vvlib_without_manifest(invalid_vvlib_name, self.library_filename)
append_any_as_manifest_to_vvlib(invalid_vvlib_manifest, invalid_vvlib_name)

# Tests
with open(invalid_vvlib_name, "br") as f, self.assertRaises(HTTPException) as e:
self.library_manger.install_library(self.library_uuid, f)
self.assertEqual(
Expand All @@ -175,14 +200,16 @@ def test_install_invalid_manifest_version_library(self) -> None:

def test_install_invalid_manifest_version_library_2(self) -> None:
# vvlib_manifestの未対応のmanifest_versionのテスト

# Inputs
invalid_vvlib_name = "test/invalid.vvlib"
invalid_vvlib_manifest = self.create_vvlib_manifest(
manifest_version="999.999.999"
invalid_vvlib_manifest = create_vvlib_manifest(
template_vvlib=self.vvlib_manifest, manifest_version="999.999.999"
)
self.create_vvlib_without_manifest(invalid_vvlib_name)
with ZipFile(invalid_vvlib_name, "a") as zf:
zf.writestr(vvlib_manifest_name, json.dumps(invalid_vvlib_manifest))
create_vvlib_without_manifest(invalid_vvlib_name, self.library_filename)
append_any_as_manifest_to_vvlib(invalid_vvlib_manifest, invalid_vvlib_name)

# Tests
with open(invalid_vvlib_name, "br") as f, self.assertRaises(HTTPException) as e:
self.library_manger.install_library(self.library_uuid, f)
self.assertEqual(
Expand All @@ -193,14 +220,17 @@ def test_install_invalid_manifest_version_library_2(self) -> None:

def test_install_non_target_engine_library(self) -> None:
# vvlib_manifestのインストール先エンジンの検証のテスト

# Inputs
invalid_vvlib_name = "test/invalid.vvlib"
invalid_vvlib_manifest = self.create_vvlib_manifest(
engine_uuid="26f7823b-20c6-40c5-bf86-6dd5d9d45c18"
invalid_vvlib_manifest = create_vvlib_manifest(
template_vvlib=self.vvlib_manifest,
engine_uuid="26f7823b-20c6-40c5-bf86-6dd5d9d45c18",
)
self.create_vvlib_without_manifest(invalid_vvlib_name)
with ZipFile(invalid_vvlib_name, "a") as zf:
zf.writestr(vvlib_manifest_name, json.dumps(invalid_vvlib_manifest))
create_vvlib_without_manifest(invalid_vvlib_name, self.library_filename)
append_any_as_manifest_to_vvlib(invalid_vvlib_manifest, invalid_vvlib_name)

# Tests
with open(invalid_vvlib_name, "br") as f, self.assertRaises(HTTPException) as e:
self.library_manger.install_library(self.library_uuid, f)
self.assertEqual(
Expand Down

0 comments on commit 63588d5

Please sign in to comment.