Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_fetch_from_non_set_cache(self):
"""Test if a error is raised when the cache was not set"""
remo = ReMo(MOZILLA_REPS_SERVER_URL)
with self.assertRaises(CacheError):
_ = [event for event in remo.fetch_from_cache()]
def __test_fetch_from_cache(self, category):
"""Test whether the cache works"""
HTTPServer.routes()
# First, we fetch the events from the server, storing them
# in a cache
cache = Cache(self.tmp_path)
remo = ReMo(MOZILLA_REPS_SERVER_URL, cache=cache)
items = [item for item in remo.fetch(category=category)]
requests_done = len(HTTPServer.requests_http)
# Now, we get the items from the cache.
# The contents should be the same and there won't be
# any new request to the server
cached_items = [item for item in remo.fetch_from_cache()]
# No new requests to the server
self.assertEqual(len(HTTPServer.requests_http), requests_done)
# The contents should be the same
self.assertEqual(len(cached_items), len(items))
for i in range(0,len(items)):
self.assertDictEqual(cached_items[i]['data'], items[i]['data'])
def test_initialization(self):
"""Test whether attributes are initializated"""
remo = ReMo(MOZILLA_REPS_SERVER_URL, tag='test')
self.assertEqual(remo.url, MOZILLA_REPS_SERVER_URL)
self.assertEqual(remo.origin, MOZILLA_REPS_SERVER_URL)
self.assertEqual(remo.tag, 'test')
self.assertIsInstance(remo.client, ReMoClient)
# When tag is empty or None it will be set to
# the value in url
remo = ReMo(MOZILLA_REPS_SERVER_URL)
self.assertEqual(remo.url, MOZILLA_REPS_SERVER_URL)
self.assertEqual(remo.origin, MOZILLA_REPS_SERVER_URL)
self.assertEqual(remo.tag, MOZILLA_REPS_SERVER_URL)
remo = ReMo(MOZILLA_REPS_SERVER_URL, tag='')
self.assertEqual(remo.url, MOZILLA_REPS_SERVER_URL)
self.assertEqual(remo.origin, MOZILLA_REPS_SERVER_URL)
self.assertEqual(remo.tag, MOZILLA_REPS_SERVER_URL)
# If no url is provided, MOZILLA_REPS_URL is used
remo = ReMo()
self.assertEqual(remo.url, MOZILLA_REPS_URL)
self.assertEqual(remo.origin, MOZILLA_REPS_URL)
self.assertEqual(remo.tag, MOZILLA_REPS_URL)
def __test_fetch(self, category='events'):
"""Test whether the events are returned"""
items_page = ReMoClient.ITEMS_PER_PAGE
pages = 2 # two pages of testing data
HTTPServer.routes()
prev_requests_http = len(HTTPServer.requests_http)
# Test fetch events with their reviews
remo = ReMo(MOZILLA_REPS_SERVER_URL)
items = [page for page in remo.fetch(category=category)]
self.assertEqual(len(items), items_page * pages)
if category == 'events':
self.__check_events_contents(items)
elif category == 'users':
self.__check_users_contents(items)
elif category == 'activities':
self.__check_activities_contents(items)
# Check requests: page list, items, page list, items
expected = [{'page':['1']}]
for i in range(0, items_page):
expected += [{}]
self.assertIsInstance(remo.client, ReMoClient)
# When tag is empty or None it will be set to
# the value in url
remo = ReMo(MOZILLA_REPS_SERVER_URL)
self.assertEqual(remo.url, MOZILLA_REPS_SERVER_URL)
self.assertEqual(remo.origin, MOZILLA_REPS_SERVER_URL)
self.assertEqual(remo.tag, MOZILLA_REPS_SERVER_URL)
remo = ReMo(MOZILLA_REPS_SERVER_URL, tag='')
self.assertEqual(remo.url, MOZILLA_REPS_SERVER_URL)
self.assertEqual(remo.origin, MOZILLA_REPS_SERVER_URL)
self.assertEqual(remo.tag, MOZILLA_REPS_SERVER_URL)
# If no url is provided, MOZILLA_REPS_URL is used
remo = ReMo()
self.assertEqual(remo.url, MOZILLA_REPS_URL)
self.assertEqual(remo.origin, MOZILLA_REPS_URL)
self.assertEqual(remo.tag, MOZILLA_REPS_URL)
base_path = os.path.expanduser('~/.perceval/cache/')
else:
base_path = self.parsed_args.cache_path
cache_path = os.path.join(base_path, self.url)
cache = Cache(cache_path)
if self.parsed_args.clean_cache:
cache.clean()
else:
cache.backup()
else:
cache = None
self.backend = ReMo(self.url, tag=self.tag, cache=cache)