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