Exemplo n.º 1
0
    def test_string_with_partial(self):
        def f(x, suffix):
            return "%s-%s" % (x.hey, suffix)

        p = partial(f, suffix="whatever")
        field = fields.String(attribute=p)
        self.assertEquals("3-whatever", field.output("foo", Foo()))
Exemplo n.º 2
0
    def test_indexable_object(self):
        class TestObject(object):
            def __init__(self, foo):
                self.foo = foo

            def __getitem__(self, n):
                if type(n) is int:
                    if n < 3:
                        return n
                    raise IndexError
                raise TypeError

        obj = TestObject("hi")
        field = fields.String(attribute="foo")
        self.assertEquals("hi", field.output("foo", obj))
Exemplo n.º 3
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))
Exemplo n.º 4
0
 def test_attribute(self):
     obj = {"bar": 3}
     field = fields.String(attribute="bar")
     self.assertEquals(field.output("foo", obj), "3")
Exemplo n.º 5
0
 def test_no_attribute(self):
     obj = {"bar": 3}
     field = fields.String()
     self.assertEquals(field.output("foo", obj), None)
Exemplo n.º 6
0
 def test_basic_dictionary(self):
     obj = {"foo": 3}
     field = fields.String()
     self.assertEquals(field.output("foo", obj), "3")
Exemplo n.º 7
0
 def test_string_none(self):
     field = fields.String()
     self.assertEquals(None, field.output("empty", {'empty': None}))
Exemplo n.º 8
0
 def test_string_no_value(self):
     field = fields.String()
     self.assertEquals(None, field.output("bar", Foo()))
Exemplo n.º 9
0
 def test_string(self):
     field = fields.String()
     self.assertEquals("3", field.output("hey", Foo()))
Exemplo n.º 10
0
 def test_string_with_lambda(self):
     field = fields.String(attribute=lambda x: x.hey)
     self.assertEquals("3", field.output("foo", Foo()))
Exemplo n.º 11
0
 def test_string_with_attribute(self):
     field = fields.String(attribute="hey")
     self.assertEquals("3", field.output("foo", Foo()))