class Source(Model): __hide_unset__ = True name = Attribute(str) url = Attribute(str) user = Attribute(str, optional=True) password = Attribute(str, optional=True) branch = Attribute(str, optional=True)
def test_attribute_should_cast_value_to_given_type(self): try: uut = Attribute(str, fallback='test') self.assertEqual(uut(12).value, '12') uut = Attribute(int, fallback='test') self.assertEqual(uut(12).value, 12) except ValueError as e: self.fail( 'creation of attribute failed in spite of "fallback" set: ' + str(e))
def test_attribute_should_be_nullifiable_if_specified(self): uut = Attribute(str, optional=True) try: self.assertIsNone(uut(None).value) except ValueError as e: self.fail( 'creation of attribute failed in spite of "optional" set to True: ' + str(e))
def test_attribute_should_use_fallback_if_specified(self): uut = Attribute(str, fallback='test') try: self.assertEqual(uut(None).value, 'test') except ValueError as e: self.fail( 'creation of attribute failed in spite of fallback being given: ' + str(e))
def test_attribute_should_call_fallback_if_function(self): def test_function(): return 'test' uut = Attribute(str, fallback=test_function) try: self.assertEqual(uut(None).value, 'test') except ValueError as e: self.fail( 'creation of attribute failed in spite of fallback function being given: ' + str(e))
class Foo(Model): __hide_unset__ = True optional_attribute = Attribute(str, optional=True)
class Foo(Model): non_attribute = 'bar' attribute = Attribute(str)
class UUT(Model): name = Attribute(str) number = Attribute(int) null = Attribute(str, optional=True) default_false = Attribute(bool, fallback=False)
class Foo(Model): a = Attribute(str)
class Script(Model): __hide_unset__ = True name = Attribute(str) path = Attribute(str) source = Attribute(str, optional=True) command = Attribute(str, optional=True)
class Data(Model): date = Attribute(parse_date)
class Data2(Model): model_value = Attribute(Data1)
class Data1(Model): value = Attribute(str)
def test_attribute_should_raise_when_type_is_none(self): with self.assertRaises(ValueError): Attribute(None)
def test_named_attributes(self): uut = Attribute(str, name='@foobar') self.assertEqual(uut('def').name, '@foobar')
class Foo(Model): __ignore_unknown__ = False data = Attribute(str)
class Data(Model): name = Attribute(str) some_value = Attribute(str, optional=True) another_value = Attribute(int, fallback=0)
class Data3(Model): data1 = Attribute(list_type(Data1)) data2 = Attribute(Data2)
class Foo(Model): a = Attribute(int) b = Attribute(str) c = Attribute(bool)
class Foo(Model): a = Attribute(str, alias='b')
class Config(Model): __hide_unset__ = True path = Attribute(str) script_dir = Attribute(str) scripts = Attribute(list_type(Script), fallback=[]) sources = Attribute(list_type(Source), fallback=[])
def test_attribute_should_not_be_nullifiable_by_default(self): with self.assertRaises(ValueError): Attribute(str)(None)