def test_download_imgs(need_save, path_exists_retval, tags):
    """test method."""
    img_url = mock.Mock()
    img_name = mock.Mock()
    with mock.patch('tumblr_ids.tumblr.Tumblr.__init__', return_value=None), \
            mock.patch('tumblr_ids.tumblr.utils') as m_utils, \
            mock.patch('tumblr_ids.tumblr.os') as m_os, \
            mock.patch('tumblr_ids.tumblr.get_filename', return_value=img_name):
        m_os.path.exists.return_value = path_exists_retval
        from tumblr_ids.tumblr import Tumblr
        obj = Tumblr(blogs=mock.Mock())
        obj.tags = tags
        obj.save_path = mock.Mock()
        obj.proxies = mock.Mock()
        obj.stream = mock.Mock()
        obj.timeout = mock.Mock()
        obj.img_queue = mock.Mock()
        obj.img_queue.empty.side_effect = [False, True]
        obj.img_queue.get.return_value = img_url
        obj.post_queue = mock.Mock()
        obj.post_queue.empty.side_effect = [False, True]
        obj.need_save = need_save
        # run
        obj._download_imgs()
        if not need_save:
            return
        if not (tags and path_exists_retval):
            m_utils.download_imgs.assert_called_once_with(
                img_url, obj.save_path, img_name, obj.proxies, stream=obj.stream,
                timeout=obj.timeout
            )
        else:
            m_utils.download_imgs.assert_not_called()
示例#2
0
def test_download_imgs(need_save, path_exists_retval, tags):
    """test method."""
    img_url = mock.Mock()
    img_name = mock.Mock()
    with mock.patch('tumblr_ids.tumblr.Tumblr.__init__', return_value=None), \
            mock.patch('tumblr_ids.tumblr.utils') as m_utils, \
            mock.patch('tumblr_ids.tumblr.os') as m_os, \
            mock.patch('tumblr_ids.tumblr.get_filename', return_value=img_name):
        m_os.path.exists.return_value = path_exists_retval
        from tumblr_ids.tumblr import Tumblr
        obj = Tumblr(blogs=mock.Mock())
        obj.tags = tags
        obj.save_path = mock.Mock()
        obj.proxies = mock.Mock()
        obj.stream = mock.Mock()
        obj.timeout = mock.Mock()
        obj.img_queue = mock.Mock()
        obj.img_queue.empty.side_effect = [False, True]
        obj.img_queue.get.return_value = img_url
        obj.post_queue = mock.Mock()
        obj.post_queue.empty.side_effect = [False, True]
        obj.need_save = need_save
        # run
        obj._download_imgs()
        if not need_save:
            return
        if not (tags and path_exists_retval):
            m_utils.download_imgs.assert_called_once_with(img_url,
                                                          obj.save_path,
                                                          img_name,
                                                          obj.proxies,
                                                          stream=obj.stream,
                                                          timeout=obj.timeout)
        else:
            m_utils.download_imgs.assert_not_called()
def test_process_images(filename_exists, is_limit_reached, need_save, image_limit):
    """test method."""
    img_item = mock.Mock()
    img_item.replace.return_value = img_item
    images = [img_item]
    image_counter = 0
    filename = mock.Mock()
    exp_res = {'is_limit_reached': is_limit_reached, 'image_counter': image_counter}
    if need_save:
        if image_limit is not None:
            if image_limit <= image_counter:
                exp_res['is_limit_reached'] = True
        else:
            exp_res['is_limit_reached'] = False
        if not(filename_exists or exp_res['is_limit_reached']):
            exp_res['image_counter'] += 1
    with mock.patch('tumblr_ids.tumblr.Tumblr.__init__', return_value=None), \
            mock.patch('tumblr_ids.tumblr.get_filename', return_value=filename):
        from tumblr_ids.tumblr import Tumblr
        obj = Tumblr(blog=mock.Mock())
        obj.need_save = need_save
        obj.image_limit = image_limit
        obj.img_queue = mock.Mock()
        obj.imglog = mock.Mock()
        obj._check_already_exists = mock.Mock(return_value=filename_exists)
        # run
        res = obj._process_images(
            images=images, image_counter=image_counter, is_limit_reached=is_limit_reached)
        assert res == exp_res
        # test
        if need_save and not(filename_exists or exp_res['is_limit_reached']):
            obj.img_queue.put.assert_called_once_with(images[0])
def test_get_imgs(is_img_queue_empty, need_save, total_posts_default, get_total_posts_retval):
    """test method."""
    with mock.patch('tumblr_ids.tumblr.Tumblr.__init__', return_value=None):
        from tumblr_ids.tumblr import Tumblr
        obj = Tumblr(blog=mock.Mock())
        obj.threads_num = 1
        obj.tags = [mock.Mock()]
        obj.total_posts = total_posts_default
        obj.need_save = need_save
        obj.img_queue = mock.Mock()
        obj.img_queue.empty.return_value = is_img_queue_empty
        obj._download_imgs = mock.Mock()
        obj._get_img_urls = mock.Mock()
        obj._get_total_posts = mock.Mock(return_value=get_total_posts_retval)
        # run
        obj.get_imgs()
        # test
        assert obj.total_posts == 0
        if total_posts_default or get_total_posts_retval:
            obj._get_img_urls.assert_called_once_with()
        if need_save and not is_img_queue_empty:
            obj._download_imgs.assert_called_once_with()
def test_get_imgs_using_threading(total_posts_default, get_total_posts_retval, need_save):
    """test method."""
    with mock.patch('tumblr_ids.tumblr.Tumblr.__init__', return_value=None):
        from tumblr_ids.tumblr import Tumblr
        obj = Tumblr(blog=mock.Mock())
        obj.need_save = need_save
        obj.tags = [mock.Mock()]
        obj.total_posts = total_posts_default
        obj._get_total_posts = mock.Mock(return_value=get_total_posts_retval)
        obj._process_img_queue = mock.Mock()
        obj._run_threads = mock.Mock()
        # run
        obj.get_imgs_using_threading()
        # test
        assert obj.tag == obj.tags[0]
        assert obj.total_posts == 0
        if total_posts_default or get_total_posts_retval:
            obj._run_threads.assert_called_once_with()
        if get_total_posts_retval:
            obj._run_threads.assert_called_once_with()
        if need_save:
            obj._process_img_queue.assert_called_once_with([])
示例#6
0
def test_get_imgs(is_img_queue_empty, need_save, total_posts_default,
                  get_total_posts_retval):
    """test method."""
    with mock.patch('tumblr_ids.tumblr.Tumblr.__init__', return_value=None):
        from tumblr_ids.tumblr import Tumblr
        obj = Tumblr(blog=mock.Mock())
        obj.threads_num = 1
        obj.tags = [mock.Mock()]
        obj.total_posts = total_posts_default
        obj.need_save = need_save
        obj.img_queue = mock.Mock()
        obj.img_queue.empty.return_value = is_img_queue_empty
        obj._download_imgs = mock.Mock()
        obj._get_img_urls = mock.Mock()
        obj._get_total_posts = mock.Mock(return_value=get_total_posts_retval)
        # run
        obj.get_imgs()
        # test
        assert obj.total_posts == 0
        if total_posts_default or get_total_posts_retval:
            obj._get_img_urls.assert_called_once_with()
        if need_save and not is_img_queue_empty:
            obj._download_imgs.assert_called_once_with()
示例#7
0
def test_get_imgs_using_threading(total_posts_default, get_total_posts_retval,
                                  need_save):
    """test method."""
    with mock.patch('tumblr_ids.tumblr.Tumblr.__init__', return_value=None):
        from tumblr_ids.tumblr import Tumblr
        obj = Tumblr(blog=mock.Mock())
        obj.need_save = need_save
        obj.tags = [mock.Mock()]
        obj.total_posts = total_posts_default
        obj._get_total_posts = mock.Mock(return_value=get_total_posts_retval)
        obj._process_img_queue = mock.Mock()
        obj._run_threads = mock.Mock()
        # run
        obj.get_imgs_using_threading()
        # test
        assert obj.tag == obj.tags[0]
        assert obj.total_posts == 0
        if total_posts_default or get_total_posts_retval:
            obj._run_threads.assert_called_once_with()
        if get_total_posts_retval:
            obj._run_threads.assert_called_once_with()
        if need_save:
            obj._process_img_queue.assert_called_once_with([])
示例#8
0
def test_process_images(filename_exists, is_limit_reached, need_save,
                        image_limit):
    """test method."""
    img_item = mock.Mock()
    img_item.replace.return_value = img_item
    images = [img_item]
    image_counter = 0
    filename = mock.Mock()
    exp_res = {
        'is_limit_reached': is_limit_reached,
        'image_counter': image_counter
    }
    if need_save:
        if image_limit is not None:
            if image_limit <= image_counter:
                exp_res['is_limit_reached'] = True
        else:
            exp_res['is_limit_reached'] = False
        if not (filename_exists or exp_res['is_limit_reached']):
            exp_res['image_counter'] += 1
    with mock.patch('tumblr_ids.tumblr.Tumblr.__init__', return_value=None), \
            mock.patch('tumblr_ids.tumblr.get_filename', return_value=filename):
        from tumblr_ids.tumblr import Tumblr
        obj = Tumblr(blog=mock.Mock())
        obj.need_save = need_save
        obj.image_limit = image_limit
        obj.img_queue = mock.Mock()
        obj.imglog = mock.Mock()
        obj._check_already_exists = mock.Mock(return_value=filename_exists)
        # run
        res = obj._process_images(images=images,
                                  image_counter=image_counter,
                                  is_limit_reached=is_limit_reached)
        assert res == exp_res
        # test
        if need_save and not (filename_exists or exp_res['is_limit_reached']):
            obj.img_queue.put.assert_called_once_with(images[0])