def update(event, context): """Update item in the collection.""" try: # Determine if required env var for region is present. val.check_region() # Determine if required env var for DynamoDB table is present. val.check_dynamodb() # Check for the url {id}. note_id = val.check_id(event) # Determine if required property body is present. val.check_body(event) # Attempt to parse JSON. data = val.check_json(event) # Determine if required properties are present. val.check_props(data) # Set up resource and environment. This is where we keep # *aaS provider resources away from biz logic. region = os.environ["AWS_DEFAULT_REGION"] table = os.environ["DYNAMODB_TABLE"] # Determine which DynamoDB host we need (local/remote)? host = val.check_dynamodb_host() conn = boto3.resource("dynamodb", region, endpoint_url=host) conn_table = conn.Table(table) # Build our model and update. note = NoteModel(conn_table) item = note.update(note_id, data) logger.info("Note updated: {}".format(item)) return respond(200, item) except ex.AwsRegionNotSetException as exc: return respond(500, {"error": str(exc)}) except ex.DynamoDbTableNotSetException as exc: return respond(500, {"error": str(exc)}) except ex.RequestUrlIdNotSetException as exc: return respond(400, {"error": str(exc)}) except ex.RequestBodyNotSetException as exc: return respond(400, {"error": str(exc)}) except ex.RequestBodyNotJsonException as exc: return respond(400, {"error": str(exc)}) except ex.RequiredPropertiesNotSetException as exc: return respond(400, {"error": str(exc)}) except Exception as exc: return respond(500, {"error": str(exc)})
def test_update_returns_valid_structure_when_valid_data(dynamodb_table): model = NoteModel(dynamodb_table) created = model.save(user_id=test_globals["user_id"], notebook=test_globals["notebook"], text=test_globals["text"]) test_globals["note_id"] = created["noteId"] test_globals["user_id"] = created["userId"] test_globals["notebook"] = created["notebook"] test_globals["text"] = created["text"] test_globals["created_at"] = created["createdAt"] test_globals["updated_at"] = created["updatedAt"] data = { "userId": test_globals["user_id"], "notebook": test_globals["notebook"], "text": "Testing if structure is valid updated" } item = model.update(test_globals["note_id"], data) assert "noteId" in item and item["noteId"] == test_globals["note_id"] assert "userId" in item and item["userId"] == test_globals["user_id"] assert "notebook" in item and item["notebook"] == test_globals["notebook"] assert "text" in item and item[ "text"] == "Testing if structure is valid updated" assert "createdAt" in item and item["createdAt"] == test_globals[ "created_at"] assert "updatedAt" in item and item["updatedAt"] > test_globals[ "updated_at"] test_globals["text"] = item["text"] test_globals["updated_at"] = item["updatedAt"]
def test_delete_returns_valid_structure_when_invalid_data(dynamodb_table): model = NoteModel(dynamodb_table) item = model.delete(test_globals["note_id"] + "_badid") assert "noteId" not in item assert "userId" not in item assert "notebook" not in item assert "text" not in item assert "createdAt" not in item assert "updatedAt" not in item
def test_search_by_user_returns_valid_structure_when_valid_data( dynamodb_table): model = NoteModel(dynamodb_table) items = model.search_by_user(test_globals["user_id"]) for item in items: assert "noteId" in item assert "userId" in item and item["userId"] == test_globals["user_id"] assert "notebook" in item assert "text" in item assert "createdAt" not in item assert "updatedAt" not in item
def test_create_returns_valid_structure_when_valid_data(dynamodb_table): model = NoteModel(dynamodb_table) item = model.save(user_id=test_globals["user_id"], notebook=test_globals["notebook"], text=test_globals["text"]) assert "noteId" in item assert "userId" in item and item["userId"] == test_globals["user_id"] assert "notebook" in item and item["notebook"] == test_globals["notebook"] assert "text" in item and item["text"] == test_globals["text"] assert "createdAt" in item and val.is_now(int(item["createdAt"])) assert "updatedAt" in item and val.is_now(int(item["updatedAt"]))
def test_update_returns_valid_structure_when_invalid_data(dynamodb_table): model = NoteModel(dynamodb_table) data = { "user_id": test_globals["user_id"], "notebook": test_globals["notebook"], "text": "Testing if structure is valid updated" } item = model.update(test_globals["note_id"] + "_badid", data) assert "noteId" not in item assert "userId" not in item assert "notebook" not in item assert "text" not in item assert "createdAt" not in item assert "updatedAt" not in item
def search_by_notebook(event, context): """Return the collection of items based on query.""" try: # Determine if required env var for region is present. val.check_region() # Determine if required env var for DynamoDB table is present. val.check_dynamodb() # Check for the url {id}. notebook = val.check_id(event) # Set up resource and environment. This is where we keep # *aaS provider resources away from biz logic. region = os.environ["AWS_DEFAULT_REGION"] table = os.environ["DYNAMODB_TABLE"] # Determine which DynamoDB host we need (local/remote)? host = val.check_dynamodb_host() conn = boto3.resource("dynamodb", region, endpoint_url=host) conn_table = conn.Table(table) # Build our model and read. note = NoteModel(conn_table) items = note.search_by_notebook(notebook) logger.info("Notes for notebook found: {} [{}]".format( notebook, len(items))) return respond(200, items) except ex.AwsRegionNotSetException as exc: return respond(500, {"error": str(exc)}) except ex.DynamoDbTableNotSetException as exc: return respond(500, {"error": str(exc)}) except ex.RequestUrlIdNotSetException as exc: return respond(400, {"error": str(exc)}) except Exception as exc: return respond(500, {"error": str(exc)})
def test_delete_returns_valid_structure_when_valid_data(dynamodb_table): model = NoteModel(dynamodb_table) created = model.save(user_id=test_globals["user_id"], notebook=test_globals["notebook"], text=test_globals["text"]) test_globals["note_id"] = created["noteId"] test_globals["user_id"] = created["userId"] test_globals["notebook"] = created["notebook"] test_globals["text"] = created["text"] test_globals["created_at"] = created["createdAt"] test_globals["updated_at"] = created["updatedAt"] item = model.delete(test_globals["note_id"]) assert "noteId" not in item assert "userId" not in item assert "notebook" not in item assert "text" not in item assert "createdAt" not in item assert "updatedAt" not in item