Changeset 6264 for main/waeup.sirp/trunk/src/waeup/sirp/utils/tests
- Timestamp:
- 2 Jun 2011, 02:46:31 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/utils/tests/test_converters.py
r6262 r6264 23 23 Tests for converterts. 24 24 """ 25 import datetime 25 26 import os 26 27 import shutil … … 32 33 33 34 from waeup.sirp.app import University 34 from waeup.sirp.interfaces import ISchemaTypeConverter35 35 from waeup.sirp.testing import FunctionalLayer, doctestsuite_for_module 36 36 from waeup.sirp.utils.converters import IObjectConverter 37 38 from zope import schema 39 from zope.interface import Interface, implements, invariant, Invalid 40 from waeup.sirp.utils.helpers import attrs_to_fields 41 42 class IContact(Interface): 43 name = schema.TextLine( 44 title = u'Name', 45 default = u'Manfred' 46 ) 47 age = schema.Int( 48 title = u'Age', 49 default = 23, 50 required = True, 51 ) 52 city = schema.TextLine( 53 title = u'City', 54 required = True, 55 ) 56 vip = schema.Bool( 57 title = u'Celebrity', 58 default = False, 59 required = True, 60 ) 61 birthday = schema.Date( 62 title = u'Birthday', 63 default = None, 64 ) 65 @invariant 66 def kevinIsYoung(contact): 67 if contact.age > 16 and contact.name == 'Kevin': 68 raise Invalid('Kevins are age 16 or below.') 69 70 class Contact(object): 71 implements(IContact) 72 name = None 73 age = None 74 Contact = attrs_to_fields(Contact) 37 75 38 76 class ConverterTests(FunctionalTestCase): … … 62 100 return 63 101 64 def test_foo(self): 65 from zope import schema 66 from zope.interface import Interface, implements 67 68 class IContact(Interface): 69 name = schema.TextLine( 70 title = u'Name', 71 default = u'Manfred' 72 ) 73 age = schema.Int( 74 title = u'Age', 75 default = 23, 76 required = True, 77 ) 78 city = schema.TextLine( 79 title = u'City', 80 required = True, 81 ) 82 83 84 class Contact(object): 85 implements(IContact) 86 name = None 87 age = None 88 89 from waeup.sirp.utils.helpers import attrs_to_fields 90 Contact = attrs_to_fields(Contact) 91 92 context = Contact() 93 conv = ISchemaTypeConverter(IContact) 94 input_data = dict(name='Foo') 95 input_data = dict(name='Foo', age='sweet sixteen') 96 err, inv_err, data = conv.applyRowData(input_data, context) 97 #print context.name, context.age 98 #print err, inv_err, data 99 #import pdb; pdb.set_trace() 100 #val = conv._convertValueFromString('Foo', context=context) 101 #print field, conv 102 def test_valid_data(self): 103 contact = Contact() 104 contact.age = 33 105 input_data = dict(name='Rudi', age='99') 106 converter = IObjectConverter(IContact) # a converter to IContact 107 err, inv_err, new_contact = converter.applyRowData( 108 input_data, contact) 109 assert new_contact is contact 110 assert contact.name == 'Rudi' 111 assert contact.age == 99 112 return 113 114 def test_bool(self): 115 contact1 = Contact() 116 contact2 = Contact() 117 input_data1 = dict(vip='on') 118 input_data2 = dict(vip='') 119 converter = IObjectConverter(IContact) # a converter to IContact 120 err1, inv_err1, new_contact1 = converter.applyRowData( 121 input_data1, contact1) 122 err2, inv_err2, new_contact2 = converter.applyRowData( 123 input_data2, contact2) 124 assert contact1.vip is True 125 assert contact2.vip is False 126 127 def test_int(self): 128 contact = Contact() 129 input_data = dict(age='99') 130 converter = IObjectConverter(IContact) # a converter to IContact 131 err, inv_err, new_contact = converter.applyRowData( 132 input_data, contact) 133 assert contact.age == 99 134 return 135 136 def test_int_invalid(self): 137 contact = Contact() 138 input_data = dict(age='sweet sixteen') 139 converter = IObjectConverter(IContact) # a converter to IContact 140 err, inv_err, new_contact = converter.applyRowData( 141 input_data, contact) 142 self.assertEqual(err, [('age', u'Invalid integer data')]) 143 return 144 145 def test_textline(self): 146 contact = Contact() 147 input_data = dict(name='Rudi') 148 converter = IObjectConverter(IContact) # a converter to IContact 149 err, inv_err, new_contact = converter.applyRowData( 150 input_data, contact) 151 self.assertEqual(contact.name, u'Rudi') 152 assert isinstance(contact.name, unicode) 153 return 154 155 def test_invariant(self): 156 contact = Contact() 157 input_data = dict(name='Kevin', age='22') 158 converter = IObjectConverter(IContact) # a converter to IContact 159 err, inv_err, new_contact = converter.applyRowData( 160 input_data, contact) 161 self.assertEqual(inv_err, ['Kevins are age 16 or below.']) 162 return 163 164 def test_date(self): 165 contact = Contact() 166 converter = IObjectConverter(IContact) # a converter to IContact 167 err, inv_err, new_contact = converter.applyRowData( 168 dict(birthday='1945/12/23'), contact) 169 assert contact.birthday == datetime.date(1945, 12, 23) 170 assert isinstance(contact.birthday, datetime.date) 171 172 err, inv_err, new_contact = converter.applyRowData( 173 dict(birthday='1945/23/12'), contact) 174 assert contact.birthday == datetime.date(1945, 12, 23) 175 176 err, inv_err, new_contact = converter.applyRowData( 177 dict(birthday='23/12/1945'), contact) 178 assert contact.birthday == datetime.date(1945, 12, 23) 179 180 err, inv_err, new_contact = converter.applyRowData( 181 dict(birthday='23.12.1945'), contact) 182 assert contact.birthday == datetime.date(1945, 12, 23) 183 184 err, inv_err, new_contact = converter.applyRowData( 185 dict(birthday='23-12-1945'), contact) 186 assert contact.birthday == datetime.date(1945, 12, 23) 187 return 188 189 def test_date_invalid(self): 190 contact = Contact() 191 converter = IObjectConverter(IContact) # a converter to IContact 192 err, inv_err, new_contact = converter.applyRowData( 193 dict(birthday='not-a-date'), contact) 194 self.assertEqual(err, [('birthday', u'Invalid datetime data')]) 102 195 103 196 def test_suite():
Note: See TracChangeset for help on using the changeset viewer.