Esempio n. 1
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)
Esempio n. 2
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)
Esempio n. 3
0
 def test_form_failure(self):
     """
         Test Case for New URL's
     """
     data={'long_url': 'http://yahoo.com', 'created_time': TIME}
     response = self.app.post(
         '/', data=data)
     self.assertEqual(response.status_code, 200)
     try:
         conn = TableConnection('flask-datastore', region='eu-north-1')
         conn.delete_item(data['long_url'], data['created_time'])
     except Exception as ex:
            app.logger.info("Exception in test_form_failure {}".format(ex))
    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)
Esempio n. 5
0
def UpdateItem(uid, userType, body=None, update=False, delete=False):
    try:
        conn = TableConnection(
            table_name=settings.TABLE_NAME, 
            region=os.getenv("AWS_REGION"),
            aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
            aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY")
        )

        if delete:
            return conn.delete_item(hash_key=uid), None
            # return conn.delete_item(hash_key=uid, range_key=userType), None
        elif update and body:
            userObj = Users.get(uid)
            # userObj = Users.get(uid, userType)
            userObj.refresh()
            r = userObj.update(actions=[
                getattr(Users, k).set(v) for k, v in body.items()
            ])
            return r, None
            # return conn.put_item(hash_key=uid, range_key=userType, attributes={"firstName": body["firstName"]}), None
    except Exception as e:
        return None, str(e)
Esempio n. 6
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()
Esempio n. 7
0
    time.sleep(2)
    table = conn.describe_table()

print("conn.put_item")
conn.put_item(
    'item1-hash',
    range_key='item1-range',
    attributes={'foo': {'S': 'bar'}},
    expected={'Forum': {'Exists': False}}
)
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-{0}".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
)
 def tearDown(self):
     """
         Post tests cleanup code    
     """
     conn = TableConnection('flask-datastore', region='eu-north-1')
     conn.delete_item(obj['long_url'], obj['created_time'])
Esempio n. 9
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')
Esempio n. 10
0
"""
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')
Esempio n. 11
0
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'
              }},
              expected={'Forum': {
                  'Exists': False
              }})
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-{0}".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",
           key_conditions={
               'Thread': {
                   'ComparisonOperator': 'BEGINS_WITH',
                   'AttributeValueList': ['thread']
               }
Esempio n. 12
0
 def tearDown(self):
     conn = TableConnection('cut-it-datastore', region='eu-north-1')
     conn.delete_item(test_long_url, test_short_url.created_time)
Esempio n. 13
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")