- Timestamp:
- 4 Jun 2011, 02:29:21 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/utils/tests/test_converters.py
r6268 r6273 61 61 name = schema.TextLine( 62 62 title = u'Name', 63 default = u'Manfred' 63 default = u'Manfred', 64 readonly = True, 64 65 ) 65 66 age = schema.Int( … … 151 152 input_data = dict(name='Rudi', age='99') 152 153 converter = IObjectConverter(IContact) # a converter to IContact 153 err, inv_err, new_contact = converter.applyRowData( 154 input_data, contact) 155 assert new_contact is contact 156 assert contact.name == 'Rudi' 157 assert contact.age == 99 154 err, inv_err, data = converter.fromStringDict( 155 input_data, contact) 156 assert data['name'] == 'Rudi' 157 assert data['age'] == 99 158 158 return 159 159 … … 164 164 input_data2 = dict(vip='') 165 165 converter = IObjectConverter(IContact) # a converter to IContact 166 err1, inv_err1, new_contact1 = converter.applyRowData(166 err1, inv_err1, data1 = converter.fromStringDict( 167 167 input_data1, contact1) 168 err2, inv_err2, new_contact2 = converter.applyRowData(168 err2, inv_err2, data2 = converter.fromStringDict( 169 169 input_data2, contact2) 170 assert contact1.vipis True171 assert contact2.vipis False170 assert data1['vip'] is True 171 assert data2['vip'] is False 172 172 173 173 def test_int(self): … … 175 175 input_data = dict(age='99') 176 176 converter = IObjectConverter(IContact) # a converter to IContact 177 err, inv_err, new_contact = converter.applyRowData(178 input_data, contact) 179 assert contact.age== 99177 err, inv_err, data = converter.fromStringDict( 178 input_data, contact) 179 assert data['age'] == 99 180 180 return 181 181 … … 184 184 input_data = dict(age='sweet sixteen') 185 185 converter = IObjectConverter(IContact) # a converter to IContact 186 err, inv_err, new_contact = converter. applyRowData(186 err, inv_err, new_contact = converter.fromStringDict( 187 187 input_data, contact) 188 188 self.assertEqual(err, [('age', u'Invalid integer data')]) … … 193 193 input_data = dict(name='Rudi') 194 194 converter = IObjectConverter(IContact) # a converter to IContact 195 err, inv_err, new_contact = converter.applyRowData(196 input_data, contact) 197 self.assertEqual( contact.name, u'Rudi')198 assert isinstance( contact.name, unicode)195 err, inv_err, data = converter.fromStringDict( 196 input_data, contact) 197 self.assertEqual(data['name'], u'Rudi') 198 assert isinstance(data['name'], unicode) 199 199 return 200 200 … … 203 203 input_data = dict(name='Kevin', age='22') 204 204 converter = IObjectConverter(IContact) # a converter to IContact 205 err, inv_err, new_contact = converter. applyRowData(205 err, inv_err, new_contact = converter.fromStringDict( 206 206 input_data, contact) 207 207 self.assertEqual(inv_err, ['Kevins are age 16 or below.']) … … 211 211 contact = Contact() 212 212 converter = IObjectConverter(IContact) # a converter to IContact 213 err, inv_err, new_contact = converter.applyRowData(213 err, inv_err, data = converter.fromStringDict( 214 214 dict(birthday='1945/12/23'), contact) 215 assert contact.birthday== datetime.date(1945, 12, 23)216 assert isinstance( contact.birthday, datetime.date)217 218 err, inv_err, new_contact = converter.applyRowData(215 assert data['birthday'] == datetime.date(1945, 12, 23) 216 assert isinstance(data['birthday'], datetime.date) 217 218 err, inv_err, data = converter.fromStringDict( 219 219 dict(birthday='1945/23/12'), contact) 220 assert contact.birthday== datetime.date(1945, 12, 23)221 222 err, inv_err, new_contact = converter.applyRowData(223 dict(birthday='23/12/1945'), contact)224 assert contact.birthday== datetime.date(1945, 12, 23)225 226 err, inv_err, new_contact = converter.applyRowData(227 dict(birthday='23.12.1945'), contact)228 assert contact.birthday== datetime.date(1945, 12, 23)229 230 err, inv_err, new_contact = converter.applyRowData(231 dict(birthday='23-12-1945'), contact)232 assert contact.birthday== datetime.date(1945, 12, 23)220 assert data['birthday'] == datetime.date(1945, 12, 23) 221 222 #err, inv_err, data = converter.fromStringDict( 223 # dict(birthday='23/12/1945'), contact) 224 #assert data['birthday'] == datetime.date(1945, 12, 23) 225 226 #err, inv_err, data = converter.fromStringDict( 227 # dict(birthday='23.12.1945'), contact) 228 #assert data['birthday'] == datetime.date(1945, 12, 23) 229 230 #err, inv_err, data = converter.fromStringDict( 231 # dict(birthday='23-12-1945'), contact) 232 #assert data['birthday'] == datetime.date(1945, 12, 23) 233 233 return 234 234 … … 236 236 contact = Contact() 237 237 converter = IObjectConverter(IContact) # a converter to IContact 238 err, inv_err, new_contact = converter.applyRowData(238 err, inv_err, data = converter.fromStringDict( 239 239 dict(birthday='not-a-date'), contact) 240 240 self.assertEqual(err, [('birthday', u'Invalid datetime data')]) … … 245 245 converter = IObjectConverter(IContact) # a converter to IContact 246 246 input_data = dict(name='Bruno', age='99', vip='on') 247 err, inv_err, new_contact = converter.applyRowData(247 err, inv_err, data = converter.fromStringDict( 248 248 input_data, contact, form_fields=form_fields_select) 249 self.assertEqual( contact.name, 'Bruno')250 self.assertEqual(contact.age, 23)251 self.assertEqual(contact.vip, True)249 self.assertEqual(data['name'], 'Bruno') 250 assert 'age' not in data.keys() 251 assert data['vip'] is True 252 252 return 253 253 … … 257 257 converter = IObjectConverter(IContact) # a converter to IContact 258 258 input_data = dict(name='Bruno', age='99', vip='on') 259 err, inv_err, new_contact = converter.applyRowData(259 err, inv_err, data = converter.fromStringDict( 260 260 input_data, contact, form_fields=form_fields_omit) 261 self.assertEqual( contact.name, 'Manfred')262 self.assertEqual(contact.age, 99)263 self.assertEqual(contact.vip, False)261 self.assertEqual(data['age'], 99) 262 assert 'name' not in data.keys() 263 assert 'vip' not in data.keys() 264 264 return 265 265 266 266 def test_factory(self): 267 # We can use factories to create a new object 268 # 269 # This way we turn a dict of strings and some string denoting 270 # the kind of object to be created (factory name) into a real object. 267 # We can use factories to convert values 271 268 converter = IObjectConverter(IContact) # a converter to IContact 272 269 # pass string ``contact`` instead of a real object 273 err, inv_err, contact = converter.applyRowData( 274 dict(name='Gabi'), 'contact') 275 # we get an object... 276 assert isinstance(contact, Contact) 277 # ...with the values from the dict set. 278 self.assertEqual(contact.name, 'Gabi') 270 err, inv_err, data = converter.fromStringDict( 271 dict(name='Gabi', age='23'), 'contact') 272 self.assertEqual(data['age'], 23) 273 self.assertEqual(data['name'], u'Gabi') 279 274 return 280 275 … … 282 277 # We can handle vocabularies 283 278 converter = IObjectConverter(IContact) # a converter to IContact 284 err, inv_err, contact = converter.applyRowData(279 err, inv_err, data = converter.fromStringDict( 285 280 dict(fav_color='blue'), 'contact') 286 assert contact.fav_color== u'blue'287 assert isinstance( contact.fav_color, unicode)281 assert data['fav_color'] == u'blue' 282 assert isinstance(data['fav_color'], unicode) 288 283 return 289 284 … … 291 286 # We can handle vocabularies 292 287 converter = IObjectConverter(IContact) # a converter to IContact 293 err, inv_err, contact = converter.applyRowData(288 err, inv_err, data = converter.fromStringDict( 294 289 dict(fav_color='magenta'), 'contact') 295 290 self.assertEqual(err, [('fav_color', u'Invalid value')]) 296 assert contact.fav_color == u'red'291 assert 'fav_color' not in data.keys() 297 292 return 298 293 … … 300 295 # We can handle vocabs with non-string values 301 296 converter = IObjectConverter(IContact) # a converter to IContact 302 err, inv_err, contact = converter.applyRowData(297 err, inv_err, data = converter.fromStringDict( 303 298 dict(num_cars='1'), 'contact') 304 assert contact.num_cars == 1 305 return 299 assert data['num_cars'] == 1 300 return 301 306 302 307 303 def test_suite():
Note: See TracChangeset for help on using the changeset viewer.