def lambda_handler(event, context): logger.debug('Received event: {}'.format(event)) try: req = json.loads(event['body']) if not validate(req): return respond({ 'code': 400, 'message': 'Bad request. Failed to create the todo.' }) todo = { 'id': str(uuid.uuid4().hex[:8]), 'title': req.get('title'), 'description': req.get('description', ''), 'due_date': req.get('due_date', ''), 'todo_status': TodoStatus.TODO.name } table(TABLE_NAME).put_item(Item=todo, Expected={'primary_key': { 'Exists': False }}) except Exception as e: logging.error(e) return respond({'code': 400, 'message': 'Failed to create the todo.'}) return respond(None, todo)
def test_todo_valid(event_todo_valid): res = table(TABLE_NAME).scan() assert len(res['Items']) == 0 res = create.lambda_handler(event_todo_valid, '') assert res['statusCode'] == 201 res = table(TABLE_NAME).scan() assert len(res['Items']) == 1 todo = res['Items'][0] assert todo['title'] == 'test title' assert todo['description'] == 'test description' assert todo['due_date'] == '2018-06-30T10:00:00Z' assert todo['todo_status'] == TodoStatus.TODO.name
def setup(): try: create_todos_table(TABLE_NAME) table(TABLE_NAME).put_item( Item={ 'id': '1', 'title': 'test', 'description': 'test', 'due_date': '2018-06-30T10:00:00Z', 'todo_status': 'TODO' }) except ClientError: import traceback traceback.print_exc()
def lambda_handler(event, context): logger.debug('Received event: {}'.format(event)) try: req = json.loads(event['body']) if not validate(req): return respond({ 'code': 400, 'message': 'Bad request. Failed to update the todo.' }) res = table(TABLE_NAME).update_item( Key={'id': event['pathParameters']['id']}, UpdateExpression= 'set title=:title, description=:description, due_date=:due_date, todo_status=:todo_status', ExpressionAttributeValues={ ':title': req.get('title'), ':description': req.get('description'), ':due_date': req.get('due_date'), ':todo_status': req.get('todo_status') }) except Exception as e: logging.error(e) return respond({'code': 400, 'message': 'Failed to update the todo.'}) return respond(None, res)
def lambda_handler(event, context): logger.debug('Received event: {}'.format(event)) params = event.get('queryStringParameters') or {} todo_status = params.get('todo_status') if not validate(todo_status): return respond({'code': 400, 'message': 'Bad request parameter.'}) if todo_status: res = table(TABLE_NAME).query( IndexName='todo_status-index', KeyConditionExpression=Key('todo_status').eq(todo_status)) else: res = table(TABLE_NAME).scan() return respond(None, res['Items'])
def test_todo_valid(event_todo_valid): res = update.lambda_handler(event_todo_valid, '') assert res['statusCode'] == 204 res = table(TABLE_NAME).get_item(Key={'id': '1'}) todo = res['Item'] assert todo['title'] == 'test title' assert todo['description'] == 'test description' assert todo['due_date'] == '2018-07-30T10:00:00Z' assert todo['todo_status'] == 'DOING'
def lambda_handler(event, context): logger.debug('Received event: {}'.format(event)) try: id = event['pathParameters']['id'] res = table(TABLE_NAME).get_item(Key={'id': id}) todo = res['Item'] except Exception as e: logging.error(e) return respond({'code': 404, 'message': 'Todo not found.'}) return respond(None, todo)
def lambda_handler(event, context): logger.debug('Received event: {}'.format(event)) try: id = event['pathParameters']['id'] res = table(TABLE_NAME).delete_item(Key={'id': id}, ReturnValues='ALL_OLD') if not res.get('Attributes'): return respond({'code': 404, 'message': 'Todo not found.'}) except Exception as e: logging.error(e) return respond({'code': 400, 'message': 'Failed to delete the todo.'}) return respond(None, res)
def setup(): try: create_todos_table(TABLE_NAME) table(TABLE_NAME).put_item( Item={ 'id': '1', 'title': 'test1', 'description': 'test1', 'due_date': '2018-06-01T10:00:00Z', 'todo_status': 'TODO' }) table(TABLE_NAME).put_item( Item={ 'id': '2', 'title': 'test2', 'description': 'test2', 'due_date': '2018-07-01T10:00:00Z', 'todo_status': 'DOING' }) table(TABLE_NAME).put_item( Item={ 'id': '3', 'title': 'test3', 'description': 'test3', 'due_date': '2018-08-01T10:00:00Z', 'todo_status': 'DOING' }) table(TABLE_NAME).put_item( Item={ 'id': '4', 'title': 'test4', 'description': 'test4', 'due_date': '2018-09-01T10:00:00Z', 'todo_status': 'DONE' }) except ClientError: import traceback traceback.print_exc()
def teardown(): try: table(TABLE_NAME).delete() except ClientError: pass