Esempio 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'}}}])
Esempio 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')]
Esempio 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)
Esempio 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]
Esempio 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')]
Esempio 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)
Esempio 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
Esempio 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))