예제 #1
0
 def test_list_of_nested(self):
     obj = {'list': [{'a': 1, 'b': 1}, {'a': 2, 'b': 1}, {'a': 3, 'b': 1}]}
     field = fields.List(fields.Nested({'a': fields.Integer}))
     self.assertEquals([
         OrderedDict([('a', 1)]),
         OrderedDict([('a', 2)]),
         OrderedDict([('a', 3)])
     ], field.output('list', obj))
예제 #2
0
    def test_null_list(self):
        class TestObject(object):
            def __init__(self, list):
                self.list = list

        obj = TestObject(None)
        field = fields.List(fields.String)
        self.assertEquals(None, field.output('list', obj))
예제 #3
0
    def test_list_with_attribute(self):
        class TestObject(object):
            def __init__(self, list):
                self.foo = list

        obj = TestObject(['a', 'b', 'c'])
        field = fields.List(fields.String, attribute='foo')
        self.assertEquals(['a', 'b', 'c'], field.output('list', obj))
예제 #4
0
    def test_list_from_object(self):
        class TestObject(object):
            def __init__(self, list):
                self.list = list

        obj = TestObject(['a', 'b', 'c'])
        field = fields.List(fields.String)
        self.assertEquals(['a', 'b', 'c'], field.output('list', obj))
예제 #5
0
    def test_list_of_raw(self):
        obj = {'list': [{'a': 1, 'b': 1}, {'a': 2, 'b': 1}, {'a': 3, 'b': 1}]}
        field = fields.List(fields.Raw)
        self.assertEquals([
            OrderedDict([
                ('a', 1),
                ('b', 1),
            ]),
            OrderedDict([
                ('a', 2),
                ('b', 1),
            ]),
            OrderedDict([
                ('a', 3),
                ('b', 1),
            ])
        ], field.output('list', obj))

        obj = {'list': [1, 2, 'a']}
        field = fields.List(fields.Raw)
        self.assertEquals([1, 2, 'a'], field.output('list', obj))
예제 #6
0
    def test_list_with_scoped_attribute_on_dict_or_obj(self):
        class TestObject(object):
            def __init__(self, list_):
                self.bar = list_

        class TestEgg(object):
            def __init__(self, val):
                self.attrib = val

        eggs = [TestEgg(i) for i in ['a', 'b', 'c']]
        test_obj = TestObject(eggs)
        test_dict = {
            'bar': [{
                'attrib': 'a'
            }, {
                'attrib': 'b'
            }, {
                'attrib': 'c'
            }]
        }

        field = fields.List(fields.String(attribute='attrib'), attribute='bar')
        self.assertEquals(['a', 'b', 'c'], field.output('bar', test_obj))
        self.assertEquals(['a', 'b', 'c'], field.output('bar', test_dict))
예제 #7
0
 def test_list_from_dict_with_attribute(self):
     obj = {'list': [{'a': 1, 'b': 1}, {'a': 2, 'b': 1}, {'a': 3, 'b': 1}]}
     field = fields.List(fields.Integer(attribute='a'))
     self.assertEquals([1, 2, 3], field.output('list', obj))
예제 #8
0
 def test_list_from_set(self):
     obj = {'list': set(['a', 'b', 'c'])}
     field = fields.List(fields.String)
     self.assertEquals(set(['a', 'b', 'c']), set(field.output('list', obj)))
예제 #9
0
 def test_list(self):
     obj = {'list': ['a', 'b', 'c']}
     field = fields.List(fields.String)
     self.assertEquals(['a', 'b', 'c'], field.output('list', obj))