<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import unittest <EOL> import requests <EOL> from mock import Mock , patch <EOL> from pants . cache . resolver import Resolver , ResponseParser , RESTfulResolver <EOL> PATCH_OPTS = dict ( autospec = True , spec_set = True ) <EOL> class TestResponseParser ( unittest . TestCase ) : <EOL> def testParse ( self ) : <EOL> response_parser = ResponseParser ( ) <EOL> self . assertEquals ( [ '<STR_LIT>' , '<STR_LIT>' ] , response_parser . parse ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( [ ] , response_parser . parse ( '<STR_LIT>' ) ) <EOL> with self . assertRaises ( ResponseParser . ResponseParserError ) : <EOL> response_parser . parse ( '<STR_LIT>' ) <EOL> with self . assertRaises ( ResponseParser . ResponseParserError ) : <EOL> response_parser . parse ( '<STR_LIT>' ) <EOL> with self . assertRaises ( ResponseParser . ResponseParserError ) : <EOL> response_parser . parse ( '<STR_LIT>' ) <EOL> with self . assertRaises ( ResponseParser . ResponseParserError ) : <EOL> response_parser . parse ( '<STR_LIT>' . encode ( '<STR_LIT>' ) ) <EOL> class TestRESTfulResolver ( unittest . TestCase ) : <EOL> TEST_TIMEOUT = <NUM_LIT:1> <EOL> TEST_RETRIES = <NUM_LIT:3> <EOL> RESOLVED_URL_1 = '<STR_LIT>' <EOL> RESOLVED_URL_2 = '<STR_LIT>' <EOL> URLS = [ RESOLVED_URL_1 , RESOLVED_URL_2 ] <EOL> TEST_RESOLVED_FROM = '<STR_LIT>' <EOL> TEST_REQUEST_EXCEPTION = requests . exceptions . ConnectionError ( ) <EOL> def setUp ( self ) : <EOL> self . parser = Mock ( spec = ResponseParser ) <EOL> self . resolver = RESTfulResolver ( self . TEST_TIMEOUT , self . TEST_RETRIES , self . parser ) <EOL> def mock_response ( self , status_code , urls = None ) : <EOL> response = Mock ( ) <EOL> response . status_code = status_code <EOL> self . parser . parse = Mock ( return_value = urls ) <EOL> return response <EOL> def testResolveSuccess ( self ) : <EOL> with patch . object ( requests . Session , '<STR_LIT>' , ** PATCH_OPTS ) as mock_get : <EOL> mock_get . return_value = self . mock_response ( requests . codes . ok , urls = self . URLS ) <EOL> self . assertEquals ( self . URLS , self . resolver . resolve ( self . TEST_RESOLVED_FROM ) ) <EOL> def testResolveErrorEmptyReturn ( self ) : <EOL> with patch . object ( requests . Session , '<STR_LIT>' , ** PATCH_OPTS ) as mock_get : <EOL> mock_get . return_value = self . mock_response ( requests . codes . ok , urls = [ ] ) <EOL> with self . assertRaises ( Resolver . ResolverError ) : <EOL> self . resolver . resolve ( self . TEST_RESOLVED_FROM ) <EOL> def testResolveParseError ( self ) : <EOL> with patch . object ( requests . Session , '<STR_LIT>' , ** PATCH_OPTS ) as mock_get : <EOL> mock_get . return_value = self . mock_response ( requests . codes . ok , urls = '<STR_LIT>' ) <EOL> self . parser . parse . side_effect = ResponseParser . ResponseParserError ( ) <EOL> with self . assertRaises ( Resolver . ResolverError ) : <EOL> self . resolver . resolve ( self . TEST_RESOLVED_FROM ) <EOL> def testResolveResponseError ( self ) : <EOL> with patch . object ( requests . Session , '<STR_LIT>' , ** PATCH_OPTS ) as mock_get : <EOL> mock_get . return_value = self . mock_response ( requests . codes . service_unavailable ) <EOL> with self . assertRaises ( Resolver . ResolverError ) : <EOL> self . resolver . resolve ( self . TEST_RESOLVED_FROM ) <EOL> def testResolveConnectionError ( self ) : <EOL> with patch . object ( requests . Session , '<STR_LIT>' , ** PATCH_OPTS ) as mock_get : <EOL> mock_get . side_effect = self . TEST_REQUEST_EXCEPTION <EOL> with self . assertRaises ( Resolver . ResolverError ) : <EOL> self . resolver . resolve ( self . TEST_RESOLVED_FROM ) </s>
