def test_number_set_serialize(self): """ NumberSetAttribute.serialize """ attr = NumberSetAttribute() self.assertEqual(attr.serialize(set([1, 2])), [json.dumps(val) for val in sorted(set([1, 2]))]) self.assertEqual(attr.serialize(None), None)
def test_number_set_serialize(self): """ NumberSetAttribute.serialize """ attr = NumberSetAttribute() assert attr.serialize(set([1, 2])) == [json.dumps(val) for val in sorted(set([1, 2]))] assert attr.serialize(None) is None
def test_number_set_deserialize(self): """ NumberSetAttribute.deserialize """ attr = NumberSetAttribute() self.assertEqual(attr.attr_type, NUMBER_SET) self.assertEqual(attr.deserialize([json.dumps(val) for val in sorted(set([1, 2]))]), set([1, 2]))
def test_number_set_deserialize(self): """ NumberSetAttribute.deserialize """ attr = NumberSetAttribute() assert attr.attr_type == NUMBER_SET assert attr.deserialize([json.dumps(val) for val in sorted(set([1, 2]))]) == set([1, 2])
def test_contains_number_set(self): condition = NumberSetAttribute(attr_name='foo').contains(1) placeholder_names, expression_attribute_values = {}, {} expression = condition.serialize(placeholder_names, expression_attribute_values) assert expression == "contains (#0, :0)" assert placeholder_names == {'foo': '#0'} assert expression_attribute_values == {':0': {'N' : '1'}}
class AttributeTestModel(Model): class Meta: host = 'http://localhost:8000' table_name = 'test' binary_attr = BinaryAttribute() binary_set_attr = BinarySetAttribute() number_attr = NumberAttribute() number_set_attr = NumberSetAttribute() unicode_attr = UnicodeAttribute() unicode_set_attr = UnicodeSetAttribute() datetime_attr = UTCDateTimeAttribute() bool_attr = BooleanAttribute() json_attr = JSONAttribute() map_attr = MapAttribute()
class TestModel(Model): """ A model for testing """ class Meta: region = 'us-east-1' table_name = 'pynamodb-ci' host = ddb_url forum = UnicodeAttribute(hash_key=True) thread = UnicodeAttribute(range_key=True) view = NumberAttribute(default=0) view_index = LSIndex() epoch_index = GSIndex() epoch = UTCDateTimeAttribute(default=datetime.now) content = BinaryAttribute(null=True) scores = NumberSetAttribute()
def test_should_number_set_convert_list(): assert_attribute_conversion(NumberSetAttribute(), graphene.List)
def setUp(self): self.attribute = UnicodeAttribute(attr_name='foo') self.set_attribute = NumberSetAttribute(attr_name='foo_set') self.list_attribute = ListAttribute(attr_name='foo_list')
class UpdateExpressionTestCase(TestCase): def setUp(self): self.attribute = UnicodeAttribute(attr_name='foo') self.set_attribute = NumberSetAttribute(attr_name='foo_set') self.list_attribute = ListAttribute(attr_name='foo_list') 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_remove_action_list_element(self): action = self.list_attribute[10].remove() placeholder_names, expression_attribute_values = {}, {} expression = action.serialize(placeholder_names, expression_attribute_values) assert expression == "#0[10]" assert placeholder_names == {'foo_list': '#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.set_attribute.add({'NS': ['0']}), self.set_attribute.delete({'NS': ['1']}) ) placeholder_names, expression_attribute_values = {}, {} expression = update.serialize(placeholder_names, expression_attribute_values) assert expression == "SET #0 = :0 REMOVE #0 ADD #1 :1 DELETE #1 :2" assert placeholder_names == {'foo': '#0', 'foo_set': '#1'} assert expression_attribute_values == { ':0': {'S': 'bar'}, ':1': {'NS': ['0']}, ':2': {'NS': ['1']} } 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 == {} 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 == {}