示例#1
0
    def test_update_item(self):
        self.storage_mocker.StubOutWithMock(storage, 'get_item')

        hash_key = "4.5621201231232132132132132132132142354E126"
        range_key = "range"

        storage.get_item(
            mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(),
            select_type=mox.IgnoreArg(),
            consistent=mox.IgnoreArg()
        ).AndReturn(
            models.SelectResult(
                items=[
                    {
                        "hash_key": models.AttributeValue('N', hash_key),
                        "range_key": models.AttributeValue('S', range_key),
                        "attr_value": models.AttributeValue('S', 'val')
                    }
                ]
            )
        )

        self.storage_mocker.StubOutWithMock(storage, 'describe_table')

        storage.describe_table(mox.IgnoreArg(), 'test_table').AndReturn(
            models.TableMeta(
                '00000000-0000-0000-0000-000000000000',
                models.TableSchema(
                    {
                        'hash_key': models.AttributeType('N'),
                        'range_key': models.AttributeType('S')
                    },
                    ['hash_key', 'range_key'],
                ),
                models.TableMeta.TABLE_STATUS_ACTIVE,
                None
            )
        )

        self.storage_mocker.StubOutWithMock(storage, 'update_item')
        storage.update_item(
            mox.IgnoreArg(), mox.IgnoreArg(),
            key_attribute_map=mox.IgnoreArg(),
            attribute_action_map=mox.IgnoreArg(),
            expected_condition_map=mox.IgnoreArg()).AndReturn((True, None))

        self.storage_mocker.ReplayAll()

        table = ddb_table.Table(
            'test_table',
            connection=self.DYNAMODB_CON
        )

        item = table.get_item(consistent=False, hash_key=1, range_key="range")

        item['attr_value'] = 'updated'

        item.partial_save()

        self.storage_mocker.VerifyAll()
示例#2
0
    def test_delete_table(self):
        self.storage_mocker.StubOutWithMock(storage, 'delete_table')
        self.storage_mocker.StubOutWithMock(storage, 'describe_table')
        storage.delete_table(mox.IgnoreArg(), 'test_table')

        storage.describe_table(mox.IgnoreArg(), 'test_table').AndReturn(
            models.TableMeta(
                '00000000-0000-0000-0000-000000000000',
                models.TableSchema(
                    {
                        'city1': models.AttributeType('S'),
                        'id': models.AttributeType('S'),
                        'name': models.AttributeType('S')
                    },
                    ['id', 'name'],
                    {'index_name': models.IndexDefinition('id', 'city1')}
                ),
                models.TableMeta.TABLE_STATUS_ACTIVE,
                None
            )
        )

        self.storage_mocker.ReplayAll()

        table = ddb_table.Table(
            'test_table',
            connection=self.DYNAMODB_CON
        )

        self.assertTrue(table.delete())

        self.storage_mocker.VerifyAll()
示例#3
0
    def test_describe_table(self):

        self.storage_mocker.StubOutWithMock(storage, 'describe_table')

        storage.describe_table(IgnoreArg(), 'test_table').AndReturn(
            models.TableMeta(
                '00000000-0000-0000-0000-000000000000',
                models.TableSchema(
                    {
                        'city1': models.AttributeType('S'),
                        'id': models.AttributeType('S'),
                        'name': models.AttributeType('S')
                    }, ['id', 'name'],
                    {'index_name': models.IndexDefinition('id', 'city1')}),
                models.TableMeta.TABLE_STATUS_ACTIVE, None))

        self.storage_mocker.ReplayAll()

        table = Table('test_table', connection=self.DYNAMODB_CON)

        table_description = table.describe()

        self.storage_mocker.VerifyAll()

        self.assertEqual('test_table', table_description['Table']['TableName'])
        self.assertItemsEqual([{
            "AttributeName": "city1",
            "AttributeType": "S"
        }, {
            "AttributeName": "id",
            "AttributeType": "S"
        }, {
            "AttributeName": "name",
            "AttributeType": "S"
        }], table_description['Table']['AttributeDefinitions'])
示例#4
0
    def test_create_table_no_range(self):
        self.storage_mocker.StubOutWithMock(storage, 'create_table')
        storage.create_table(IgnoreArg(), IgnoreArg(), IgnoreArg()).AndReturn(
            models.TableMeta(
                models.TableSchema(
                    {
                        'hash': models.AttributeType('N'),
                        'indexed_field': models.AttributeType('S')
                    }, ['hash'], {
                        "index_name":
                        models.IndexDefinition('hash', 'indexed_field')
                    }), models.TableMeta.TABLE_STATUS_ACTIVE))
        self.storage_mocker.ReplayAll()

        Table.create("test",
                     schema=[
                         fields.HashKey('hash', data_type=schema_types.NUMBER),
                     ],
                     throughput={
                         'read': 20,
                         'write': 10,
                     },
                     indexes=[
                         fields.KeysOnlyIndex(
                             'index_name',
                             parts=[
                                 fields.HashKey('hash',
                                                data_type=schema_types.NUMBER),
                                 fields.RangeKey('indexed_field',
                                                 data_type=schema_types.STRING)
                             ])
                     ],
                     connection=self.DYNAMODB_CON)

        self.storage_mocker.VerifyAll()
示例#5
0
    def parse_attribute_definition(cls, attr_def_json):
        dynamodb_attr_name = attr_def_json.get(Props.ATTRIBUTE_NAME, None)
        dynamodb_attr_type = attr_def_json.get(Props.ATTRIBUTE_TYPE, "")

        storage_type = models.AttributeType(dynamodb_attr_type)

        return dynamodb_attr_name, storage_type
示例#6
0
    def parse_attribute_definition(cls, attr_def_json):
        attr_name_json = attr_def_json.pop(Props.ATTRIBUTE_NAME, None)
        attr_type_json = attr_def_json.pop(Props.ATTRIBUTE_TYPE, None)

        validation.validate_attr_name(attr_name_json)
        storage_type = models.AttributeType(attr_type_json)

        validation.validate_unexpected_props(attr_def_json,
                                             "attribute_definition")

        return attr_name_json, storage_type
示例#7
0
    def test_describe_table(self, mock_describe_table):

        attr_map = {
            'ForumName': models.AttributeType('S'),
            'Subject': models.AttributeType('S'),
            'LastPostDateTime': models.AttributeType('S')
        }

        key_attrs = ['ForumName', 'Subject']

        index_map = {
            'LastPostIndex':
            models.IndexDefinition('ForumName', 'LastPostDateTime')
        }

        table_meta = models.TableMeta(
            '00000000-0000-0000-0000-000000000000',
            models.TableSchema(attr_map, key_attrs, index_map),
            models.TableMeta.TABLE_STATUS_ACTIVE, 123)

        mock_describe_table.return_value = table_meta

        headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        }

        conn = httplib.HTTPConnection('localhost:8080')
        url = '/v1/data/default_tenant/tables/Thread'

        table_url = ('http://localhost:8080/v1/data/default_tenant'
                     '/tables/Thread')
        expected_response = {
            'table': {
                'attribute_definitions': [{
                    'attribute_name': 'Subject',
                    'attribute_type': 'S'
                }, {
                    'attribute_name': 'LastPostDateTime',
                    'attribute_type': 'S'
                }, {
                    'attribute_name': 'ForumName',
                    'attribute_type': 'S'
                }],
                'creation_date_time':
                123,
                'item_count':
                0,
                'key_schema': [{
                    'attribute_name': 'ForumName',
                    'key_type': 'HASH'
                }, {
                    'attribute_name': 'Subject',
                    'key_type': 'RANGE'
                }],
                'local_secondary_indexes': [{
                    'index_name':
                    'LastPostIndex',
                    'index_size_bytes':
                    0,
                    'item_count':
                    0,
                    'key_schema': [{
                        'attribute_name': 'ForumName',
                        'key_type': 'HASH'
                    }, {
                        'attribute_name': 'LastPostDateTime',
                        'key_type': 'RANGE'
                    }],
                    'projection': {
                        'projection_type': 'ALL'
                    }
                }],
                'table_id':
                '00000000-0000-0000-0000-000000000000',
                'table_name':
                'Thread',
                'table_size_bytes':
                0,
                'table_status':
                'ACTIVE',
                'links': [{
                    'href': table_url,
                    'rel': 'self'
                }, {
                    'href': table_url,
                    'rel': 'bookmark'
                }]
            }
        }

        conn.request("GET", url, headers=headers)

        response = conn.getresponse()

        self.assertTrue(mock_describe_table.called)

        json_response = response.read()
        response_payload = json.loads(json_response)

        self.assertEqual(expected_response, response_payload)
示例#8
0
    def test_create_table(self, mock_create_table):
        mock_create_table.return_value = models.TableMeta(
            '00000000-0000-0000-0000-000000000000',
            models.TableSchema(attribute_type_map={
                "ForumName": models.AttributeType('S'),
                "Subject": models.AttributeType('S'),
                "LastPostDateTime": models.AttributeType('S')
            },
                               key_attributes=["ForumName", "Subject"],
                               index_def_map={
                                   "LastPostIndex":
                                   models.IndexDefinition(
                                       "ForumName", "LastPostDateTime")
                               }), models.TableMeta.TABLE_STATUS_ACTIVE, 123)

        conn = httplib.HTTPConnection('localhost:8080')
        body = """
            {
                "attribute_definitions": [
                    {
                        "attribute_name": "ForumName",
                        "attribute_type": "S"
                    },
                    {
                        "attribute_name": "Subject",
                        "attribute_type": "S"
                    },
                    {
                        "attribute_name": "LastPostDateTime",
                        "attribute_type": "S"
                    }
                ],
                "table_name": "Thread",
                "key_schema": [
                    {
                        "attribute_name": "ForumName",
                        "key_type": "HASH"
                    },
                    {
                        "attribute_name": "Subject",
                        "key_type": "RANGE"
                    }
                ],
                "local_secondary_indexes": [
                    {
                        "index_name": "LastPostIndex",
                        "key_schema": [
                            {
                                "attribute_name": "ForumName",
                                "key_type": "HASH"
                            },
                            {
                                "attribute_name": "LastPostDateTime",
                                "key_type": "RANGE"
                            }
                        ],
                        "projection": {
                            "projection_type": "KEYS_ONLY"
                        }
                    }
                ]
            }
        """

        expected_response = {
            'table_description': {
                'attribute_definitions': [{
                    'attribute_name': 'Subject',
                    'attribute_type': 'S'
                }, {
                    'attribute_name': 'LastPostDateTime',
                    'attribute_type': 'S'
                }, {
                    'attribute_name': 'ForumName',
                    'attribute_type': 'S'
                }],
                'creation_date_time':
                123,
                'item_count':
                0,
                'key_schema': [{
                    'attribute_name': 'ForumName',
                    'key_type': 'HASH'
                }, {
                    'attribute_name': 'Subject',
                    'key_type': 'RANGE'
                }],
                'local_secondary_indexes': [{
                    'index_name':
                    'LastPostIndex',
                    'index_size_bytes':
                    0,
                    'item_count':
                    0,
                    'key_schema': [{
                        'attribute_name': 'ForumName',
                        'key_type': 'HASH'
                    }, {
                        'attribute_name': 'LastPostDateTime',
                        'key_type': 'RANGE'
                    }],
                    'projection': {
                        'projection_type': 'ALL'
                    }
                }],
                'table_id':
                '00000000-0000-0000-0000-000000000000',
                'table_name':
                'Thread',
                'table_size_bytes':
                0,
                'table_status':
                'ACTIVE',
                'links': [{
                    'href': self.table_url,
                    'rel': 'self'
                }, {
                    'href': self.table_url,
                    'rel': 'bookmark'
                }]
            }
        }

        conn.request("POST", self.url, headers=self.headers, body=body)

        response = conn.getresponse()

        self.assertTrue(mock_create_table.called)

        json_response = response.read()
        response_payload = json.loads(json_response)

        self.assertEqual(expected_response, response_payload)
示例#9
0
    def test_create_table_duplicate(self):
        self.storage_mocker.StubOutWithMock(storage, 'create_table')
        storage.create_table(
            mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()
        ).AndReturn(
            models.TableMeta(
                '00000000-0000-0000-0000-000000000000',
                models.TableSchema(
                    {
                        'hash': models.AttributeType('N'),
                        'range': models.AttributeType('S'),
                        'indexed_field': models.AttributeType('S')
                    },
                    ['hash', 'range'],
                    {
                        "index_name": models.IndexDefinition('hash',
                                                             'indexed_field')
                    }
                ),
                models.TableMeta.TABLE_STATUS_ACTIVE,
                None
            )
        )
        storage.create_table(
            mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()
        ).AndRaise(exception.TableAlreadyExistsException)
        self.storage_mocker.ReplayAll()

        ddb_table.Table.create(
            "test",
            schema=[
                fields.HashKey('hash', data_type=schema_types.NUMBER),
                fields.RangeKey('range', data_type=schema_types.STRING)
            ],
            throughput={
                'read': 20,
                'write': 10,
            },
            indexes=[
                fields.KeysOnlyIndex(
                    'index_name',
                    parts=[
                        fields.HashKey('hash', data_type=schema_types.NUMBER),
                        fields.RangeKey('indexed_field',
                                        data_type=schema_types.STRING)
                    ]
                )
            ],
            connection=self.DYNAMODB_CON
        )

        try:
            ddb_table.Table.create(
                "test",
                schema=[
                    fields.HashKey('hash', data_type=schema_types.NUMBER),
                    fields.RangeKey('range', data_type=schema_types.STRING)
                ],
                throughput={
                    'read': 20,
                    'write': 10,
                },
                indexes=[
                    fields.KeysOnlyIndex(
                        'index_name',
                        parts=[
                            fields.HashKey('hash',
                                           data_type=schema_types.NUMBER),
                            fields.RangeKey('indexed_field',
                                            data_type=schema_types.STRING)
                        ]
                    )
                ],
                connection=self.DYNAMODB_CON
            )

            self.fail()
        except boto_exc.JSONResponseError as e:
            self.assertEqual('ResourceInUseException', e.error_code)
            self.assertEqual('Table already exists: test', e.body['message'])
            self.storage_mocker.VerifyAll()
        except Exception as e:
            self.fail()