def test_default_manager_recent(self): "The default manager includes tweets from public AND private users" public_user = UserFactory(is_private=False) private_user = UserFactory(is_private=True) public_tweets = TweetFactory.create_batch(2, user=public_user) private_tweet = TweetFactory(user=private_user) self.assertEqual(len(Tweet.objects.all()), 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) 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_makes_description_html(self, htmlify_method): "When save() is called description_html should be created from raw JSON" htmlify_method.return_value = 'my test description' user = UserFactory() user.raw = '{"description":"my test description"}' user.save() htmlify_method.assert_called_once_with( {'description': 'my test description'}) self.assertEqual(user.description_html, 'my test description')
def test_default_manager(self): "Default Manager shows both private and public photos" public_user = UserFactory(is_private=False) private_user = UserFactory(is_private=True) public_tweet = TweetFactory(user=public_user) private_tweet = TweetFactory(user=private_user) public_photos = PhotoFactory.create_batch(2, tweets=(public_tweet,)) private_photos = PhotoFactory.create_batch(1, tweets=(private_tweet,)) self.assertEqual(len(Media.objects.all()), 3)
def test_makes_description_html(self, htmlify_method): "When save() is called description_html should be created from raw JSON" htmlify_method.return_value = 'my test description' user = UserFactory() user.raw = '{"description":"my test description"}' user.save() htmlify_method.assert_called_once_with( {'description': 'my test description'}) self.assertEqual(user.description_html, 'my test description')
def test_public_manager(self): "The public manager ONLY includes tweets from public users" public_user = UserFactory(is_private=False) private_user = UserFactory(is_private=True) public_tweets = TweetFactory.create_batch(2, user=public_user) private_tweet = TweetFactory(user=private_user) tweets = Tweet.public_objects.all() self.assertEqual(len(tweets), 2) self.assertEqual(tweets[0].pk, public_tweets[1].pk) self.assertEqual(tweets[1].pk, public_tweets[0].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_going_public(self): "Makes the user's tweets public if they go public" # Start off private: user = UserFactory(is_private=True) tweet = TweetFactory(user=user) self.assertTrue(tweet.is_private) # Now change to public: user.is_private = False # Should change the user's tweets to public: user.save() tweet.refresh_from_db() self.assertFalse(tweet.is_private)
def setUp(self): """We add the last_recent_id and last_favorite_id to prevent the fetcher fetching multiple pages of tweets. Keeps things simpler. """ user_1 = UserFactory(screen_name='jill', twitter_id=1) user_2 = UserFactory(screen_name='debs', twitter_id=2) self.account_1 = AccountWithCredentialsFactory(user=user_1, last_recent_id=100, last_favorite_id=100) self.account_2 = AccountWithCredentialsFactory(user=user_2, last_recent_id=100, last_favorite_id=100)
def test_going_public(self): "Makes the user's tweets public if they go public" # Start off private: user = UserFactory(is_private=True) tweet = TweetFactory(user=user) self.assertTrue(tweet.is_private) # Now change to public: user.is_private = False # Should change the user's tweets to public: user.save() tweet.refresh_from_db() self.assertFalse(tweet.is_private)
def test_public_favorites_tweets_manager(self): "Should contain recent PUBLIC tweets favorited the Accounts" accounts = AccountFactory.create_batch(2) public_user = UserFactory(is_private=False) private_user = UserFactory(is_private=True) public_tweet = TweetFactory(user=public_user) private_tweet = TweetFactory(user=private_user) accounts[0].user.favorites.add(private_tweet) accounts[0].user.favorites.add(public_tweet) accounts[1].user.favorites.add(private_tweet) favorites = Tweet.public_favorite_objects.all() self.assertEqual(len(favorites), 1) self.assertEqual(favorites[0].pk, public_tweet.pk)
def test_updates_users(self): user = UserFactory.create( twitter_id=26727655, screen_name='bill') self.add_response(body=self.make_response_body()) result = UsersFetcher(screen_name='jill').fetch([460060168, 26727655, 6795192]) self.assertEqual('Aiannucci', User.objects.get(twitter_id=26727655).screen_name)
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_updates_users(self): user = UserFactory.create(twitter_id=26727655, screen_name='bill') self.add_response(body=self.make_response_body()) result = UsersFetcher(screen_name='jill').fetch( [460060168, 26727655, 6795192]) self.assertEqual('Aiannucci', User.objects.get(twitter_id=26727655).screen_name)
def test_requests_all_users(self): "If no ids supplied, uses all users in the DB" users = UserFactory.create_batch(5) result = UsersFetcher(screen_name='jill').fetch() ids = User.objects.values_list('twitter_id', flat=True).order_by('fetch_time') ids = '%2C'.join(map(str, ids)) self.assertIn(ids, responses.calls[0][0].url)
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_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 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 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_requests_all_users(self): "If no ids supplied, uses all users in the DB" users = UserFactory.create_batch(5) result = UsersFetcher(screen_name='jill').fetch() ids = User.objects.values_list('twitter_id', flat=True).order_by('fetch_time') ids = '%2C'.join(map(str, ids)) self.assertIn(ids, responses.calls[0][0].url)
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 test_does_not_update_user(self): "Shouldn't fetch user from API on save if it already has a user" self.add_response(body=self.make_verify_credentials_body(), call='account/verify_credentials') user = UserFactory(twitter_id=12552, screen_name='oldname') account = AccountWithCredentialsFactory(user=user) self.assertIsInstance(account.user, User) # Screen name is not changed to the one in our mocked API response: self.assertEqual(account.user.screen_name, 'oldname') self.assertEqual(0, len(responses.calls))
def test_favorites(self): user = UserFactory(screen_name='bill') tweet_1 = TweetFactory(text ='Tweet 1') tweet_2 = TweetFactory(text ='Tweet 2') user.favorites.add(tweet_1) user.favorites.add(tweet_2) faves = User.objects.get(screen_name='bill').favorites.all() self.assertEqual(len(faves), 2) self.assertIsInstance(faves[0], Tweet) self.assertEqual(faves[0].text, 'Tweet 2') self.assertEqual(faves[1].text, 'Tweet 1')
def test_saves_users(self): "Updates the Account's user data in the DB with fetched data." user = UserFactory(twitter_id=12552, screen_name='philgyford', name='This should change') account = AccountWithCredentialsFactory(id=4, user=user) self.add_response(body=self.make_response_body()) result = VerifyFetcher(screen_name='philgyford').fetch() user_reloaded = User.objects.get(screen_name='philgyford') self.assertEqual(user_reloaded.name, 'Phil Gyford')
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_fetch_for_account_updates(self, fetch_avatar): "Saves and returns updated existing user after successful API call" # Just make the mocked method return the User that's passed in: fetch_avatar.side_effect = lambda value: value self.add_response(body=self.make_response_body()) user = UserFactory(twitter_id=12552, screen_name='bob') account = AccountWithCredentialsFactory(user=user) result = FetchVerify(account=account).fetch() updated_user = User.objects.get(twitter_id=12552) self.assertEqual(result['account'], 'bob') self.assertIsInstance(result['user'], User) self.assertEqual(result['user'].screen_name, 'philgyford') self.assertEqual(updated_user.screen_name, 'philgyford') self.assertEqual(1, len(responses.calls)) self.assertEqual( '%s/%s.json' % (self.api_url, 'account/verify_credentials'), responses.calls[0].request.url)
def test_get_absolute_url(self): user = UserFactory(screen_name='bob') self.assertEqual(user.get_absolute_url(), '/twitter/bob/')
def test_str(self): "Has the correct string represntation" user = UserFactory(screen_name='bill') self.assertEqual(user.__str__(), '@bill')
def test_get_absolute_url(self): user = UserFactory(screen_name='bob') self.assertEqual(user.get_absolute_url(), '/twitter/bob/')
def test_unique_twitter_id(self): "Ensures twitter_id is unique" user_1 = UserFactory(twitter_id=123) with self.assertRaises(IntegrityError): user_2 = UserFactory(twitter_id=123)
def test_favorites_count(self): user = UserFactory(favourites_count=37) self.assertEqual(user.favorites_count, 37)
def test_profile_image_url(self): url = 'https://twitter.com/tester.jpg' user = UserFactory(profile_image_url_https=url) self.assertEqual(user.profile_image_url, url)