# Buggy code
def on_move_files(self):
    if len(self.selected_items) != 1:
        return

    dest_dir = QFileDialog.getExistingDirectory(
        self,
        "Please select the destination directory",
        self.selected_items[0].download_info["destination"],
        QFileDialog.ShowDirsOnly,
    )

    _infohash = self.selected_items[0].download_info["infohash"]
    _name = self.selected_items[0].download_info["name"]

    data = {"state": "move_storage", "dest_dir": dest_dir}

    TriblerNetworkRequest(
        "downloads/%s" % _infohash,
        lambda res, _, name=_name, target=dest_dir: self.on_files_moved(
            res, name, target
        ),
        data=data,
        method="PATCH",
    )


# Traceback (AssertionError)
======================================================================
FAIL: Testing whether moving the torrent storage to an existing directory works as expected.
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\users\heteoma\appdata\local\programs\python\python37\lib\site-packages\asynctest\case.py", line 297, in run
self._run_test_method(testMethod)
File "c:\users\heteoma\appdata\local\programs\python\python37\lib\site-packages\asynctest\case.py", line 354, in _run_test_method
self.loop.run_until_complete(result)
File "c:\users\heteoma\appdata\local\programs\python\python37\lib\site-packages\asynctest\case.py", line 224, in wrapper
return method(*args, **kwargs)
File "c:\users\heteoma\appdata\local\programs\python\python37\lib\asyncio\base_events.py", line 579, in run_until_complete
return future.result()
File "C:\workspace\jenkins2\workspace\GH_Tribler_PR_Tests\PR_async_win64\tribler\src\tribler-core\tribler_core\tests\tools\tools.py", line 12, in wrapper
await wait_for(coro(*args, **kwargs), timeout)
File "c:\users\heteoma\appdata\local\programs\python\python37\lib\asyncio\tasks.py", line 442, in wait_for
return fut.result()
File "C:\workspace\jenkins2\workspace\GH_Tribler_PR_Tests\PR_async_win64\tribler\src\tribler-core\tribler_core\modules\libtorrent\restapi\tests\test_downloads_endpoint.py", line 429, in test_move_to_existing_dir
expected_code=200, post_data=data, request_type='PATCH')
File "C:\workspace\jenkins2\workspace\GH_Tribler_PR_Tests\PR_async_win64\tribler\src\tribler-core\tribler_core\restapi\base_api_test.py", line 68, in do_request
self.assertEqual(status, expected_code, response)
AssertionError: 500 != 200 : {'error': {'handled': False, 'code': 'ArgumentError', 'message': 'Python argument types in\n    torrent_handle.move_storage(torrent_handle, Path)\ndid not match C++ signature:\n    move_storage(struct libtorrent::torrent_handle {lvalue}, class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > path, int flags=libtorrent.deprecated_move_flags_t.always_replace_files)\n    move_storage(struct libtorrent::torrent_handle {lvalue}, class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > path, enum libtorrent::move_flags_t flags=libtorrent.move_flags_t.always_replace_files)'}}
-------------------- >> begin captured logging << --------------------
DEBUG   1580758487 selector_events:asyncio:53   Using selector: SelectSelector
DEBUG   1580758487   network_utils:tribler_core.utilities.network_utils:38   Got a working random port 10436
INFO    1580758487         session:Session:96   Session is using state directory: C:\workspace\jenkins2\workspace\GH_Tribler_PR_Tests\PR_async_win64\tribler\src\tribler-core\tribler_core\tests\tools\temp\TestDownloadsEndpoint_tribler_test_session_YIW5XAYJ\dot.Tribler\7.0.0-GIT
DEBUG   1580758487   network_utils:tribler_core.utilities.network_utils:93   Port test failed (port=8443, family=AddressFamily.AF_INET, type=SocketKind.SOCK_DGRAM): [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted
DEBUG   1580758487   network_utils:tribler_core.utilities.network_utils:38   Got a working random port 8462
DEBUG   1580758487  tribler_config:TriblerConfig:131  Get random port 8462 for [libtorrent~port]
DEBUG   1580758487   network_utils:tribler_core.utilities.network_utils:38   Got a working random port 22570
DEBUG   1580758487  tribler_config:TriblerConfig:131  Get random port 22570 for [video_server~port]
DEBUG   1580758487   network_utils:tribler_core.utilities.network_utils:93   Port test failed (port=10725, family=AddressFamily.AF_INET, type=SocketKind.SOCK_DGRAM): [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted
DEBUG   1580758487   network_utils:tribler_core.utilities.network_utils:38   Got a working random port 10742
DEBUG   1580758487  tribler_config:TriblerConfig:131  Get random port 10742 for [libtorrent~anon_listen_port]
DEBUG   1580758487   network_utils:tribler_core.utilities.network_utils:38   Got a working random port 55422
DEBUG   1580758487  tribler_config:TriblerConfig:131  Get random port 55422 for [tunnel_community~socks5_listen_ports~0]
DEBUG   1580758487   network_utils:tribler_core.utilities.network_utils:38   Got a working random port 31716
DEBUG   1580758487  tribler_config:TriblerConfig:131  Get random port 31716 for [tunnel_community~socks5_listen_ports~1]
DEBUG   1580758487   network_utils:tribler_core.utilities.network_utils:38   Got a working random port 22496
DEBUG   1580758487  tribler_config:TriblerConfig:131  Get random port 22496 for [tunnel_community~socks5_listen_ports~2]
DEBUG   1580758487   network_utils:tribler_core.utilities.network_utils:38   Got a working random port 12086
DEBUG   1580758487  tribler_config:TriblerConfig:131  Get random port 12086 for [tunnel_community~socks5_listen_ports~3]
DEBUG   1580758487   network_utils:tribler_core.utilities.network_utils:38   Got a working random port 59980
DEBUG   1580758487  tribler_config:TriblerConfig:131  Get random port 59980 for [tunnel_community~socks5_listen_ports~4]
DEBUG   1580758487        notifier:Notifier:21   Add observer topic NTFY.UPGRADER_DONE callback <function EventsEndpoint.__init__.<locals>.<lambda> at 0x000000000FB5A708>
DEBUG   1580758487        notifier:Notifier:21   Add observer topic NTFY.UPGRADER_TICK callback <function EventsEndpoint.__init__.<locals>.<lambda> at 0x000000000FC16558>
DEBUG   1580758487        notifier:Notifier:21   Add observer topic NTFY.WATCH_FOLDER_CORRUPT_FILE callback <function EventsEndpoint.__init__.<locals>.<lambda> at 0x000000000FC164C8>
DEBUG   1580758487        notifier:Notifier:21   Add observer topic NTFY.TRIBLER_NEW_VERSION callback <function EventsEndpoint.__init__.<locals>.<lambda> at 0x000000000FC16678>
DEBUG   1580758487        notifier:Notifier:21   Add observer topic NTFY.CHANNEL_DISCOVERED callback <function EventsEndpoint.__init__.<locals>.<lambda> at 0x000000000FC165E8>
DEBUG   1580758487        notifier:Notifier:21   Add observer topic NTFY.TORRENT_FINISHED callback <function EventsEndpoint.__init__.<locals>.<lambda> at 0x000000000FC16708>
DEBUG   1580758487        notifier:Notifier:21   Add observer topic NTFY.CHANNEL_ENTITY_UPDATED callback <function EventsEndpoint.__init__.<locals>.<lambda> at 0x000000000FC16798>
DEBUG   1580758487        notifier:Notifier:21   Add observer topic NTFY.CREDIT_MINING_ERROR callback <function EventsEndpoint.__init__.<locals>.<lambda> at 0x000000000FC16828>
DEBUG   1580758487        notifier:Notifier:21   Add observer topic NTFY.TRIBLER_SHUTDOWN_STATE callback <function EventsEndpoint.__init__.<locals>.<lambda> at 0x000000000FC168B8>
DEBUG   1580758487        notifier:Notifier:21   Add observer topic NTFY.REMOTE_QUERY_RESULTS callback <function EventsEndpoint.__init__.<locals>.<lambda> at 0x000000000FC16948>
DEBUG   1580758487        notifier:Notifier:21   Add observer topic NTFY.TRIBLER_STARTED callback <function EventsEndpoint.__init__.<locals>.<lambda> at 0x000000000FC169D8>
DEBUG   1580758487        notifier:Notifier:21   Add observer topic NTFY.LOW_SPACE callback <function EventsEndpoint.__init__.<locals>.<lambda> at 0x000000000FC16A68>
DEBUG   1580758487        notifier:Notifier:21   Add observer topic NTFY.TUNNEL_REMOVE callback <function EventsEndpoint.__init__.<locals>.on_circuit_removed at 0x000000000FC16AF8>
DEBUG   1580758487        notifier:Notifier:21   Add observer topic NTFY.UPGRADER_STARTED callback <bound method StateEndpoint.on_tribler_upgrade_started of <tribler_core.restapi.state_endpoint.StateEndpoint object at 0x000000000FBB4B48>>
DEBUG   1580758487        notifier:Notifier:21   Add observer topic NTFY.UPGRADER_DONE callback <bound method StateEndpoint.on_tribler_upgrade_finished of <tribler_core.restapi.state_endpoint.StateEndpoint object at 0x000000000FBB4B48>>
DEBUG   1580758487        notifier:Notifier:21   Add observer topic NTFY.TRIBLER_STARTED callback <bound method StateEndpoint.on_tribler_started of <tribler_core.restapi.state_endpoint.StateEndpoint object at 0x000000000FBB4B48>>
INFO    1580758487    rest_manager:RESTManager:87   Starting REST API on port 10436
INFO    1580758487 tracker_manager:TrackerManager:38   No tracker blacklist file found at C:\workspace\jenkins2\workspace\GH_Tribler_PR_Tests\PR_async_win64\tribler\src\tribler-core\tribler_core\tests\tools\temp\TestDownloadsEndpoint_tribler_test_session_YIW5XAYJ\dot.Tribler\7.0.0-GIT\tracker_blacklist.txt.
INFO    1580758488  libtorrent_mgr:LibtorrentMgr:223  could not load libtorrent state, got exception: FileNotFoundError(2, 'No such file or directory'). starting from scratch
DEBUG   1580758488  libtorrent_mgr:LibtorrentMgr:238  Started libtorrent session for 0 hops on port 8462
DEBUG   1580758488  libtorrent_mgr:LibtorrentMgr:361  Updating IP filter ['1.1.1.1']
DEBUG   1580758488  libtorrent_mgr:LibtorrentMgr:238  Started libtorrent session for 1 hops on port 10742
DEBUG   1580758488  libtorrent_mgr:LibtorrentMgr:689  Starting the download state callback with interval 1.000000
DEBUG   1580758488  libtorrent_mgr:LibtorrentMgr:468  Starting download: filename: None, torrent def: <tribler_core.modules.libtorrent.torrentdef.TorrentDef object at 0x000000000FBF6988>
DEBUG   1580758490 libtorrent_download_impl:LibtorrentDownloadImpl:204  Setup: c9a19e7fe5d9a6c106d6ea3c01746ac88ca3c7a5
DEBUG   1580758490 libtorrent_download_impl:LibtorrentDownloadImpl:485  Saving download config to file C:\workspace\jenkins2\workspace\GH_Tribler_PR_Tests\PR_async_win64\tribler\src\tribler-core\tribler_core\tests\tools\temp\TestDownloadsEndpoint_tribler_test_session_YIW5XAYJ\dot.Tribler\7.0.0-GIT\dlcheckpoints\c9a19e7fe5d9a6c106d6ea3c01746ac88ca3c7a5.conf
DEBUG   1580758490  libtorrent_mgr:LibtorrentMgr:539  Adding handle c9a19e7fe5d9a6c106d6ea3c01746ac88ca3c7a5
DEBUG   1580758490 libtorrent_download_impl:LibtorrentDownloadImpl:245  Added torrent c9a19e7fe5d9a6c106d6ea3c01746ac88ca3c7a5
DEBUG   1580758490 libtorrent_download_impl:LibtorrentDownloadImpl:277  LibtorrentDownloadImpl: set_vod_mode for b'video.avi' (enable = False)
INFO    1580758490         session:Session:622  Tribler shutdown state notification:Shutting down Gigachannel Manager...
INFO    1580758490         session:Session:622  Tribler shutdown state notification:Saving configuration...
INFO    1580758490         session:Session:622  Tribler shutdown state notification:Checkpointing Downloads...
DEBUG   1580758490 libtorrent_download_impl:LibtorrentDownloadImpl:812  Stopping b'video.avi'
DEBUG   1580758490 libtorrent_download_impl:LibtorrentDownloadImpl:277  LibtorrentDownloadImpl: set_vod_mode for b'video.avi' (enable = False)
DEBUG   1580758491 libtorrent_download_impl:LibtorrentDownloadImpl:485  Saving download config to file C:\workspace\jenkins2\workspace\GH_Tribler_PR_Tests\PR_async_win64\tribler\src\tribler-core\tribler_core\tests\tools\temp\TestDownloadsEndpoint_tribler_test_session_YIW5XAYJ\dot.Tribler\7.0.0-GIT\dlcheckpoints\c9a19e7fe5d9a6c106d6ea3c01746ac88ca3c7a5.conf
DEBUG   1580758491 libtorrent_download_impl:LibtorrentDownloadImpl:485  Saving download config to file C:\workspace\jenkins2\workspace\GH_Tribler_PR_Tests\PR_async_win64\tribler\src\tribler-core\tribler_core\tests\tools\temp\TestDownloadsEndpoint_tribler_test_session_YIW5XAYJ\dot.Tribler\7.0.0-GIT\dlcheckpoints\c9a19e7fe5d9a6c106d6ea3c01746ac88ca3c7a5.conf
DEBUG   1580758491 libtorrent_download_impl:LibtorrentDownloadImpl:449  Setting IP filter for c9a19e7fe5d9a6c106d6ea3c01746ac88ca3c7a5 to False
INFO    1580758491         session:Session:622  Tribler shutdown state notification:Shutting down Downloads...
INFO    1580758491         session:Session:622  Tribler shutdown state notification:Shutting down Libtorrent Manager...
INFO    1580758491         session:Session:622  Tribler shutdown state notification:Shutting down Metadata Store...
INFO    1580758491         session:Session:622  Tribler shutdown state notification:Shutting down API Manager...
DEBUG   1580758491  test_as_server:TestDownloadsEndpoint:294  test_as_server: Number of threads still running 4
DEBUG   1580758491  test_as_server:TestDownloadsEndpoint:296  Thread still running MainThread, daemon: False, instance: <_MainThread(MainThread, started 4748)>
DEBUG   1580758491  test_as_server:TestDownloadsEndpoint:296  Thread still running WatchDog, daemon: True, instance: <WatchDog(WatchDog, started daemon 9892)>
DEBUG   1580758491  test_as_server:TestDownloadsEndpoint:296  Thread still running ThreadPoolExecutor-27_0, daemon: True, instance: <Thread(ThreadPoolExecutor-27_0, started daemon 5280)>
DEBUG   1580758491  test_as_server:TestDownloadsEndpoint:296  Thread still running ThreadPoolExecutor-27_1, daemon: True, instance: <Thread(ThreadPoolExecutor-27_1, started daemon 9372)>

# Fixed code
def on_move_files(self):
    if len(self.selected_items) != 1:
        return

    dest_dir = QFileDialog.getExistingDirectory(
        self,
        "Please select the destination directory",
        self.selected_items[0].download_info["destination"],
        QFileDialog.ShowDirsOnly,
    )
    if not dest_dir:
        return

    _infohash = self.selected_items[0].download_info["infohash"]
    _name = self.selected_items[0].download_info["name"]

    data = {"state": "move_storage", "dest_dir": dest_dir}

    TriblerNetworkRequest(
        "downloads/%s" % _infohash,
        lambda res, _, name=_name, target=dest_dir: self.on_files_moved(
            res, name, target
        ),
        data=data,
        method="PATCH",
    )