コード例 #1
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()
コード例 #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()
コード例 #3
0
def test_process_img_queue(is_img_queue_empty):
    """test method."""
    with mock.patch('tumblr_ids.tumblr.Tumblr.__init__', return_value=None), \
            mock.patch('tumblr_ids.tumblr.threading') as m_threading:
        from tumblr_ids.tumblr import Tumblr
        obj = Tumblr(blog=mock.Mock())
        obj.threads_num = 1
        obj.img_queue = mock.Mock()
        obj.img_queue.empty.return_value = is_img_queue_empty
        obj._download_imgs = mock.Mock()
        # run
        obj._process_img_queue(consumer=[])
        # test
        if not is_img_queue_empty:
            m_threading.assert_has_calls([
                mock.call.Thread(target=obj._download_imgs),
                mock.call.Thread().start()
            ])
コード例 #4
0
def test_process_img_queue(is_img_queue_empty):
    """test method."""
    with mock.patch('tumblr_ids.tumblr.Tumblr.__init__', return_value=None), \
            mock.patch('tumblr_ids.tumblr.threading') as m_threading:
        from tumblr_ids.tumblr import Tumblr
        obj = Tumblr(blog=mock.Mock())
        obj.threads_num = 1
        obj.img_queue = mock.Mock()
        obj.img_queue.empty.return_value = is_img_queue_empty
        obj._download_imgs = mock.Mock()
        # run
        obj._process_img_queue(consumer=[])
        # test
        if not is_img_queue_empty:
            m_threading.assert_has_calls([
                mock.call.Thread(target=obj._download_imgs),
                mock.call.Thread().start()
            ])
コード例 #5
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()
コード例 #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()