Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Apr 13, 2022
1 parent 2a96c4e commit 1609e8d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ def start_download(self, torrent_file=None, tdef=None, config: DownloadConfig =
self._logger.debug(f'Starting download: filename: {torrent_file}, torrent def: {tdef}')
if config is None:
config = DownloadConfig.convert(self.download_defaults)
self._logger.debug(f'Use a default config.')
self._logger.debug('Use a default config.')

# the priority of the parameters is: (1) tdef, (2) torrent_file.
# so if we have tdef, and torrent_file will be ignored, and so on.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from tribler.core import notifications
from tribler.core.components.libtorrent.restapi.torrentinfo_endpoint import TorrentInfoEndpoint
from tribler.core.components.libtorrent.settings import LibtorrentSettings
from tribler.core.components.libtorrent.settings import DownloadDefaultsSettings, LibtorrentSettings
from tribler.core.components.libtorrent.torrentdef import TorrentDef
from tribler.core.components.metadata_store.db.orm_bindings.torrent_metadata import tdef_to_metadata_dict
from tribler.core.components.restapi.rest.base_api_test import do_request
Expand All @@ -29,6 +29,7 @@
def download_manager(state_dir):
dlmgr = MagicMock()
dlmgr.config = LibtorrentSettings()
dlmgr.download_defaults = DownloadDefaultsSettings()
dlmgr.shutdown = lambda: succeed(None)
checkpoints_dir = state_dir / 'dlcheckpoints'
checkpoints_dir.mkdir()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest

from tribler.core.components.libtorrent.download_manager.download_config import DownloadConfig
from tribler.core.tests.tools.common import TORRENT_UBUNTU_FILE
from tribler.core.utilities.rest_utils import path_to_url
from tribler.core.utilities.simpledefs import DLSTATUS_DOWNLOADING
Expand All @@ -11,14 +12,15 @@
async def test_download_torrent_from_url(tmp_path, file_server, download_manager):
# Setup file server to serve torrent file
shutil.copyfile(TORRENT_UBUNTU_FILE, tmp_path / "ubuntu.torrent")
download = await download_manager.start_download_from_uri(f'http://localhost:{file_server}/ubuntu.torrent')
download = await download_manager.start_download_from_uri(f'http://localhost:{file_server}/ubuntu.torrent',
config=DownloadConfig())
await download.wait_for_status(DLSTATUS_DOWNLOADING)


@pytest.mark.asyncio
async def test_download_torrent_from_file(download_manager):
uri = path_to_url(TORRENT_UBUNTU_FILE)
d = await download_manager.start_download_from_uri(uri)
d = await download_manager.start_download_from_uri(uri, config=DownloadConfig())
await d.wait_for_status(DLSTATUS_DOWNLOADING)


Expand All @@ -27,5 +29,5 @@ async def test_download_torrent_from_file_with_escaped_characters(download_manag
destination = tmp_path / 'ubuntu%20%21 15.04.torrent'
shutil.copyfile(TORRENT_UBUNTU_FILE, destination)
uri = path_to_url(destination)
d = await download_manager.start_download_from_uri(uri)
d = await download_manager.start_download_from_uri(uri, config=DownloadConfig())
await d.wait_for_status(DLSTATUS_DOWNLOADING)
59 changes: 36 additions & 23 deletions src/tribler/core/components/watch_folder/tests/test_watch_folder.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,68 @@
import os
import shutil

from asynctest import MagicMock

import pytest
from asynctest import MagicMock

from tribler.core.components.watch_folder.settings import WatchFolderSettings
from tribler.core.components.watch_folder.watch_folder import WatchFolder
from tribler.core.tests.tools.common import TESTS_DATA_DIR, TORRENT_UBUNTU_FILE


# pylint: disable=redefined-outer-name, protected-access

@pytest.fixture
async def watcher_fixture(tmp_path):
watch = WatchFolder(watch_folder_path=tmp_path, download_manager=MagicMock(), notifier=MagicMock())
async def watch_folder(tmp_path):
watch = WatchFolder(
state_dir=tmp_path,
settings=WatchFolderSettings(
enabled=True,
directory=''
),
download_manager=MagicMock(),
notifier=MagicMock()
)
yield watch
await watch.stop()


def test_watchfolder_no_files(watcher_fixture):
watcher_fixture.check_watch_folder()
watcher_fixture.download_manager.start_download.assert_not_called()
def test_watchfolder_no_files(watch_folder):
watch_folder.check_watch_folder()
watch_folder.download_manager.start_download.assert_not_called()


def test_watchfolder_no_torrent_file(watch_folder: WatchFolder):
directory = watch_folder.settings.get_path_as_absolute('directory', watch_folder.state_dir)

def test_watchfolder_no_torrent_file(watcher_fixture):
shutil.copyfile(TORRENT_UBUNTU_FILE, watcher_fixture.watch_folder / "test.txt")
watcher_fixture.check_watch_folder()
watcher_fixture.download_manager.start_download.assert_not_called()
shutil.copyfile(TORRENT_UBUNTU_FILE, directory / "test.txt")
watch_folder.check_watch_folder()
watch_folder.download_manager.start_download.assert_not_called()


def test_watchfolder_utf8_dir(watcher_fixture, tmp_path):
def test_watchfolder_utf8_dir(watch_folder, tmp_path):
new_watch_dir = tmp_path / "\xe2\x82\xac"
os.mkdir(new_watch_dir)
shutil.copyfile(TORRENT_UBUNTU_FILE, new_watch_dir / "\xe2\x82\xac.torrent")
watcher_fixture.watch_folder = new_watch_dir
watcher_fixture.check_watch_folder()
watch_folder.watch_folder = new_watch_dir
watch_folder.check_watch_folder()


def test_watchfolder_torrent_file_one_corrupt(watcher_fixture):
def test_watchfolder_torrent_file_one_corrupt(watch_folder: WatchFolder):
directory = watch_folder.settings.get_path_as_absolute('directory', watch_folder.state_dir)
def mock_start_download(*_, **__):
mock_start_download.downloads_started += 1

mock_start_download.downloads_started = 0

shutil.copyfile(TORRENT_UBUNTU_FILE, watcher_fixture.watch_folder / "test.torrent")
shutil.copyfile(TESTS_DATA_DIR / 'test_rss.xml', watcher_fixture.watch_folder / "test2.torrent")
watcher_fixture.download_manager.start_download = mock_start_download
watcher_fixture.download_manager.download_exists = lambda *_: False
watcher_fixture.check_watch_folder()
shutil.copyfile(TORRENT_UBUNTU_FILE, directory / "test.torrent")
shutil.copyfile(TESTS_DATA_DIR / 'test_rss.xml', directory / "test2.torrent")
watch_folder.download_manager.start_download = mock_start_download
watch_folder.download_manager.download_exists = lambda *_: False
watch_folder.check_watch_folder()
assert mock_start_download.downloads_started == 1
assert (watcher_fixture.watch_folder / "test2.torrent.corrupt").is_file()
assert (directory / "test2.torrent.corrupt").is_file()


def test_cleanup(watcher_fixture):
watcher_fixture.cleanup_torrent_file(TESTS_DATA_DIR, 'thisdoesnotexist123.bla')
def test_cleanup(watch_folder):
watch_folder.cleanup_torrent_file(TESTS_DATA_DIR, 'thisdoesnotexist123.bla')
assert not (TESTS_DATA_DIR / 'thisdoesnotexist123.bla.corrupt').exists()
4 changes: 2 additions & 2 deletions src/tribler/core/components/watch_folder/watch_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def cleanup_torrent_file(self, root, name):
self.notifier[notifications.watch_folder_corrupt_file](name)

def check_watch_folder(self):
self._logger.debug(f'Checking watch folder...')
self._logger.debug('Checking watch folder...')

if not self.settings.enabled or not self.state_dir:
self._logger.debug(f'Cancelled. Enabled: {self.settings.enabled}. State dir: {self.state_dir}.')
Expand Down Expand Up @@ -83,4 +83,4 @@ def check_watch_folder(self):
if not self.download_manager.download_exists(infohash):
self._logger.info("Starting download from torrent file %s", name)
self.download_manager.start_download(torrent_file=root / name)
self._logger.debug(f'Checking watch folder completed.')
self._logger.debug('Checking watch folder completed.')

0 comments on commit 1609e8d

Please sign in to comment.