Beispiel #1
0
 def test_has_next_tweets_exceeded_cutoff(self):
     old_status = generate_mock_status(1)
     old_status.created_at = old_status.created_at + timedelta(hours=-2)
     statuses = [old_status]
     api = MockAPI(statuses=statuses)
     timeline = classes.Timeline(api=api, username='******', timeframe=1)
     self.assertFalse(timeline._has_next_tweets())
Beispiel #2
0
 def test_load_status_with_reply_error(self):
     child_status = generate_mock_status(2)
     child_status.in_reply_to_status_id = 7
     api = MockAPI([child_status])
     out = StringIO()
     sys.stdout = out
     classes.Timeline(api=api, username='******')
     output = out.getvalue().strip()
     self.assertEqual(output, 'Error while fetching origin for tweet 2')
Beispiel #3
0
 def test_load_status_with_reply_id(self):
     original_user = generate_mock_user()
     original_user.id = 2
     original_user.screen_name = 'reply_user'
     original_status = generate_mock_status(1, user=original_user)
     child_status = generate_mock_status(2)
     child_status.in_reply_to_status_id = 1
     api = MockAPI([original_status, child_status])
     timeline = classes.Timeline(api=api, username='******')
     self.assertEqual(timeline.data['2'].origin.author_name, 'reply_user')
Beispiel #4
0
 def test_has_next_tweets_under_cutoff(self):
     status = generate_mock_status(1)
     status2 = generate_mock_status(2)
     statuses = [status, status2]
     api = MockAPI(statuses=statuses)
     timeline = classes.Timeline(api=api, username='******', timeframe=5)
     status3 = generate_mock_status(3)
     status3.created_at = status.created_at + timedelta(hours=-1)
     timeline.data[str(status3.id)] = status3
     self.assertTrue(timeline._has_next_tweets())
     self.assertEqual(timeline.earliest_status.id, 3)
Beispiel #5
0
 def test_load_statuses(self):
     status1 = generate_mock_status(1)
     original_created_at = status1.created_at
     bad_status1 = generate_mock_status(1)
     statuses = [status1, bad_status1]
     api = MockAPI(statuses=statuses)
     timeline = classes.Timeline(api=api, username='******')
     timeline.load(statuses)
     self.assertTrue(len(list(timeline.data.values())), 1)
     self.assertEqual(timeline.data['1'].created_at.microsecond,
                      original_created_at.microsecond)
Beispiel #6
0
 def test_get_earliest_status(self):
     timeline = classes.Timeline()
     timeline.data = self.mock_timeline_data
     earliest_status = timeline.get_earliest_status()
     self.assertTrue(earliest_status.created_at == self.earliest_datetime)
     self.assertTrue(
         earliest_status.created_at.day == self.earliest_datetime.day)
     self.assertTrue(
         earliest_status.created_at.hour == self.earliest_datetime.hour)
     self.assertTrue(
         earliest_status.created_at.minute == self.earliest_datetime.minute)
     self.assertTrue(
         earliest_status.created_at.second == self.earliest_datetime.second)
Beispiel #7
0
 def test_old_status_beyond_cutoff(self):
     current_datetime = datetime.now(tz=timezone.utc)
     fresh_status = generate_mock_status(2, created_at=current_datetime)
     expired_datetime = current_datetime + timedelta(hours=-10)
     old_status = generate_mock_status(1, created_at=expired_datetime)
     api = MockAPI(statuses=[fresh_status, old_status])
     timeline = classes.Timeline(username='******', timeframe=2)
     timeline.api = api
     timeline.earliest_id = 2
     timeline.earliest_status = fresh_status
     timeline.data['1'] = old_status
     timeline.data['2'] = fresh_status
     self.assertEqual(timeline.earliest_status.id, 2)
     self.assertFalse(timeline._has_next_tweets())
     self.assertEqual(timeline.earliest_status.id, 1)
Beispiel #8
0
 def setUp(self):
     dt1 = datetime(2001, 2, 3, 4, 5, 6, tzinfo=timezone.utc)
     dt2 = dt1 + timedelta(hours=-1)
     dt3 = dt1 + timedelta(hours=-3)
     dt4 = dt1 + timedelta(hours=-5)
     dt5 = dt1 + timedelta(hours=-6)
     dt6 = dt1 + timedelta(hours=-7)
     dt7 = dt1 + timedelta(hours=-10)
     self.earliest_datetime = dt7
     self.latest_datetime = dt1
     datetime_fixtures = [dt1, dt2, dt3, dt4, dt5, dt6, dt7]
     statuses = generate_mock_statuses(datetime_fixtures=datetime_fixtures)
     api = MockAPI(statuses=statuses)
     # we skip passing in the API so we can force start and cutoff properties
     timeline = classes.Timeline(username='******')
     timeline.start = datetime(2001, 2, 3, 5, 30, 0, tzinfo=timezone.utc)
     timeline.cutoff = timeline.start + timedelta(hours=-24)
     timeline.api = api
     timeline._generate_timeline()
     encoder = classes.TimelineEncoder()
     timeline_json = json.loads(encoder.encode(timeline))
     self.test_timeline = timeline_json
Beispiel #9
0
 def test_timeline_to_json(self):
     statuses = list()
     for index in range(1, 6):
         text = "The text for mock status {0}".format(index)
         created_at = datetime.now()
         status = generate_mock_status(index,
                                       text=text,
                                       created_at=created_at)
         statuses.append(status)
     api = MockAPI(statuses=statuses)
     timeline = classes.Timeline(api=api, username='******')
     test_output_file_path = os.path.join(self.test_file_directory,
                                          'test_timeline_to_json.json')
     try:
         timeline.to_json(test_output_file_path)
         with open(test_output_file_path) as data_file:
             data = json.load(data_file)
             self.assertTrue('start' in data)
             start_iso_date_string = data['start']
             start = parse(start_iso_date_string)
             self.assertTrue(isinstance(start, datetime))
             self.assertTrue(start.tzinfo)
             self.assertEqual(start.utcoffset(), timedelta(0))
             self.assertTrue('cutoff' in data)
             self.assertTrue('cutoff' in data)
             cutoff_iso_date_string = data['cutoff']
             cutoff = parse(cutoff_iso_date_string)
             self.assertTrue(isinstance(cutoff, datetime))
             self.assertTrue(cutoff.tzinfo)
             self.assertEqual(cutoff.utcoffset(), timedelta(0))
             self.assertTrue('total' in data)
             self.assertEqual(data['total'], 5)
             self.assertTrue('data' in data)
             self.assertTrue(isinstance(data['data'], dict))
             self.assertTrue('username' in data)
             self.assertEqual(data['username'], 'testuser')
     finally:
         if os.path.isfile(test_output_file_path):
             os.remove(test_output_file_path)
Beispiel #10
0
 def test_batch_with_maximum_id(self):
     timeline = classes.Timeline(username='******')
     api = MockAPI(multi_response=True)
     timeline.api = api
     statuses = timeline.get_timeline_batch(max_id=2)
     self.assertEqual(statuses[0].text, 'Content for tweet mock status 1')
Beispiel #11
0
 def test_generate_timeline_with_tight_utc_cutoff(self):
     api = MockAPI()
     timeline = classes.Timeline(api=api, username='******', timeframe=1)
     self.assertTrue(len(list(timeline.data.values())), 1)
Beispiel #12
0
 def test_generate_timeline_with_tight_central_cutoff(self):
     api = MockAPI()
     timeline = classes.Timeline(api=api,
                                 username='******',
                                 timeframe=-12)
     self.assertEqual(len(list(timeline.data.values())), 7)
Beispiel #13
0
 def test_generate_timeline_multiple_requests(self):
     api = MockAPI(multi_response=True)
     timeline = classes.Timeline(api=api, username='******')
     self.assertEqual(len(list(timeline.data.values())), 7)
Beispiel #14
0
 def test_generate_timeline(self):
     api = MockAPI()
     timeline = classes.Timeline(api=api, username='******')
     self.assertEqual(len(list(timeline.data.values())), 7)
Beispiel #15
0
 def test_has_next_tweets_exhausted(self):
     status = generate_mock_status(1)
     statuses = [status]
     api = MockAPI(statuses=statuses)
     timeline = classes.Timeline(api=api, username='******')
     self.assertFalse(timeline._has_next_tweets())
Beispiel #16
0
 def test_has_next_tweets_no_statuses(self):
     statuses = []
     api = MockAPI(statuses=statuses)
     timeline = classes.Timeline(api=api, username='******')
     self.assertFalse(timeline._has_next_tweets())