def test_fetch_from_date(self): """Test when fetching messages from a given date""" setup_http_server() from_date = datetime.datetime(2020, 3, 24, 0, 0, tzinfo=dateutil.tz.tzutc()) backend = Gitter(group='testapicomm', room='community', api_token='aaa', max_items=1) messages = [m for m in backend.fetch(from_date=from_date)] print(messages) self.assertEqual(len(messages), 1) message = messages[0] self.assertEqual(message['data']['id'], '5e7990ecada8262f814a8056') self.assertEqual(message['origin'], 'https://gitter.im/testapicomm/community') self.assertEqual(message['uuid'], '8053a8916819636b24e795c594cd24310835477f') self.assertEqual(message['updated_on'], 1585025260.777) self.assertEqual(message['category'], 'message') self.assertEqual(message['tag'], 'https://gitter.im/testapicomm/community') self.assertEqual(message['data']['text'], 'This is test message 2') self.assertEqual(message['data']['fromUser']['id'], '5e2f7e72d73408ce4fd7f187') self.assertEqual(message['data']['fromUser']['displayName'], 'Animesh Kumar')
def test_initialization(self): """Test whether attributes are initialized""" backend = Gitter(group='testapicomm', room='community', api_token='aaa', tag='test') self.assertEqual(backend.group, 'testapicomm') self.assertEqual(backend.room, 'community') self.assertEqual(backend.api_token, "aaa") self.assertEqual(backend.min_rate_to_sleep, MIN_RATE_LIMIT) self.assertIsNone(backend.room_id) self.assertIsNone(backend.client) self.assertFalse(backend.sleep_for_rate) self.assertTrue(backend.ssl_verify) self.assertEqual(backend.origin, 'https://gitter.im/testapicomm/community') self.assertEqual(backend.tag, 'test') self.assertEqual(backend.max_items, MAX_ITEMS) # When tag is empty or None it will be set to # the value in uri backend = Gitter(group='testapicomm', room='community', api_token='aaa', tag=None) self.assertEqual(backend.origin, 'https://gitter.im/testapicomm/community') self.assertEqual(backend.tag, 'https://gitter.im/testapicomm/community') backend = Gitter(group='testapicomm', room='community', api_token='aaa', tag='') self.assertEqual(backend.origin, 'https://gitter.im/testapicomm/community') self.assertEqual(backend.tag, 'https://gitter.im/testapicomm/community') backend = Gitter(group='testapicomm', room='community', api_token='aaa', tag='', ssl_verify=False, sleep_for_rate=True, max_items=20, min_rate_to_sleep=1) self.assertEqual(backend.origin, 'https://gitter.im/testapicomm/community') self.assertEqual(backend.tag, 'https://gitter.im/testapicomm/community') self.assertFalse(backend.ssl_verify) self.assertTrue(backend.sleep_for_rate) self.assertEqual(backend.max_items, 20) self.assertEqual(backend.min_rate_to_sleep, 1)
def test_fetch_empty(self): """Test whether an empty list is returned when there are no messages """ setup_http_server(no_message=True) backend = Gitter(group='testapicomm', room='community', api_token='aaa') messages = [m for m in backend.fetch()] self.assertListEqual(messages, [])
def setUp(self): super().setUp() self.backend_write_archive = Gitter(group='testapicomm', room='community', api_token='aaa', max_items=5, archive=self.archive) self.backend_read_archive = Gitter(group='testapicomm', room='community', api_token='aaa', max_items=5, archive=self.archive)
def test_room_id_not_found(self): """Test whether an error is raised if room id is not found""" setup_http_server() backend = Gitter(group='testapicomm', room='community_not', api_token='aaa') with self.assertLogs(logger, level='ERROR') as cm: with self.assertRaises(BackendError): _ = [m for m in backend.fetch()] self.assertEqual( cm.output[0], 'ERROR:perceval.backends.core.gitter:' 'Room id not found for room testapicomm/community_not')
def test_search_fields_messages(self): """Test whether the search_fields is properly set""" setup_http_server() backend = Gitter(group='testapicomm', room='community', api_token='aaa') messages = [m for m in backend.fetch()] message = messages[0] self.assertEqual(message['search_fields']['item_id'], backend.metadata_id(message['data'])) self.assertEqual(message['search_fields']['group'], 'testapicomm') self.assertEqual(message['search_fields']['room'], 'community') self.assertEqual(message['search_fields']['room_id'], GITTER_ROOM_ID)
def test_fetch_messages(self): """Test whether a list of messages is returned""" setup_http_server() backend = Gitter(group='testapicomm', room='community', api_token='aaa', max_items=1) messages = [m for m in backend.fetch()] self.assertEqual(len(messages), 2) message = messages[0] self.assertEqual(message['data']['id'], '5e7990ecada8262f814a8056') self.assertEqual(message['origin'], 'https://gitter.im/testapicomm/community') self.assertEqual(message['uuid'], '8053a8916819636b24e795c594cd24310835477f') self.assertEqual(message['updated_on'], 1585025260.777) self.assertEqual(message['category'], 'message') self.assertEqual(message['tag'], 'https://gitter.im/testapicomm/community') self.assertEqual(message['data']['text'], 'This is test message 2') self.assertEqual(message['data']['fromUser']['id'], '5e2f7e72d73408ce4fd7f187') self.assertEqual(message['data']['fromUser']['displayName'], 'Animesh Kumar') message = messages[1] self.assertEqual(message['data']['id'], '5e78b710c2676245a82ab85a') self.assertEqual(message['origin'], 'https://gitter.im/testapicomm/community') self.assertEqual(message['uuid'], 'f44b4cdbec932bb1dded7749941b3973853a6bae') self.assertEqual(message['updated_on'], 1584969488.8) self.assertEqual(message['category'], 'message') self.assertEqual(message['tag'], 'https://gitter.im/testapicomm/community') self.assertEqual(message['data']['text'], 'This is test message 1') self.assertEqual(message['data']['fromUser']['id'], '5e2f7e72d73408ce4fd7f187') self.assertEqual(message['data']['fromUser']['displayName'], 'Animesh Kumar')
def test_has_resuming(self): """Test if it returns True when has_resuming is called""" self.assertEqual(Gitter.has_resuming(), False)
def test_has_archiving(self): """Test if it returns True when has_archiving is called""" self.assertEqual(Gitter.has_archiving(), True)