/
test_mixins.py
113 lines (86 loc) · 4.27 KB
/
test_mixins.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
import os
from io import BytesIO
from pathlib import Path
from unittest.mock import AsyncMock
import pytest
import pytest_asyncio
from aiohttp import ClientResponseError
from aiogram import Bot
from aiogram.types import File
from aiogram.types.mixins import Downloadable
from aiogram.utils.json import json
from tests import TOKEN
from tests.types.dataset import FILE
@pytest_asyncio.fixture(name='bot')
async def bot_fixture():
""" Bot fixture """
_bot = Bot(TOKEN)
yield _bot
session = await _bot.get_session()
await session.close()
@pytest.fixture
def tmppath(tmpdir, request):
os.chdir(tmpdir)
yield Path(tmpdir)
os.chdir(request.config.invocation_dir)
@pytest.fixture
def downloadable(bot):
downloadable = Downloadable()
downloadable.get_file = AsyncMock(return_value=File(**FILE))
downloadable.bot = bot
return downloadable
@pytest.fixture()
def get_file_response(aresponses):
aresponses.add(response=aresponses.Response(body=json.dumps(FILE)))
class TestDownloadable:
async def test_download_make_dirs_false_nodir(self, tmppath, downloadable, get_file_response):
with pytest.raises(FileNotFoundError):
await downloadable.download(make_dirs=False)
async def test_download_make_dirs_false_mkdir(self, tmppath, downloadable, get_file_response):
os.mkdir('voice')
await downloadable.download(make_dirs=False)
assert os.path.isfile(tmppath.joinpath(FILE["file_path"]))
async def test_download_make_dirs_true(self, tmppath, downloadable, get_file_response):
await downloadable.download(make_dirs=True)
assert os.path.isfile(tmppath.joinpath(FILE["file_path"]))
async def test_download_deprecation_warning(self, tmppath, downloadable, get_file_response):
with pytest.deprecated_call():
await downloadable.download("test.file")
async def test_download_destination(self, tmppath, downloadable, get_file_response):
with pytest.deprecated_call():
await downloadable.download("test.file")
assert os.path.isfile(tmppath.joinpath('test.file'))
async def test_download_destination_dir_exist(self, tmppath, downloadable, get_file_response):
os.mkdir("test_folder")
with pytest.deprecated_call():
await downloadable.download("test_folder")
assert os.path.isfile(tmppath.joinpath('test_folder', FILE["file_path"]))
async def test_download_destination_with_dir(self, tmppath, downloadable, get_file_response):
with pytest.deprecated_call():
await downloadable.download(os.path.join('dir_name', 'file_name'))
assert os.path.isfile(tmppath.joinpath('dir_name', 'file_name'))
async def test_download_destination_io_bytes(self, tmppath, downloadable, get_file_response):
file = BytesIO()
with pytest.deprecated_call():
await downloadable.download(file)
assert len(file.read()) != 0
async def test_download_raise_value_error(self, tmppath, downloadable, get_file_response):
with pytest.raises(ValueError):
await downloadable.download(destination_dir="a", destination_file="b")
async def test_download_destination_dir(self, tmppath, downloadable, get_file_response):
await downloadable.download(destination_dir='test_dir')
assert os.path.isfile(tmppath.joinpath('test_dir', FILE["file_path"]))
async def test_download_destination_file(self, tmppath, downloadable, get_file_response):
await downloadable.download(destination_file='file_name')
assert os.path.isfile(tmppath.joinpath('file_name'))
async def test_download_destination_file_with_dir(self, tmppath, downloadable, get_file_response):
await downloadable.download(destination_file=os.path.join('dir_name', 'file_name'))
assert os.path.isfile(tmppath.joinpath('dir_name', 'file_name'))
async def test_download_io_bytes(self, tmppath, downloadable, get_file_response):
file = BytesIO()
await downloadable.download(destination_file=file)
assert len(file.read()) != 0
async def test_download_404(self, tmppath, downloadable):
with pytest.raises(ClientResponseError) as exc_info:
await downloadable.download(destination_file='file_name')
assert exc_info.value.status == 404