class UpdateExpressionTestCase(TestCase): def setUp(self): self.attribute = UnicodeAttribute(attr_name='foo') self.list_attribute = ListAttribute(attr_name='foo_list', default=[]) def test_set_action(self): action = self.attribute.set('bar') 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': {'S': 'bar'}} def test_set_action_attribute_container(self): # Simulate initialization from inside an AttributeContainer my_map_attribute = MapAttribute(attr_name='foo') my_map_attribute._make_attribute() my_map_attribute._update_attribute_paths(my_map_attribute.attr_name) action = my_map_attribute.set(MapAttribute(bar='baz')) 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': {'M': {'bar': {'S': 'baz'}}}} 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'}} def test_increment_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'}} def test_decrement_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'}} 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'}} def test_append_action(self): action = self.attribute.set(Path('bar').append(['baz'])) placeholder_names, expression_attribute_values = {}, {} expression = action.serialize(placeholder_names, expression_attribute_values) assert expression == "#0 = list_append (#1, :0)" assert placeholder_names == {'foo': '#0', 'bar': '#1'} assert expression_attribute_values == {':0': {'L': [{'S': 'baz'}]}} 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'}]}} 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'}} def test_remove_action(self): action = self.attribute.remove() placeholder_names, expression_attribute_values = {}, {} expression = action.serialize(placeholder_names, expression_attribute_values) assert expression == "#0" assert placeholder_names == {'foo': '#0'} assert expression_attribute_values == {} 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'}} def test_add_action_set(self): action = NumberSetAttribute(attr_name='foo').add(0, 1) 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': {'NS': ['0', '1']}} def test_add_action_serialized(self): action = NumberSetAttribute(attr_name='foo').add({'NS': ['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': {'NS': ['0']}} def test_add_action_list(self): with self.assertRaises(ValueError): Path('foo').add({'L': [{'N': '0'}]}) def test_delete_action(self): action = NumberSetAttribute(attr_name='foo').delete(0, 1) 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': {'NS': ['0', '1']}} def test_delete_action_set(self): action = NumberSetAttribute(attr_name='foo').delete({0, 1}) 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': {'NS': ['0', '1']}} def test_delete_action_serialized(self): action = NumberSetAttribute(attr_name='foo').delete({'NS': ['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': {'NS': ['0']}} def test_delete_action_non_set(self): with self.assertRaises(ValueError): Path('foo').delete({'N': '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']} } 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'"
class UpdateExpressionTestCase(TestCase): def setUp(self): self.attribute = UnicodeAttribute(attr_name='foo') def test_set_action(self): action = self.attribute.set('bar') 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': {'S': 'bar'}} 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'}} def test_increment_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'}} def test_decrement_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'}} 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'}} def test_append_action(self): action = self.attribute.set(Path('bar').append(['baz'])) placeholder_names, expression_attribute_values = {}, {} expression = action.serialize(placeholder_names, expression_attribute_values) assert expression == "#0 = list_append (#1, :0)" assert placeholder_names == {'foo': '#0', 'bar': '#1'} assert expression_attribute_values == {':0': {'L': [{'S': 'baz'}]}} 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'}]}} 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'}} def test_remove_action(self): action = self.attribute.remove() placeholder_names, expression_attribute_values = {}, {} expression = action.serialize(placeholder_names, expression_attribute_values) assert expression == "#0" assert placeholder_names == {'foo': '#0'} assert expression_attribute_values == {} 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'}} def test_add_action_set(self): action = NumberSetAttribute(attr_name='foo').add(0, 1) 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': {'NS': ['0', '1']}} def test_add_action_serialized(self): action = NumberSetAttribute(attr_name='foo').add({'NS': ['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': {'NS': ['0']}} def test_add_action_list(self): with self.assertRaises(ValueError): Path('foo').add({'L': [{'N': '0'}]}) def test_delete_action(self): action = NumberSetAttribute(attr_name='foo').delete(0, 1) 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': {'NS': ['0', '1']}} def test_delete_action_set(self): action = NumberSetAttribute(attr_name='foo').delete(set([0, 1])) 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': {'NS': ['0', '1']}} def test_delete_action_serialized(self): action = NumberSetAttribute(attr_name='foo').delete({'NS': ['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': {'NS': ['0']}} def test_delete_action_non_set(self): with self.assertRaises(ValueError): Path('foo').delete({'N': '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'] } }