def test_non_time_based_unprocessed(self): tweet = Tweet(json.loads(tweet_samples.standard_ascii_tweet)) tweet.is_time_based = MagicMock(return_value=False) self.assertTrue(self.tci.current_chunk_container is not None) self.tci.current_chunk_container.tweet_fits = MagicMock() self.tci.to_db(tweet) # nothing called self.tci.current_chunk_container.tweet_fits.assert_has_calls([])
def test_pick_container_from_msg_date(self): # newly created container tweet = Tweet(json.loads(tweet_samples.standard_ascii_tweet)) c = self.tci.pick_container_from_msg_date(tweet) self.assertEquals(datetime(2013, 8, 10, 18, 44), c.start_date) # container from db, there is a container in the db with date 9:48 tweet.message['created_at'] = "Fri Aug 16 09:48:55 +0000 2013" c = self.tci.pick_container_from_msg_date(tweet) self.assertEquals(datetime(2013, 8, 16, 9, 48), c.start_date)
def test_get_creation_time(self): tweet = Tweet(json.loads(tweet_samples.hashtags_tweet)) d = tweet.get_creation_time() self.assertEquals(d.year, 2013) self.assertEquals(d.month, 8) self.assertEquals(d.day, 7) self.assertEquals(d.hour, 8) self.assertEquals(d.minute, 30) self.assertEquals(d.second, 39) d = tweet.get_creation_time(process=False) self.assertEquals(d, u'Wed Aug 07 08:30:39 +0000 2013')
def test_get_user_mentions(self): tweet = Tweet(json.loads(tweet_samples.user_mentions_tweet)) self.assertEquals(tweet.get_user_mentions(), [u'CedricDickies', u'AumS_TTD']) self.assertEquals(tweet.get_user_mentions(prop='indices'), [[3, 17], [19, 28]]) self.assertEquals(tweet.get_user_mentions(prop=''), [{u'indices': [3, 17], u'screen_name': u'CedricDickies', u'id': 488837080, u'name': u'Cedric Dickies', u'id_str': u'488837080'}, {u'indices': [19, 28], u'screen_name': u'AumS_TTD', u'id': 533771089, u'name': u'Purple Drank \u274c\u274c\u2757', u'id_str': u'533771089'}])
def test_get_associated_container_key(self): tweet = Tweet(json.loads(tweet_samples.hashtags_tweet)) self.tci.container_mgr = MagicMock() self.tci.container_mgr.size = 10 self.assertEquals(self.tci._get_associated_container_key(tweet).time(), time(8, 30)) tweet.message['created_at'] = u'Wed Aug 07 08:44:39 +0000 2013' self.assertEquals(self.tci._get_associated_container_key(tweet).time(), time(8, 40)) self.tci.container_mgr.size = 50 self.assertRaises(Exception, self.tci._get_associated_container_key, tweet) self.tci.container_mgr.size = 120 self.assertRaises(Exception, self.tci._get_associated_container_key, tweet) tweet.message['created_at'] = u'Wed Aug 07 00:59:39 +0000 2013' self.tci.container_mgr.size = 30 self.assertEquals(self.tci._get_associated_container_key(tweet).time(), time(0, 30))
def test_to_db_generic(self): # 3 containers: 9:48, 9:49 and 9:50 # created at 18:44, current chunk pointing at 9:48 tweet = Tweet(json.loads(tweet_samples.standard_ascii_tweet)) self.assertEquals( self.conn['stats']['chunk_containers'].find().count(), 3) self.tci.to_db(tweet) # no db update, current_chunk points now at 18:44 and has changed_since_retrieval self.assertEquals( self.conn['stats']['chunk_containers'].find().count(), 3) # created at 18:40 tweet = Tweet(json.loads(tweet_samples.non_ascii_tweet)) self.tci.to_db(tweet) self.assertEquals( self.conn['stats']['chunk_containers'].find().count(), 4)
def test_non_current_chunk(self): tweet = Tweet(json.loads(tweet_samples.standard_ascii_tweet)) self.tci.current_chunk_container = None self.tci.to_db(tweet) self.assertEquals(datetime(2013, 8, 10, 18, 44), self.tci.current_chunk_container.start_date) chunk = self.tci.current_chunk_container.current_chunk self.assertTrue(chunk.changed_since_retrieval) self.assertTrue([] != chunk.tweet_ids)
def test_get_associated_container_key(self): tweet = Tweet(json.loads(tweet_samples.hashtags_tweet)) self.tci.container_mgr = MagicMock() self.tci.container_mgr.size = 10 self.assertEquals( self.tci._get_associated_container_key(tweet).time(), time(8, 30)) tweet.message['created_at'] = u'Wed Aug 07 08:44:39 +0000 2013' self.assertEquals( self.tci._get_associated_container_key(tweet).time(), time(8, 40)) self.tci.container_mgr.size = 50 self.assertRaises(Exception, self.tci._get_associated_container_key, tweet) self.tci.container_mgr.size = 120 self.assertRaises(Exception, self.tci._get_associated_container_key, tweet) tweet.message['created_at'] = u'Wed Aug 07 00:59:39 +0000 2013' self.tci.container_mgr.size = 30 self.assertEquals( self.tci._get_associated_container_key(tweet).time(), time(0, 30))
def test_refresh_current_container(self): self.assertTrue( self.tci.current_chunk_container.changed_since_retrieval) tweet = Tweet(json.loads(tweet_samples.standard_ascii_tweet)) previous_container = self.tci.current_chunk_container self.tci.container_mgr.save_in_db = MagicMock() self.tci._refresh_current_container(tweet) self.assertNotEquals(self.tci.current_chunk_container, previous_container) # the previous container was stored in the db self.tci.container_mgr.save_in_db.assert_called_once_with( previous_container)
def test_get_text_nonascii(self): tweet = Tweet(json.loads(tweet_samples.non_ascii_tweet)) self.assertEquals(tweet.get_text(), tweet_samples.non_ascii_text)
def test_get_user_mentions_empty(self): tweet = Tweet(json.loads(tweet_samples.no_mentions_tweet)) self.assertEquals(tweet.get_user_mentions(), [])
def test_get_hashtags(self): tweet = Tweet(json.loads(tweet_samples.hashtags_tweet)) self.assertEquals(tweet.get_hashtags(), [u'SougoFollow', u'THF', u'TeamFollowBack', u'HITFOLLOWSTEAM', u'TFBJP', u'OpenFollow', u'MustFollow'])
def test_to_db_full_chunk(self): tweet = Tweet(json.loads(tweet_samples.standard_ascii_tweet)) self.tci.current_chunk_container.current_chunk_isfull = MagicMock( return_value=True) self.tci.to_db(tweet) self.assertEquals([], self.tci.current_chunk_container.current_chunk)
def test_get_creation_time_empty(self): tweet = Tweet(json.loads(tweet_samples.hashtags_tweet)) tweet.message['created_at'] = '' self.assertEquals(tweet.get_creation_time(), None)
def test_get_hashtags_empty(self): tweet = Tweet(json.loads(tweet_samples.no_hashtags_tweet)) self.assertEquals(tweet.get_hashtags(), [])
def test_get_location(self): tweet = Tweet(json.loads(tweet_samples.standard_ascii_tweet)) self.assertEquals({u'type': u'Point', u'coordinates': [-6.827979, 37.132955000000003]}, tweet.get_location())
def test_get_lang(self): tweet = Tweet(json.loads(tweet_samples.standard_ascii_tweet)) self.assertEquals('es', tweet.get_lang())
def test_get_terms(self): tweet = Tweet(json.loads(tweet_samples.hashtags_tweet)) self.assertEquals(tweet.get_terms(), {u'RT': 1, u'HITFOLLOWSTEAM': 1, u'HitFollowsJp': 1, u'TeamFollowBack': 1, u'OpenFollow': 1, u'SougoFollow': 1, u'ONLY': 1, u'FOLLOWERS': 1, u'WANT': 1, u'THF': 1, u'NEW': 1, u'YOU': 1, u'TFBJP': 1, u'RETWEET': 1, u'MustFollow': 1, u'IF': 1}) tweet = Tweet(json.loads(tweet_samples.extended_ascii_tweet)) self.assertEquals(tweet.get_terms(), {u'el': 2, u'gracias': 1, u'la': 1, u'tienen': 1, u'pero': 1, u'al': 1, u'da': 1, u'los': 1, u'miedo': 2, u'que': 1, u'ra': 1, u'misma': 1, u'problemas': 1, u'amor': 2, u'z': 1, u'desaparece': 1, u'Todos': 1, u'nos': 1})
def test_get_user_wrong_field(self): tweet = Tweet(json.loads(tweet_samples.user_mentions_tweet)) self.assertEquals(tweet.get_user(field='i dont exist'), '')
def test_get_id(self): tweet = Tweet(json.loads(tweet_samples.hashtags_tweet)) self.assertEquals(tweet.get_id(), u'365027194331865088')
def test_get_user(self): tweet = Tweet(json.loads(tweet_samples.user_mentions_tweet)) self.assertEquals(tweet.get_user(), 'AumS_TTD') self.assertEquals(tweet.get_user(field='id'), 533771089)
def test_get_favorite_count(self): tweet = Tweet(json.loads(tweet_samples.hashtags_tweet)) self.assertEquals(tweet.get_favorite_count(), 6)