def test_fetch_from_non_set_cache(self): """Test if a error is raised when the cache was not set""" mozillaclub = MozillaClub(MozillaClub_FEED_URL) with self.assertRaises(CacheError): _ = [event for event in mozillaclub.fetch_from_cache()]
def test_fetch(self): """Test whether a list of events is returned""" http_requests = configure_http_server() # Test fetch events from feed mozillaclub = MozillaClub(MozillaClub_FEED_URL) events = [event for event in mozillaclub.fetch()] self.assertEqual(len(events), 92) self.assertEqual(len(http_requests), 1) # Test metadata expected = [('a60e67f5094f325f1cc826159749eaecbd177fc9', 1481643844.821, 'Rio Mozilla Club'), ('2e6eaf1f174600d20233e5051e85d730952a8a31', 1481643844.821, 'Firefox club uog-skt'), ('da8820deeb01616d22d6851272e8db915efc351b', 1481643844.821, 'Mozilla HETEC Club')] for x in range(len(expected)): event = events[x] self.assertEqual(event['origin'], 'http://example.com/feed') self.assertEqual(event['uuid'], expected[x][0]) self.assertEqual(event['updated_on'], expected[x][1]) self.assertEqual(event['category'], 'event') self.assertEqual(event['tag'], 'http://example.com/feed') self.assertEqual(event['data']['Club Name'], expected[x][2])
def test_fetch_from_empty_cache(self): """Test if there are not any events returned when the cache is empty""" cache = Cache(self.tmp_path) mozillaclub = MozillaClub(MozillaClub_FEED_URL, cache=cache) cached_events = [event for event in mozillaclub.fetch_from_cache()] self.assertEqual(len(cached_events), 0)
def test_fetch_empty(self): """Test whether it works when no events are fetched""" body = """ { "encoding": "UTF-8", "feed": { "event": [] } } """ httpretty.register_uri(httpretty.GET, MozillaClub_FEED_URL, body=body, status=200) mozillaclub = MozillaClub(MozillaClub_FEED_URL) events = [event for event in mozillaclub.fetch()] self.assertEqual(len(events), 0)
def test_empty_cells(self): """ Test whether the empty fields cells that are not included in the json feed, are filled with None in the generated event. """ http_requests = configure_http_server() # https://docs.google.com/spreadsheets/d/1QHl2bjBhMslyFzR5XXPzMLdzzx7oeSKTbgR5PM8qp64/pubhtml # Club Link is always empty so it is not included in # https://spreadsheets.google.com/feeds/cells/1QHl2bjBhMslyFzR5XXPzMLdzzx7oeSKTbgR5PM8qp64/ohaibtm/public/values?alt=json # Check that the field 'Club Link' exist in the items with None value mozillaclub = MozillaClub(MozillaClub_FEED_URL) events = [event for event in mozillaclub.fetch()] self.assertEqual(len(events), 92) for event in events: self.assertEqual('Club Link' in event['data'].keys(), True) self.assertEqual(event['data']['Club Link'], None)
def test_initialization(self): """Test whether attributes are initializated""" mozillaclub = MozillaClub(MozillaClub_FEED_URL, tag='test') self.assertEqual(mozillaclub.url, MozillaClub_FEED_URL) self.assertEqual(mozillaclub.origin, MozillaClub_FEED_URL) self.assertEqual(mozillaclub.tag, 'test') self.assertIsNone(mozillaclub.client) # When tag is empty or None it will be set to # the value in url mozillaclub = MozillaClub(MozillaClub_FEED_URL) self.assertEqual(mozillaclub.url, MozillaClub_FEED_URL) self.assertEqual(mozillaclub.origin, MozillaClub_FEED_URL) self.assertEqual(mozillaclub.tag, MozillaClub_FEED_URL) mozillaclub = MozillaClub(MozillaClub_FEED_URL, tag='') self.assertEqual(mozillaclub.url, MozillaClub_FEED_URL) self.assertEqual(mozillaclub.origin, MozillaClub_FEED_URL) self.assertEqual(mozillaclub.tag, MozillaClub_FEED_URL)
def test_fetch_from_cache(self): """Test whether the cache works""" http_requests = configure_http_server() # First, we fetch the events from the server, storing them # in a cache cache = Cache(self.tmp_path) mozillaclub = MozillaClub(MozillaClub_FEED_URL, cache=cache) events = [event for event in mozillaclub.fetch()] self.assertEqual(len(http_requests), 1) # Now, we get the events from the cache. # The contents should be the same and there won't be # any new request to the server cached_events = [event for event in mozillaclub.fetch_from_cache()] self.assertEqual(len(cached_events), len(events)) for i in range(0, len(events)): self.assertDictEqual(cached_events[i]['data'], events[i]['data']) self.assertEqual(len(http_requests), 1) # no more requests done
def test_search_fields(self): """Test whether the search_fields is properly set""" configure_http_server() mozillaclub = MozillaClub(MozillaClub_FEED_URL) events = [event for event in mozillaclub.fetch()] event = events[0] self.assertEqual(mozillaclub.metadata_id(event['data']), event['search_fields']['item_id']) self.assertEqual(event['data']['Club Name'], 'Rio Mozilla Club') self.assertEqual(event['data']['Club Name'], event['search_fields']['club_name']) event = events[1] self.assertEqual(mozillaclub.metadata_id(event['data']), event['search_fields']['item_id']) self.assertEqual(event['data']['Club Name'], 'Firefox club uog-skt') self.assertEqual(event['data']['Club Name'], event['search_fields']['club_name']) event = events[2] self.assertEqual(mozillaclub.metadata_id(event['data']), event['search_fields']['item_id']) self.assertEqual(event['data']['Club Name'], 'Mozilla HETEC Club') self.assertEqual(event['data']['Club Name'], event['search_fields']['club_name'])
def setUp(self): super().setUp() self.backend_write_archive = MozillaClub(MozillaClub_FEED_URL, archive=self.archive) self.backend_read_archive = MozillaClub(MozillaClub_FEED_URL, archive=self.archive)
def test_has_resuming(self): """Test if it returns False when has_resuming is called""" self.assertEqual(MozillaClub.has_resuming(), False)
def test_has_archiving(self): """Test if it returns True when has_archiving is called""" self.assertEqual(MozillaClub.has_archiving(), True)