class ListFieldTests(TestCase): """ Test ListField Test validation, creation and serialization. """ class MyTestRepresentation(Representation): """ Representation to be used in tests """ rev = ListField(ReverseCharField()) mylist = ListField(django_fields.CharField(max_length=5)) myoptlist = ListField(django_fields.CharField(max_length=5), required=False) def __init__(self, *args, **kw): super(ListFieldTests, self).__init__(*args, **kw) self.factory = Factory() self.factory.spec = ListFieldTests.MyTestRepresentation() self.factory.klass = Foo self.factory.property_name_map =\ self.factory._create_mappings(self.factory.spec) def test_validation(self): lspec = ListFieldTests.MyTestRepresentation() lspec.validate({'rev': ['one'], 'mylist': ['jedi', 'luke', 'shmi']}) self.assertRaises(ValidationError, lspec.validate, { 'rev': ['one'], 'mylist': ['jedi', 'luke', 'skywalker'] }) self.assertRaises(ValidationError, lspec.validate, { 'rev': ['one'], 'mylist': ['jedi'], 'myoptlist': ['quigon'] }) def test_creation(self): foo = self.factory.create({'rev': ['one', 'two'], 'mylist': ['s']}) self.assertEquals(foo.rev, ['eno', 'owt']) self.assertEquals(foo.mylist, ['s']) self.assertEquals(foo.myoptlist, None) def test_serialization(self): foo = Foo() foo.rev = ['one', 'two'] foo.mylist = ['one'] foo.myoptlist = None fdict = self.factory.serialize(foo) self.assertEquals(fdict['rev'], ['eno', 'owt']) self.assertEquals(fdict['mylist'], ['one']) self.assertFalse('myoptlist' in fdict)
class ListFieldTests(TestCase): """ Test ListField Test validation, creation and serialization. """ class MyTestRepresentation(Representation): """ Representation to be used in tests """ rev = ListField(ReverseCharField()) mylist = ListField(django_fields.CharField(max_length=5)) myoptlist = ListField(django_fields.CharField(max_length=5), required=False) def __init__(self, *args, **kw): super(ListFieldTests, self).__init__(*args, **kw) self.factory = Factory() self.factory.spec = ListFieldTests.MyTestRepresentation() self.factory.klass = Foo self.factory.property_name_map =\ self.factory._create_mappings(self.factory.spec) def test_validation(self): lspec = ListFieldTests.MyTestRepresentation() lspec.validate({'rev': ['one'], 'mylist': ['jedi', 'luke', 'shmi']}) self.assertRaises(ValidationError, lspec.validate, {'rev': ['one'], 'mylist': ['jedi', 'luke', 'skywalker']}) self.assertRaises(ValidationError, lspec.validate, { 'rev': ['one'], 'mylist': ['jedi'], 'myoptlist': ['quigon']}) def test_creation(self): foo = self.factory.create({'rev': ['one', 'two'], 'mylist': ['s']}) self.assertEquals(foo.rev, ['eno', 'owt']) self.assertEquals(foo.mylist, ['s']) self.assertEquals(foo.myoptlist, None) def test_serialization(self): foo = Foo() foo.rev = ['one', 'two'] foo.mylist = ['one'] foo.myoptlist = None fdict = self.factory.serialize(foo) self.assertEquals(fdict['rev'], ['eno', 'owt']) self.assertEquals(fdict['mylist'], ['one']) self.assertFalse('myoptlist' in fdict)
class NestedFieldTests(TestCase): """ Test NestedField field """ class Life(object): pass class LifeRepresentation(Representation): id = django_fields.IntegerField() lifetype = MeaningOfLifeField() def __init__(self, *args, **kw): super(NestedFieldTests, self).__init__(*args, **kw) self.factory = Factory() self.factory.spec = self.LifeRepresentation() self.factory.klass = self.Life self.factory.property_name_map =\ self.factory._create_mappings(self.factory.spec) def setUp(self): self.data = { 'id': 3, 'lifetype': { 'meaning': 'always look at the bright side of the life', 'submeaning': { 'meaning': 'that' } }, } def test_life(self): life = self.factory.create(self.data) self.assertEquals(life.id, 3) self.assertTrue(life.lifetype.meaning.startswith('always')) self.assertEquals(life.lifetype.submeaning.meaning, 'that') self.LifeRepresentation().validate(self.data) def test_validate_meaning_fails(self): self.assertRaises(ValidationError, MeaningOfLifeField().validate, self.data) self.data['lifetype'][ 'meaning'] = 'always look at the bright side of the leggings' self.assertRaises(ValidationError, self.LifeRepresentation().validate, self.data) def test_validate_submeaning_fails(self): self.data['lifetype']['submeaning']['meaning'] = 'this is true' self.assertRaises(ValidationError, self.LifeRepresentation().validate, self.data)
class NestedFieldTests(TestCase): """ Test NestedField field """ class Life(object): pass class LifeRepresentation(Representation): id = django_fields.IntegerField() lifetype = MeaningOfLifeField() def __init__(self, *args, **kw): super(NestedFieldTests, self).__init__(*args, **kw) self.factory = Factory() self.factory.spec = self.LifeRepresentation() self.factory.klass = self.Life self.factory.property_name_map =\ self.factory._create_mappings(self.factory.spec) def setUp(self): self.data = { 'id': 3, 'lifetype': { 'meaning': 'always look at the bright side of the life', 'submeaning': { 'meaning': 'that' } }, } def test_life(self): life = self.factory.create(self.data) self.assertEquals(life.id, 3) self.assertTrue(life.lifetype.meaning.startswith('always')) self.assertEquals(life.lifetype.submeaning.meaning, 'that') self.LifeRepresentation().validate(self.data) def test_validate_meaning_fails(self): self.assertRaises(ValidationError, MeaningOfLifeField().validate, self.data) self.data['lifetype']['meaning'] = 'always look at the bright side of the leggings' self.assertRaises(ValidationError, self.LifeRepresentation().validate, self.data) def test_validate_submeaning_fails(self): self.data['lifetype']['submeaning']['meaning'] = 'this is true' self.assertRaises(ValidationError, self.LifeRepresentation().validate, self.data)