<s> from datetime import datetime <EOL> import unittest <EOL> from mock import Mock , sentinel , patch , ANY <EOL> from nose . tools import assert_equal , assert_true <EOL> import pytz <EOL> from six import advance_iterator <EOL> from twilio . rest . resources . imports import json <EOL> from twilio . rest . resources import Resource , NextGenListResource , NextGenInstanceResource <EOL> from twilio . rest . resources import ListResource <EOL> from twilio . rest . resources import InstanceResource <EOL> base_uri = "<STR_LIT>" <EOL> account_sid = "<STR_LIT>" <EOL> auth = ( account_sid , "<STR_LIT>" ) <EOL> def test_resource_init ( ) : <EOL> r = Resource ( base_uri , auth ) <EOL> uri = "<STR_LIT>" % ( base_uri , r . name ) <EOL> assert_equal ( r . base_uri , base_uri ) <EOL> assert_equal ( r . auth , auth ) <EOL> assert_equal ( r . uri , uri ) <EOL> def test_equivalence ( ) : <EOL> p = ListResource ( base_uri , auth ) <EOL> r1 = p . load_instance ( { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> r2 = p . load_instance ( { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> assert_equal ( r1 , r2 ) <EOL> class ListResourceTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . r = ListResource ( base_uri , auth ) <EOL> def testListResourceInit ( self ) : <EOL> uri = "<STR_LIT>" % ( base_uri , self . r . name ) <EOL> assert_equal ( self . r . uri , uri ) <EOL> assert_equal ( str ( self . r ) , "<STR_LIT>" ) <EOL> def testKeyValueLower ( self ) : <EOL> assert_equal ( self . r . key , self . r . name . lower ( ) ) <EOL> def testIterNoKey ( self ) : <EOL> self . r . request = Mock ( ) <EOL> self . r . request . return_value = Mock ( ) , { } <EOL> self . assertRaises ( StopIteration , advance_iterator , self . r . iter ( ) ) <EOL> def testRequest ( self ) : <EOL> self . r . request = Mock ( ) <EOL> self . r . request . return_value = Mock ( ) , { self . r . key : [ { '<STR_LIT>' : '<STR_LIT:foo>' } ] } <EOL> advance_iterator ( self . r . iter ( ) ) <EOL> self . r . request . assert_called_with ( "<STR_LIT:GET>" , "<STR_LIT>" , params = { } ) <EOL> def testIterOneItem ( self ) : <EOL> self . r . request = Mock ( ) <EOL> self . r . request . return_value = Mock ( ) , { self . r . key : [ { '<STR_LIT>' : '<STR_LIT:foo>' } ] } <EOL> items = self . r . iter ( ) <EOL> advance_iterator ( items ) <EOL> self . assertRaises ( StopIteration , advance_iterator , items ) <EOL> def testIterNoNextPage ( self ) : <EOL> self . r . request = Mock ( ) <EOL> self . r . request . return_value = Mock ( ) , { self . r . key : [ ] } <EOL> self . assertRaises ( StopIteration , advance_iterator , self . r . iter ( ) ) <EOL> def testKeyValue ( self ) : <EOL> self . r . key = "<STR_LIT>" <EOL> assert_equal ( self . r . key , "<STR_LIT>" ) <EOL> def testInstanceLoading ( self ) : <EOL> instance = self . r . load_instance ( { "<STR_LIT>" : "<STR_LIT:foo>" } ) <EOL> assert_true ( isinstance ( instance , InstanceResource ) ) <EOL> assert_equal ( instance . sid , "<STR_LIT:foo>" ) <EOL> def testListResourceCreateResponse200 ( self ) : <EOL> """<STR_LIT>""" <EOL> self . r . request = Mock ( ) <EOL> return_value = Mock ( ) <EOL> return_value . status_code = <NUM_LIT:200> <EOL> self . r . request . return_value = return_value , { '<STR_LIT>' : '<STR_LIT:foo>' } <EOL> self . r . create_instance ( { } ) <EOL> self . r . request . assert_called_with ( "<STR_LIT:POST>" , "<STR_LIT>" , data = { } ) <EOL> def testListResourceCreateResponse201 ( self ) : <EOL> """<STR_LIT>""" <EOL> self . r . request = Mock ( ) <EOL> return_value = Mock ( ) <EOL> return_value . status_code = <NUM_LIT> <EOL> self . r . request . return_value = return_value , { '<STR_LIT>' : '<STR_LIT:foo>' } <EOL> self . r . create_instance ( { } ) <EOL> self . r . request . assert_called_with ( "<STR_LIT:POST>" , "<STR_LIT>" , data = { } ) <EOL> class NextGenListResourceTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . r = NextGenListResource ( base_uri , auth ) <EOL> def test_list_resource_init ( self ) : <EOL> uri = "<STR_LIT>" % ( base_uri , self . r . name ) <EOL> assert_equal ( self . r . uri , uri ) <EOL> def test_iter_no_key ( self ) : <EOL> self . r . request = Mock ( ) <EOL> self . r . request . return_value = Mock ( ) , { } <EOL> self . assertRaises ( StopIteration , advance_iterator , self . r . iter ( ) ) <EOL> def test_iter_key_not_present ( self ) : <EOL> self . r . request = Mock ( ) <EOL> self . r . request . return_value = Mock ( ) , { '<STR_LIT>' : { '<STR_LIT:key>' : '<STR_LIT>' } } <EOL> def test_iter_request ( self ) : <EOL> self . r . request = Mock ( ) <EOL> self . r . request . return_value = Mock ( ) , { '<STR_LIT>' : { '<STR_LIT:key>' : '<STR_LIT>' } , '<STR_LIT>' : [ { '<STR_LIT>' : '<STR_LIT>' } ] } <EOL> item = advance_iterator ( self . r . iter ( ) ) <EOL> self . r . request . assert_called_with ( "<STR_LIT:GET>" , "<STR_LIT>" ) <EOL> assert_equal ( item . sid , '<STR_LIT>' ) <EOL> def test_iter_one_item ( self ) : <EOL> self . r . request = Mock ( ) <EOL> self . r . request . return_value = Mock ( ) , { '<STR_LIT>' : { '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT>' : None } , '<STR_LIT>' : [ { '<STR_LIT>' : '<STR_LIT>' } ] } <EOL> items = self . r . iter ( ) <EOL> advance_iterator ( items ) <EOL> self . assertRaises ( StopIteration , advance_iterator , items ) <EOL> def test_instance_loading ( self ) : <EOL> instance = self . r . load_instance ( { "<STR_LIT>" : "<STR_LIT:foo>" } ) <EOL> assert_true ( isinstance ( instance , NextGenInstanceResource ) ) <EOL> assert_equal ( instance . sid , "<STR_LIT:foo>" ) <EOL> class testInstanceResourceInit ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . parent = ListResource ( base_uri , auth ) <EOL> self . r = InstanceResource ( self . parent , "<STR_LIT>" ) <EOL> self . uri = "<STR_LIT>" % ( self . parent . uri , "<STR_LIT>" ) <EOL> def testInit ( self ) : <EOL> assert_equal ( self . r . uri , self . uri ) <EOL> def testLoad ( self ) : <EOL> self . r . load ( { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> assert_equal ( self . r . hey , "<STR_LIT>" ) <EOL> def testLoadWithUri ( self ) : <EOL> self . r . load ( { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } ) <EOL> assert_equal ( self . r . hey , "<STR_LIT>" ) <EOL> assert_equal ( self . r . uri , self . uri ) <EOL> def testLoadDateCreated ( self ) : <EOL> self . r . load ( { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } ) <EOL> try : <EOL> assert_true ( hasattr ( self . r . date_created , "<STR_LIT>" ) ) <EOL> assert_equal ( self . r . date_created . day , <NUM_LIT> ) <EOL> except AttributeError : <EOL> pass <EOL> def testLoadNullDate ( self ) : <EOL> self . r . load ( { "<STR_LIT>" : None , "<STR_LIT>" : "<STR_LIT>" } ) <EOL> assert self . r . date_created is None <EOL> def testLoadWithFrom ( self ) : <EOL> self . r . load ( { "<STR_LIT>" : "<STR_LIT:foo>" } ) <EOL> assert_equal ( self . r . from_ , "<STR_LIT:foo>" ) <EOL> def testLoadSubresources ( self ) : <EOL> m = Mock ( ) <EOL> self . r . subresources = [ m ] <EOL> self . r . load_subresources ( ) <EOL> m . assert_called_with ( self . r . uri , self . r . auth , self . r . timeout ) <EOL> class NextGenInstanceResourceTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . parent = NextGenListResource ( base_uri , auth ) <EOL> self . r = NextGenInstanceResource ( self . parent , "<STR_LIT>" ) <EOL> def test_load ( self ) : <EOL> self . r . load ( { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> assert_equal ( self . r . hey , "<STR_LIT>" ) <EOL> def test_iso_date_parser ( self ) : <EOL> self . r . load ( { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> assert_equal ( <EOL> self . r . date_created , <EOL> datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , tzinfo = pytz . utc ) , <EOL> ) <EOL> class testTimeoutPropagation ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . parent = ListResource ( base_uri , auth , timeout = sentinel . timeout ) <EOL> self . r = InstanceResource ( self . parent , "<STR_LIT>" ) <EOL> self . uri = "<STR_LIT>" % ( self . parent . uri , "<STR_LIT>" ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def testPassThrough ( self , mock_request ) : <EOL> mock_response = Mock ( ) <EOL> mock_response . ok = True , <EOL> mock_response . content = json . dumps ( { '<STR_LIT:key>' : '<STR_LIT:value>' } ) <EOL> mock_request . return_value = mock_response <EOL> assert_equal ( self . r . timeout , sentinel . timeout ) <EOL> assert_equal ( ( mock_response , { '<STR_LIT:key>' : '<STR_LIT:value>' } ) , self . r . request ( '<STR_LIT:GET>' , base_uri ) ) <EOL> mock_request . assert_called_once_with ( <EOL> '<STR_LIT:GET>' , <EOL> base_uri + '<STR_LIT>' , <EOL> headers = ANY , <EOL> timeout = sentinel . timeout , <EOL> auth = ANY <EOL> ) </s>
