File size: 3,455 Bytes
edc06cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import base64
from pathlib import Path

import pytest

from voicevox_engine.resource_manager import ResourceManager, ResourceManagerError

with_filemap_dir = Path(__file__).parent / "with_filemap"
without_filemap_dir = Path(__file__).parent / "without_filemap"


def b64encode_str(s: bytes) -> str:
    return base64.b64encode(s).decode("utf-8")


def _assert_resource(manager: ResourceManager, input_path: Path) -> None:
    """
    `input_path`で指定したファイルから正しくbase64が取得できるか確認する
    また、ハッシュを取得し、対応するファイルから同じバイト列が取得できるか確認する
    """
    true_bytes = input_path.read_bytes()

    assert manager.resource_str(input_path, "base64") == b64encode_str(true_bytes)

    result_filehash = manager.resource_str(input_path, "hash")
    result_path = manager.resource_path(result_filehash)
    assert result_path.read_bytes() == true_bytes


def test_with_filemap() -> None:
    """
    "filemap.json"があるディレクトリでのテスト
    (fimemapの生成コマンド)
    `python tools/generate_filemap.py --target_dir test/unit/resource_manager/with_filemap`
    """
    manager = ResourceManager(False)
    manager.register_dir(with_filemap_dir)

    png_path = with_filemap_dir / "dummy.png"
    _assert_resource(manager, png_path)

    wav_path = with_filemap_dir / "dummy.wav"
    _assert_resource(manager, wav_path)

    # 同じバイナリがある場合のテスト
    same_wav_path = with_filemap_dir / "dummy_same_binary.wav"
    assert wav_path.read_bytes() == same_wav_path.read_bytes()
    _assert_resource(manager, same_wav_path)

    # filemap.jsonに含まれないものはエラー
    # NOTE: 通常、テキストはResourceManagerで管理しない
    txt_path = with_filemap_dir / "dummy.txt"
    with pytest.raises(ResourceManagerError):
        manager.resource_str(txt_path, "base64")
    with pytest.raises(ResourceManagerError):
        manager.resource_str(txt_path, "hash")

    # 登録されていないハッシュが渡された場合エラー
    with pytest.raises(ResourceManagerError):
        manager.resource_path("NOT_EXIST_HASH")


def test_without_filemap_when_production() -> None:
    """
    "create_filemap_if_not_exist"がFalseで"filemap.json"が無い場合エラーにする
    """
    manager = ResourceManager(False)
    with pytest.raises(ResourceManagerError):
        manager.register_dir(without_filemap_dir)


def test_without_filemap() -> None:
    """
    "create_filemap_if_not_exist"がTrueで"filemap.json"が無い場合は登録時にfilemapを生成する
    """
    manager = ResourceManager(True)
    manager.register_dir(without_filemap_dir)

    # 全てのファイルが管理される
    png_path = without_filemap_dir / "dummy.png"
    _assert_resource(manager, png_path)

    wav_path = without_filemap_dir / "dummy.wav"
    _assert_resource(manager, wav_path)

    txt_path = without_filemap_dir / "dummy.txt"
    _assert_resource(manager, txt_path)

    # 同じバイナリがある場合のテスト
    same_wav_path = without_filemap_dir / "dummy_same_binary.wav"
    assert wav_path.read_bytes() == same_wav_path.read_bytes()
    _assert_resource(manager, same_wav_path)

    # 登録されていないハッシュが渡された場合エラー
    with pytest.raises(ResourceManagerError):
        manager.resource_path("NOT_EXIST_HASH")