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_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'}}