def test_batch_action_creates_parameters_from_items(self): self.action_def['request']['params'] = [ {'target': 'Bucket', 'source': 'data', 'path': 'BucketName'}, {'target': 'Delete.Objects[].Key', 'source': 'data', 'path': 'Key'} ] client = mock.Mock() item1 = mock.Mock() item1.meta = ResourceMeta('test', client=client, data={ 'BucketName': 'bucket', 'Key': 'item1' }) item2 = mock.Mock() item2.meta = ResourceMeta('test', client=client, data={ 'BucketName': 'bucket', 'Key': 'item2' }) collection = mock.Mock() collection.pages.return_value = [[item1, item2]] action = BatchAction(self.model) action(collection) client.get_frobs.assert_called_with(Bucket='bucket', Delete={ 'Objects': [ {'Key': 'item1'}, {'Key': 'item2'} ] })
def test_service_action_params_reuse(self): request_model = Request({ 'operation': 'GetFrobs', 'params': [{ 'target': 'Delete.Objects[].Key', 'source': 'data', 'path': 'Key' }] }) item1 = mock.Mock() item1.meta = ResourceMeta('test', data={'Key': 'item1'}) item2 = mock.Mock() item2.meta = ResourceMeta('test', data={'Key': 'item2'}) # Here we create params and then re-use it to build up a more # complex structure over multiple calls. params = create_request_parameters(item1, request_model) create_request_parameters(item2, request_model, params=params) self.assertEqual( params, {'Delete': { 'Objects': [{ 'Key': 'item1' }, { 'Key': 'item2' }] }})
def test_batch_action_calls_operation(self, crp_mock): # In this test we have an item and parameters, so the call # to the batch operation should be made. def side_effect(resource, model, params=None, index=None): params['foo'] = 'bar' crp_mock.side_effect = side_effect client = mock.Mock() item = mock.Mock() item.meta = ResourceMeta('test', client=client) collection = mock.Mock() collection.pages.return_value = [[item]] model = self.model action = BatchAction(model) action(collection) # Here the call is made with params={}, but they are edited # in-place so we need to compare to the final edited value. crp_mock.assert_called_with(item, model.request, params={'foo': 'bar'}, index=0) client.get_frobs.assert_called_with(foo='bar')
def test_service_action_creates_params(self, params_mock): resource = mock.Mock() resource.meta = ResourceMeta('test', client=mock.Mock()) action = ServiceAction(self.action) action(resource, foo=1) self.assertTrue(params_mock.called, 'Parameters for operation not created')
def test_service_action_calls_operation(self, params_mock): resource = mock.Mock() resource.meta = ResourceMeta('test', client=mock.Mock()) operation = resource.meta.client.get_frobs operation.return_value = 'response' action = ServiceAction(self.action) response = action(resource, foo=1) operation.assert_called_with(foo=1, bar='baz') self.assertEqual(response, 'response', 'Unexpected low-level response data returned')
def test_service_action_calls_operation(self, params_mock): resource = mock.Mock() resource.meta = ResourceMeta('test', client=mock.Mock()) get_waiter = resource.meta.client.get_waiter mock_waiter = mock.Mock() get_waiter.return_value = mock_waiter action = WaiterAction(self.waiter, self.waiter_resource_name) action(resource, foo=1) get_waiter.assert_called_with('frob_exists') mock_waiter.wait.assert_called_with(foo=1, bar='baz')
def setUp(self): super(TestCollectionFactory, self).setUp() self.client = mock.Mock() self.client.can_paginate.return_value = False self.parent = mock.Mock() self.parent.meta = ResourceMeta('test', client=self.client) self.resource_factory = ResourceFactory(mock.Mock()) self.service_model = ServiceModel({}) self.event_emitter = HierarchicalEmitter() self.factory = CollectionFactory() self.load = self.factory.load_from_definition
def test_service_action_calls_raw_handler(self, handler_mock, params_mock): resource = mock.Mock() resource.meta = ResourceMeta('test', client=mock.Mock()) operation = resource.meta.client.get_frobs operation.return_value = 'response' action = ServiceAction(self.action) handler_mock.return_value.return_value = 'response' action(resource) handler_mock.assert_called_with(None) handler_mock.return_value.assert_called_with(resource, {}, 'response')
def test_service_action_params_data_member_missing_no_load(self): request_model = Request({ 'operation': 'GetFrobs', 'params': [{ 'target': 'WarehouseUrl', 'source': 'data', 'path': 'SomeMember' }] }) # This mock has no ``load`` method. parent = mock.Mock(spec=ServiceResource) parent.meta = ResourceMeta('test', data=None) with self.assertRaises(ResourceLoadException): params = create_request_parameters(parent, request_model)
def test_service_action_params_data_member(self): request_model = Request({ 'operation': 'GetFrobs', 'params': [{ 'target': 'WarehouseUrl', 'source': 'data', 'path': 'SomeMember' }] }) parent = mock.Mock() parent.meta = ResourceMeta('test', data={'SomeMember': 'w-url'}) params = create_request_parameters(parent, request_model) self.assertEqual(params['WarehouseUrl'], 'w-url', 'Parameter not set from resource property')
def test_batch_action_skips_operation(self, crp_mock): # In this test we have an item from the collection, but no # parameters are set up. Because of this, we do NOT call # the batch operation. client = mock.Mock() item = mock.Mock() item.meta = ResourceMeta('test', client=client) collection = mock.Mock() collection.pages.return_value = [[item]] model = self.model action = BatchAction(model) action(collection) crp_mock.assert_called_with(item, model.request, params={}, index=0) client.get_frobs.assert_not_called()
def setUp(self): super(TestResourceCollection, self).setUp() # Minimal definition so things like repr work self.collection_def = { 'request': { 'operation': 'TestOperation' }, 'resource': { 'type': 'Frob' } } self.client = mock.Mock() self.client.can_paginate.return_value = False self.parent = mock.Mock() self.parent.meta = ResourceMeta('test', client=self.client) self.factory = ResourceFactory(mock.Mock()) self.service_model = ServiceModel({})
def test_build_identifier_from_parent_data_member(self): identifiers = [Parameter(target='Id', source='data', path='Member')] parent = mock.Mock() parent.meta = ResourceMeta('test', data={ 'Member': 'data-member' }) params = {} response = { 'Container': { 'Frobs': [] } } values = build_identifiers(identifiers, parent, params, response) self.assertEqual(values[0][1], 'data-member', 'Identifier loaded from parent data member not set')
def test_service_action_calls_resource_handler(self, handler_mock, params_mock): self.action_def['resource'] = { 'type': 'Frob', 'path': 'Container' } resource = mock.Mock() resource.meta = ResourceMeta('test', client=mock.Mock()) operation = resource.meta.client.get_frobs operation.return_value = 'response' factory = mock.Mock() resource_defs = {} service_model = mock.Mock() action_model = self.action service_context = ServiceContext( service_name='test', service_model=service_model, resource_json_definitions=resource_defs, service_waiter_model=None ) action = ServiceAction( action_model=action_model, factory=factory, service_context=service_context ) handler_mock.return_value.return_value = 'response' action(resource) handler_mock.assert_called_with( search_path='Container', factory=factory, resource_model=action_model.resource, service_context=service_context, operation_name='GetFrobs' )
def test_service_action_params_data_member_missing(self): request_model = Request({ 'operation': 'GetFrobs', 'params': [{ 'target': 'WarehouseUrl', 'source': 'data', 'path': 'SomeMember' }] }) parent = mock.Mock() def load_data(): parent.meta.data = {'SomeMember': 'w-url'} parent.load.side_effect = load_data parent.meta = ResourceMeta('test') params = create_request_parameters(parent, request_model) parent.load.assert_called_with() self.assertEqual(params['WarehouseUrl'], 'w-url', 'Parameter not set from resource property')
def setUp(self): super(TestResourceHandler, self).setUp() self.identifier_path = '' self.factory = ResourceFactory(mock.Mock()) self.resource_defs = { 'Frob': { 'shape': 'Frob', 'identifiers': [ {'name': 'Id'} ] } } self.service_model = mock.Mock() shape = mock.Mock() shape.members = {} self.service_model.shape_for.return_value = shape frobs = mock.Mock() frobs.type_name = 'list' container = mock.Mock() container.type_name = 'structure' container.members = { 'Frobs': frobs } self.output_shape = mock.Mock() self.output_shape.type_name = 'structure' self.output_shape.members = { 'Container': container } operation_model = mock.Mock() operation_model.output_shape = self.output_shape self.service_model.operation_model.return_value = operation_model self.parent = mock.Mock() self.parent.meta = ResourceMeta('test', client=mock.Mock()) self.params = {}
def setUp(self): self.events = mock.Mock() self.client = mock.Mock() self.client.meta.events = self.events self.meta = ResourceMeta('dynamodb')