Added a test that queries an index when created via the table resource update

When you create an index via the table resource update, in python 3.6, it is saved as a `dict_value`,
which causes an error - this test replicates this bug
This commit is contained in:
William Richard 2018-06-06 12:50:29 -04:00
parent 25b5f03ea4
commit 014fbbb8cb
No known key found for this signature in database
GPG Key ID: F7F8BA4DDBE1ABED

View File

@ -1,16 +1,17 @@
from __future__ import unicode_literals, print_function
from decimal import Decimal
import six
import boto
import boto3
from boto3.dynamodb.conditions import Attr
from boto3.dynamodb.conditions import Attr, Key
import sure # noqa
import requests
from moto import mock_dynamodb2, mock_dynamodb2_deprecated
from moto.dynamodb2 import dynamodb_backend2
from boto.exception import JSONResponseError
from botocore.exceptions import ClientError
from boto3.dynamodb.conditions import Key
from tests.helpers import requires_boto_gte
import tests.backport_assert_raises
@ -1199,3 +1200,95 @@ def test_update_if_not_exists():
resp = table.scan()
# Still the original value
assert resp['Items'][0]['created_at'] == 123
@mock_dynamodb2
def test_query_global_secondary_index_when_created_via_update_table_resource():
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
# Create the DynamoDB table.
dynamodb.create_table(
TableName='users',
KeySchema=[
{
'AttributeName': 'user_id',
'KeyType': 'HASH'
},
],
AttributeDefinitions=[
{
'AttributeName': 'user_id',
'AttributeType': 'N',
},
{
'AttributeName': 'forum_name',
'AttributeType': 'S'
},
{
'AttributeName': 'subject',
'AttributeType': 'S'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
},
)
table = dynamodb.Table('users')
table.update(
AttributeDefinitions=[
{
'AttributeName': 'forum_name',
'AttributeType': 'S'
},
],
GlobalSecondaryIndexUpdates=[
{'Create':
{
'IndexName': 'forum_name_index',
'KeySchema': [
{
'AttributeName': 'forum_name',
'KeyType': 'HASH',
},
],
'Projection': {
'ProjectionType': 'ALL',
},
'ProvisionedThroughput': {
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
},
}
}
]
)
next_user_id = 1
for my_forum_name in ['cats', 'dogs']:
for my_subject in ['my pet is the cutest', 'wow look at what my pet did', "don't you love my pet?"]:
table.put_item(Item={'user_id': next_user_id, 'forum_name': my_forum_name, 'subject': my_subject})
next_user_id += 1
# get all the cat users
forum_only_query_response = table.query(
IndexName='forum_name_index',
Select='ALL_ATTRIBUTES',
KeyConditionExpression=Key('forum_name').eq('cats'),
)
forum_only_items = forum_only_query_response['Items']
assert len(forum_only_items) == 3
for item in forum_only_items:
assert item['forum_name'] == 'cats'
# query all cat users with a particular subject
forum_and_subject_query_results = table.query(
IndexName='forum_name_index',
Select='ALL_ATTRIBUTES',
KeyConditionExpression=Key('forum_name').eq('cats'),
FilterExpression=Attr('subject').eq('my pet is the cutest'),
)
forum_and_subject_items = forum_and_subject_query_results['Items']
assert len(forum_and_subject_items) == 1
assert forum_and_subject_items[0] == {'user_id': Decimal('1'), 'forum_name': 'cats',
'subject': 'my pet is the cutest'}