Exemplo n.º 1
0
    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'}}}])
Exemplo n.º 2
0
    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')]
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
 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]
Exemplo n.º 5
0
    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')]
Exemplo n.º 6
0
    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)
Exemplo n.º 7
0
 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
Exemplo n.º 8
0
 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))