def test_get_from_cache_when_twitter_api_fails(self, logging_mock): exception_message = 'Too many requests' HTTPretty.register_uri( HTTPretty.GET, self.api_url, responses=[ HTTPretty.Response(body=get_json('jeresig.json'), status=200, content_encoding='identity'), HTTPretty.Response(body=exception_message, status=429, content_encoding='identity'), ]) # it should be ok by now output, context = render_template( """{% get_tweets for "jresig" as tweets %}""") cache_key = get_user_cache_key(asvar='tweets', username='******') expect(cache.get(cache_key)).should.have.length_of(1) expect(context['tweets'][0]['text']).to.equal( "This is not John Resig - you should be following @jeresig instead!!!" ) # when twitter api fails, should use cache output2, context2 = render_template( """{% get_tweets for "jresig" as tweets %}""") expect(cache.get(cache_key)).should.have.length_of(1) expect(context2['tweets'][0]['text']).to.equal( "This is not John Resig - you should be following @jeresig instead!!!" ) logging_mock.assert_called_with(self.logger_name) expect(logging_mock.return_value.error.call_args[0][0]).should.contain( exception_message)
def test_get_from_cache_when_twitter_api_fails(self, logging_mock): exception_message = 'Too many requests' HTTPretty.register_uri(HTTPretty.GET, self.api_url, responses=[ HTTPretty.Response(body=get_json('jeresig.json'), status=200, content_encoding='identity'), HTTPretty.Response(body=exception_message, status=429, content_encoding='identity'), ]) # it should be ok by now output, context = render_template("""{% get_tweets for "jresig" as tweets %}""") cache_key = get_user_cache_key(asvar='tweets', username='******') expect(cache.get(cache_key)).should.have.length_of(1) expect(context['tweets'][0]['text']).to.equal("This is not John Resig - you should be following @jeresig instead!!!") # when twitter api fails, should use cache output2, context2 = render_template("""{% get_tweets for "jresig" as tweets %}""") expect(cache.get(cache_key)).should.have.length_of(1) expect(context2['tweets'][0]['text']).to.equal("This is not John Resig - you should be following @jeresig instead!!!") logging_mock.assert_called_with(self.logger_name) expect(logging_mock.return_value.error.call_args[0][0]).should.contain(exception_message)
def get_cache_key(self, kwargs_dict): return get_user_cache_key(**kwargs_dict)