def test_send_unsent_tweets(self, mocked_twython): event = Event.objects.get(title='Test event') assert event in Event.objects.approved() now = timezone.now() past = now - datetime.timedelta(hours=1) event_tweet = EventTweet.objects.create( event=event, text=u'\xa310,000 for a cup of tea? #testing', send_date=past, ) assert not event_tweet.sent_date assert not event_tweet.tweet_id sends = [] def mocked_update_status(status): sends.append(status) return {'id': '0000000001'} mocker = mock.MagicMock() mocker.update_status.side_effect = mocked_update_status mocked_twython.return_value = mocker # change so that it needs an approval group = Group.objects.create(name='testapprover') approval = Approval.objects.create( event=event, group=group, ) assert event not in Event.objects.approved() event_tweet.send_date = past event_tweet.save() send_unsent_tweets() ok_(not sends) # but if it gets approved, it gets sent approval.approved = True approval.save() send_unsent_tweets() eq_(len(sends), 1)
def test_send_unsent_tweets_with_error(self, mocked_twython): event = Event.objects.get(title='Test event') event_tweet = EventTweet.objects.create( event=event, text=u'\xa310,000 for a cup of tea? #testing', ) assert not event_tweet.sent_date assert not event_tweet.tweet_id sends = [] def mocked_update_status(status): sends.append(status) if len(sends) < settings.MAX_TWEET_ATTEMPTS: raise Exception("Some Error") return {'id': '0000000001'} mocker = mock.MagicMock() mocker.update_status.side_effect = mocked_update_status mocked_twython.return_value = mocker send_unsent_tweets() event_tweet = EventTweet.objects.get(pk=event_tweet.pk) eq_(len(sends), 1) ok_(event_tweet.error) ok_(event_tweet.sent_date) ok_(not event_tweet.tweet_id) # try again send_unsent_tweets() event_tweet = EventTweet.objects.get(pk=event_tweet.pk) eq_(len(sends), 2) ok_(event_tweet.error) ok_(event_tweet.sent_date) ok_(not event_tweet.tweet_id) # third times the charm send_unsent_tweets() event_tweet = EventTweet.objects.get(pk=event_tweet.pk) eq_(len(sends), 3) ok_(not event_tweet.error) ok_(event_tweet.sent_date) ok_(event_tweet.tweet_id) # a fourth time and it won't even be attempted send_unsent_tweets() eq_(len(sends), 3)
def test_send_unsent_tweets_by_send_date(self, mocked_twython): event = Event.objects.get(title='Test event') now = timezone.now() future = now + datetime.timedelta(hours=1) past = now - datetime.timedelta(hours=1) event_tweet = EventTweet.objects.create( event=event, text=u'\xa310,000 for a cup of tea? #testing', send_date=future, ) sends = [] def mocked_update_status(status): sends.append(status) return {'id': '0000000001'} mocker = mock.MagicMock() mocker.update_status.side_effect = mocked_update_status mocked_twython.return_value = mocker send_unsent_tweets() ok_(not sends) event_tweet.send_date = past event_tweet.save() send_unsent_tweets() eq_(len(sends), 1) # all it again send_unsent_tweets() eq_(len(sends), 1)
def test_send_unsent_tweets_no_approval_needed(self, mocked_twython): event = Event.objects.get(title='Test event') now = timezone.now() past = now - datetime.timedelta(hours=1) EventTweet.objects.create( event=event, text=u'\xa310,000 for a cup of tea? #testing', send_date=past, ) sends = [] def mocked_updateStatus(status): sends.append(status) return {'id': '0000000001'} mocker = mock.MagicMock() mocker.updateStatus.side_effect = mocked_updateStatus mocked_twython.return_value = mocker send_unsent_tweets() eq_(len(sends), 1)