def test_batch_get_item(self):
        """
        TableConnection.batch_get_item
        """
        items = []
        conn = TableConnection(self.test_table_name)
        for i in range(10):
            items.append({"ForumName": "FooForum", "Subject": "thread-{0}".format(i)})
        with patch(PATCH_METHOD) as req:
            req.return_value = DESCRIBE_TABLE_DATA
            conn.describe_table()

        with patch(PATCH_METHOD) as req:
            req.return_value = {}
            conn.batch_get_item(items)
            params = {
                "ReturnConsumedCapacity": "TOTAL",
                "RequestItems": {
                    self.test_table_name: {
                        "Keys": [
                            {"ForumName": {"S": "FooForum"}, "Subject": {"S": "thread-0"}},
                            {"ForumName": {"S": "FooForum"}, "Subject": {"S": "thread-1"}},
                            {"ForumName": {"S": "FooForum"}, "Subject": {"S": "thread-2"}},
                            {"ForumName": {"S": "FooForum"}, "Subject": {"S": "thread-3"}},
                            {"ForumName": {"S": "FooForum"}, "Subject": {"S": "thread-4"}},
                            {"ForumName": {"S": "FooForum"}, "Subject": {"S": "thread-5"}},
                            {"ForumName": {"S": "FooForum"}, "Subject": {"S": "thread-6"}},
                            {"ForumName": {"S": "FooForum"}, "Subject": {"S": "thread-7"}},
                            {"ForumName": {"S": "FooForum"}, "Subject": {"S": "thread-8"}},
                            {"ForumName": {"S": "FooForum"}, "Subject": {"S": "thread-9"}},
                        ]
                    }
                },
            }
            self.assertEqual(req.call_args[0][1], params)
Exemple #2
0
    def test_query(self):
        """
        TableConnection.query
        """
        conn = TableConnection(self.test_table_name)
        with patch(PATCH_METHOD) as req:
            req.return_value = DESCRIBE_TABLE_DATA
            conn.describe_table()

        with patch(PATCH_METHOD) as req:
            req.return_value = {}
            conn.query(
                "FooForum",
                Path('Subject').startswith('thread')
            )
            params = {
                'ReturnConsumedCapacity': 'TOTAL',
                'KeyConditionExpression': '(#0 = :0 AND begins_with (#1, :1))',
                'ExpressionAttributeNames': {
                    '#0': 'ForumName',
                    '#1': 'Subject'
                },
                'ExpressionAttributeValues': {
                    ':0': {
                        'S': 'FooForum'
                    },
                    ':1': {
                        'S': 'thread'
                    }
                },
                'TableName': self.test_table_name
            }
            self.assertEqual(req.call_args[0][1], params)
Exemple #3
0
    def test_delete_item(self):
        """
        TableConnection.delete_item
        """
        conn = TableConnection(self.test_table_name)
        with patch(PATCH_METHOD) as req:
            req.return_value = DESCRIBE_TABLE_DATA
            conn.describe_table()

        with patch(PATCH_METHOD) as req:
            req.return_value = {}
            conn.delete_item("Amazon DynamoDB",
                             "How do I update multiple items?")
            params = {
                'ReturnConsumedCapacity': 'TOTAL',
                'Key': {
                    'ForumName': {
                        'S': 'Amazon DynamoDB'
                    },
                    'Subject': {
                        'S': 'How do I update multiple items?'
                    }
                },
                'TableName': self.test_table_name
            }
            self.assertEqual(req.call_args[0][1], params)
    def test_delete_item(self):
        """
        TableConnection.delete_item
        """
        conn = TableConnection(self.test_table_name)
        with patch(PATCH_METHOD) as req:
            req.return_value = DESCRIBE_TABLE_DATA
            conn.describe_table()

        with patch(PATCH_METHOD) as req:
            req.return_value = {}
            conn.delete_item(
                "Amazon DynamoDB",
                "How do I update multiple items?")
            params = {
                'ReturnConsumedCapacity': 'TOTAL',
                'Key': {
                    'ForumName': {
                        'S': 'Amazon DynamoDB'
                    },
                    'Subject': {
                        'S': 'How do I update multiple items?'
                    }
                },
                'TableName': self.test_table_name
            }
            self.assertEqual(req.call_args[0][1], params)
Exemple #5
0
 def test_query(self):
     """
     TableConnection.query
     """
     conn = TableConnection(self.test_table_name)
     with patch(PATCH_METHOD) as req:
         req.return_value = HttpOK(), DESCRIBE_TABLE_DATA
         conn.describe_table()
     with patch(PATCH_METHOD) as req:
         req.return_value = HttpOK(), {}
         conn.query("FooForum",
                    key_conditions={
                        'ForumName': {
                            'ComparisonOperator': 'BEGINS_WITH',
                            'AttributeValueList': ['thread']
                        }
                    })
         params = {
             'return_consumed_capacity': 'TOTAL',
             'key_conditions': {
                 'ForumName': {
                     'ComparisonOperator': 'BEGINS_WITH',
                     'AttributeValueList': [{
                         'S': 'thread'
                     }]
                 }
             },
             'table_name': self.test_table_name
         }
         self.assertEqual(req.call_args[1], params)
 def test_query(self):
     """
     TableConnection.query
     """
     conn = TableConnection(self.test_table_name)
     with patch(PATCH_METHOD) as req:
         req.return_value = HttpOK(), DESCRIBE_TABLE_DATA
         conn.describe_table()
     with patch(PATCH_METHOD) as req:
         req.return_value = HttpOK(), {}
         conn.query(
             "FooForum",
             key_conditions={'ForumName': {'ComparisonOperator': 'BEGINS_WITH', 'AttributeValueList': ['thread']}},
             exclusive_start_key="test_start_key"
         )
         params = {
             'return_consumed_capacity': 'TOTAL',
             'key_conditions': {
                 'ForumName': {
                     'ComparisonOperator': 'BEGINS_WITH', 'AttributeValueList': [{
                         'S': 'thread'
                     }]
                 }
             },
             'table_name': self.test_table_name,
             'exclusive_start_key': {'ForumName': {'S': 'test_start_key'}}
         }
         self.assertEqual(req.call_args[1], params)
 def test_query(self):
     """
     TableConnection.query
     """
     conn = TableConnection(self.test_table_name)
     with patch(PATCH_METHOD) as req:
         req.return_value = DESCRIBE_TABLE_DATA
         conn.describe_table()
     with patch(PATCH_METHOD) as req:
         req.return_value = {}
         conn.query(
             "FooForum",
             key_conditions={'ForumName': {'ComparisonOperator': 'BEGINS_WITH', 'AttributeValueList': ['thread']}}
         )
         params = {
             'ReturnConsumedCapacity': 'TOTAL',
             'KeyConditions': {
                 'ForumName': {
                     'ComparisonOperator': 'BEGINS_WITH', 'AttributeValueList': [{
                         'S': 'thread'
                     }]
                 }
             },
             'TableName': self.test_table_name
         }
         self.assertEqual(req.call_args[0][1], params)
    def test_query(self):
        """
        TableConnection.query
        """
        conn = TableConnection(self.test_table_name)
        with patch(PATCH_METHOD) as req:
            req.return_value = DESCRIBE_TABLE_DATA
            conn.describe_table()

        with patch(PATCH_METHOD) as req:
            req.return_value = {}
            conn.query(
                "FooForum",
                Path('Subject').startswith('thread')
            )
            params = {
                'ReturnConsumedCapacity': 'TOTAL',
                'KeyConditionExpression': '(#0 = :0 AND begins_with (#1, :1))',
                'ExpressionAttributeNames': {
                    '#0': 'ForumName',
                    '#1': 'Subject'
                },
                'ExpressionAttributeValues': {
                    ':0': {
                        'S': 'FooForum'
                    },
                    ':1': {
                        'S': 'thread'
                    }
                },
                'TableName': self.test_table_name
            }
            self.assertEqual(req.call_args[0][1], params)

        with patch(PATCH_METHOD) as req:
            req.return_value = {}
            conn.query(
                "FooForum",
                key_conditions={'Subject': {'ComparisonOperator': 'BEGINS_WITH', 'AttributeValueList': ['thread']}}
            )
            params = {
                'ReturnConsumedCapacity': 'TOTAL',
                'KeyConditionExpression': '(#0 = :0 AND begins_with (#1, :1))',
                'ExpressionAttributeNames': {
                    '#0': 'ForumName',
                    '#1': 'Subject'
                },
                'ExpressionAttributeValues': {
                    ':0': {
                        'S': 'FooForum'
                    },
                    ':1': {
                        'S': 'thread'
                    }
                },
                'TableName': self.test_table_name
            }
            self.assertEqual(req.call_args[0][1], params)
 def test_describe_table(self):
     """
     TableConnection.describe_table
     """
     with patch(PATCH_METHOD) as req:
         req.return_value = DESCRIBE_TABLE_DATA
         conn = TableConnection(self.test_table_name)
         conn.describe_table()
         self.assertEqual(conn.table_name, self.test_table_name)
         self.assertEqual(req.call_args[0][1], {"TableName": "ci-table"})
Exemple #10
0
 def test_describe_table(self):
     """
     TableConnection.describe_table
     """
     with patch(PATCH_METHOD) as req:
         req.return_value = DESCRIBE_TABLE_DATA
         conn = TableConnection(self.test_table_name)
         conn.describe_table()
         self.assertEqual(conn.table_name, self.test_table_name)
         self.assertEqual(req.call_args[0][1], {'TableName': 'ci-table'})
 def test_scan(self):
     """
     TableConnection.scan
     """
     conn = TableConnection(self.test_table_name)
     with patch(PATCH_METHOD) as req:
         req.return_value = DESCRIBE_TABLE_DATA
         conn.describe_table()
     with patch(PATCH_METHOD) as req:
         req.return_value = HttpOK(), {}
         conn.scan()
         params = {"ReturnConsumedCapacity": "TOTAL", "TableName": self.test_table_name}
         self.assertEqual(req.call_args[0][1], params)
Exemple #12
0
    def test_get_item(self):
        """
        TableConnection.get_item
        """
        conn = TableConnection(self.test_table_name)
        with patch(PATCH_METHOD) as req:
            req.return_value = DESCRIBE_TABLE_DATA
            conn.describe_table()

        with patch(PATCH_METHOD) as req:
            req.return_value = GET_ITEM_DATA
            item = conn.get_item("Amazon DynamoDB", "How do I update multiple items?")
            self.assertEqual(item, GET_ITEM_DATA)
    def test_get_item(self):
        """
        TableConnection.get_item
        """
        conn = TableConnection(self.test_table_name)
        with patch(PATCH_METHOD) as req:
            req.return_value = DESCRIBE_TABLE_DATA
            conn.describe_table()

        with patch(PATCH_METHOD) as req:
            req.return_value = GET_ITEM_DATA
            item = conn.get_item("Amazon DynamoDB", "How do I update multiple items?")
            self.assertEqual(item, GET_ITEM_DATA)
 def test_scan(self):
     """
     TableConnection.scan
     """
     conn = TableConnection(self.test_table_name)
     with patch(PATCH_METHOD) as req:
         req.return_value = HttpOK(), DESCRIBE_TABLE_DATA
         conn.describe_table()
     with patch(PATCH_METHOD) as req:
         req.return_value = HttpOK(), {}
         conn.scan()
         params = {
             'return_consumed_capacity': 'TOTAL',
             'table_name': self.test_table_name
         }
         self.assertEqual(req.call_args[1], params)
Exemple #15
0
 def test_scan(self):
     """
     TableConnection.scan
     """
     conn = TableConnection(self.test_table_name)
     with patch(PATCH_METHOD) as req:
         req.return_value = DESCRIBE_TABLE_DATA
         conn.describe_table()
     with patch(PATCH_METHOD) as req:
         req.return_value = HttpOK(), {}
         conn.scan()
         params = {
             'ReturnConsumedCapacity': 'TOTAL',
             'TableName': self.test_table_name
         }
         self.assertEqual(req.call_args[0][1], params)
    def test_put_item(self):
        """
        TableConnection.put_item
        """
        conn = TableConnection(self.test_table_name)
        with patch(PATCH_METHOD) as req:
            req.return_value = DESCRIBE_TABLE_DATA
            conn.describe_table()

        with patch(PATCH_METHOD) as req:
            req.return_value = {}
            conn.put_item("foo-key", range_key="foo-range-key", attributes={"ForumName": "foo-value"})
            params = {
                "ReturnConsumedCapacity": "TOTAL",
                "TableName": self.test_table_name,
                "Item": {"ForumName": {"S": "foo-value"}, "Subject": {"S": "foo-range-key"}},
            }
            self.assertEqual(req.call_args[0][1], params)

        with patch(PATCH_METHOD) as req:
            req.return_value = {}
            conn.put_item("foo-key", range_key="foo-range-key", attributes={"ForumName": "foo-value"})
            params = {
                "ReturnConsumedCapacity": "TOTAL",
                "Item": {"ForumName": {"S": "foo-value"}, "Subject": {"S": "foo-range-key"}},
                "TableName": self.test_table_name,
            }
            self.assertEqual(req.call_args[0][1], params)

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), {}
            conn.put_item(
                "foo-key",
                range_key="foo-range-key",
                attributes={"ForumName": "foo-value"},
                conditional_operator="and",
                expected={"ForumName": {"Exists": False}},
            )
            params = {
                "ReturnConsumedCapacity": "TOTAL",
                "Item": {"ForumName": {"S": "foo-value"}, "Subject": {"S": "foo-range-key"}},
                "TableName": self.test_table_name,
                "ConditionalOperator": "AND",
                "Expected": {"ForumName": {"Exists": False}},
            }
            self.assertEqual(req.call_args[0][1], params)
    def test_update_item(self):
        """
        TableConnection.update_item
        """
        conn = TableConnection(self.test_table_name)
        with patch(PATCH_METHOD) as req:
            req.return_value = DESCRIBE_TABLE_DATA
            conn.describe_table()

        attr_updates = {
            'Subject': {
                'Value': 'foo-subject',
                'Action': 'PUT'
            },
        }

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), {}
            conn.update_item(
                'foo-key',
                attribute_updates=attr_updates,
                range_key='foo-range-key',
            )
            params = {
                'Key': {
                    'ForumName': {
                        'S': 'foo-key'
                    },
                    'Subject': {
                        'S': 'foo-range-key'
                    }
                },
                'UpdateExpression': 'SET #0 = :0',
                'ExpressionAttributeNames': {
                    '#0': 'Subject'
                },
                'ExpressionAttributeValues': {
                    ':0': {
                        'S': 'foo-subject'
                    }
                },
                'ReturnConsumedCapacity': 'TOTAL',
                'TableName': 'ci-table'
            }
            self.assertEqual(req.call_args[0][1], params)
Exemple #18
0
    def test_update_item(self):
        """
        TableConnection.update_item
        """
        conn = TableConnection(self.test_table_name)
        with patch(PATCH_METHOD) as req:
            req.return_value = DESCRIBE_TABLE_DATA
            conn.describe_table()

        attr_updates = {
            'Subject': {
                'Value': 'foo-subject',
                'Action': 'PUT'
            },
        }

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), {}
            conn.update_item(
                'foo-key',
                actions=[Path('Subject').set('foo-subject')],
                range_key='foo-range-key',
            )
            params = {
                'Key': {
                    'ForumName': {
                        'S': 'foo-key'
                    },
                    'Subject': {
                        'S': 'foo-range-key'
                    }
                },
                'UpdateExpression': 'SET #0 = :0',
                'ExpressionAttributeNames': {
                    '#0': 'Subject'
                },
                'ExpressionAttributeValues': {
                    ':0': {
                        'S': 'foo-subject'
                    }
                },
                'ReturnConsumedCapacity': 'TOTAL',
                'TableName': 'ci-table'
            }
            self.assertEqual(req.call_args[0][1], params)
    def test_delete_item(self):
        """
        TableConnection.delete_item
        """
        conn = TableConnection(self.test_table_name)
        with patch(PATCH_METHOD) as req:
            req.return_value = DESCRIBE_TABLE_DATA
            conn.describe_table()

        with patch(PATCH_METHOD) as req:
            req.return_value = {}
            conn.delete_item("Amazon DynamoDB", "How do I update multiple items?")
            params = {
                "ReturnConsumedCapacity": "TOTAL",
                "Key": {"ForumName": {"S": "Amazon DynamoDB"}, "Subject": {"S": "How do I update multiple items?"}},
                "TableName": self.test_table_name,
            }
            self.assertEqual(req.call_args[0][1], params)
Exemple #20
0
    def test_update_item(self):
        """
        TableConnection.delete_item
        """
        conn = TableConnection(self.test_table_name)
        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), DESCRIBE_TABLE_DATA
            conn.describe_table()

        attr_updates = {
            'Subject': {
                'Value': 'foo-subject',
                'Action': 'PUT'
            },
        }

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), {}
            conn.update_item(
                'foo-key',
                attribute_updates=attr_updates,
                range_key='foo-range-key',
            )
            params = {
                'key': {
                    'ForumName': {
                        'S': 'foo-key'
                    },
                    'Subject': {
                        'S': 'foo-range-key'
                    }
                },
                'attribute_updates': {
                    'Subject': {
                        'Value': {
                            'S': 'foo-subject'
                        },
                        'Action': 'PUT'
                    }
                },
                'return_consumed_capacity': 'TOTAL',
                'table_name': 'ci-table'
            }
            self.assertEqual(req.call_args[1], params)
    def test_put_item(self):
        """
        TableConnection.put_item
        """
        conn = TableConnection(self.test_table_name)
        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), DESCRIBE_TABLE_DATA
            conn.describe_table()

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), {}
            conn.put_item(
                'foo-key',
                range_key='foo-range-key',
                attributes={'ForumName': 'foo-value'}
            )
            params = {
                'return_consumed_capacity': 'TOTAL',
                'table_name': self.test_table_name,
                'item': {'ForumName': {'S': 'foo-value'}, 'Subject': {'S': 'foo-range-key'}}
            }
            self.assertEqual(req.call_args[1], params)

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), {}
            conn.put_item(
                'foo-key',
                range_key='foo-range-key',
                attributes={'ForumName': 'foo-value'}
            )
            params = {
                'return_consumed_capacity': 'TOTAL',
                'item': {
                    'ForumName': {
                        'S': 'foo-value'
                    },
                    'Subject': {
                        'S': 'foo-range-key'
                    }
                },
                'table_name': self.test_table_name
            }
            self.assertEqual(req.call_args[1], params)
    def test_update_item(self):
        """
        TableConnection.delete_item
        """
        conn = TableConnection(self.test_table_name)
        with patch(PATCH_METHOD) as req:
            req.return_value = DESCRIBE_TABLE_DATA
            conn.describe_table()

        attr_updates = {"Subject": {"Value": "foo-subject", "Action": "PUT"}}

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), {}
            conn.update_item("foo-key", attribute_updates=attr_updates, range_key="foo-range-key")
            params = {
                "Key": {"ForumName": {"S": "foo-key"}, "Subject": {"S": "foo-range-key"}},
                "AttributeUpdates": {"Subject": {"Value": {"S": "foo-subject"}, "Action": "PUT"}},
                "ReturnConsumedCapacity": "TOTAL",
                "TableName": "ci-table",
            }
            self.assertEqual(req.call_args[0][1], params)
 def test_query(self):
     """
     TableConnection.query
     """
     conn = TableConnection(self.test_table_name)
     with patch(PATCH_METHOD) as req:
         req.return_value = DESCRIBE_TABLE_DATA
         conn.describe_table()
     with patch(PATCH_METHOD) as req:
         req.return_value = {}
         conn.query(
             "FooForum",
             key_conditions={"ForumName": {"ComparisonOperator": "BEGINS_WITH", "AttributeValueList": ["thread"]}},
         )
         params = {
             "ReturnConsumedCapacity": "TOTAL",
             "KeyConditions": {
                 "ForumName": {"ComparisonOperator": "BEGINS_WITH", "AttributeValueList": [{"S": "thread"}]}
             },
             "TableName": self.test_table_name,
         }
         self.assertEqual(req.call_args[0][1], params)
    def test_batch_get_item(self):
        """
        TableConnection.batch_get_item
        """
        items = []
        conn = TableConnection(self.test_table_name)
        for i in range(10):
            items.append(
                {"ForumName": "FooForum", "Subject": "thread-{0}".format(i)}
            )
        with patch(PATCH_METHOD) as req:
            req.return_value = DESCRIBE_TABLE_DATA
            conn.describe_table()

        with patch(PATCH_METHOD) as req:
            req.return_value = {}
            conn.batch_get_item(
                items
            )
            params = {
                'ReturnConsumedCapacity': 'TOTAL',
                'RequestItems': {
                    self.test_table_name: {
                        'Keys': [
                            {'ForumName': {'S': 'FooForum'}, 'Subject': {'S': 'thread-0'}},
                            {'ForumName': {'S': 'FooForum'}, 'Subject': {'S': 'thread-1'}},
                            {'ForumName': {'S': 'FooForum'}, 'Subject': {'S': 'thread-2'}},
                            {'ForumName': {'S': 'FooForum'}, 'Subject': {'S': 'thread-3'}},
                            {'ForumName': {'S': 'FooForum'}, 'Subject': {'S': 'thread-4'}},
                            {'ForumName': {'S': 'FooForum'}, 'Subject': {'S': 'thread-5'}},
                            {'ForumName': {'S': 'FooForum'}, 'Subject': {'S': 'thread-6'}},
                            {'ForumName': {'S': 'FooForum'}, 'Subject': {'S': 'thread-7'}},
                            {'ForumName': {'S': 'FooForum'}, 'Subject': {'S': 'thread-8'}},
                            {'ForumName': {'S': 'FooForum'}, 'Subject': {'S': 'thread-9'}}
                        ]
                    }
                }
            }
            self.assertEqual(req.call_args[0][1], params)
Exemple #25
0
    def test_batch_get_item(self):
        """
        TableConnection.batch_get_item
        """
        items = []
        conn = TableConnection(self.test_table_name)
        for i in range(10):
            items.append(
                {"ForumName": "FooForum", "Subject": f"thread-{i}"}
            )
        with patch(PATCH_METHOD) as req:
            req.return_value = DESCRIBE_TABLE_DATA
            conn.describe_table()

        with patch(PATCH_METHOD) as req:
            req.return_value = {}
            conn.batch_get_item(
                items
            )
            params = {
                'ReturnConsumedCapacity': 'TOTAL',
                'RequestItems': {
                    self.test_table_name: {
                        'Keys': [
                            {'ForumName': {'S': 'FooForum'}, 'Subject': {'S': 'thread-0'}},
                            {'ForumName': {'S': 'FooForum'}, 'Subject': {'S': 'thread-1'}},
                            {'ForumName': {'S': 'FooForum'}, 'Subject': {'S': 'thread-2'}},
                            {'ForumName': {'S': 'FooForum'}, 'Subject': {'S': 'thread-3'}},
                            {'ForumName': {'S': 'FooForum'}, 'Subject': {'S': 'thread-4'}},
                            {'ForumName': {'S': 'FooForum'}, 'Subject': {'S': 'thread-5'}},
                            {'ForumName': {'S': 'FooForum'}, 'Subject': {'S': 'thread-6'}},
                            {'ForumName': {'S': 'FooForum'}, 'Subject': {'S': 'thread-7'}},
                            {'ForumName': {'S': 'FooForum'}, 'Subject': {'S': 'thread-8'}},
                            {'ForumName': {'S': 'FooForum'}, 'Subject': {'S': 'thread-9'}}
                        ]
                    }
                }
            }
            self.assertEqual(req.call_args[0][1], params)
Exemple #26
0
def test_table_integration(ddb_url):
    table_name = 'pynamodb-ci-table'

    # For use with a fake dynamodb connection
    # See: http://aws.amazon.com/dynamodb/developer-resources/
    conn = TableConnection(table_name, host=ddb_url)
    print(conn)

    print("conn.describe_table...")
    table = None
    try:
        table = conn.describe_table()
    except TableDoesNotExist:
        params = {
            'read_capacity_units':
            1,
            'write_capacity_units':
            1,
            'attribute_definitions': [{
                'attribute_type': STRING,
                'attribute_name': 'Forum'
            }, {
                'attribute_type': STRING,
                'attribute_name': 'Thread'
            }, {
                'attribute_type': STRING,
                'attribute_name': 'AltKey'
            }, {
                'attribute_type': NUMBER,
                'attribute_name': 'number'
            }],
            'key_schema': [{
                'key_type': HASH,
                'attribute_name': 'Forum'
            }, {
                'key_type': RANGE,
                'attribute_name': 'Thread'
            }],
            'global_secondary_indexes': [{
                'index_name':
                'alt-index',
                'key_schema': [{
                    'KeyType': 'HASH',
                    'AttributeName': 'AltKey'
                }],
                'projection': {
                    'ProjectionType': 'KEYS_ONLY'
                },
                'provisioned_throughput': {
                    'ReadCapacityUnits': 1,
                    'WriteCapacityUnits': 1,
                }
            }],
            'local_secondary_indexes': [{
                'index_name':
                'view-index',
                'key_schema': [{
                    'KeyType': 'HASH',
                    'AttributeName': 'Forum'
                }, {
                    'KeyType': 'RANGE',
                    'AttributeName': 'AltKey'
                }],
                'projection': {
                    'ProjectionType': 'KEYS_ONLY'
                }
            }]
        }
        print("conn.create_table...")
        conn.create_table(**params)

    while table is None:
        time.sleep(2)
        table = conn.describe_table()
    while table['TableStatus'] == 'CREATING':
        time.sleep(5)
        print(table['TableStatus'])
        table = conn.describe_table()
    print("conn.update_table...")

    conn.update_table(read_capacity_units=table.get(
        PROVISIONED_THROUGHPUT).get(READ_CAPACITY_UNITS) + 1,
                      write_capacity_units=2)

    table = conn.describe_table()
    while table['TableStatus'] != 'ACTIVE':
        time.sleep(2)
        table = conn.describe_table()

    print("conn.put_item")
    conn.put_item(
        'item1-hash',
        range_key='item1-range',
        attributes={'foo': {
            'S': 'bar'
        }},
        condition=NotExists(Path('Forum')),
    )
    conn.get_item('item1-hash', range_key='item1-range')
    conn.delete_item('item1-hash', range_key='item1-range')

    items = []
    for i in range(10):
        items.append({"Forum": "FooForum", "Thread": "thread-{}".format(i)})
    print("conn.batch_write_items...")
    conn.batch_write_item(put_items=items)
    print("conn.batch_get_items...")
    data = conn.batch_get_item(items)
    print("conn.query...")
    conn.query(
        "FooForum",
        range_key_condition=(BeginsWith(Path('Thread'), Value('thread'))),
    )
    print("conn.scan...")
    conn.scan()
    print("conn.delete_table...")
    conn.delete_table()
Exemple #27
0
"""
import time
import config as cfg
from pynamodb.constants import PROVISIONED_THROUGHPUT, READ_CAPACITY_UNITS
from pynamodb.connection import TableConnection
from pynamodb.types import STRING, HASH, RANGE, NUMBER

table_name = 'pynamodb-ci'

# For use with a fake dynamodb connection
# See: http://aws.amazon.com/dynamodb/developer-resources/
conn = TableConnection('pynamodb', host=cfg.DYNAMODB_HOST)
print(conn)

print("conn.describe_table...")
table = conn.describe_table()
if table is None:
    params = {
        'read_capacity_units': 1,
        'write_capacity_units': 1,
        'attribute_definitions': [
            {
                'attribute_type': STRING,
                'attribute_name': 'Forum'
            },
            {
                'attribute_type': STRING,
                'attribute_name': 'Thread'
            },
            {
                'attribute_type': STRING,
Exemple #28
0
"""
import time
import config as cfg
from pynamodb.constants import PROVISIONED_THROUGHPUT, READ_CAPACITY_UNITS
from pynamodb.connection import TableConnection
from pynamodb.types import STRING, HASH, RANGE, NUMBER

table_name = 'pynamodb-ci'

# For use with a fake dynamodb connection
# See: http://aws.amazon.com/dynamodb/developer-resources/
conn = TableConnection('pynamodb', host=cfg.DYNAMODB_HOST)
print(conn)

print("conn.describe_table...")
table = conn.describe_table()
if table is None:
    params = {
        'read_capacity_units':
        1,
        'write_capacity_units':
        1,
        'attribute_definitions': [{
            'attribute_type': STRING,
            'attribute_name': 'Forum'
        }, {
            'attribute_type': STRING,
            'attribute_name': 'Thread'
        }, {
            'attribute_type': STRING,
            'attribute_name': 'AltKey'
Exemple #29
0
    def test_put_item(self):
        """
        TableConnection.put_item
        """
        conn = TableConnection(self.test_table_name)
        with patch(PATCH_METHOD) as req:
            req.return_value = DESCRIBE_TABLE_DATA
            conn.describe_table()

        with patch(PATCH_METHOD) as req:
            req.return_value = {}
            conn.put_item('foo-key',
                          range_key='foo-range-key',
                          attributes={'ForumName': 'foo-value'})
            params = {
                'ReturnConsumedCapacity': 'TOTAL',
                'TableName': self.test_table_name,
                'Item': {
                    'ForumName': {
                        'S': 'foo-value'
                    },
                    'Subject': {
                        'S': 'foo-range-key'
                    }
                }
            }
            self.assertEqual(req.call_args[0][1], params)

        with patch(PATCH_METHOD) as req:
            req.return_value = {}
            conn.put_item('foo-key',
                          range_key='foo-range-key',
                          attributes={'ForumName': 'foo-value'})
            params = {
                'ReturnConsumedCapacity': 'TOTAL',
                'Item': {
                    'ForumName': {
                        'S': 'foo-value'
                    },
                    'Subject': {
                        'S': 'foo-range-key'
                    }
                },
                'TableName': self.test_table_name
            }
            self.assertEqual(req.call_args[0][1], params)

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), {}
            conn.put_item('foo-key',
                          range_key='foo-range-key',
                          attributes={'ForumName': 'foo-value'},
                          condition=Path('ForumName').does_not_exist())
            params = {
                'ReturnConsumedCapacity': 'TOTAL',
                'Item': {
                    'ForumName': {
                        'S': 'foo-value'
                    },
                    'Subject': {
                        'S': 'foo-range-key'
                    }
                },
                'TableName': self.test_table_name,
                'ConditionExpression': 'attribute_not_exists (#0)',
                'ExpressionAttributeNames': {
                    '#0': 'ForumName'
                }
            }
            self.assertEqual(req.call_args[0][1], params)

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), {}
            conn.put_item('foo-key',
                          range_key='foo-range-key',
                          attributes={'ForumName': 'foo-value'},
                          conditional_operator='and',
                          expected={'ForumName': {
                              'Exists': False
                          }})
            params = {
                'ReturnConsumedCapacity': 'TOTAL',
                'Item': {
                    'ForumName': {
                        'S': 'foo-value'
                    },
                    'Subject': {
                        'S': 'foo-range-key'
                    }
                },
                'TableName': self.test_table_name,
                'ConditionExpression': 'attribute_not_exists (#0)',
                'ExpressionAttributeNames': {
                    '#0': 'ForumName'
                }
            }
            self.assertEqual(req.call_args[0][1], params)
"""
Example use of the TableConnection API
"""
from pynamodb.connection import TableConnection

# Get a table connection
table = TableConnection('table-name', host='http://localhost')

# If the table doesn't already exist, the rest of this example will not work.

# Describe the table
print(table.describe_table())

# Get an item
print(table.get_item('hash-key', 'range-key'))

# Put an item
table.put_item('hash-key', 'range-key', attributes={'name': 'value'})

# Delete an item
table.delete_item('hash-key', 'range-key')
Exemple #31
0
"""
Example use of the TableConnection API
"""
from pynamodb.connection import TableConnection

# Get a table connection
table = TableConnection('Thread', host='http://localhost:8000')

# If the table doesn't already exist, the rest of this example will not work.

# Describe the table
print(table.describe_table())

# Get an item
print(table.get_item('hash-key', 'range-key'))

# Put an item
table.put_item('hash-key', 'range-key', attributes={'forum_name': 'value'})

# Delete an item
table.delete_item('hash-key', 'range-key')
    def test_put_item(self):
        """
        TableConnection.put_item
        """
        conn = TableConnection(self.test_table_name)
        with patch(PATCH_METHOD) as req:
            req.return_value = DESCRIBE_TABLE_DATA
            conn.describe_table()

        with patch(PATCH_METHOD) as req:
            req.return_value = {}
            conn.put_item(
                'foo-key',
                range_key='foo-range-key',
                attributes={'ForumName': 'foo-value'}
            )
            params = {
                'ReturnConsumedCapacity': 'TOTAL',
                'TableName': self.test_table_name,
                'Item': {'ForumName': {'S': 'foo-value'}, 'Subject': {'S': 'foo-range-key'}}
            }
            self.assertEqual(req.call_args[0][1], params)

        with patch(PATCH_METHOD) as req:
            req.return_value = {}
            conn.put_item(
                'foo-key',
                range_key='foo-range-key',
                attributes={'ForumName': 'foo-value'}
            )
            params = {
                'ReturnConsumedCapacity': 'TOTAL',
                'Item': {
                    'ForumName': {
                        'S': 'foo-value'
                    },
                    'Subject': {
                        'S': 'foo-range-key'
                    }
                },
                'TableName': self.test_table_name
            }
            self.assertEqual(req.call_args[0][1], params)

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), {}
            conn.put_item(
                'foo-key',
                range_key='foo-range-key',
                attributes={'ForumName': 'foo-value'},
                conditional_operator='and',
                expected={
                    'ForumName': {
                        'Exists': False
                    }
                }
            )
            params = {
                'ReturnConsumedCapacity': 'TOTAL',
                'Item': {
                    'ForumName': {
                        'S': 'foo-value'
                    },
                    'Subject': {
                        'S': 'foo-range-key'
                    }
                },
                'TableName': self.test_table_name,
                'ConditionalOperator': 'AND',
                'Expected': {
                    'ForumName': {
                        'Exists': False
                    }
                }
            }
            self.assertEqual(req.call_args[0][1], params)
Exemple #33
0
    def test_put_item(self):
        """
        TableConnection.put_item
        """
        conn = TableConnection(self.test_table_name)
        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), DESCRIBE_TABLE_DATA
            conn.describe_table()

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), {}
            conn.put_item('foo-key',
                          range_key='foo-range-key',
                          attributes={'ForumName': 'foo-value'})
            params = {
                'return_consumed_capacity': 'TOTAL',
                'table_name': self.test_table_name,
                'item': {
                    'ForumName': {
                        'S': 'foo-value'
                    },
                    'Subject': {
                        'S': 'foo-range-key'
                    }
                }
            }
            self.assertEqual(req.call_args[1], params)

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), {}
            conn.put_item('foo-key',
                          range_key='foo-range-key',
                          attributes={'ForumName': 'foo-value'})
            params = {
                'return_consumed_capacity': 'TOTAL',
                'item': {
                    'ForumName': {
                        'S': 'foo-value'
                    },
                    'Subject': {
                        'S': 'foo-range-key'
                    }
                },
                'table_name': self.test_table_name
            }
            self.assertEqual(req.call_args[1], params)

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), {}
            conn.put_item('foo-key',
                          range_key='foo-range-key',
                          attributes={'ForumName': 'foo-value'},
                          conditional_operator='and',
                          expected={'ForumName': {
                              'Exists': False
                          }})
            params = {
                'return_consumed_capacity': 'TOTAL',
                'item': {
                    'ForumName': {
                        'S': 'foo-value'
                    },
                    'Subject': {
                        'S': 'foo-range-key'
                    }
                },
                'table_name': self.test_table_name,
                'conditional_operator': 'AND',
                'expected': {
                    'ForumName': {
                        'Exists': False
                    }
                }
            }
            self.assertEqual(req.call_args[1], params)
Exemple #34
0
 def test_batch_write_item(self):
     """
     TableConnection.batch_write_item
     """
     items = []
     conn = TableConnection(self.test_table_name)
     for i in range(10):
         items.append({
             "ForumName": "FooForum",
             "Subject": "thread-{0}".format(i)
         })
     with patch(PATCH_METHOD) as req:
         req.return_value = HttpOK(), DESCRIBE_TABLE_DATA
         conn.describe_table()
     with patch(PATCH_METHOD) as req:
         req.return_value = HttpOK(), {}
         conn.batch_write_item(put_items=items)
         params = {
             'return_consumed_capacity': 'TOTAL',
             'request_items': {
                 self.test_table_name: [{
                     'PutRequest': {
                         'Item': {
                             'ForumName': {
                                 'S': 'FooForum'
                             },
                             'Subject': {
                                 'S': 'thread-0'
                             }
                         }
                     }
                 }, {
                     'PutRequest': {
                         'Item': {
                             'ForumName': {
                                 'S': 'FooForum'
                             },
                             'Subject': {
                                 'S': 'thread-1'
                             }
                         }
                     }
                 }, {
                     'PutRequest': {
                         'Item': {
                             'ForumName': {
                                 'S': 'FooForum'
                             },
                             'Subject': {
                                 'S': 'thread-2'
                             }
                         }
                     }
                 }, {
                     'PutRequest': {
                         'Item': {
                             'ForumName': {
                                 'S': 'FooForum'
                             },
                             'Subject': {
                                 'S': 'thread-3'
                             }
                         }
                     }
                 }, {
                     'PutRequest': {
                         'Item': {
                             'ForumName': {
                                 'S': 'FooForum'
                             },
                             'Subject': {
                                 'S': 'thread-4'
                             }
                         }
                     }
                 }, {
                     'PutRequest': {
                         'Item': {
                             'ForumName': {
                                 'S': 'FooForum'
                             },
                             'Subject': {
                                 'S': 'thread-5'
                             }
                         }
                     }
                 }, {
                     'PutRequest': {
                         'Item': {
                             'ForumName': {
                                 'S': 'FooForum'
                             },
                             'Subject': {
                                 'S': 'thread-6'
                             }
                         }
                     }
                 }, {
                     'PutRequest': {
                         'Item': {
                             'ForumName': {
                                 'S': 'FooForum'
                             },
                             'Subject': {
                                 'S': 'thread-7'
                             }
                         }
                     }
                 }, {
                     'PutRequest': {
                         'Item': {
                             'ForumName': {
                                 'S': 'FooForum'
                             },
                             'Subject': {
                                 'S': 'thread-8'
                             }
                         }
                     }
                 }, {
                     'PutRequest': {
                         'Item': {
                             'ForumName': {
                                 'S': 'FooForum'
                             },
                             'Subject': {
                                 'S': 'thread-9'
                             }
                         }
                     }
                 }]
             }
         }
         self.assertEqual(req.call_args[1], params)