def test_exceptions(self): # Should only accept strings with self.assertRaises(TypeError): TimeZoneConverter(1) # Should only accepts strings with format -HH:MM or +HH:MM with self.assertRaises(ValueError): TimeZoneConverter("02:30")
def test_offset_generation(self): # Should correctly generate the offsets t = TimeZoneConverter("+02:30") expected = timedelta(hours=2, minutes=30) self.assertEqual(t.offset, expected) # Since we have to subtract 2:30, we have to split it up so each component is subtracted as well # Thus we expect hours = -2 and minutes = -30 instead of minutes = 30 (which would add to the time delta) t1 = TimeZoneConverter("-02:30") expected1 = timedelta(hours=-2, minutes=-30) self.assertEqual(t1.offset, expected1)
def main(argv): try: args = ArgParser(argv) # authenticate auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) print("Twitter ID:", args.twitter_id, "From:", args.start_date, "To:", args.end_date) converter = TimeZoneConverter(args.timezone) print("Fetching...") fetcher = TweetFetcher(api, args.twitter_id, args.start_date, args.end_date, converter) tweets = fetcher.fetch() print("Analysing and Building Graph...") frequencies = TweetAnalyser(tweets, converter).get_frequencies() submitter = TweetSubmitter(api, args.twitter_id, args.start_date, args.end_date, frequencies) print("Tweeting...") submitter.submit() print("Finished!!") except Exception as e: print("An error occurred: " + str(e))
def test_frequencies_should_be_set_to_zero(self): tz = TimeZoneConverter("+00:00") t = TweetAnalyser([], tz) frequencies = t.get_frequencies() # Must be 24 values, as there are 24 hours in a day self.assertEqual(len(frequencies), 24) # Since no tweets were parsed then the frequencies should be 0 for key, value in frequencies.items(): self.assertEqual(value, 0)
def test_add_tweet_on_boundary_values(self): tz = TimeZoneConverter("+00:00") start = datetime(2017, 1, 2) end = datetime(2017, 1, 3) t = TweetFetcher(None, "", start, end, tz) upper = MockTweet(0, start) lower = MockTweet(1, end) self.assertEqual(t.add_tweet(upper), 0) self.assertEqual(t.add_tweet(lower), 0) self.assertEqual(len(t.tweets), 2) self.assertTrue(upper in t.tweets) self.assertTrue(lower in t.tweets)
def test_correctly_changes_hours_and_minutes(self): # Test to see if hours and minutes correctly subtract initial = datetime(2017, 1, 1, 1, 59) t = TimeZoneConverter("-01:59") expected = datetime(2017, 1, 1) self.assertEqual(t.convert(initial), expected) # Test to see if they correctly add t = TimeZoneConverter("+01:02") expected = datetime(2017, 1, 1, 3, 1) self.assertEqual(t.convert(initial), expected)
def test_frequency_with_other_timezone(self): t1 = MockTweet(0, datetime(2017, 1, 1, 0)) t2 = MockTweet(0, datetime(2017, 1, 1, 0)) t3 = MockTweet(0, datetime(2017, 1, 1, 1)) tz = TimeZoneConverter("-01:00") t = TweetAnalyser([t1, t2, t3], tz) frequencies = t.get_frequencies() # There should be 2 tweets at 23 and 1 tweet at 0 (-1 to utc) for key, value in frequencies.items(): if key == 23: self.assertEqual(value, 2) elif key == 0: self.assertEqual(value, 1) else: self.assertEqual(value, 0)
def test_frequency_with_UTC_timezone(self): t1 = MockTweet(0, datetime(2017, 1, 1, 1)) t2 = MockTweet(0, datetime(2017, 1, 1, 1)) t3 = MockTweet(0, datetime(2017, 1, 1, 2)) tz = TimeZoneConverter("+00:00") t = TweetAnalyser([t1, t2, t3], tz) frequencies = t.get_frequencies() # There should be 2 tweets at 1 and 1 tweet at 2 for key, value in frequencies.items(): if key == 1: self.assertEqual(value, 2) elif key == 2: self.assertEqual(value, 1) else: self.assertEqual(value, 0)
def test_correctly_changes_day(self): # See if time rolls back initial = datetime(2017, 1, 1) t = TimeZoneConverter("-01:00") expected = datetime(2016, 12, 31, 23, 0) self.assertEqual(t.convert(initial), expected) # See if time rolls forward t = TimeZoneConverter("+24:30") expected = datetime(2017, 1, 2, 0, 30) self.assertEqual(t.convert(initial), expected)
def test_add_tweet_with_UTC_timezone(self): tz = TimeZoneConverter("+00:00") start = datetime(2017, 1, 2) end = datetime(2017, 1, 3) t = TweetFetcher(None, "", start, end, tz) # The tweets before = MockTweet(0, datetime(2017, 1, 1)) # Should return -1 between = MockTweet(1, datetime(2017, 1, 2, 12)) # Should return 0 after = MockTweet(2, datetime(2017, 1, 4)) # Should return 1 self.assertEqual(t.add_tweet(before), -1) self.assertEqual(t.add_tweet(between), 0) self.assertEqual(t.add_tweet(after), 1) # It should have added the between tweet self.assertEqual(len(t.tweets), 1) self.assertTrue(between in t.tweets)
def test_fetch_api_should_terminate_if_tweet_is_before_start_date( self, mock_api): before = MockTweet(0, datetime(2017, 1, 1)) between = MockTweet(1, datetime(2017, 1, 2, 12)) after = MockTweet(2, datetime(2017, 1, 4)) api = mock_api() api.user_timeline.return_value = [before, between, after] tz = TimeZoneConverter("+00:00") start = datetime(2017, 1, 2) end = datetime(2017, 1, 3) t = TweetFetcher(api, "id", start, end, tz) # No tweets should be returned results = t.fetch() self.assertEqual(len(results), 0)
def test_fetch_api_returns_tweets(self, mock_api): before = MockTweet(0, datetime(2017, 1, 1)) between = MockTweet(1, datetime(2017, 1, 2, 12)) after = MockTweet(2, datetime(2017, 1, 4)) api = mock_api() api.user_timeline.return_value = [after, between, before] tz = TimeZoneConverter("+00:00") start = datetime(2017, 1, 2) end = datetime(2017, 1, 3) t = TweetFetcher(api, "id", start, end, tz) # Only the between tweet should be returned results = t.fetch() self.assertEqual(len(results), 1) self.assertTrue(between in results)
def test_add_tweet_with_positive_timezone(self): tz = TimeZoneConverter("+01:30") start = datetime(2017, 1, 2) # Local to the timezone end = datetime(2017, 1, 3) # Local to the timezone t = TweetFetcher(None, "", start, end, tz) # The tweets before = MockTweet(0, datetime(2017, 1, 1)) # Should return -1 between = MockTweet( 1, datetime(2017, 1, 1, 22, 30)) # Should return 0 as it adds to 2017, 1, 2 Local after = MockTweet(2, datetime( 2017, 1, 3)) # Should return 1 as it goes past 2017, 1, 3 Local self.assertEqual(t.add_tweet(before), -1) self.assertEqual(t.add_tweet(between), 0) self.assertEqual(t.add_tweet(after), 1) # It should have added the between tweet self.assertEqual(len(t.tweets), 1) self.assertTrue(between in t.tweets)
def test_fetch_api_recursively_fetches_tweets(self, mock_api): before = MockTweet(0, datetime(2017, 1, 1)) between = MockTweet(1, datetime(2017, 1, 2, 12)) after = MockTweet(2, datetime(2017, 1, 4)) api = mock_api() api.user_timeline.side_effect = [[after], [between], [before]] tz = TimeZoneConverter("+00:00") start = datetime(2017, 1, 2) end = datetime(2017, 1, 3) t = TweetFetcher(api, "id", start, end, tz) # Only the between tweet should be returned results = t.fetch() self.assertEqual(len(results), 1) self.assertTrue(between in results) # Check that local variables have been updated accordingly and we called the appropriate amount of times self.assertEqual(t.current_page, 2) self.assertEqual(api.user_timeline.call_count, 3)