/
conftest.py
123 lines (93 loc) · 3.08 KB
/
conftest.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import enum
import os
import shutil
from pathlib import Path
from typing import IO, Any, Generator
import pytest
from django.core.files import File
from django.core.files.storage import FileSystemStorage
from test_proj.media_library.models import Video
from video_encoding.backends.ffmpeg import FFmpegBackend
class StorageType(enum.Enum):
LOCAL = enum.auto()
REMOTE = enum.auto()
@pytest.fixture
def video_path():
path = os.path.dirname(os.path.abspath(__file__))
return os.path.join(path, 'waterfall.mp4')
@pytest.fixture
def ffmpeg():
return FFmpegBackend()
@pytest.fixture
def local_video(video_path) -> Generator[Video, None, None]:
"""
Return a video object which is stored locally.
"""
video = Video.objects.create()
video.file.save('test.MTS', File(open(video_path, 'rb')), save=True)
try:
yield video
finally:
try:
video.file.delete()
except ValueError:
# file has already been deleted
pass
for format in video.format_set.all():
format.file.delete()
video.delete()
@pytest.fixture
def remote_video(local_video) -> Generator[Video, None, None]:
"""
Return a video which is stored "remotely".
"""
storage_path = Path(local_video.file.path).parent
remote_video = local_video
remote_video.file.storage = FakeRemoteStorage(storage_path)
yield remote_video
@pytest.fixture(params=StorageType)
def video(
request, local_video: Video, remote_video: Video
) -> Generator[Video, None, None]:
"""
Return a locally and a remotely stored video.
"""
storage_type = request.param
if storage_type == StorageType.LOCAL:
yield local_video
elif storage_type == StorageType.REMOTE:
yield remote_video
else:
raise ValueError(f"Invalid storage type {storage_type}")
class FakeRemoteStorage(FileSystemStorage):
"""
Fake remote storage which does not support accessing a file by path.
"""
def __init__(self, root_path: Path) -> None:
super().__init__()
self.root_path = root_path
def delete(self, name: str) -> None:
file_path = self.__path(name)
file_path.unlink()
def exists(self, name: str) -> bool:
return self.__path(name).exists()
def open(self, name: str, mode: str) -> IO[Any]:
return open(self.__path(name), mode)
def path(self, *args, **kwargs):
raise NotImplementedError("Remote storage does not implement path()")
def _save(self, name: str, content: File) -> str:
file_path = self.__path(name)
folder_path = file_path.parent
if not folder_path.is_dir():
file_path.parent.mkdir(parents=True)
if hasattr(content, 'temporary_file_path'):
shutil.move(content.temporary_file_path(), file_path)
else:
with open(file_path, 'wb') as fp:
fp.write(content.read())
return str(file_path)
def __path(self, name: str) -> Path:
"""
Return path to local file.
"""
return self.root_path / name