def test_list_type_error(self): string_list_attribute = ListAttribute(of=UnicodeAttribute) with pytest.raises(ValueError): string_list_attribute.serialize([MapAttribute(foo='bar')]) with pytest.raises(TypeError): string_list_attribute.deserialize([{'M': {'foo': {'S': 'bar'}}}])
def test_double_indexing(self): condition = ListAttribute(attr_name='foo')[0][1] == 'bar' placeholder_names, expression_attribute_values = {}, {} expression = condition.serialize(placeholder_names, expression_attribute_values) assert expression == "#0[0][1] = :0" assert placeholder_names == {'foo': '#0'} assert expression_attribute_values == {':0': {'S' : 'bar'}}
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 == {}
def test_contains_list(self): condition = ListAttribute(attr_name='foo').contains('bar') 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': {'S' : 'bar'}}
def test_list_of_map_with_of_and_custom_attribute(self, mocker): class CustomMapAttribute(MapAttribute): custom = NumberAttribute() def __eq__(self, other): return self.custom == other.custom serialize_mock = mocker.spy(CustomMapAttribute.custom, 'serialize',) deserialize_mock = mocker.spy(CustomMapAttribute.custom, 'deserialize') attribute1 = CustomMapAttribute() attribute1.custom = 1 attribute2 = CustomMapAttribute() attribute2.custom = 2 inp = [attribute1, attribute2] list_attribute = ListAttribute(default=[], of=CustomMapAttribute) serialized = list_attribute.serialize(inp) deserialized = list_attribute.deserialize(serialized) assert deserialized == inp assert serialize_mock.call_args_list == [call(1), call(2)] assert deserialize_mock.call_args_list == [call('1'), call('2')]
def test_list_of_map_with_of(self): class Person(MapAttribute): name = UnicodeAttribute() age = NumberAttribute() def __lt__(self, other): return self.name < other.name def __eq__(self, other): return (self.name == other.name and self.age == other.age) person1 = Person() person1.name = 'john' person1.age = 40 person2 = Person() person2.name = 'Dana' person2.age = 41 inp = [person1, person2] list_attribute = ListAttribute(default=[], of=Person) serialized = list_attribute.serialize(inp) deserialized = list_attribute.deserialize(serialized) assert sorted(deserialized) == sorted(inp)
def test_list_comparison(self): condition = ListAttribute(attr_name='foo') == ['bar', 'baz'] placeholder_names, expression_attribute_values = {}, {} expression = condition.serialize(placeholder_names, expression_attribute_values) assert expression == "#0 = :0" assert placeholder_names == {'foo': '#0'} assert expression_attribute_values == {':0': {'L': [{'S' : 'bar'}, {'S': 'baz'}]}}
def test_double_indexing(self): condition = ListAttribute(attr_name='foo')[0][1] == 'bar' placeholder_names, expression_attribute_values = {}, {} expression = condition.serialize(placeholder_names, expression_attribute_values) assert expression == "#0[0][1] = :0" assert placeholder_names == {'foo': '#0'} assert expression_attribute_values == {':0': {'S' : 'bar'}}
def test_list_comparison(self): condition = ListAttribute(attr_name='foo') == ['bar', 'baz'] placeholder_names, expression_attribute_values = {}, {} expression = condition.serialize(placeholder_names, expression_attribute_values) assert expression == "#0 = :0" assert placeholder_names == {'foo': '#0'} assert expression_attribute_values == {':0': {'L': [{'S' : 'bar'}, {'S': 'baz'}]}}
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 == {}
def test_list_of_map_with_of_and_custom_attribute(self, mocker): class CustomMapAttribute(MapAttribute): custom = NumberAttribute() def __eq__(self, other): return self.custom == other.custom serialize_mock = mocker.spy( CustomMapAttribute.custom, 'serialize', ) deserialize_mock = mocker.spy(CustomMapAttribute.custom, 'deserialize') attribute1 = CustomMapAttribute() attribute1.custom = 1 attribute2 = CustomMapAttribute() attribute2.custom = 2 inp = [attribute1, attribute2] list_attribute = ListAttribute(default=[], of=CustomMapAttribute) serialized = list_attribute.serialize(inp) deserialized = list_attribute.deserialize(serialized) assert deserialized == inp assert serialize_mock.call_args_list == [call(1), call(2)] assert deserialize_mock.call_args_list == [call('1'), call('2')]
def test_contains_list(self): condition = ListAttribute(attr_name='foo').contains('bar') 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': {'S' : 'bar'}}
def test_list_of_map_with_of(self): class Person(MapAttribute): name = UnicodeAttribute() age = NumberAttribute() def __lt__(self, other): return self.name < other.name def __eq__(self, other): return (self.name == other.name and self.age == other.age) person1 = Person() person1.name = 'john' person1.age = 40 person2 = Person() person2.name = 'Dana' person2.age = 41 inp = [person1, person2] list_attribute = ListAttribute(default=[], of=Person) serialized = list_attribute.serialize(inp) deserialized = list_attribute.deserialize(serialized) assert sorted(deserialized) == sorted(inp)
def test_serialize_null(self): string_set_list_attribute = ListAttribute(of=UnicodeSetAttribute) list_with_empty_set = [{'foo'}, {}, None] serialized = string_set_list_attribute.serialize(list_with_empty_set) assert string_set_list_attribute.deserialize(serialized) == [{'foo'}, None, None]
def test_typed_list_indexing(self): class StringMap(MapAttribute): bar = UnicodeAttribute() condition = ListAttribute(attr_name='foo', of=StringMap)[0].bar == 'baz' placeholder_names, expression_attribute_values = {}, {} expression = condition.serialize(placeholder_names, expression_attribute_values) assert expression == "#0[0].#1 = :0" assert placeholder_names == {'foo': '#0', 'bar': '#1'} assert expression_attribute_values == {':0': {'S': 'baz'}}
def test_list_of_strings(self): string_list_attribute = ListAttribute(of=UnicodeAttribute) string_list = ['foo', 'bar', 'baz'] serialized = string_list_attribute.serialize(string_list) assert string_list_attribute.deserialize(serialized) == string_list
def test_list_of_unicode(self): list_attribute = ListAttribute() inp = ['rome', 'berlin'] serialized = list_attribute.serialize(inp) deserialized = list_attribute.deserialize(serialized) self.assertEqual(sorted(deserialized), sorted(inp))