-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_handler.py
More file actions
72 lines (53 loc) · 1.62 KB
/
Copy pathtest_handler.py
File metadata and controls
72 lines (53 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import json
import boto3
from moto import mock_dynamodb
from src.Function.handler import handler, getitem, putitem
@mock_dynamodb
def test_handle():
# Initiate a mock dynamodb
table_name = "mock_db"
boto3.setup_default_session()
table = boto3.client("dynamodb", region_name='us-east-1')
table.create_table(
KeySchema=[
{'AttributeName': 'id', 'KeyType': 'HASH'}
# {'AttributeName': 'visitors', 'KeyType': 'RANGE'}
],
AttributeDefinitions=[
{'AttributeName': 'id', 'AttributeType': 'S'},
# {'AttributeName': 'visitors', 'AttributeType': 'N'},
],
TableName=table_name,
BillingMode="PAY_PER_REQUEST"
)
# Put some mock data in the database
mock_data = {'id':'1', 'visitors': 2}
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
table = dynamodb.Table(table_name)
table.put_item(
Item = mock_data
)
# Test get event
#call getItem
result = getitem(table,'1')
response = {
'headers':
{
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
'body': '2', 'statusCode': 200}
assert response == result
# mock_put_event = {'routeKey': 'PUT /items', 'body': json.dumps({'id': '1', 'visitors': 9})}
result = putitem(table, {'id': '1', 'visitors': 9})
print(result)
data = table.get_item(
Key = {
'id': '1'
},
)
print(data)
assert data['Item'] == {'id': '1', 'visitors': 9}
#Test put event
# @mock_dynamodb
# def test_putitem():