def test_strategies(mocker, test_data, test_env): # Logger lg = logger.Logger.register(__name__) # Exceptions mapping exception_map = { IllegalCharacter: 'IllegalCharacter', ConditionSyntaxError: 'ConditionSyntaxError', NoSuchCondition: 'NoSuchCondition' } # Check each case base_dir = os.path.join(os.path.realpath(os.path.dirname(__file__)), 'cases') lg.info('Base directory: %s' % base_dir) for item in os.listdir(base_dir): if os.path.isdir(os.path.join(base_dir, item)): # Enter a directory lg.info("Entering directory '%s'..." % item) for conf_file in os.listdir(os.path.join(base_dir, item)): conf_path = os.path.join(base_dir, item, conf_file) if os.path.isfile(conf_path): # Load file lg.info('Loading file: %s' % conf_file) with _open(conf_path, encoding='utf-8') as f: conf = yaml.safe_load(f) try: # Make strategy and run mocker.patch('time.time', return_value=test_env['time.time']) mocker.patch( 'shutil.disk_usage', return_value=test_env['shutil.disk_usage']) stgy = Strategy(conf_file, conf['test']) stgy.execute(test_data) # Check result if 'remain' in conf: assert set([ x.name for x in stgy.remain_list ]) == set(conf['remain'] if conf['remain'] is not None else []) if 'remove' in conf: assert set([ x.name for x in stgy.remove_list ]) == set(conf['remove'] if conf['remove'] is not None else []) except Exception as e: if 'exceptions' in conf and exception_map[ sys.exc_info()[0]] in conf['exceptions']: pass else: raise e # Leave the directory lg.info("Leaving directory '%s'..." % item)
def runner(): # Set root directory root_dir = os.path.join(os.path.realpath(os.path.dirname(__file__))) # Mock qBittorrent API version # Force caller to use API v1.0 requests_mock.get('mock://qbittorrent/api/v2/app/webapiVersion', status_code=404) requests_mock.get('mock://qbittorrent/version/api', text='10') # Mock qBittorrent version requests_mock.get('mock://qbittorrent/version/qbittorrent', text='Fake qBittorrent') #lg.info('qBittorrent version was mocked.') # Mock qBittorrent logging in interface requests_mock.post('mock://qbittorrent/login', text='Ok.') #lg.info('Logging in was mocked.') # Mock qBittorrent torrents list with _open(os.path.join(root_dir, 'qbittorrent-snapshots', '4-1-5.json'), 'r', encoding='utf-8') as f: torrent_list = json.load(f) requests_mock.get('mock://qbittorrent/query/torrents', json=torrent_list) #lg.info('Torrents list was mocked.') # Mock qBittorent torrent details for torrent in torrent_list: with _open( os.path.join(root_dir, 'qbittorrent-snapshots', 'torrents', '%s.json' % torrent['hash'])) as fp: metadata = json.load(fp) requests_mock.get( 'mock://qbittorrent/query/propertiesGeneral/%s' % torrent['hash'], json=metadata['properties']) requests_mock.get( 'mock://qbittorrent/query/propertiesTrackers/%s' % torrent['hash'], json=metadata['trackers'])
def test_client(env_dist): # Logger lg = logger.Logger.register(__name__) # Mapping of exceptions exception_map = { ConnectionFailure: 'ConnectionFailure', DeletionFailure: 'DeletionFailure', LoginFailure: 'LoginFailure', NoSuchClient: 'NoSuchClient', NoSuchTorrent: 'NoSuchTorrent', RemoteFailure: 'RemoteFailure' } # Set basic directory basic_dir = os.path.join(os.path.realpath(os.path.dirname(__file__)), 'cases') lg.info('Basic directory: %s' % basic_dir) for file in os.listdir(basic_dir): file_path = os.path.join(basic_dir, file) if os.path.isfile(file_path): # Load file lg.info('Loading file: %s' % file) with _open(file_path, encoding='utf-8') as f: conf = yaml.safe_load(f) # Make take and run try: Task(file, conf['task'], True).execute() if 'exceptions' in conf and len( conf['exceptions']) > 0: # No exceptions was raised raise AssertionError( "It didn't raise exceptions as expected") except Exception as e: # Check if the excpetion is expected found = False for e in exception_map: if sys.exc_info()[0] == e: if 'exceptions' in conf and exception_map[e] in conf[ 'exceptions']: lg.info('An expected exception was raised: %s.' % exception_map[e]) found = True break # An expected exception if not found: raise e # Unexpected exception
def test_data(): # Load input data input_torrents = [] with _open(os.path.join(os.path.realpath(os.path.dirname(__file__)), 'data.json'), encoding='utf-8') as f: data = json.load(f) for torrent in data: input_torrents.append( Torrent(torrent['hash'], torrent['name'], torrent['category'], torrent['tracker'], TorrentStatus[str(torrent['state']).capitalize()], torrent['is_stalled'], torrent['size'], torrent['ratio'], torrent['uploaded'], torrent['added_on'], torrent['seeding_time'])) return input_torrents
def test_task(qbittorrent_mocker): # Loggger lg = logger.Logger.register(__name__) # Set root directory root_dir = os.path.join(os.path.realpath(os.path.dirname(__file__))) lg.info('Root directory: %s', root_dir) qbittorrent_mocker() # Load files in directory for file in os.listdir(os.path.join(root_dir, 'cases')): file_path = os.path.join(root_dir, 'cases', file) if os.path.isfile(file_path): lg.info('Loading file: %s', file) with _open(file_path, 'r', encoding='utf-8') as f: conf = yaml.safe_load(f) # Run task instance = Task(file, conf['task'], False) instance.execute() assert len(instance.get_removed_torrents() ) == conf['result']['num-of-removed']
#-*- coding:UTF-8 -*- from setuptools import setup, find_packages from autoremovetorrents.version import __version__ from autoremovetorrents.compatibility.open import _open setup( name='autoremove-torrents', version=__version__, description='Automatically remove torrents according to your strategies.', long_description=_open('README.rst', 'r', encoding='utf-8').read(), classifiers=[ 'Development Status :: 4 - Beta', 'Environment :: Console', 'License :: OSI Approved :: MIT License', 'Programming Language :: Python', 'Topic :: Utilities' ], # Get classifiers from https://pypi.org/pypi?%3Aaction=list_classifiers keywords='python autoremove torrent', author='jerrymakesjelly', author_email='*****@*****.**', url='https://github.com/jerrymakesjelly/autoremove-torrents', license='MIT', packages=find_packages(), include_package_data=True, zip_safe=True, install_requires=['requests', 'pyyaml', 'enum34', 'ply', 'psutil'], entry_points={ 'console_scripts': ['autoremove-torrents = autoremovetorrents.main:main'] })
def test_env(): with _open(os.path.join(os.path.realpath(os.path.dirname(__file__)), 'environment.json'), encoding='utf-8') as f: env = json.load(f) return env