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): 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 test_tweets_years(self): "Should include all intermediate years." user = TwitterUserFactory(screen_name='philgyford', is_private=False) TwitterAccountFactory(user=user) TweetFactory(user=user, post_time=make_datetime('2014-01-01 12:00:00')) TweetFactory.create_batch(3, user=user, post_time=make_datetime('2016-01-01 12:00:00')) TweetFactory(user=user, post_time=make_datetime('2018-01-01 12:00:00')) # From a user without an Account; should be ignored: TweetFactory(post_time=make_datetime('2018-01-01 12:00:00')) result = TwitterGenerator( screen_name='philgyford').get_tweets_per_year() self.assertIn('data', result) self.assertEqual(result['data'], [ {'label': '2014', 'value': 1}, {'label': '2015', 'value': 0}, {'label': '2016', 'value': 3}, {'label': '2017', 'value': 0}, {'label': '2018', 'value': 1}, ])
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 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_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 test_tweets_data(self): user = TwitterUserFactory(screen_name='philgyford', is_private=False) TwitterAccountFactory(user=user) TweetFactory.create_batch(3, user=user, post_time=make_datetime('2018-01-01 12:00:00')) # From a user without an Account; should be ignored: TweetFactory(post_time=make_datetime('2018-01-01 12:00:00')) result = TwitterGenerator( screen_name='philgyford').get_tweets_per_year() self.assertIn('data', result) self.assertEqual(len(result['data']), 1) self.assertEqual(result['data'][0]['label'], '2018') self.assertEqual(result['data'][0]['value'], 3)
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_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): 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_requests_all_tweets(self): "If no ids supplied, uses all tweets in the DB" tweets = TweetFactory.create_batch(5) result = TweetsFetcher(screen_name='jill').fetch() ids = Tweet.objects.values_list('twitter_id', flat=True).order_by('fetch_time') params = QueryDict(responses.calls[0][0].body) self.assertIn('id', params) self.assertEqual(','.join(map(str, ids)), params['id'])
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') 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_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') 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_requests_all_tweets(self): "If no ids supplied, uses all tweets in the DB" tweets = TweetFactory.create_batch(5) result = TweetsFetcher(screen_name='jill').fetch() ids = Tweet.objects.values_list('twitter_id', flat=True).order_by('fetch_time') params = QueryDict(responses.calls[0][0].body) self.assertIn('id', params) self.assertEqual(','.join(map(str, ids)), params['id'])
def test_favorites_manager(self): "Should contain recent tweets favorited by any account." accounts = AccountFactory.create_batch(2) tweets = TweetFactory.create_batch(5) accounts[0].user.favorites.add(tweets[2]) accounts[0].user.favorites.add(tweets[4]) accounts[1].user.favorites.add(tweets[2]) favorites = Tweet.favorite_objects.all() self.assertEqual(len(favorites), 2) self.assertEqual(favorites[0].pk, tweets[4].pk) self.assertEqual(favorites[1].pk, tweets[2].pk)
def test_favorites_manager(self): "Should contain recent tweets favorited by any account." accounts = AccountFactory.create_batch(2) tweets = TweetFactory.create_batch(5) accounts[0].user.favorites.add(tweets[2]) accounts[0].user.favorites.add(tweets[4]) accounts[1].user.favorites.add(tweets[2]) favorites = Tweet.favorite_objects.all() self.assertEqual(len(favorites), 2) self.assertEqual(favorites[0].pk, tweets[4].pk) self.assertEqual(favorites[1].pk, tweets[2].pk)
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 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_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_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 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) 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])