def getResponse(self, reset_cache=True, q = config.SEARCH): ''' Handles interaction with Twitter module and manages memcache. Returns response JSON or None as well as a tuple with memcache info. ''' # Initialize Twitter Search twsearch = TwitterSearch(keys.CONSUMER_KEY, keys.CONSUMER_SECRET) # See what and when the last status we fetched was fetched_on = memcache.get('last_status_fetched_on') # Delete last fetched status ID if it's too old (or doesn't exist) to prevent loading 1000's of statuses if (not fetched_on or time.time() - fetched_on > config.LASTFETCHEDTIMEOUT) and reset_cache: memcache.delete('last_status_fetched_id') # Get results till you hit this guy fetch_until = memcache.get('last_status_fetched_id') # Get JSON response response = twsearch.search(q,fetch_until=fetch_until,count=config.SEARCHCOUNT) # Update memcache if response and reset_cache: memcache.set('last_status_fetched_id', response.get('statuses')[0].get('id'),time=config.LASTFETCHEDTIMEOUT*2) memcache.set('last_status_fetched_on', time.time(),time=config.LASTFETCHEDTIMEOUT*2) return (response,memcache.get('last_status_fetched_id'),memcache.get('last_status_fetched_on'),fetch_until)
def test_add_good_domain(self): for domain in 'asdf.com', 'https://asdf.com/', 'asdf.com/foo?bar#baz': self.datastore_stub.Clear() resp = twitter.application.get_response('/twitter/add?domain=%s' % domain, method='POST') self.assertEquals(302, resp.status_int, resp.body) self.assertEquals('http://localhost/', resp.headers['Location']) searches = TwitterSearch.all().fetch(10) self.assertEqual(1, len(searches)) ts = searches[0] self.assertEqual('asdf.com', ts.key().name()) self.assertEqual('http://asdf.com/', ts.url) self.assertEqual('http://asdf.com/favicon.ico', ts.picture) self.assertEqual(self.current_user_id, ts.owner.key().name())
def test_add_bad_domain(self): for domain in '', ' ', 'com', 'com.', 'a/b/c': resp = twitter.application.get_response('/twitter/add?domain=%s' % domain, method='POST') self.assertEquals(400, resp.status_int, resp.body) self.assertEqual(0, TwitterSearch.all().count())
def setUp(self): super(TwitterSearchTest, self).setUp() self.twitter = TwitterSearch(key_name='example.com') self.datastore_stub = self.testbed.get_stub('datastore_v3')
class TwitterSearchTest(testutil.HandlerTest): def setUp(self): super(TwitterSearchTest, self).setUp() self.twitter = TwitterSearch(key_name='example.com') self.datastore_stub = self.testbed.get_stub('datastore_v3') def test_tweet_to_salmon_with_expanded_url(self): self.assert_equals(TWEETS_SALMON_VARS[0], self.twitter.tweet_to_salmon_vars(TWEETS_JSON[0])) def test_tweet_to_salmon_with_url(self): self.assert_equals(TWEETS_SALMON_VARS[1], self.twitter.tweet_to_salmon_vars(TWEETS_JSON[1])) def test_tweet_to_salmon_minimal(self): salmon = self.twitter.tweet_to_salmon_vars({'id': 123}) self.assert_equals('tag:twitter.com,2012:123', salmon['id']) def test_tweet_to_salmon_no_matching_url(self): tweet = copy.deepcopy(TWEETS_JSON[0]) tweet['entities']['urls'][0]['expanded_url'] = 'http://foo.com/bar' expected = copy.deepcopy(TWEETS_SALMON_VARS[0]) expected['in_reply_to'] = None self.assert_equals(expected, self.twitter.tweet_to_salmon_vars(tweet)) def test_get_salmon(self): self.expect_urlfetch(twitter.API_SEARCH_URL % 'example.com', json.dumps({'results': TWEETS_JSON})) self.expect_urlfetch(twitter.API_SEARCH_URL % '@snarfed', json.dumps({'results': []})) self.expect_urlfetch(twitter.API_SEARCH_URL % '@user1', json.dumps({'results': MENTIONS_JSON})) self.mox.ReplayAll() self.assert_equals(TWEETS_SALMON_VARS, self.twitter.get_salmon()) def test_add_good_domain(self): for domain in 'asdf.com', 'https://asdf.com/', 'asdf.com/foo?bar#baz': self.datastore_stub.Clear() resp = twitter.application.get_response('/twitter/add?domain=%s' % domain, method='POST') self.assertEquals(302, resp.status_int, resp.body) self.assertEquals('http://localhost/', resp.headers['Location']) searches = TwitterSearch.all().fetch(10) self.assertEqual(1, len(searches)) ts = searches[0] self.assertEqual('asdf.com', ts.key().name()) self.assertEqual('http://asdf.com/', ts.url) self.assertEqual('http://asdf.com/favicon.ico', ts.picture) self.assertEqual(self.current_user_id, ts.owner.key().name()) def test_add_bad_domain(self): for domain in '', ' ', 'com', 'com.', 'a/b/c': resp = twitter.application.get_response('/twitter/add?domain=%s' % domain, method='POST') self.assertEquals(400, resp.status_int, resp.body) self.assertEqual(0, TwitterSearch.all().count())