def test_dlmediaShouldCheckAbortFlag_ForEveryUser(mock_bindings):
    users = ['abby', 'bobby', 'charlie', 'daddy']
    mock_bindings.get_users.return_value = users
    mock_abort = mock_bindings.get_abort.return_value

    dlmedia(mock_bindings)
    assert len(mock_abort.is_set.call_args_list) == len(users)
def test_dlmediaShouldWriteToHistoryFile(mock_bindings):
    mock_history = mock_bindings.get_history.return_value

    dlmedia(mock_bindings)

    assert len(mock_history.writeToFile.call_args_list) == 1
    filepath, *_ = mock_history.writeToFile.call_args_list[0][0]
    assert filepath == TEST_DL_LOCATION / constants.FILENAME_HISTORY
def test_dlmedaShouldNotCallDownloadMedia_whenAbortFlagIsSet(mock_bindings):
    users = ['abby', 'bobby', 'charlie', 'daddy']
    mock_bindings.get_users.return_value = users
    mock_bindings.get_abort.return_value.is_set.return_value = True
    mock_download_media = mock_bindings.download_media

    dlmedia(mock_bindings)

    assert len(mock_download_media.call_args_list) == 0
def test_dlmediaShouldCaptureExceptionsFromValidateSettings(mock_bindings):
    exceptions_to_throw = [
        exceptions.SaveLocationNotExist('Save location does not exist'),
        exceptions.APINotFound('Api was not found'),
    ]

    for exception in exceptions_to_throw:
        mock_bindings.get_settings.side_effect = exception

        dlmedia(mock_bindings)
def test_dlMediaDownloads3Images(use_test_settings):
    userListFile = PROJECT_DIR / 'testdata' / 'userlists'/ 'me.txt'

    shutil.copyfile(userListFile, TEST_DL_LOCATION / constants.FILENAME_USERS)
    bindings = RuntimeBindings(PROJECT_DIR)

    dlmedia(bindings)

    assert Path(TEST_DL_LOCATION, 'poopoopanda21', 'images', 'EGWZFWPUYAABM6m.jpg').exists()
    assert Path(TEST_DL_LOCATION, 'poopoopanda21', 'images', 'EGWZDQgU0AAAOgl.jpg').exists()
    assert Path(TEST_DL_LOCATION, 'poopoopanda21', 'images', 'EGWZAe4UEAAabKm.jpg').exists()
def test_dlMediaDownloads0ImagesWhenHistoryPresent(use_test_settings):
    userListFile = PROJECT_DIR / 'testdata' / 'userlists'/ 'me.txt'
    existingHistory = PROJECT_DIR / 'testdata' / 'history' / 'history_poopoopanda21.json'

    shutil.copyfile(existingHistory, TEST_DL_LOCATION / constants.FILENAME_HISTORY)
    shutil.copyfile(userListFile, TEST_DL_LOCATION / constants.FILENAME_USERS)
    bindings = RuntimeBindings(PROJECT_DIR)
    
    dlmedia(bindings)

    assert not Path(TEST_DL_LOCATION, 'poopoopanda21', 'images', 'EGWZFWPUYAABM6m.jpg').exists()
    assert not Path(TEST_DL_LOCATION, 'poopoopanda21', 'images', 'EGWZDQgU0AAAOgl.jpg').exists()
    assert not Path(TEST_DL_LOCATION, 'poopoopanda21', 'images', 'EGWZAe4UEAAabKm.jpg').exists()

    assert Path(existingHistory).exists()
Beispiel #7
0
from pathlib import Path
import shutil
import sys
import os
import logging

PROJECT_DIR = Path(__file__).resolve().parents[0]
TEST_WORK_DIR = PROJECT_DIR / 'testdata' / 'work'
SETTINGS_DIR = PROJECT_DIR / 'testdata' / 'settings'

sys.path.append(str(PROJECT_DIR / 'src'))

from twitter_image_dl.twitterimagedl import dlmedia
from twitter_image_dl.runtime_bindings import RuntimeBindings
from twitter_image_dl.log_setup import setup_logger

if os.getenv('IS_TEST', None) == 'True':
    shutil.copyfile(SETTINGS_DIR / 'settings.conf',
                    TEST_WORK_DIR / 'settings.conf')
    work_dir = TEST_WORK_DIR
else:
    work_dir = Path(sys._MEIPASS)  # dir where exe is located at

setup_logger(work_dir, logging.DEBUG)
bindings = RuntimeBindings(work_dir)
dlmedia(bindings)
def test_dlmediaShouldValidateSettings(mock_bindings):
    mock_settings = mock_bindings.get_settings.return_value

    dlmedia(mock_bindings)

    assert len(mock_settings.validate_settings.call_args_list) == 1