def test_create_from_json_example0(self): """ create_from_json() should return a Tweet object with the fields set to their proper values. """ # Iterates through the test data below and checks that all the fields match up. # The test data is a set of raw JSON text from the Twitter api and documentation, # along with corresponding manually-extracted data. for idx, body in enumerate(example_tweet_bodies): data = example_data[idx] raw_tweet = json.loads(body) tweet = Tweet.create_from_json(raw_tweet) self.assertIsInstance(tweet, Tweet) # check for model validity tweet.clean_fields() self.assertEqual(tweet.tweet_id, data['tweet_id'], 'tweet_id matches') self.assertEqual(tweet.text, data['text'], 'text matches') self.assertEqual(tweet.truncated, data['truncated'], 'truncated matches') self.assertEqual(tweet.lang, data['lang'], 'lang matches') # Basic user info self.assertEqual(tweet.user_id, data['user_id'], 'user_id matches') self.assertEqual(tweet.user_screen_name, data['user_screen_name'], 'user_screen_name matches') self.assertEqual(tweet.user_name, data['user_name'], 'user_name matches') self.assertEqual(tweet.user_verified, data['user_verified'], 'user_verified matches') # Timing parameters # May need to convert the date depending on timezone settings if settings.USE_TZ: data['created_at'] = timezone.make_aware(data['created_at'], timezone.get_current_timezone()) self.assertEqual(tweet.created_at, data['created_at'], 'created_at matches') self.assertEqual(tweet.user_utc_offset, data['user_utc_offset'], 'user_utc_offset matches') self.assertEqual(tweet.user_time_zone, data['user_time_zone'], 'user_time_zone matches') # none, low, or medium self.assertEqual(tweet.filter_level, data['filter_level'], 'filter_level matches') # Geo parameters self.assertEqual(tweet.latitude, data['latitude'], 'latitude matches') self.assertEqual(tweet.longitude, data['longitude'], 'longitude matches') self.assertEqual(tweet.user_geo_enabled, data['user_geo_enabled'], 'user_geo_enabled matches') self.assertEqual(tweet.user_location, data['user_location'], 'user_location matches') # Engagement - not likely to be very useful for streamed tweets but whatever self.assertEqual(tweet.favorite_count, data['favorite_count'], 'favorite_count matches') self.assertEqual(tweet.retweet_count, data['retweet_count'], 'retweet_count matches') self.assertEqual(tweet.user_followers_count, data['user_followers_count'], 'user_followers_count matches') self.assertEqual(tweet.user_friends_count, data['user_friends_count'], 'user_friends_count matches') # Relation to other tweets self.assertEqual(tweet.in_reply_to_status_id, data['in_reply_to_status_id'], 'in_reply_to_status_id matches') self.assertEqual(tweet.retweeted_status_id, data['retweeted_status_id'], 'retweeted_status_id matches')
def test_create_from_json_example0(self): """ create_from_json() should return a Tweet object with the fields set to their proper values. """ # Iterates through the test data below and checks that all the fields match up. # The test data is a set of raw JSON text from the Twitter api and documentation, # along with corresponding manually-extracted data. for idx, body in enumerate(example_tweet_bodies): data = example_data[idx] raw_tweet = json.loads(body) tweet = Tweet.create_from_json(raw_tweet) self.assertIsInstance(tweet, Tweet) # check for model validity tweet.clean_fields() self.assertEqual(tweet.tweet_id, data['tweet_id'], 'tweet_id matches') self.assertEqual(tweet.text, data['text'], 'text matches') self.assertEqual(tweet.truncated, data['truncated'], 'truncated matches') self.assertEqual(tweet.lang, data['lang'], 'lang matches') # Basic user info self.assertEqual(tweet.user_id, data['user_id'], 'user_id matches') self.assertEqual(tweet.user_screen_name, data['user_screen_name'], 'user_screen_name matches') self.assertEqual(tweet.user_name, data['user_name'], 'user_name matches') self.assertEqual(tweet.user_verified, data['user_verified'], 'user_verified matches') # Timing parameters # May need to convert the date depending on timezone settings if settings.USE_TZ: data['created_at'] = timezone.make_aware( data['created_at'], timezone.get_current_timezone()) self.assertEqual(tweet.created_at, data['created_at'], 'created_at matches') self.assertEqual(tweet.user_utc_offset, data['user_utc_offset'], 'user_utc_offset matches') self.assertEqual(tweet.user_time_zone, data['user_time_zone'], 'user_time_zone matches') # none, low, or medium self.assertEqual(tweet.filter_level, data['filter_level'], 'filter_level matches') # Geo parameters self.assertEqual(tweet.latitude, data['latitude'], 'latitude matches') self.assertEqual(tweet.longitude, data['longitude'], 'longitude matches') self.assertEqual(tweet.user_geo_enabled, data['user_geo_enabled'], 'user_geo_enabled matches') self.assertEqual(tweet.user_location, data['user_location'], 'user_location matches') # Engagement - not likely to be very useful for streamed tweets but whatever self.assertEqual(tweet.favorite_count, data['favorite_count'], 'favorite_count matches') self.assertEqual(tweet.retweet_count, data['retweet_count'], 'retweet_count matches') self.assertEqual(tweet.user_followers_count, data['user_followers_count'], 'user_followers_count matches') self.assertEqual(tweet.user_friends_count, data['user_friends_count'], 'user_friends_count matches') # Relation to other tweets self.assertEqual(tweet.in_reply_to_status_id, data['in_reply_to_status_id'], 'in_reply_to_status_id matches') self.assertEqual(tweet.retweeted_status_id, data['retweeted_status_id'], 'retweeted_status_id matches')