use tmpdir fixture in tests

This commit is contained in:
darksidecat 2021-09-01 20:12:03 +03:00
parent 586bfdff77
commit 7a4fc9f273

View file

@ -1,5 +1,4 @@
import os
import shutil
from io import BytesIO
from pathlib import Path
@ -11,9 +10,6 @@ from aiogram.types.mixins import Downloadable
from tests import TOKEN
from tests.types.dataset import FILE
DIR_NAME = 'downloadable_tests'
DIR = Path.joinpath(Path(__file__).parent, DIR_NAME)
pytestmark = pytest.mark.asyncio
@ -26,12 +22,10 @@ async def bot_fixture():
@pytest.fixture
def work_directory(request):
os.makedirs(DIR, exist_ok=True)
os.chdir(Path.joinpath(Path(request.fspath.dirname), DIR_NAME))
yield DIR
def tmppath(tmpdir, request):
os.chdir(tmpdir)
yield Path(tmpdir)
os.chdir(request.config.invocation_dir)
shutil.rmtree(DIR)
@pytest.fixture
@ -47,62 +41,62 @@ def downloadable(bot):
class TestDownloadable:
async def test_download_make_dirs_false_nodir(self, work_directory, downloadable):
async def test_download_make_dirs_false_nodir(self, tmppath, downloadable):
with pytest.raises(FileNotFoundError):
await downloadable.download(make_dirs=False)
async def test_download_make_dirs_false_mkdir(self, work_directory, downloadable):
async def test_download_make_dirs_false_mkdir(self, tmppath, downloadable):
os.mkdir('voice')
await downloadable.download(make_dirs=False)
assert os.path.isfile(work_directory.joinpath(FILE["file_path"]))
assert os.path.isfile(tmppath.joinpath(FILE["file_path"]))
async def test_download_make_dirs_true(self, work_directory, downloadable):
async def test_download_make_dirs_true(self, tmppath, downloadable):
await downloadable.download(make_dirs=True)
assert os.path.isfile(work_directory.joinpath(FILE["file_path"]))
assert os.path.isfile(tmppath.joinpath(FILE["file_path"]))
async def test_download_deprecation_warning(self, work_directory, downloadable):
async def test_download_deprecation_warning(self, tmppath, downloadable):
with pytest.deprecated_call():
await downloadable.download("test.file")
async def test_download_destination(self, work_directory, downloadable):
async def test_download_destination(self, tmppath, downloadable):
with pytest.deprecated_call():
await downloadable.download("test.file")
assert os.path.isfile(work_directory.joinpath('test.file'))
assert os.path.isfile(tmppath.joinpath('test.file'))
async def test_download_destination_dir_exist(self, work_directory, downloadable):
async def test_download_destination_dir_exist(self, tmppath, downloadable):
os.mkdir("test_folder")
with pytest.deprecated_call():
await downloadable.download("test_folder")
assert os.path.isfile(work_directory.joinpath('test_folder', FILE["file_path"]))
assert os.path.isfile(tmppath.joinpath('test_folder', FILE["file_path"]))
async def test_download_destination_with_dir(self, work_directory, downloadable):
async def test_download_destination_with_dir(self, tmppath, downloadable):
with pytest.deprecated_call():
await downloadable.download(os.path.join('dir_name', 'file_name'))
assert os.path.isfile(work_directory.joinpath(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, work_directory, downloadable):
async def test_download_destination_io_bytes(self, tmppath, downloadable):
file = BytesIO()
with pytest.deprecated_call():
await downloadable.download(file)
assert len(file.read()) != 0
async def test_download_raise_value_error(self, work_directory, downloadable):
async def test_download_raise_value_error(self, tmppath, downloadable):
with pytest.raises(ValueError):
await downloadable.download(destination_dir="a", destination_file="b")
async def test_download_destination_dir(self, work_directory, downloadable):
async def test_download_destination_dir(self, tmppath, downloadable):
await downloadable.download(destination_dir='test_dir')
assert os.path.isfile(work_directory.joinpath('test_dir', FILE["file_path"]))
assert os.path.isfile(tmppath.joinpath('test_dir', FILE["file_path"]))
async def test_download_destination_file(self, work_directory, downloadable):
async def test_download_destination_file(self, tmppath, downloadable):
await downloadable.download(destination_file='file_name')
assert os.path.isfile(work_directory.joinpath('file_name'))
assert os.path.isfile(tmppath.joinpath('file_name'))
async def test_download_destination_file_with_dir(self, work_directory, downloadable):
async def test_download_destination_file_with_dir(self, tmppath, downloadable):
await downloadable.download(destination_file=os.path.join('dir_name', 'file_name'))
assert os.path.isfile(work_directory.joinpath('dir_name', 'file_name'))
assert os.path.isfile(tmppath.joinpath('dir_name', 'file_name'))
async def test_download_io_bytes(self, work_directory, downloadable):
async def test_download_io_bytes(self, tmppath, downloadable):
file = BytesIO()
await downloadable.download(destination_file=file)
assert len(file.read()) != 0