Exemplo n.º 1
0
 def test_add_action(self):
     action = Path('foo').add(0)
     placeholder_names, expression_attribute_values = {}, {}
     expression = action.serialize(placeholder_names, expression_attribute_values)
     assert expression == "#0 :0"
     assert placeholder_names == {'foo': '#0'}
     assert expression_attribute_values == {':0': {'N': '0'}}
Exemplo n.º 2
0
 def test_conditional_set_action(self):
     action = self.attribute.set(Path('bar') | 'baz')
     placeholder_names, expression_attribute_values = {}, {}
     expression = action.serialize(placeholder_names, expression_attribute_values)
     assert expression == "#0 = if_not_exists (#1, :0)"
     assert placeholder_names == {'foo': '#0', 'bar': '#1'}
     assert expression_attribute_values == {':0': {'S': 'baz'}}
Exemplo n.º 3
0
 def test_prepend_action(self):
     action = self.attribute.set(Path('bar').prepend(['baz']))
     placeholder_names, expression_attribute_values = {}, {}
     expression = action.serialize(placeholder_names, expression_attribute_values)
     assert expression == "#0 = list_append (:0, #1)"
     assert placeholder_names == {'foo': '#0', 'bar': '#1'}
     assert expression_attribute_values == {':0': {'L': [{'S': 'baz'}]}}
Exemplo n.º 4
0
 def test_decrement_action_value(self):
     action = self.attribute.set(Value(0) - Path('bar'))
     placeholder_names, expression_attribute_values = {}, {}
     expression = action.serialize(placeholder_names, expression_attribute_values)
     assert expression == "#0 = :0 - #1"
     assert placeholder_names == {'foo': '#0', 'bar': '#1'}
     assert expression_attribute_values == {':0': {'N': '0'}}
Exemplo n.º 5
0
 def test_increment_action(self):
     action = self.attribute.set(Path('bar') + 0)
     placeholder_names, expression_attribute_values = {}, {}
     expression = action.serialize(placeholder_names, expression_attribute_values)
     assert expression == "#0 = #1 + :0"
     assert placeholder_names == {'foo': '#0', 'bar': '#1'}
     assert expression_attribute_values == {':0': {'N': '0'}}
Exemplo n.º 6
0
 def test_sizes(self):
     condition = size(self.attribute) == size(Path('bar'))
     placeholder_names, expression_attribute_values = {}, {}
     expression = condition.serialize(placeholder_names, expression_attribute_values)
     assert expression == "size (#0) = size (#1)"
     assert placeholder_names == {'foo': '#0', 'bar': '#1'}
     assert expression_attribute_values == {}
Exemplo n.º 7
0
 def test_contains_attribute(self):
     condition = ListAttribute(attr_name='foo').contains(Path('bar'))
     placeholder_names, expression_attribute_values = {}, {}
     expression = condition.serialize(placeholder_names, expression_attribute_values)
     assert expression == "contains (#0, #1)"
     assert placeholder_names == {'foo': '#0', 'bar': '#1'}
     assert expression_attribute_values == {}
    async def test_query(self):
        """
        TableConnection.query
        """
        conn = TableConnection(self.test_table_name)
        with patch(PATCH_METHOD) as req:
            req.return_value = DESCRIBE_TABLE_DATA
            await conn.describe_table()

        with patch(PATCH_METHOD) as req:
            req.return_value = {}
            await 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 = {}
            await 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)
Exemplo n.º 9
0
 def __getitem__(self, item):
     if self._is_attribute_container():
         return self.attribute_values[item]
     # If this instance is being used as an Attribute, treat item access like the map dereference operator.
     # This provides equivalence between DynamoDB's nested attribute access for map elements (MyMap.nestedField)
     # and Python's item access for dictionaries (MyMap['nestedField']).
     if self.is_raw():
         return Path(self.attr_path + [str(item)])
     elif item in self._attributes:
         return getattr(self, item)
     else:
         raise AttributeError("'{0}' has no attribute '{1}'".format(
             self.__class__.__name__, item))
Exemplo n.º 10
0
 def add(self, value):
     return Path(self).add(value)
Exemplo n.º 11
0
 def remove(self):
     return Path(self).remove()
Exemplo n.º 12
0
 def set(self, value):
     return Path(self).set(value)
Exemplo n.º 13
0
 def prepend(self, other):
     return Path(self).prepend(other)
Exemplo n.º 14
0
 def between(self, lower, upper):
     return Path(self).between(lower, upper)
    async 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
            await conn.describe_table()

        with patch(PATCH_METHOD) as req:
            req.return_value = {}
            await 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 = {}
            await 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(), {}
            await 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(), {}
            await 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)
Exemplo n.º 16
0
 def __eq__(self, other):
     if other is None or isinstance(
             other, Attribute):  # handle object identity comparison
         return self is other
     return Path(self).__eq__(other)
Exemplo n.º 17
0
 def __radd__(self, other):
     return Path(self).__radd__(other)
Exemplo n.º 18
0
 def test_create_project_expression_with_attribute_names(self):
     attributes_to_get = [Path(['foo.bar'])[0]]
     placeholders = {}
     projection_expression = create_projection_expression(attributes_to_get, placeholders)
     assert projection_expression == "#0[0]"
     assert placeholders == {'foo.bar': '#0'}
Exemplo n.º 19
0
 def startswith(self, prefix):
     return Path(self).startswith(prefix)
Exemplo n.º 20
0
 def is_type(self):
     # What makes sense here? Are we using this to check if deserialization will be successful?
     return Path(self).is_type(ATTR_TYPE_MAP[self.attr_type])
Exemplo n.º 21
0
 def does_not_exist(self):
     return Path(self).does_not_exist()
Exemplo n.º 22
0
 def exists(self):
     return Path(self).exists()
Exemplo n.º 23
0
 def is_in(self, *values):
     return Path(self).is_in(*values)
Exemplo n.º 24
0
 def delete(self, value):
     return Path(self).delete(value)
Exemplo n.º 25
0
 def __rsub__(self, other):
     return Path(self).__rsub__(other)
Exemplo n.º 26
0
 def __or__(self, other):
     return Path(self).__or__(other)
    async 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
            await conn.describe_table()

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

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), {}
            await 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)

        with patch(PATCH_METHOD) as req:
            req.return_value = HttpOK(), {}
            await 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)
Exemplo n.º 28
0
 def append(self, other):
     return Path(self).append(other)
Exemplo n.º 29
0
 def contains(self, item):
     return Path(self).contains(item)
Exemplo n.º 30
0
 def __getitem__(self, idx):
     return Path(self).__getitem__(idx)