def test_parse_nested_dict_all_sio_elems(self): locality = Dict('locality', Int('type'), Text('name'), AsIs('coords'), Decimal('geo_skip'), Float('geo_diff')) address = Dict('address', locality, UUID('street_id'), CSV('prefs'), DateTime('since'), List('types'), Opaque('opaque1')) email = Dict('email', Text('value'), Bool('is_business'), Date('join_date'), DictList('preferred_order', 'name', 'pos')) customer = Dict('customer', 'name', email, address) class MyService(Service): class SimpleIO: input = customer CySimpleIO.attach_sio(self.get_server_config(), MyService) data = Bunch() data.customer = Bunch() data.customer.name = 'my-name' data.customer.email = Bunch() data.customer.email.value = 'my-email' data.customer.email.is_business = True data.customer.email.join_date = '1999-12-31' data.customer.email.preferred_order = [{'name':'address2', 'pos':'2'}, {'name':'address1', 'pos':'1'}] data.customer.address = Bunch() data.customer.address.locality = Bunch() data.customer.address.locality.type = '111' data.customer.address.locality.name = 'my-locality' data.customer.address.locality.coords = object() data.customer.address.locality.geo_skip = '123.456' data.customer.address.locality.geo_diff = '999.777' data.customer.address.street_id = uuid4().hex data.customer.address.prefs = '1,2,3,4' data.customer.address.since = '27-11-1988T11:22:33' data.customer.address.types = ['a', 'b', 'c', 'd'] data.customer.address.opaque1 = object() input = MyService._sio.parse_input(data, DATA_FORMAT.JSON) self.assertIsInstance(input, Bunch) self.assertEquals(input.customer.name, data.customer.name) self.assertEquals(input.customer.email.value, data.customer.email.value) self.assertEquals(input.customer.email.is_business, data.customer.email.is_business) self.assertEquals(input.customer.email.join_date, dt_parse(data.customer.email.join_date)) self.assertListEqual(input.customer.email.preferred_order, data.customer.email.preferred_order) self.assertEquals(input.customer.address.locality.type, int(data.customer.address.locality.type)) self.assertEquals(input.customer.address.locality.name, data.customer.address.locality.name) self.assertIs(input.customer.address.locality.coords, data.customer.address.locality.coords) self.assertEquals(input.customer.address.locality.geo_skip, decimal_Decimal(data.customer.address.locality.geo_skip)) self.assertEquals(input.customer.address.locality.geo_diff, float(data.customer.address.locality.geo_diff)) self.assertEquals(input.customer.address.street_id, uuid_UUID(data.customer.address.street_id)) self.assertEquals(input.customer.address.prefs, data.customer.address.prefs.split(',')) self.assertEquals(input.customer.address.since, dt_parse(data.customer.address.since)) self.assertEquals(input.customer.address.types, data.customer.address.types) self.assertIs(input.customer.address.opaque1, data.customer.address.opaque1)
def test_text(self): sio = Text('myname') data = 123 parsed = self._parse(sio, data) self.assertIsInstance(parsed, basestring) self.assertEquals(parsed, unicode(data))
class SimpleIO: output = 'aaa', AsIs('bbb'), Bool('ccc'), 'ddd', Date('eee'), DateTime('fff'), Decimal('ggg'), \ Float('jjj'), Int('mmm'), Opaque('ooo'), Text('ppp'), UUID('qqq')
class SimpleIO: xml_pretty_print = False xml_declaration = False output = 'aaa', AsIs('bbb'), Bool('ccc'), 'ddd', Date('eee'), DateTime('fff'), Decimal('ggg'), \ Float('jjj'), Int('mmm'), Opaque('ooo'), Text('ppp'), UUID('qqq')
class SimpleIO: input = 'aaa', AsIs('bbb'), Bool('ccc'), CSV('ddd'), Date('eee'), DateTime('fff'), Decimal('ggg'), \ Dict('hhh', 'a', 'b', 'c'), DictList('iii', 'd', 'e', 'f'), Float('jjj'), Int('mmm'), List('nnn'), \ Opaque('ooo'), Text('ppp'), UUID('qqq')
def test_parse_nested_dict_all_sio_elems_some_missing(self): _default_input_value = 'default-input-value' default_locality = 'default-locality' default_address = 'default-address' locality = Dict('locality', Int('type'), Text('name'), AsIs('-coords'), Decimal('geo_skip'), Float('geo_diff'), default=default_locality) address = Dict('address', locality, UUID('-street_id'), CSV('prefs'), DateTime('since'), List('types'), Opaque('opaque1'), default=default_address) email = Dict('email', Text('value'), Bool('is_business'), Date('-join_date'), DictList('preferred_order', 'name', '-pos')) customer = Dict('customer', 'name', email, address) class MyService(Service): class SimpleIO: input = customer default_input_value = 'default-input-value' CySimpleIO.attach_sio(self.get_server_config(), MyService) # Note that 'join_date', 'street_id', 'coords' and one of 'pos' keys are missing in input below, # the test ensures that default values are used in their place. data = Bunch() data.customer = Bunch() data.customer.name = 'my-name' data.customer.email = Bunch() data.customer.email.value = 'my-email' data.customer.email.is_business = True data.customer.email.preferred_order = [{ 'name': 'address2', 'pos': '2' }, { 'name': 'address1' }] data.customer.address = Bunch() data.customer.address.locality = Bunch() data.customer.address.locality.type = '111' data.customer.address.locality.name = 'my-locality' data.customer.address.locality.geo_skip = '123.456' data.customer.address.locality.geo_diff = '999.777' data.customer.address.prefs = '1,2,3,4' data.customer.address.since = '27-11-1988T11:22:33' data.customer.address.types = ['a', 'b', 'c', 'd'] data.customer.address.opaque1 = object() input = MyService._sio.parse_input(data, DATA_FORMAT.JSON) self.assertIsInstance(input, Bunch) self.assertEquals(input.customer.name, data.customer.name) self.assertEquals(input.customer.email.value, data.customer.email.value) self.assertEquals(input.customer.email.is_business, data.customer.email.is_business) self.assertEquals(input.customer.email.join_date, _default_input_value) self.assertDictEqual(input.customer.email.preferred_order[0], data.customer.email.preferred_order[0]) self.assertEquals(input.customer.email.preferred_order[1].name, data.customer.email.preferred_order[1]['name']) self.assertEquals(input.customer.email.preferred_order[1].pos, _default_input_value) self.assertEquals(input.customer.address.locality.type, int(data.customer.address.locality.type)) self.assertEquals(input.customer.address.locality.name, data.customer.address.locality.name) self.assertEquals(input.customer.address.locality.coords, default_locality) self.assertEquals( input.customer.address.locality.geo_skip, decimal_Decimal(data.customer.address.locality.geo_skip)) self.assertEquals(input.customer.address.locality.geo_diff, float(data.customer.address.locality.geo_diff)) self.assertEquals(input.customer.address.street_id, default_address) self.assertEquals(input.customer.address.prefs, data.customer.address.prefs.split(',')) self.assertEquals(input.customer.address.since, dt_parse(data.customer.address.since)) self.assertEquals(input.customer.address.types, data.customer.address.types) self.assertIs(input.customer.address.opaque1, data.customer.address.opaque1)
class SimpleIO: input = '-aaa', AsIs('-bbb', default=bbb), Bool('-ccc', default=ccc), CSV('-ddd', default=ddd), \ Date('-eee', default=eee), DateTime('-fff', default=fff), Decimal('-ggg', default=ggg), \ Dict('-hhh', 'a', 'b', 'c', default=hhh), DictList('-iii', 'd', 'e', 'f', default=iii), \ Float('-jjj', default=jjj), Int('-mmm', default=mmm), List('-nnn', default=nnn), \ Opaque('-ooo', default=ooo), Text('-ppp', default=ppp), UUID('-qqq', default=qqq)
class SimpleIO: input = 'aaa', Int('-bbb', default=_default_bbb), Opaque('ccc'), '-ddd', Text('-eee'), \ Text('-fff', default=_default_fff) default_value = _default_input_value