diff --git a/src/functions/heroes/index.py b/src/functions/heroes/index.py index 6d0cd15..61839da 100644 --- a/src/functions/heroes/index.py +++ b/src/functions/heroes/index.py @@ -3,6 +3,7 @@ import boto3 import datetime import uuid +import json from builtins import Exception import os from src.functions.heroes.utils import * @@ -23,15 +24,16 @@ def get(event, context): try: - # get DeviceSerialID hero_id = event['id'] - response = DYNAMODB_TABLE.get_item( + dynamo_response = DYNAMODB_TABLE.get_item( Key={ 'id': hero_id } ) + response = json.dumps(dynamo_response['Item'], cls=DecimalEncoder, ensure_ascii=False) + return response except Exception as error: diff --git a/src/functions/heroes/utils.py b/src/functions/heroes/utils.py index 9555ee0..8665eec 100644 --- a/src/functions/heroes/utils.py +++ b/src/functions/heroes/utils.py @@ -2,7 +2,17 @@ from datetime import datetime import decimal +import json def epoc_by_second_precision(time: datetime): return decimal.Decimal(time.replace(microsecond=0).timestamp()) + +class DecimalEncoder(json.JSONEncoder): + def default(self, o): + if isinstance(o, decimal.Decimal): + if o % 1 > 0: + return float(o) + else: + return int(o) + return super(DecimalEncoder, self).default(o) \ No newline at end of file diff --git a/template_heroes.yaml b/template_heroes.yaml index 16e99ad..f835219 100644 --- a/template_heroes.yaml +++ b/template_heroes.yaml @@ -18,19 +18,6 @@ Parameters: Type: String Default: api_backend/0000.zip Resources: - CM-HeroesTable: - Type: AWS::DynamoDB::Table - Properties: - TableName: !Ref HeroTableName - AttributeDefinitions: - - AttributeName: id - AttributeType: S - KeySchema: - - AttributeName: id - KeyType: HASH - ProvisionedThroughput: - ReadCapacityUnits: 5 - WriteCapacityUnits: 5 GetHeroes: Type: AWS::Serverless::Function Properties: @@ -45,12 +32,6 @@ Resources: Variables: DYNAMODB_ENDPOINT: !Ref DynamoDBEndpoint HERO_TABLE_NAME: !Ref HeroTableName - Events: - GetResource: - Type: Api - Properties: - Path: /heroes/{heroId} - Method: get PutHeroes: FunctionName: heroes-PutHeroes Type: AWS::Serverless::Function @@ -64,10 +45,4 @@ Resources: Environment: Variables: DYNAMODB_ENDPOINT: !Ref DynamoDBEndpoint - HERO_TABLE_NAME: !Ref HeroTableName - Events: - GetResource: - Type: Api - Properties: - Path: /heroes - Method: put \ No newline at end of file + HERO_TABLE_NAME: !Ref HeroTableName \ No newline at end of file diff --git a/test/functions/heroes/integration.bats b/test/functions/heroes/integration.bats index 5afc933..ac8a7d2 100644 --- a/test/functions/heroes/integration.bats +++ b/test/functions/heroes/integration.bats @@ -12,23 +12,30 @@ teardown() { } @test "DynamoDB Get Funciton response the correct item" { - data='{ + data='{ "id": {"S": "test-id"}, "name": {"S": "Test-man"}, "created_at": {"N": "1520400553"}, "updated_at": {"N": "1520400554"}, "office": {"S": "virtual"} }' - expected=`echo "${data}" | jq -r .` + expected=`echo "${data}" | jq -r .` - aws --endpoint-url=http://localhost:4569 dynamodb put-item --table-name CM-Heroes --item "${data}" + aws --endpoint-url=http://localhost:4569 dynamodb put-item --table-name CM-Heroes --item "${data}" - result=`sam local invoke --docker-network ${docker_network_id} -t template_heroes.yaml --event test/functions/heroes/examples/get_payload.json --env-vars environments/sam-local.json GetHeroes | jq -r .Item` + actual=`sam local invoke --docker-network ${docker_network_id} -t template_heroes.yaml --event test/functions/heroes/examples/get_payload.json --env-vars environments/sam-local.json GetHeroes | jq -r .` - [ $result -eq $expected ] + echo $actual + echo "${actual}" | jq .created_at + echo "${expected}" | jq .created_at.N + [ `echo "${actual}" | jq .id` = `echo "${expected}" | jq .id.S` ] + [ `echo "${actual}" | jq .name` = `echo "${expected}" | jq .name.S` ] + [ `echo "${actual}" | jq .created_at` -eq `echo "${expected}" | jq .created_at.N | bc` ] + [ `echo "${actual}" | jq .updated_at` -eq `echo "${expected}" | jq .updated_at.N | bc` ] + [ `echo "${actual}" | jq .office` = `echo "${expected}" | jq .office.S` ] } @test "DynamoDB PUT Funciton response code is 200" { - result=`sam local invoke --docker-network a27c0476cb8e -t template_heroes.yaml --event test/functions/heroes/examples/put_payload.json --env-vars environments/sam-local.json PutHeroes | jq '.ResponseMetadata.HTTPStatusCode'` - [ $result -eq 200 ] + result=`sam local invoke --docker-network a27c0476cb8e -t template_heroes.yaml --event test/functions/heroes/examples/put_payload.json --env-vars environments/sam-local.json PutHeroes | jq '.ResponseMetadata.HTTPStatusCode'` + [ $result -eq 200 ] }