def test_response_multiline(self): class MyService(Service): class SimpleIO: output = 'aaa', Int('bbb'), Opaque('ccc'), '-ddd', '-eee' csv_delimiter = ':' CySimpleIO.attach_sio(self.get_server_config(), MyService) aaa1 = 'aaa-111-1' bbb1 = '2221' ccc1 = 'ccc-ccc-ccc-1' eee1 = 'eee-444-1' aaa2 = 'aaa-111-2' bbb2 = '2222' ccc2 = 'ccc-ccc-ccc-2' eee2 = 'eee-444-2' # Note that 'ddd' is optional and we are free to skip it data1 = {'aaa': aaa1, 'bbb': bbb1, 'ccc': ccc1, 'eee': eee1} data2 = {'aaa': aaa2, 'bbb': bbb2, 'ccc': ccc2, 'eee': eee2} data = [data1, data2] result = MyService._sio.get_output(data, DATA_FORMAT.JSON) json_data = json_loads(result) self.assertEquals(json_data[0]['aaa'], aaa1) self.assertEquals(json_data[0]['bbb'], int(bbb1)) self.assertEquals(json_data[0]['ccc'], ccc1) self.assertEquals(json_data[0]['eee'], eee1) self.assertEquals(json_data[1]['aaa'], aaa2) self.assertEquals(json_data[1]['bbb'], int(bbb2)) self.assertEquals(json_data[1]['ccc'], ccc2) self.assertEquals(json_data[1]['eee'], eee2)
def test_response_with_response_elem(self): class MyService(Service): class SimpleIO: output = 'aaa', Int('bbb'), Opaque('ccc'), '-ddd', '-eee' response_elem = 'my_response_elem' CySimpleIO.attach_sio(self.get_server_config(), MyService) aaa = 'aaa-111' bbb = '222' ccc = 'ccc-ccc-ccc' eee = 'eee-444' # Note that 'ddd' is optional and we are free to skip it data = { 'aaa': aaa, 'bbb': bbb, 'ccc': ccc, 'eee': eee, } result = MyService._sio.get_output(data, DATA_FORMAT.JSON) json_data = json_loads(result) json_data = bunchify(json_data) self.assertEquals(json_data.my_response_elem.aaa, aaa) self.assertEquals(json_data.my_response_elem.bbb, int(bbb)) self.assertEquals(json_data.my_response_elem.ccc, ccc) self.assertEquals(json_data.my_response_elem.eee, eee)
def test_sio_response_from_sqlalchemy_orm_single_json(self): result = self._prepare_sio_response_from_orm(DATA_FORMAT.JSON, False) result = json_loads(result) self.assertEqual(result['cluster_id'], test_odb_data.cluster_id) self.assertEqual(result['name'], test_odb_data.name) self.assertIs(result['is_active'], test_odb_data.is_active)
def test_sio_response_from_zato_single_json(self): result = self._prepare_sio_response_from_zato(DATA_FORMAT.JSON, False) result = json_loads(result) result = result[response_elem] self.assertEqual(result['cluster_id'], test_odb_data.cluster_id) self.assertEqual(result['name'], test_odb_data.name) self.assertTrue(result['is_active'])
def test_sio_response_from_zato_list_json(self): result = self._prepare_sio_response_from_zato(DATA_FORMAT.JSON, True) result = json_loads(result) result = result[0] self.assertEqual(result['cluster_id'], test_odb_data.cluster_id) self.assertEqual(result['name'], test_odb_data.name) self.assertIs(result['is_active'], test_odb_data.is_active)
def test_response_all_elem_types(self): class MyService(Service): 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') CySimpleIO.attach_sio(self.get_server_config(), MyService) aaa = 'aaa-111' bbb = 'bbb-222-bbb' ccc = True ddd = '' eee = dt_parse('1999-12-31') fff = dt_parse('1988-01-29T11:22:33.0000Z') ggg = '123.456' jjj = '111.222' mmm = '9090' ooo = 'ZZZ-ZZZ-ZZZ' ppp = 'mytext' qqq = uuid_UUID('d011d054-db4b-4320-9e24-7f4c217af673') # Note that 'ddd' is optional and we are free to skip it data = { 'aaa': aaa, 'bbb': bbb, 'ccc': ccc, 'ddd': ddd, 'eee': eee, 'fff': fff, 'ggg': ggg, 'jjj': jjj, 'mmm': mmm, 'ooo': ooo, 'ppp': ppp, 'qqq': qqq } result = MyService._sio.get_output(data, DATA_FORMAT.JSON) json_data = json_loads(result) self.assertEquals(json_data['aaa'], aaa) self.assertEquals(json_data['bbb'], bbb) self.assertEquals(json_data['ccc'], ccc) self.assertEquals(json_data['eee'], '1999-12-31') self.assertEquals(json_data['fff'], '1988-01-29T11:22:33+00:00') self.assertEquals(json_data['ggg'], ggg) self.assertEquals(json_data['jjj'], float(jjj)) self.assertEquals(json_data['mmm'], int(mmm)) self.assertEquals(json_data['ooo'], ooo) self.assertEquals(json_data['ppp'], ppp) self.assertEquals(json_data['qqq'], qqq.hex)
def format_info(value, format, cols_width=None, dumper=None): if format in (INFO_FORMAT.DICT, INFO_FORMAT.JSON, INFO_FORMAT.YAML): value['component_details'] = json_loads(value['component_details']) if format == INFO_FORMAT.JSON: return json_dumps(value) elif format == INFO_FORMAT.YAML: buff = StringIO() yaml.dump_all([value], default_flow_style=False, indent=4, Dumper=dumper, stream=buff) value = buff.getvalue() buff.close() return value elif format == INFO_FORMAT.TEXT: cols_width = (elem.strip() for elem in cols_width.split(',')) cols_width = [int(elem) for elem in cols_width] table = Texttable() table.set_cols_width(cols_width) # Use text ('t') instead of auto so that boolean values don't get converted into ints table.set_cols_dtype(['t', 't']) rows = [['Key', 'Value']] rows.extend(sorted(value.items())) table.add_rows(rows) return table.draw() else: return value
def process_result_value(self, value, dialect): if value is not None and value != 'null': try: return json_loads(value) except(ValueError, TypeError): return None