def setUp(self): self.user_1 = UserFactory(screen_name='terry') self.user_2 = UserFactory(screen_name='bob') self.user_3 = UserFactory(screen_name='thelma', is_private=True) self.account_1 = AccountFactory(user=self.user_1) account_2 = AccountFactory(user=self.user_2) account_3 = AccountFactory(user=self.user_3) # private # Public tweets in 2015 and 2016: tweets2015 = TweetFactory.create_batch( 3, post_time=datetime_from_str('2015-01-01 12:00:00')) tweets2016 = TweetFactory.create_batch( 2, post_time=datetime_from_str('2016-01-01 12:00:00')) # One private tweet in 2015: private_tweet = TweetFactory( post_time=datetime_from_str('2015-01-01 12:00:00')) private_tweet.user.is_private = True private_tweet.user.save() self.account_1.user.favorites.add(private_tweet) self.account_1.user.favorites.add(tweets2015[0]) self.account_1.user.favorites.add(tweets2015[1]) self.account_1.user.favorites.add(tweets2015[2]) self.account_1.user.favorites.add(tweets2016[0]) self.account_1.user.favorites.add(tweets2016[1]) account_2.user.favorites.add(tweets2015[1]) account_2.user.favorites.add(tweets2016[1]) account_3.user.favorites.add(tweets2015[1]) # private user favoriting
def setUp(self): user_1 = UserFactory(screen_name='terry') user_2 = UserFactory(screen_name='bob') user_3 = UserFactory(screen_name='thelma', is_private=True) account_1 = AccountFactory(user=user_1) account_2 = AccountFactory(user=user_2) account_3 = AccountFactory(user=user_3) self.tweets = TweetFactory.create_batch(6) # One of the tweets is private: self.tweets[0].user.is_private = True self.tweets[0].user.save() post_time = datetime.datetime(2015, 3, 18, 12, 0, 0).replace(tzinfo=pytz.utc) self.tweets[0].post_time = post_time self.tweets[0].save() self.tweets[1].post_time = post_time + datetime.timedelta(hours=1) self.tweets[1].save() self.tweets[5].post_time = post_time + datetime.timedelta(hours=2) self.tweets[5].save() account_1.user.favorites.add(self.tweets[0]) # private tweet account_1.user.favorites.add(self.tweets[1]) account_1.user.favorites.add(self.tweets[2]) account_1.user.favorites.add(self.tweets[3]) account_2.user.favorites.add(self.tweets[4]) account_3.user.favorites.add(self.tweets[5]) # private user favoriting
def test_ordering(self): """Multiple accounts are by user.screen_name time ascending""" user_1 = UserFactory(screen_name='terry') user_2 = UserFactory(screen_name='june') account_1 = AccountFactory(user=user_1) account_2 = AccountFactory(user=user_2) accounts = Account.objects.all() self.assertEqual(accounts[0].pk, account_2.pk)
def test_with_accounts_manager(self): "Only returns Users with Accounts" users = UserFactory.create_batch(4) account_1 = AccountFactory(user=users[0]) account_2 = AccountFactory(user=users[2]) users_with_accounts = User.objects_with_accounts.all() self.assertEqual(2, len(users_with_accounts)) self.assertEqual(users_with_accounts[0].pk, users[0].pk) self.assertEqual(users_with_accounts[1].pk, users[2].pk)
def setUp(self): user_1 = UserFactory(screen_name='terry') user_2 = UserFactory(screen_name='bob', is_private=True) user_3 = UserFactory(screen_name='thelma') account_1 = AccountFactory(user=user_1) account_2 = AccountFactory(user=user_2) account_3 = AccountFactory(user=user_3) self.tweets_1 = TweetFactory.create_batch(2, user=user_1) self.tweets_2 = TweetFactory.create_batch(3, user=user_2) self.tweets_3 = TweetFactory.create_batch(4, user=user_3)
def test_public_favorites_accounts_manager(self): "Should only show tweets favorited by public Accounts" public_user = UserFactory(is_private=False) private_user = UserFactory(is_private=True) account_1 = AccountFactory(user=public_user) account_2 = AccountFactory(user=private_user) tweets = TweetFactory.create_batch(5) account_1.user.favorites.add(tweets[0]) account_1.user.favorites.add(tweets[3]) account_2.user.favorites.add(tweets[1]) account_2.user.favorites.add(tweets[3]) favorites = Tweet.public_favorite_objects.all() self.assertEqual(len(favorites), 2) self.assertEqual(favorites[0].pk, tweets[3].pk) self.assertEqual(favorites[1].pk, tweets[0].pk)
def test_ignores_account_with_no_creds(self, download): # Quietly prevents avatar files being fetched: download.side_effect = DownloadException('Oops') user_3 = UserFactory() account_3 = AccountFactory(user=user_3) self.add_response(body=self.make_response_body()) result = RecentTweetsFetcher().fetch() self.assertEqual(2, len(responses.calls))
def test_returns_error_if_no_creds(self): "If an account has no API credentials, the result is correct" user = UserFactory(screen_name='bobby') account = AccountFactory(user=user) self.add_response(body=self.make_response_body()) result = VerifyFetcher(screen_name='bobby').fetch() self.assertFalse(result[0]['success']) self.assertIn('Account has no API credentials', result[0]['messages'][0])
def test_ignores_account_with_no_creds(self, download): # This will just stop us requesting avatars from Twitter: download.side_effect = DownloadException('Ooops') # Add a third Account that has no API credentials: account_3 = AccountFactory(user=UserFactory()) self.add_response(body=self.make_response_body()) result = FavoriteTweetsFetcher().fetch() # Should only have fetched faves for the two accounts with API creds: self.assertEqual(2, len(responses.calls))
def setUp(self): user_1 = UserFactory(screen_name='terry') user_2 = UserFactory(screen_name='bob') user_3 = UserFactory(screen_name='thelma', is_private=True) account_1 = AccountFactory(user=user_1) account_2 = AccountFactory(user=user_2) account_3 = AccountFactory(user=user_3) tweets = TweetFactory.create_batch(6) # One of the tweets is private: tweets[0].user.is_private = True tweets[0].user.save() account_1.user.favorites.add(tweets[0]) account_1.user.favorites.add(tweets[1]) account_1.user.favorites.add(tweets[3]) account_2.user.favorites.add(tweets[5]) # Private user favoriting public tweets: account_3.user.favorites.add(tweets[5])
def setUp(self): user_1 = UserFactory(screen_name='terry') user_2 = UserFactory(screen_name='bob') user_3 = UserFactory(screen_name='thelma', is_private=True) account_1 = AccountFactory(user=user_1) account_2 = AccountFactory(user=user_2) account_3 = AccountFactory(user=user_3) self.tweets_1 = TweetFactory.create_batch(2, user=user_1) self.tweets_2 = TweetFactory.create_batch(2, user=user_2) self.tweets_3 = TweetFactory.create_batch(2, user=user_3) post_time = datetime.datetime(2015, 3, 18, 12, 0, 0).replace(tzinfo=pytz.utc) self.tweets_1[0].post_time = post_time self.tweets_1[0].save() self.tweets_2[1].post_time = post_time + datetime.timedelta(hours=1) self.tweets_2[1].save() self.tweets_3[0].post_time = post_time + datetime.timedelta(hours=2) self.tweets_3[0].save()
def setUp(self): dt = datetime.datetime.strptime( '2016-04-08 12:00:00', '%Y-%m-%d %H:%M:%S' ).replace(tzinfo=pytz.utc) user = UserFactory() account = AccountFactory(user=user) self.tweet_1 = TweetFactory(user=user, post_time=dt) private_user = UserFactory(is_private=True) private_account = AccountFactory(user=private_user) self.private_tweeet = TweetFactory(user=private_user, post_time=dt + datetime.timedelta(days=1)) # Tweet by a different user: user_2 = UserFactory() account_2 = AccountFactory(user=user_2) self.other_tweet = TweetFactory(user=user_2, post_time=dt + datetime.timedelta(days=2)) self.tweet_2 = TweetFactory(user=user, post_time=dt + datetime.timedelta(days=3))
def setUp(self): self.user_1 = UserFactory(screen_name='terry') self.user_2 = UserFactory(screen_name='bob') self.user_3 = UserFactory(screen_name='thelma', is_private=True) account_1 = AccountFactory(user=self.user_1) account_2 = AccountFactory(user=self.user_2) account_3 = AccountFactory(user=self.user_3) # Tweets in 2015 and 2016 for user 1: TweetFactory.create_batch( 3, post_time=datetime_from_str('2015-01-01 12:00:00'), user=self.user_1) TweetFactory.create_batch( 2, post_time=datetime_from_str('2016-01-01 12:00:00'), user=self.user_1) # And one for self.user_2 in 2015: TweetFactory(user=self.user_2, post_time=datetime_from_str('2015-01-01 12:00:00')) # And one tweet in 2015 for the private user 3. tw = TweetFactory(user=self.user_3, is_private=True, post_time=datetime_from_str('2015-01-01 12:00:00'))
def test_ignores_account_with_no_creds(self, fetch_avatar): user_3 = UserFactory() account_3 = AccountFactory(user=user_3) self.add_response(body=self.make_response_body()) result = VerifyFetcher().fetch() self.assertEqual(2, len(responses.calls))
def test_str_2(self): "Has the correct string represntation when it has a user" user = UserFactory() account = AccountFactory(user=user) self.assertEqual(account.__str__(), '@%s' % user.screen_name)
def test_str_1(self): "Has the correct string represntation when it has no user" account = AccountFactory(user=None) self.assertEqual(account.__str__(), '%d' % account.pk)
def test_account_exists(self): "If the tweet is from an Account the account property should return it." user = UserFactory() account = AccountFactory(user=user) tweet = TweetFactory(text='This is my tweet text', user=user) self.assertEqual(tweet.account, account)
def test_get_absolute_url(self): user = UserFactory(screen_name='bill') account = AccountFactory(user=user) self.assertEqual(account.get_absolute_url(), reverse('twitter:user_detail', kwargs={'screen_name': 'bill'}))
def test_raises_error_with_invalid_screen_name(self): user = UserFactory(screen_name='goodname') account = AccountFactory(user=user) with self.assertRaises(FetchError): result = RecentTweetsFetcher(screen_name='badname')
def setUp(self): user_1 = UserFactory(screen_name='jill') user_2 = UserFactory(screen_name='debs') account_1 = AccountFactory(user=user_1, is_active=False) account_2 = AccountFactory(user=user_2, is_active=False)
def setUp(self): user_1 = UserFactory(screen_name='jill') user_2 = UserFactory(screen_name='debs') account_1 = AccountFactory(user=user_1) account_2 = AccountFactory(user=user_2) self.fetcher = TwitterFetcher()