def test_img_uploader_with_default_imgur_acc():
    """test func."""
    speech_text = mock.Mock()
    msg = 'upload requires a client id and secret'
    with mock.patch('sys.stdout', new_callable=StringIO) \
            as m_stdout, \
            mock.patch('melissa.actions.imgur_handler.tts') \
            as m_tts:
        from melissa.actions import imgur_handler
        imgur_handler.profile.data = {
            'imgur': {
                'client_id': "xxxx",
                'client_secret': "xxxx",
            },
            'memory_db': 'm_memory_db',
        }
        imgur_handler.image_uploader(speech_text)
        assert msg in m_stdout.getvalue()
        m_tts.assert_called_once_with(msg)
def test_img_uploader(arg, m_image, gen_return_image):
    """test image uploader."""
    m_result_link = 'm_result_link'
    m_datetime_strftime = '04-11-2016'
    with mock.patch('melissa.actions.imgur_handler.tts') as m_tts, \
            mock.patch('melissa.actions.imgur_handler.ImgurClient') \
            as m_i_client, \
            mock.patch('sys.stdout', new_callable=StringIO) \
            as m_stdout, \
            mock.patch('melissa.actions.imgur_handler.sqlite3') \
            as m_sqlite3, \
            mock.patch('melissa.actions.imgur_handler.datetime') \
            as m_datetime, \
            mock.patch('melissa.actions.imgur_handler.img_list_gen') \
            as m_img_list_gen:
        from melissa.actions import imgur_handler
        imgur_handler.profile.data = {
            'imgur': {
                'client_id': M_CLIENT_ID,
                'client_secret': M_CLIENT_SECRET,
            },
            'memory_db': M_MEMORY_DB,
        }
        if isinstance(arg, mock.Mock):
            # run
            with pytest.raises(TypeError):
                imgur_handler.image_uploader(arg)
            # test
            arg.assert_has_calls([
                mock.call.split(),
                mock.call.split().remove('upload')
            ])

        elif arg == 'upload':
            imgur_handler.image_uploader(arg)
            m_tts.assert_called_once_with(
                'upload requires a picture name')
        elif arg == 'upload image':
            if not gen_return_image:
                imgur_handler.image_uploader(arg)
                m_tts.assert_not_called()
            elif m_image == 'nothing':
                m_img_list_gen.return_value = [m_image]
                imgur_handler.image_uploader(arg)
            elif m_image == 'image':
                # pre run
                m_img_list_gen.return_value = [m_image]
                m_i_client.return_value.upload_from_path.return_value = {
                    'link': m_result_link}
                m_datetime.strftime.return_value = m_datetime_strftime
                # run
                imgur_handler.image_uploader(arg)
                # test
                m_tts.assert_called_once_with(
                    'Your image has been uploaded')
                m_i_client.return_value.upload_from_path(
                    m_image, anon=True, config=None)
                m_sqlite3.assert_has_calls([
                    mock.call.connect(M_MEMORY_DB),
                    mock.call.connect().execute(
                        (
                            'INSERT INTO image_uploads '
                            '(filename, url, upload_date) '
                            'VALUES (?, ?, ?)'
                        ),
                        (
                            m_image,
                            m_result_link,
                            m_datetime_strftime
                        )
                    ),
                    mock.call.connect().commit(),
                    mock.call.connect().close()
                ])
                m_datetime.assert_has_calls([
                    mock.call.now(),
                    mock.call.strftime(
                        m_datetime.now.return_value, '%d-%m-%Y')
                ])
                assert m_result_link in m_stdout.getvalue()
            m_i_client.assert_called_once_with(
                M_CLIENT_ID,
                M_CLIENT_SECRET,
            )
            m_img_list_gen.assert_called_once_with()