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