def test_find_meme_api_call(self):

        # verify that there are no data in cache
        self.assertFalse(os.path.isdir(
            meme_cache.cache_folder))  # verify that folder doesn't exist
        self.assertFalse(os.path.exists(
            meme_cache.cache_file_path))  # verify that file doesn't exist

        memes = meme_finder.find_meme('cat', True)

        # verify that there are 3 Meme objects from each api sources in memes list
        self.assertEqual(len(memes), 3)

        source_list = ['giphy', 'imgur', 'reddit']
        # verify that source_list has 3 objects
        self.assertTrue(len(source_list) == 3)

        for meme in memes:
            self.assertIsInstance(
                meme,
                Meme)  # verify that all 3 objects in the list are Meme objects
            # if meme.source matches the source in source_list, remove it,
            if meme.source in source_list:
                source_list.remove(meme.source)

        # after for loop iteration, source_list should be empty
        self.assertTrue(len(source_list) == 0)

        # remove temp folder & file
        shutil.rmtree(meme_cache.cache_folder)
Beispiel #2
0
def index():

    # validate that there is no blank input
    search_form = MemeSearchForm(request.form)
    if request.method == 'POST' and search_form.validate():

        keyword = search_form.keyword.data
        meme_only = search_form.meme_only.data

        # values debugging
        logging.debug('keyword: ' + keyword + " --search query")
        logging.debug('meme_only: ' + str(meme_only) + " --on for meme only checked/ None for unchecked")

        memes = find_meme(keyword, meme_only)

        try:
            for meme in memes:
                # logging the meme data
                logging.debug("{}: title:{} img:{} link:{}".format(meme.source.upper(), str(meme.title), meme.img_src,
                                                                   meme.post_link))

        except AttributeError as ae:
            logging.error(ae)
        except UnicodeEncodeError as ue:
            logging.error(ue)

        return render_template('meme.html', keyword=keyword, memes=memes)

    return render_template('index.html')
    def test_get_new_memes_from_api(self):
        db_sqlite.cur.execute('SELECT * FROM memecache')
        cache = db_sqlite.cur.fetchall()

        self.assertCountEqual(cache, [])  # assert that cache is empty

        cat_memes = meme_finder.find_meme(keyword='cat', meme_only=True)

        print(len(cat_memes))
    def test_find_meme_cache(self, mock_get_fresh_memes,
                             mock_giphy_create_object,
                             mock_imgur_create_object,
                             mock_reddit_create_object):
        # verify that there are no data in cache
        self.assertFalse(os.path.isdir(
            meme_cache.cache_folder))  # verify that folder doesn't exist
        self.assertFalse(os.path.exists(
            meme_cache.cache_file_path))  # verify that file doesn't exist

        mock_get_fresh_memes.return_value = None  # api calls won't return any new memes

        # execute find_meme function
        memes = meme_finder.find_meme('cat', True)

        # memes value should be none because it didn't get anything from the cache nor api.
        self.assertIsNone(memes)

        # create a temporary meme cache
        temp_cache = [
            MemeCache('cat', True, 'giphy', 'data'),
            MemeCache('cat', True, 'imgur', 'data'),
            MemeCache('cat', True, 'reddit', 'data')
        ]

        # pickle the cache data and load data
        meme_cache.pickle_data(temp_cache)
        meme_cache.unpickle_data()

        # mock create_object return value because we don't have proper data in our test MemeCache objects
        # However, each function only will be evoked when there are cache data with the matching source value
        mock_giphy_create_object.return_value = Meme('giphy', 'giphy title',
                                                     'giphy img_src',
                                                     'giphy link')
        mock_imgur_create_object.return_value = Meme('imgur', 'imgur title',
                                                     'imgur img_src',
                                                     'imgur link')
        mock_reddit_create_object.return_value = Meme('reddit', 'reddit title',
                                                      'reddit img_src',
                                                      'reddit link')

        # execute find_meme function
        memes = meme_finder.find_meme('cat', True)

        # verify that there are 3 Meme objects from each api sources in memes list
        self.assertEqual(len(memes), 3)

        source_list = ['giphy', 'imgur', 'reddit']
        # verify that source_list has 3 objects
        self.assertTrue(len(source_list) == 3)

        for meme in memes:
            self.assertIsInstance(
                meme,
                Meme)  # verify that all 3 objects in the list are Meme objects
            # if meme.source matches the source in source_list, remove it,
            if meme.source in source_list:
                source_list.remove(meme.source)

        # after for loop iteration, source_list should be empty
        self.assertTrue(len(source_list) == 0)