Skip to content

Commit

Permalink
remove resources and fix integration test.
Browse files Browse the repository at this point in the history
  • Loading branch information
cm-wada-yusuke committed Mar 7, 2018
1 parent 6055fa3 commit de27227
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 35 deletions.
6 changes: 4 additions & 2 deletions src/functions/heroes/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import boto3
import datetime
import uuid
import json
from builtins import Exception
import os
from src.functions.heroes.utils import *
Expand All @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions src/functions/heroes/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
27 changes: 1 addition & 26 deletions template_heroes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
HERO_TABLE_NAME: !Ref HeroTableName
21 changes: 14 additions & 7 deletions test/functions/heroes/integration.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
}

0 comments on commit de27227

Please sign in to comment.