def modify_resource(self, modified_resource): ddb_response = self.table.query( KeyConditionExpression=Key(Constants.ddb_field_identifier()).eq( modified_resource[Constants.event_identifier()])) if len(ddb_response[Constants.ddb_response_attribute_name_items()]) == 0: raise ValueError('Resource with identifier ' + modified_resource[Constants.event_identifier()] + ' not found') ddb_response = self.table.put_item(Item=modified_resource) return ddb_response
def test_insert_resource(self): from resource_api.insert_resource.main.RequestHandler import RequestHandler dynamodb = self.setup_mock_database('eu-west-1', 'testing') request_handler = RequestHandler(dynamodb) resource = self.generate_mock_resource() event = generate_mock_event(HttpConstants.http_method_post(), resource) handler_insert_response = request_handler.handler(event, None) resource_dict_from_json = json.loads(event[Constants.event_body()]) resource_inserted = resource_dict_from_json self.assertEqual( handler_insert_response[Constants.response_status_code()], http.HTTPStatus.CREATED, 'HTTP Status code not 201') resource_identifier = resource_inserted[Constants.event_identifier()] query_results = request_handler.get_table_connection().query( KeyConditionExpression=Key( Constants.ddb_field_identifier()).eq(resource_identifier), ScanIndexForward=True) inserted_resource = query_results[ Constants.ddb_response_attribute_name_items()][0] self.assertIsNotNone( inserted_resource[Constants.ddb_field_created_date()], 'Value not persisted as expected') self.assertIsNotNone( inserted_resource[Constants.ddb_field_modified_date()], 'Value not persisted as expected') self.assertIsNotNone( inserted_resource[Constants.ddb_field_entity_description()], 'Value not persisted as expected') self.assertEqual( inserted_resource[Constants.ddb_field_modified_date()], inserted_resource[Constants.ddb_field_created_date()], 'Value not persisted as expected') self.assertEqual( inserted_resource[Constants.ddb_field_entity_description()], resource_inserted['entityDescription'], 'Value not persisted as expected') remove_mock_database(dynamodb)
def __retrieve_resource(self, uuid): _ddb_response = self.table.query(KeyConditionExpression=Key( Constants.ddb_field_identifier()).eq(uuid), ScanIndexForward=False) return _ddb_response