Skip to content

Commit

Permalink
moto is working
Browse files Browse the repository at this point in the history
  • Loading branch information
jottenlips committed Nov 29, 2019
1 parent da4cf1f commit e13412b
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
1 change: 0 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from flask import Flask, request, jsonify
from features.Composers.composer import resolve_composer, update_composer, resolve_composer, create_composer
from features.Songs.song import create_song, resolve_song, update_song

queryTypes = load_schema_from_path("./root_types/queries.gql")
mutationTypes = load_schema_from_path("./root_types/mutations.gql")

Expand Down
9 changes: 6 additions & 3 deletions aws_resources/dynamo.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import boto3
import os
dynamodb = boto3.resource('dynamodb')
table_name = os.environ['TABLE_NAME']
table = dynamodb.Table(table_name)

def table():
dynamodb = boto3.resource('dynamodb')
table_name = os.environ['TABLE_NAME']
table = dynamodb.Table(table_name)
return table

def build_update_expression(items):
keys = items.keys()
Expand Down
14 changes: 6 additions & 8 deletions aws_resources/mock_dynamo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

from moto import mock_dynamodb2
import boto3
from moto import mock_dynamodb
import os

mock_song = {
'id': '1',
Expand All @@ -17,11 +18,12 @@
'name': 'Duke Ellington'
}

@mock_dynamodb
@mock_dynamodb2
def setup_mocks():
dynamodb = boto3.resource('dynamodb')

table = dynamodb.create_table(
TableName='jazz-charts-test',
TableName= os.environ['TABLE_NAME'],
KeySchema=[
{
'AttributeName': 'id',
Expand All @@ -33,13 +35,9 @@ def setup_mocks():
'AttributeName': 'id',
'AttributeType': 'S'
},

],
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
)

print(table)

table.put_item(
Expand Down
8 changes: 4 additions & 4 deletions features/Composers/composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
def resolve_composer(obj, info, id=None):
if (obj and 'composer' in obj):
id = obj['composer']
composer = table.query(
composer = table().query(
KeyConditionExpression=Key('id').eq(id)
)['Items'][0]
return composer

def create_composer(obj, info, composer):
id = str(uuid.uuid4())
composer['id'] = id
table.put_item(Item=composer)
table().put_item(Item=composer)
return {
'composer': composer,
'message': 'success',
Expand All @@ -24,14 +24,14 @@ def create_composer(obj, info, composer):
def update_composer(obj, info, composer):
attributes_to_update = build_update_attributes_dictionary(composer)
update_expression = build_update_expression(composer)
table.update_item(
table().update_item(
Key={
'id': composer['id']
},
UpdateExpression=update_expression,
ExpressionAttributeValues=attributes_to_update,
)
updated_composer = table.query(
updated_composer = table().query(
KeyConditionExpression=Key('id').eq(composer['id'])
)['Items'][0]

Expand Down
8 changes: 4 additions & 4 deletions features/Songs/song.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import uuid

def resolve_song(obj, info, id):
song = table.query(
song = table().query(
KeyConditionExpression=Key('id').eq(id)
)['Items'][0]
return song
Expand All @@ -16,7 +16,7 @@ def resolve_songs(obj, info):
def create_song(obj, info, song):
id = str(uuid.uuid4())
song['id'] = id
table.put_item(Item=song)
table().put_item(Item=song)
return {
'song': song,
'message': 'success',
Expand All @@ -27,14 +27,14 @@ def create_song(obj, info, song):
def update_song(obj, info, song):
attributes_to_update = build_update_attributes_dictionary(song)
update_expression = build_update_expression(song)
table.update_item(
table().update_item(
Key={
'id': song['id']
},
UpdateExpression=update_expression,
ExpressionAttributeValues=attributes_to_update,
)
updated_song = table.query(
updated_song = table().query(
KeyConditionExpression=Key('id').eq(song['id'])
)['Items'][0]

Expand Down
2 changes: 1 addition & 1 deletion features/Songs/tests/test_song.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

def test_resolve_song():
setup_mocks()
song = resolve_song(_, _, "1")
song = resolve_song({}, {}, "1")
print(mock_song)
print(song, '::::song')
assert 1 is 1

0 comments on commit e13412b

Please sign in to comment.