Example #1
0
    def test_check_cache_old_meme_source(self):

        default_sources = ['giphy', 'imgur', 'reddit']
        # try to check an empty cache and verify that old_meme_source has all 3 sources in the list
        fresh_meme_list, old_meme_source = meme_cache.check_cache('cat', True)

        # verify that old_meme_source will have 3 objects that matches default
        self.assertEqual(len(old_meme_source), 3)
        self.assertCountEqual(old_meme_source, default_sources)

        # create a test MemeCache object and create an object list
        test_memecache_giphy = MemeCache(
            'cat', True, 'giphy', 'giphy data',
            meme_cache.TODAY)  # both match. Should be in fresh_meme_list
        test_memecache_imgur = MemeCache(
            'cat', True, 'imgur', 'imgur data',
            meme_cache.TODAY)  # keyword match only
        test_memecache_list = [test_memecache_giphy, test_memecache_imgur]

        # pickle test_memecache_list and unpickle to unpickled_cache_data
        meme_cache.pickle_data(test_memecache_list)
        meme_cache.unpickle_data()

        # searches for keyword=cat and meme_only=True within cache
        # fresh_meme_list would have the list of    matching keyword/meme_only value Meme objects
        fresh_meme_list, old_meme_source = meme_cache.check_cache('cat', True)

        # verify that old_meme_source will have 1 object
        default_sources.remove('giphy')
        default_sources.remove('imgur')
        self.assertEqual(len(old_meme_source), 1)
        self.assertCountEqual(old_meme_source, default_sources)
Example #2
0
    def test_pickle_unpickle_data(self):
        # create a test MemeCache object and create an object list for pickle/unpickle testing
        test_memecache = MemeCache('cat', True, 'source', 'memedata', 'date')
        test_memecache_2 = MemeCache('dog', True, 'source', 'memedata', 'date')
        test_memecache_list = [test_memecache, test_memecache_2]

        # pickle_data() creates directory and creates pickle file if it doesn't exist
        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

        # pickle test_memecache_list and unpickle to unpickled_cache_data
        meme_cache.pickle_data(test_memecache_list)
        unpickled_cache_data = meme_cache.unpickle_data()

        # It should have created folder & file
        self.assertTrue(os.path.isdir(
            meme_cache.cache_folder))  # verify that folder has been created
        self.assertTrue(os.path.exists(
            meme_cache.cache_file_path))  # verify that file has been created

        # test of they still have the same data after pickle/unpickling by converting
        # the MemeCache object to json/dictionary value using to_json function.
        self.assertEqual(test_memecache_list[0].to_json(),
                         unpickled_cache_data[0].to_json())
        self.assertEqual(test_memecache_list[1].to_json(),
                         unpickled_cache_data[1].to_json())
Example #3
0
    def test_check_cache_keyword_and_meme_only(self):
        # create a test MemeCache object and create an object list
        test_memecache_keyword_meme_only = MemeCache(
            'cat', True, 'source', 'cat true data',
            meme_cache.TODAY)  # both match. Should be in fresh_meme_list
        test_memecache_keyword = MemeCache(
            'cat', False, 'source', 'cat false data',
            meme_cache.TODAY)  # keyword match only
        test_memecache_meme_only = MemeCache(
            'dog', True, 'source', 'dog true data',
            meme_cache.TODAY)  # meme_only match only
        test_memecache_no_match = MemeCache('dog', False, 'source',
                                            'dog false data',
                                            meme_cache.TODAY)  # no matches
        test_memecache_list = [
            test_memecache_keyword_meme_only, test_memecache_keyword,
            test_memecache_meme_only, test_memecache_no_match
        ]

        # pickle test_memecache_list and unpickle to unpickled_cache_data
        meme_cache.pickle_data(test_memecache_list)
        meme_cache.unpickle_data()

        # searches for keyword=cat and meme_only=True within cache
        # fresh_meme_list would have the list of matching keyword/meme_only value Meme objects
        fresh_meme_list, old_meme_source = meme_cache.check_cache('cat', True)

        # verify the test_memecache_list still contains 4 Meme objects
        self.assertEqual(len(test_memecache_list), 4)
        # verify that the fresh_meme_list only contains 1 Meme object,
        self.assertEqual(len(fresh_meme_list), 1)

        # verify that the objects in the fresh_meme_list contains correct objects test_memecache_keyword_meme_only
        self.assertEqual(fresh_meme_list[0].keyword,
                         test_memecache_keyword_meme_only.keyword)
        self.assertEqual(fresh_meme_list[0].meme_only,
                         test_memecache_keyword_meme_only.meme_only)
        self.assertEqual(fresh_meme_list[0].data,
                         test_memecache_keyword_meme_only.data)
Example #4
0
    def test_check_cache_fresh_period(self):

        expired = int(
            meme_cache.TODAY
        ) - meme_cache.fresh_period  # today+fresh_period, which makes it expired
        still_okay = int(meme_cache.TODAY) - (
            meme_cache.fresh_period - 1
        )  # today+fresh_period-1, which makes it still fresh enough

        # create a test MemeCache object and create an object list
        test_memecache_fresh = MemeCache('cat', True, 'source', 'freshdata',
                                         meme_cache.TODAY)
        test_memecache_still_okay = MemeCache('cat', True, 'source',
                                              'okaydata', still_okay)
        test_memecache_old = MemeCache('cat', True, 'source', 'olddata',
                                       expired)  # will be dropped
        test_memecache_list = [
            test_memecache_fresh, test_memecache_still_okay, test_memecache_old
        ]

        # pickle test_memecache_list and unpickle to unpickled_cache_data
        meme_cache.pickle_data(test_memecache_list)
        meme_cache.unpickle_data()

        # searches for keyword=cat and meme_only=True within cache
        # fresh_meme_list would have the list of fresh/unexpired meme objects
        fresh_meme_list, old_source_list = meme_cache.check_cache('cat', True)

        # verify the test_memecache_list still contains 3 Meme objects
        self.assertEqual(len(test_memecache_list), 3)
        # verify that the fresh_meme_list only contains 2 Meme objects, dropping the test_memecache_old Meme object
        self.assertEqual(len(fresh_meme_list), 2)

        # verify that the objects in the fresh_meme_list contains correct objects
        self.assertEqual(fresh_meme_list[0].data, test_memecache_fresh.data)
        self.assertEqual(fresh_meme_list[1].data,
                         test_memecache_still_okay.data)
Example #5
0
    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)