Ejemplo n.º 1
0
 def test_update_empty(self):
     update = Update()
     placeholder_names, expression_attribute_values = {}, {}
     expression = update.serialize(placeholder_names, expression_attribute_values)
     assert expression is None
     assert placeholder_names == {}
     assert expression_attribute_values == {}
Ejemplo n.º 2
0
 def test_update_skips_empty_clauses(self):
     update = Update(self.attribute.remove())
     placeholder_names, expression_attribute_values = {}, {}
     expression = update.serialize(placeholder_names, expression_attribute_values)
     assert expression == "REMOVE #0"
     assert placeholder_names == {'foo': '#0'}
     assert expression_attribute_values == {}
Ejemplo n.º 3
0
 def test_update(self):
     update = Update(
         self.attribute.set({'S': 'bar'}),
         self.attribute.remove(),
         self.attribute.add({'N': '0'}),
         self.attribute.delete({'NS': ['0']})
     )
     placeholder_names, expression_attribute_values = {}, {}
     expression = update.serialize(placeholder_names, expression_attribute_values)
     assert expression == "SET #0 = :0 REMOVE #0 ADD #0 :1 DELETE #0 :2"
     assert placeholder_names == {'foo': '#0'}
     assert expression_attribute_values == {
         ':0': {'S': 'bar'},
         ':1': {'N': '0'},
         ':2': {'NS': ['0']}
     }
Ejemplo n.º 4
0
    def update_item(self,
                    table_name,
                    hash_key,
                    range_key=None,
                    actions=None,
                    condition=None,
                    return_consumed_capacity=None,
                    return_item_collection_metrics=None,
                    return_values=None):
        """
        Performs the UpdateItem operation
        """
        self._check_condition('condition', condition)

        operation_kwargs = {TABLE_NAME: table_name}
        operation_kwargs.update(self.get_identifier_map(table_name, hash_key, range_key))
        name_placeholders = {}
        expression_attribute_values = {}

        if condition is not None:
            condition_expression = condition.serialize(name_placeholders, expression_attribute_values)
            operation_kwargs[CONDITION_EXPRESSION] = condition_expression
        if return_consumed_capacity:
            operation_kwargs.update(self.get_consumed_capacity_map(return_consumed_capacity))
        if return_item_collection_metrics:
            operation_kwargs.update(self.get_item_collection_map(return_item_collection_metrics))
        if return_values:
            operation_kwargs.update(self.get_return_values_map(return_values))
        if not actions:
            raise ValueError("'actions' cannot be empty")

        update_expression = Update(*actions)
        operation_kwargs[UPDATE_EXPRESSION] = update_expression.serialize(name_placeholders, expression_attribute_values)

        if name_placeholders:
            operation_kwargs[EXPRESSION_ATTRIBUTE_NAMES] = self._reverse_dict(name_placeholders)
        if expression_attribute_values:
            operation_kwargs[EXPRESSION_ATTRIBUTE_VALUES] = expression_attribute_values

        try:
            return self.dispatch(UPDATE_ITEM, operation_kwargs)
        except BOTOCORE_EXCEPTIONS as e:
            raise UpdateError("Failed to update item: {}".format(e), e)
Ejemplo n.º 5
0
    def test_list_update_remove_by_index(self):
        update = Update(
            self.list_attribute.remove_indexes(0),
        )
        placeholder_names, expression_attribute_values = {}, {}
        expression = update.serialize(placeholder_names, expression_attribute_values)
        assert expression == "REMOVE #0[0]"
        assert placeholder_names == {'foo_list': '#0'}
        assert expression_attribute_values == {}

        update = Update(
            self.list_attribute.remove_indexes(0, 10),
        )
        placeholder_names, expression_attribute_values = {}, {}
        expression = update.serialize(placeholder_names, expression_attribute_values)
        assert expression == "REMOVE #0[0], #0[10]"
        assert placeholder_names == {'foo_list': '#0'}
        assert expression_attribute_values == {}

        with self.assertRaises(ValueError) as e:
            Update(self.list_attribute.remove_indexes(0, "a"))
        assert str(e.exception) == "Method 'remove_indexes' arguments must be 'int'"
Ejemplo n.º 6
0
 def test_update(self):
     path = Path('foo')
     update = Update()
     update.add_action(path.set({'S': 'bar'}))
     update.add_action(path.remove())
     update.add_action(path.update({'N': '0'}))
     update.add_action(path.difference_update({'NS': ['0']}))
     placeholder_names, expression_attribute_values = {}, {}
     expression = update.serialize(placeholder_names, expression_attribute_values)
     assert expression == "SET #0 = :0 REMOVE #0 ADD #0 :1 DELETE #0 :2"
     assert placeholder_names == {'foo': '#0'}
     assert expression_attribute_values == {
         ':0': {'S': 'bar'},
         ':1': {'N': '0'},
         ':2': {'NS': ['0']}
     }
Ejemplo n.º 7
0
    async def get_operation_kwargs(self,
                                   table_name,
                                   hash_key,
                                   range_key=None,
                                   key=KEY,
                                   attributes=None,
                                   attributes_to_get=None,
                                   actions=None,
                                   condition=None,
                                   consistent_read=None,
                                   return_values=None,
                                   return_consumed_capacity=None,
                                   return_item_collection_metrics=None,
                                   return_values_on_condition_failure=None):
        self._check_condition('condition', condition)

        operation_kwargs = {}
        name_placeholders = {}
        expression_attribute_values = {}

        operation_kwargs[TABLE_NAME] = table_name
        operation_kwargs.update(await self.get_identifier_map(table_name,
                                                              hash_key,
                                                              range_key,
                                                              key=key))
        if attributes and operation_kwargs.get(ITEM) is not None:
            attrs = await self.get_item_attribute_map(table_name, attributes)
            operation_kwargs[ITEM].update(attrs[ITEM])
        if attributes_to_get is not None:
            projection_expression = create_projection_expression(
                attributes_to_get, name_placeholders)
            operation_kwargs[PROJECTION_EXPRESSION] = projection_expression
        if condition is not None:
            condition_expression = condition.serialize(
                name_placeholders, expression_attribute_values)
            operation_kwargs[CONDITION_EXPRESSION] = condition_expression
        if consistent_read is not None:
            operation_kwargs[CONSISTENT_READ] = consistent_read
        if return_values is not None:
            operation_kwargs.update(self.get_return_values_map(return_values))
        if return_values_on_condition_failure is not None:
            operation_kwargs.update(
                self.get_return_values_on_condition_failure_map(
                    return_values_on_condition_failure))
        if return_consumed_capacity is not None:
            operation_kwargs.update(
                self.get_consumed_capacity_map(return_consumed_capacity))
        if return_item_collection_metrics is not None:
            operation_kwargs.update(
                self.get_item_collection_map(return_item_collection_metrics))
        if actions is not None:
            update_expression = Update(*actions)
            operation_kwargs[UPDATE_EXPRESSION] = update_expression.serialize(
                name_placeholders, expression_attribute_values)
        if name_placeholders:
            operation_kwargs[EXPRESSION_ATTRIBUTE_NAMES] = self._reverse_dict(
                name_placeholders)
        if expression_attribute_values:
            operation_kwargs[
                EXPRESSION_ATTRIBUTE_VALUES] = expression_attribute_values
        return operation_kwargs