コード例 #1
0
ファイル: test_download.py プロジェクト: wsxy162/tribler
def test_selected_files(mock_handle, test_download):
    """
    Test whether the selected files are set correctly
    """
    def mocked_set_file_prios(_):
        mocked_set_file_prios.called = True

    mocked_set_file_prios.called = False

    mocked_file = MockObject()
    mocked_file.path = 'my/path'
    mock_torrent_info = MockObject()
    mock_torrent_info.files = lambda: [mocked_file, mocked_file]
    test_download.handle.prioritize_files = mocked_set_file_prios
    test_download.handle.get_torrent_info = lambda: mock_torrent_info
    test_download.handle.rename_file = lambda *_: None

    test_download.get_share_mode = lambda: False
    test_download.tdef.get_infohash = lambda: b'a' * 20
    test_download.set_selected_files([0])
    assert mocked_set_file_prios.called

    test_download.get_share_mode = lambda: False
    mocked_set_file_prios.called = False
    assert not mocked_set_file_prios.called
コード例 #2
0
ファイル: test_download.py プロジェクト: hbiyik/tribler
    async def test_on_metadata_received_alert(self):
        """
        Testing whether the right operations happen when we receive metadata
        """
        test_future = Future()

        mocked_file = MockObject()
        mocked_file.path = 'test'

        self.download.handle.trackers = lambda: []
        self.download.handle.get_peer_info = lambda: []
        self.download.handle.save_resume_data = lambda: test_future
        self.download.handle.rename_file = lambda *_: None
        with open(TESTS_DATA_DIR / "bak_single.torrent",
                  mode='rb') as torrent_file:
            encoded_metainfo = torrent_file.read()
        decoded_metainfo = bdecode_compat(encoded_metainfo)
        get_info_from_handle(self.download.handle).metadata = lambda: bencode(
            decoded_metainfo[b'info'])
        get_info_from_handle(
            self.download.handle).files = lambda: [mocked_file]

        self.download.checkpoint = lambda: test_future.set_result(None)
        self.download.session = MockObject()
        self.download.session = MockObject()
        self.download.session.torrent_db = None
        self.download.handle.save_path = lambda: None
        self.download.handle.prioritize_files = lambda _: None
        self.download.get_share_mode = lambda: False
        self.download.on_metadata_received_alert(None)

        await test_future
コード例 #3
0
ファイル: test_download.py プロジェクト: hbiyik/tribler
    def test_selected_files(self):
        """
        Test whether the selected files are set correctly
        """
        def mocked_set_file_prios(_):
            mocked_set_file_prios.called = True

        mocked_set_file_prios.called = False

        mocked_file = MockObject()
        mocked_file.path = 'my/path'
        mock_torrent_info = MockObject()
        mock_torrent_info.files = lambda: [mocked_file, mocked_file]
        self.download.handle.prioritize_files = mocked_set_file_prios
        self.download.handle.get_torrent_info = lambda: mock_torrent_info
        self.download.handle.rename_file = lambda *_: None

        self.download.get_share_mode = lambda: False
        self.download.tdef.get_infohash = lambda: b'a' * 20
        self.download.set_selected_files([0])
        self.assertTrue(mocked_set_file_prios.called)

        self.download.get_share_mode = lambda: False
        mocked_set_file_prios.called = False
        self.assertFalse(mocked_set_file_prios.called)
コード例 #4
0
ファイル: test_download.py プロジェクト: hbiyik/tribler
    def test_metadata_received_invalid_torrent_with_value_error(self):
        """
        Testing whether the right operations happen when we receive metadata but the torrent info is invalid and throws
        Value Error
        """
        def mocked_checkpoint():
            raise RuntimeError("This code should not be reached!")

        mocked_file = MockObject()
        mocked_file.path = 'test'

        # The line below should trigger Value Error
        self.download.handle.trackers = lambda: [{'url': 'no-DHT'}]
        self.download.handle.get_peer_info = lambda: []

        get_info_from_handle(
            self.download.handle).metadata = lambda: lt.bencode({})
        get_info_from_handle(
            self.download.handle).files = lambda: [mocked_file]

        self.download.checkpoint = mocked_checkpoint
        self.download.on_metadata_received_alert(None)
コード例 #5
0
ファイル: test_download.py プロジェクト: hbiyik/tribler
    def test_selected_files_no_files(self):
        """
        Test that no files are selected if torrent info is not available.
        """
        def mocked_set_file_prios(_):
            mocked_set_file_prios.called = True

        mocked_set_file_prios.called = False

        mocked_file = MockObject()
        mocked_file.path = 'my/path'
        mock_torrent_info = MockObject()
        self.download.handle.prioritize_files = mocked_set_file_prios
        self.download.handle.get_torrent_info = lambda: mock_torrent_info
        self.download.handle.rename_file = lambda *_: None
        self.download.tdef.get_infohash = lambda: b'a' * 20

        # If share mode is not enabled and everything else is fine, file priority should be set
        # when set_selected_files() is called. But in this test, no files attribute is set in torrent info
        # in order to test AttributeError, therfore, no call to set file priority is expected.
        self.download.get_share_mode = lambda: False
        self.download.set_selected_files([0])
        self.assertFalse(mocked_set_file_prios.called)