- Timestamp:
- 2 Jul 2016, 16:48:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/trunk/src/waeup/kofa/utils/tests/test_converters.py
r13159 r14005 40 40 from waeup.kofa.university import Faculty 41 41 from waeup.kofa.utils.converters import ( 42 IObjectConverter, IFieldConverter, DefaultFieldConverter, 43 ListFieldConverter, PhoneNumberFieldConverter, ResultEntryConverter, 44 DefaultObjectConverter) 42 IObjectConverter, DefaultFieldConverter, ListFieldConverter, 43 PhoneNumberFieldConverter, ResultEntryConverter, DefaultObjectConverter) 45 44 from waeup.kofa.utils.helpers import attrs_to_fields 46 45 … … 56 55 ('Three', 3), 57 56 ) 57 58 58 59 class IContact(Interface): 59 60 """Sample interface for sample content type used here in tests. 60 61 """ 61 62 name = schema.TextLine( 62 title =u'Name',63 default =u'Manfred',64 readonly =True,63 title=u'Name', 64 default=u'Manfred', 65 readonly=True, 65 66 ) 66 67 age = schema.Int( 67 title =u'Age',68 default =23,69 required =True,68 title=u'Age', 69 default=23, 70 required=True, 70 71 ) 71 72 city = schema.TextLine( 72 title =u'City',73 required =True,73 title=u'City', 74 required=True, 74 75 ) 75 76 vip = schema.Bool( 76 title =u'Celebrity',77 default =False,78 required =True,77 title=u'Celebrity', 78 default=False, 79 required=True, 79 80 ) 80 81 birthday = schema.Date( 81 title =u'Birthday',82 default =None,82 title=u'Birthday', 83 default=None, 83 84 ) 84 85 fav_color = schema.Choice( 85 title =u'Favourite color',86 default =u'red',87 vocabulary =colors,86 title=u'Favourite color', 87 default=u'red', 88 vocabulary=colors, 88 89 ) 89 90 num_cars = schema.Choice( 90 title =u'Number of cars owned',91 default =None,92 vocabulary =car_nums,91 title=u'Number of cars owned', 92 default=None, 93 vocabulary=car_nums, 93 94 ) 94 95 grades = schema.List( 95 title = u'School Grades', 96 value_type = ResultEntryField(), 97 required = True, 98 default = [], 96 title=u'School Grades', 97 value_type=ResultEntryField(), 98 required=True, 99 #defaultFactory=list, 100 default=[], 99 101 ) 100 102 fav_colors = schema.List( 101 title =u'Favourite colors',102 value_type =schema.Choice(103 vocabulary =colors103 title=u'Favourite colors', 104 value_type=schema.Choice( 105 vocabulary=colors 104 106 ), 105 required = True, 106 default = [], 107 required=True, 108 #defaultFactory=list 109 default=[], 107 110 ) 108 111 friends = schema.List( 109 title =u'Friends',110 value_type =schema.TextLine(111 title =u'Name',112 title=u'Friends', 113 value_type=schema.TextLine( 114 title=u'Name', 112 115 ) 113 116 ) … … 117 120 if contact.age > 16 and contact.name == 'Kevin': 118 121 raise Invalid('Kevins are age 16 or below.') 122 119 123 120 124 class Contact(object): … … 127 131 form_fields_omit = form.Fields(IContact).omit('name', 'vip') 128 132 133 129 134 class ContactFactory(object): 130 135 """A factory for faculty containers. … … 137 142 def getInterfaces(self): 138 143 return implementedBy(Faculty) 144 139 145 140 146 class FieldConverterTests(unittest.TestCase): … … 156 162 return 157 163 164 158 165 class ConverterTests(FunctionalTestCase): 159 166 … … 197 204 contact.age = 33 198 205 input_data = dict(name='Rudi', age='99') 199 converter = IObjectConverter(IContact) # a converter to IContact206 converter = IObjectConverter(IContact) # a converter to IContact 200 207 err, inv_err, data = converter.fromStringDict( 201 208 input_data, contact) … … 209 216 input_data1 = dict(vip='on') 210 217 input_data2 = dict(vip='') 211 converter = IObjectConverter(IContact) # a converter to IContact218 converter = IObjectConverter(IContact) # a converter to IContact 212 219 err1, inv_err1, data1 = converter.fromStringDict( 213 220 input_data1, contact1) … … 218 225 219 226 def test_bool_nonstandard_values1(self): 220 # We accept 'true', 'True', 'tRuE', 'faLSE' and similar.227 # We accept 'true', 'True', 'tRuE', 'faLSE' and similar. 221 228 contact1 = Contact() 222 229 contact2 = Contact() 223 230 input_data1 = dict(vip='True') 224 231 input_data2 = dict(vip='false') 225 converter = IObjectConverter(IContact) # a converter to IContact232 converter = IObjectConverter(IContact) # a converter to IContact 226 233 err1, inv_err1, data1 = converter.fromStringDict( 227 234 input_data1, contact1) … … 237 244 input_data1 = dict(vip='1') 238 245 input_data2 = dict(vip='0') 239 converter = IObjectConverter(IContact) # a converter to IContact246 converter = IObjectConverter(IContact) # a converter to IContact 240 247 err1, inv_err1, data1 = converter.fromStringDict( 241 248 input_data1, contact1) … … 251 258 input_data1 = dict(vip='Yes') 252 259 input_data2 = dict(vip='no') 253 converter = IObjectConverter(IContact) # a converter to IContact260 converter = IObjectConverter(IContact) # a converter to IContact 254 261 err1, inv_err1, data1 = converter.fromStringDict( 255 262 input_data1, contact1) … … 262 269 contact = Contact() 263 270 input_data = dict(age='99') 264 converter = IObjectConverter(IContact) # a converter to IContact271 converter = IObjectConverter(IContact) # a converter to IContact 265 272 err, inv_err, data = converter.fromStringDict( 266 273 input_data, contact) … … 271 278 contact = Contact() 272 279 input_data = dict(age='sweet sixteen') 273 converter = IObjectConverter(IContact) # a converter to IContact280 converter = IObjectConverter(IContact) # a converter to IContact 274 281 err, inv_err, new_contact = converter.fromStringDict( 275 282 input_data, contact) … … 280 287 contact = Contact() 281 288 input_data = dict(name='Rudi') 282 converter = IObjectConverter(IContact) # a converter to IContact289 converter = IObjectConverter(IContact) # a converter to IContact 283 290 err, inv_err, data = converter.fromStringDict( 284 291 input_data, contact) … … 290 297 contact = Contact() 291 298 input_data = dict(name='Kevin', age='22') 292 converter = IObjectConverter(IContact) # a converter to IContact299 converter = IObjectConverter(IContact) # a converter to IContact 293 300 err, inv_err, new_contact = converter.fromStringDict( 294 301 input_data, contact) … … 298 305 def test_date(self): 299 306 contact = Contact() 300 converter = IObjectConverter(IContact) # a converter to IContact307 converter = IObjectConverter(IContact) # a converter to IContact 301 308 302 309 # The input format for dates: YYYY-MM-DD … … 309 316 dict(birthday='1945-23-12'), contact) 310 317 #assert data['birthday'] == datetime.date(1945, 12, 23) 311 assert err[0][1] == 'Invalid datetime data'318 assert err[0][1] == 'Invalid datetime data' 312 319 313 320 # '08' is not interpreted as octal number … … 319 326 def test_date_invalid(self): 320 327 contact = Contact() 321 converter = IObjectConverter(IContact) # a converter to IContact328 converter = IObjectConverter(IContact) # a converter to IContact 322 329 err, inv_err, data = converter.fromStringDict( 323 330 dict(birthday='not-a-date'), contact) … … 327 334 # We can use our own formfields and select only a subset of fields 328 335 contact = Contact() 329 converter = IObjectConverter(IContact) # a converter to IContact336 converter = IObjectConverter(IContact) # a converter to IContact 330 337 input_data = dict(name='Bruno', age='99', vip='on') 331 338 err, inv_err, data = converter.fromStringDict( … … 339 346 # We can use our own formfields and omit some fields 340 347 contact = Contact() 341 converter = IObjectConverter(IContact) # a converter to IContact348 converter = IObjectConverter(IContact) # a converter to IContact 342 349 input_data = dict(name='Bruno', age='99', vip='on') 343 350 err, inv_err, data = converter.fromStringDict( … … 350 357 def test_factory(self): 351 358 # We can use factories to convert values 352 converter = IObjectConverter(IContact) # a converter to IContact359 converter = IObjectConverter(IContact) # a converter to IContact 353 360 # pass string ``contact`` instead of a real object 354 361 err, inv_err, data = converter.fromStringDict( … … 360 367 def test_choice_vocab(self): 361 368 # We can handle vocabularies 362 converter = IObjectConverter(IContact) # a converter to IContact369 converter = IObjectConverter(IContact) # a converter to IContact 363 370 err, inv_err, data = converter.fromStringDict( 364 371 dict(fav_color='blue'), 'contact') … … 369 376 def test_choice_vocab_invalid_value(self): 370 377 # We can handle vocabularies 371 converter = IObjectConverter(IContact) # a converter to IContact378 converter = IObjectConverter(IContact) # a converter to IContact 372 379 err, inv_err, data = converter.fromStringDict( 373 380 dict(fav_color='magenta'), 'contact') … … 378 385 def test_non_string_choice(self): 379 386 # We can handle vocabs with non-string values 380 converter = IObjectConverter(IContact) # a converter to IContact387 converter = IObjectConverter(IContact) # a converter to IContact 381 388 err, inv_err, data = converter.fromStringDict( 382 389 dict(num_cars='1'), 'contact') … … 401 408 g_val1, g_val2 = list(g_src.factory.getValues())[0:2] 402 409 req_string = u"[('%s', '%s'), ('%s', '%s')]" % ( 403 410 s_val1, g_val1, s_val2, g_val2) 404 411 err, inv_err, data = converter.fromStringDict( 405 412 {"grades": req_string,
Note: See TracChangeset for help on using the changeset viewer.