Пример #1
0
    def test_parse_basic_request(self):
        class MyService(Service):
            class SimpleIO:
                input = 'aaa', Int('bbb'), Opaque('ccc'), '-ddd', '-eee'

        CySimpleIO.attach_sio(self.get_server_config(), MyService)

        aaa = 'aaa-111'
        bbb = '222'
        ccc = object()
        eee = 'eee-111'

        # Note that 'ddd' is optional and we are free to skip it
        data = {
            'aaa': aaa,
            'bbb': bbb,
            'ccc': ccc,
            'eee': eee,
        }

        input = MyService._sio.parse_input(data, DATA_FORMAT.JSON)

        self.assertIsInstance(input, Bunch)

        self.assertEquals(input.aaa, aaa)
        self.assertEquals(input.bbb, int(bbb))
        self.assertIs(input.ccc, ccc)
        self.assertEquals(input.ddd, backward_compat_default_value)
        self.assertEquals(input.eee, eee)
Пример #2
0
    def test_response_basic(self):
        class MyService(Service):
            class SimpleIO:
                output = 'aaa', Int('bbb'), Opaque('ccc'), '-ddd', '-eee'
                xml_namespace = 'https://myns.zato.io'
                xml_pretty_print = False
                xml_declaration = False
                response_elem = 'my_response'

        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.XML)
        self.assertEquals(result, '<ns0:my_response xmlns:ns0="https://myns.zato.io">' \
            '<ns0:aaa>aaa-111</ns0:aaa><ns0:bbb>222</ns0:bbb><ns0:ccc>ccc-ccc-ccc</ns0:ccc>' \
            '<ns0:eee>eee-444</ns0:eee></ns0:my_response>')
Пример #3
0
    def test_top_level_skip_empty_input_single(self):
        class MyService(Service):
            class SimpleIO:
                input = 'aaa', 'bbb', '-ccc', '-ddd', '-eee', '-fff'
                default_value = NotGiven

                class SkipEmpty:
                    input = 'ccc'

        CySimpleIO.attach_sio(self.get_server_config(), MyService)

        data = Bunch()
        data.aaa = 'aaa'
        data.bbb = 'bbb'

        input = MyService._sio.parse_input(data, DATA_FORMAT.JSON)

        self.assertIsInstance(input, Bunch)
        self.assertDictEqual(
            input, {
                'aaa': 'aaa',
                'bbb': 'bbb',
                'ddd': NotGiven,
                'eee': NotGiven,
                'fff': NotGiven,
            })
Пример #4
0
    def test_csv_config(self):
        class MyService(Service):
            class SimpleIO:
                input = 'aaa', Int('bbb'), Opaque('ccc'), '-ddd', '-eee'
                csv_delimiter = '|'

        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 = '{}|{}|{}||{}'.format(aaa, bbb, ccc, eee)

        input = MyService._sio.parse_input(data, DATA_FORMAT.CSV)
        self.assertIsInstance(input, list)

        input = input[0]
        self.assertEquals(input.aaa, aaa)
        self.assertEquals(input.bbb, int(bbb))
        self.assertEquals(input.ccc, ccc)
        self.assertEquals(input.ddd, backward_compat_default_value)
        self.assertEquals(input.eee, eee)
Пример #5
0
    def test_parse_nested_dict_customer_no_defaults(self):

        locality = Dict('locality', 'type', 'name')
        address = Dict('address', locality, 'street')
        email = Dict('email', 'personal', 'business')
        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.personal = 'my-personal-email'
        data.customer.email.business = 'my-business-email'
        data.customer.address = Bunch()
        data.customer.address.street = 'my-street'
        data.customer.address.locality = Bunch()
        data.customer.address.locality.type = 'my-type'
        data.customer.address.locality.name = 'my-name'

        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.personal, data.customer.email.personal)
        self.assertEquals(input.customer.email.business, data.customer.email.business)
        self.assertEquals(input.customer.address.street, data.customer.address.street)
        self.assertEquals(input.customer.address.locality.type, data.customer.address.locality.type)
        self.assertEquals(input.customer.address.locality.name, data.customer.address.locality.name)
Пример #6
0
    def test_eval_int(self):

        MyClass = deepcopy(MyService)
        CySimpleIO.attach_sio(self.get_server_config(), MyClass)
        sio = MyClass._sio  # type: CySimpleIO

        elem_name = 'user_id'
        encrypt_func = None

        value = ''
        result = sio.eval_(elem_name, value, encrypt_func)
        self.assertIsNone(result)

        value = None
        result = sio.eval_(elem_name, value, encrypt_func)
        self.assertIsNone(result)

        value = '111'
        result = sio.eval_(elem_name, value, encrypt_func)
        self.assertIsInstance(result, int)
        self.assertEqual(result, 111)

        value = 222
        result = sio.eval_(elem_name, value, encrypt_func)
        self.assertIsInstance(result, int)
        self.assertEqual(result, 222)
Пример #7
0
    def test_response_basic_with_declaration_and_encoding(self):
        class MyService(Service):
            class SimpleIO:
                output = 'aaa', Int('bbb'), Opaque('ccc'), '-ddd', '-eee'
                xml_namespace = 'https://myns.zato.io'
                xml_pretty_print = False
                xml_declaration = True
                xml_encoding = 'ASCII'
                response_elem = 'my_response'

        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.XML)

        self.assertEquals(repr(result), repr("""<?xml version=\'1.0\' encoding=\'ASCII\'?>\n<ns0:my_response """ \
            """xmlns:ns0="https://myns.zato.io"><ns0:aaa>aaa-111</ns0:aaa><ns0:bbb>222</ns0:bbb><ns0:ccc>ccc-ccc-ccc""" \
            """</ns0:ccc><ns0:eee>eee-444</ns0:eee></ns0:my_response>"""))
Пример #8
0
    def test_response_multiline(self):
        class MyService(Service):
            class SimpleIO:
                output = 'aaa', Int('bbb'), Opaque('ccc'), '-ddd', '-eee'
                xml_pretty_print = False
                xml_declaration = False

        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.XML)
        self.assertEquals(result, '<response><item><aaa>aaa-111-1</aaa><bbb>2221</bbb><ccc>ccc-ccc-ccc-1</ccc>' \
            '<eee>eee-444-1</eee></item><item><aaa>aaa-111-2</aaa><bbb>2222</bbb><ccc>ccc-ccc-ccc-2</ccc>' \
            '<eee>eee-444-2</eee></item></response>')
Пример #9
0
    def test_eval_bool(self):

        MyClass = deepcopy(MyService)
        CySimpleIO.attach_sio(self.get_server_config(), MyClass)
        sio = MyClass._sio  # type: CySimpleIO

        elem_name = 'is_abc'
        encrypt_func = None

        value = ''
        result = sio.eval_(elem_name, value, encrypt_func)
        self.assertIsInstance(result, bool)
        self.assertFalse(result)

        value = None
        result = sio.eval_(elem_name, value, encrypt_func)
        self.assertIsInstance(result, bool)
        self.assertFalse(result)

        value = 't'
        result = sio.eval_(elem_name, value, encrypt_func)
        self.assertIsInstance(result, bool)
        self.assertTrue(result)

        value = 'true'
        result = sio.eval_(elem_name, value, encrypt_func)
        self.assertIsInstance(result, bool)
        self.assertTrue(result)

        value = 'on'
        result = sio.eval_(elem_name, value, encrypt_func)
        self.assertIsInstance(result, bool)
        self.assertTrue(result)
Пример #10
0
    def test_response_basic(self):
        class MyService(Service):
            class SimpleIO:
                output = 'aaa', Int('bbb'), Opaque('ccc'), '-ddd', '-eee'
                csv_delimiter = ';'

        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.CSV)
        lines = result.splitlines()

        self.assertEquals(lines[0], 'aaa;bbb;ccc;ddd;eee')
        self.assertEquals(lines[1], 'aaa-111;222;ccc-ccc-ccc;;eee-444')
Пример #11
0
    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.CSV)
        lines = result.splitlines()

        self.assertEquals(lines[0], 'aaa:bbb:ccc:ddd:eee')
        self.assertEquals(lines[1], 'aaa-111-1:2221:ccc-ccc-ccc-1::eee-444-1')
        self.assertEquals(lines[2], 'aaa-111-2:2222:ccc-ccc-ccc-2::eee-444-2')
Пример #12
0
    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)
Пример #13
0
    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)
Пример #14
0
    def test_parse_basic_request(self):
        class MyService(Service):
            class SimpleIO:
                input = 'aaa', Int('bbb'), Opaque('ccc'), '-ddd', '-eee'

        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 = lxml_fromstring("""<?xml version="1.0"?><root>
            <aaa>{}</aaa>
            <bbb>{}</bbb>
            <ccc>{}</ccc>
            <eee>{}</eee>
        </root>
        """.format(aaa, bbb, ccc, eee))

        input = MyService._sio.parse_input(data, DATA_FORMAT.XML)

        self.assertIsInstance(input, Bunch)

        self.assertEquals(input.aaa, aaa)
        self.assertEquals(input.bbb, int(bbb))
        self.assertEquals(input.ccc, ccc)
        self.assertEquals(input.ddd, backward_compat_default_value)
        self.assertEquals(input.eee, eee)
Пример #15
0
    def test_parse_default_backward_compat_default_input_value(self):

        _default_bbb = 112233
        _default_fff = object()
        _default_input_value = object()

        class MyService(Service):
            class SimpleIO:
                input = 'aaa', Int('-bbb', default=_default_bbb), Opaque('ccc'), '-ddd', Text('-eee'), \
                    Text('-fff', default=_default_fff)
                default_value = _default_input_value

        CySimpleIO.attach_sio(self.get_server_config(), MyService)

        aaa = 'aaa-111'
        ccc = object()
        eee = 'eee-111'

        # Note that 'ddd' is optional and we are free to skip it
        data = {
            'aaa': aaa,
            'ccc': ccc,
            'eee': eee,
        }

        input = MyService._sio.parse_input(data, DATA_FORMAT.JSON)
        self.assertIsInstance(input, Bunch)

        self.assertEquals(input.aaa, aaa)
        self.assertEquals(input.bbb, _default_bbb)
        self.assertIs(input.ccc, ccc)
        self.assertEquals(input.ddd, _default_input_value)
        self.assertEquals(input.eee, eee)
        self.assertEquals(input.fff, _default_fff)
Пример #16
0
    def test_get_sio_desc_multiline_no_separator(self):

        MyClass = deepcopy(MyService)
        CySimpleIO.attach_sio(self.get_server_config(), MyClass)

        info = ServiceInfo(service_name, MyClass, sio_config, 'public')
        description = info.simple_io['zato'].description # type: SimpleIODescription

        # There are multiple lines and no I/O separator
        # so input and output descriptions will be the same.

        input_user_id      = description.input['input_req_user_id']
        input_user_name    = description.input['input_opt_user_name']
        input_address_id   = description.input['output_req_address_id']
        input_address_name = description.input['output_opt_address_name']

        output_user_id      = description.output['input_req_user_id']
        output_user_name    = description.output['input_opt_user_name']
        output_address_id   = description.output['output_req_address_id']
        output_address_name = description.output['output_opt_address_name']

        self.assertEqual(input_user_id, output_user_id)
        self.assertEqual(input_user_name, output_user_name)
        self.assertEqual(input_address_id, output_address_id)
        self.assertEqual(input_address_name, output_address_name)

        self.assertEqual(input_user_id, 'This is the first line.\nHere is another.\nAnd here are some more lines.')
        self.assertEqual(input_user_name, 'b111')
        self.assertEqual(input_address_id, 'c111 c222 c333 c444')
        self.assertEqual(input_address_name, 'd111\nd222')

        self.assertEqual(output_user_id, 'This is the first line.\nHere is another.\nAnd here are some more lines.')
        self.assertEqual(output_user_name, 'b111')
        self.assertEqual(output_address_id, 'c111 c222 c333 c444')
        self.assertEqual(output_address_name, 'd111\nd222')
Пример #17
0
    def xtest_init_has_sio(self):

        MyService = deepcopy(MyBaseService)
        CySimpleIO.attach_sio(self.get_server_config(), MyService)

        response = Response()
        response.init('abc', MyService._sio, DATA_FORMAT.CSV)

        self.assertIsInstance(response.payload, SimpleIOPayload)
Пример #18
0
    def test_parse_bool(self):
        class MyService(Service):
            class SimpleIO:
                input = 'is_ready', 'abc_timeout'

        CySimpleIO.attach_sio(self.get_server_config(), MyService)

        MyService._sio.parse_input({
            'is_ready': 'true',
            'abc_timeout': '123'
        }, DATA_FORMAT.DICT)
Пример #19
0
    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)
Пример #20
0
    def test_parse_all_elem_types(self):
        class MyService(Service):
            class SimpleIO:
                input = '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 = '1999-12-31'
        fff = '1988-01-29T11:22:33.0000Z'
        ggg = '123.456'

        jjj = '111.222'
        mmm = '9090'

        ooo = 'ZZZ-ZZZ-ZZZ'
        ppp = 'mytext'
        qqq = 'd011d054-db4b-4320-9e24-7f4c217af673'

        # Note that 'ddd' is optional and we are free to skip it
        data = ','.join(
            [aaa, bbb, ccc, ddd, eee, fff, ggg, jjj, mmm, ooo, ppp, qqq])

        input = MyService._sio.parse_input(data, DATA_FORMAT.CSV)
        self.assertIsInstance(input, list)
        input = input[0]

        self.assertEquals(input.aaa, aaa)
        self.assertEquals(input.bbb, bbb)
        self.assertTrue(input.ccc)
        self.assertEquals(input.ddd, '')

        self.assertIsInstance(input.eee, datetime)
        self.assertEquals(input.eee.year, 1999)
        self.assertEquals(input.eee.month, 12)
        self.assertEquals(input.eee.day, 31)

        self.assertIsInstance(input.fff, datetime)
        self.assertEquals(input.fff.year, 1988)
        self.assertEquals(input.fff.month, 1)
        self.assertEquals(input.fff.day, 29)

        self.assertEquals(input.ggg, decimal_Decimal(ggg))
        self.assertEquals(input.jjj, float(jjj))
        self.assertEquals(input.mmm, int(mmm))
        self.assertEquals(input.ooo, ooo)
        self.assertEquals(input.ppp, ppp)
        self.assertEquals(input.qqq, uuid_UUID(qqq))
Пример #21
0
    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)
Пример #22
0
    def test_elem_required_minus_is_insignificant(self):
        class MyService(Service):
            class SimpleIO:
                input_required = 'aaa', 'bbb', 'ccc', '-ddd', '-eee'
                output_required = 'qqq', 'www', '-eee', '-fff'

        CySimpleIO.attach_sio(self.get_server_config(), MyService)

        self.assertEquals(
            MyService._sio.definition._input_required.get_elem_names(),
            ['-ddd', '-eee', 'aaa', 'bbb', 'ccc'])
        self.assertEquals(
            MyService._sio.definition._output_required.get_elem_names(),
            ['-eee', '-fff', 'qqq', 'www'])
Пример #23
0
    def xtest_setslice(self):

        MyService = deepcopy(MyBaseService)
        CySimpleIO.attach_sio(self.get_server_config(), MyService)

        response = Response()
        response.init('abc', MyService._sio, DATA_FORMAT.CSV)

        data = [{'a': 'aa', 'b': 'bb'}, {'a': 'aa2', 'b': 'bb2'}]
        response.payload[:] = data

        for idx, elem in enumerate(data):
            self.assertDictEqual(data[idx],
                                 response.payload.user_attrs_list[idx])
Пример #24
0
    def test_response_invalid_input(self):
        class MyService(Service):
            class SimpleIO:
                output = Int('aaa'), 'bbb'

        CySimpleIO.attach_sio(self.get_server_config(), MyService)

        aaa = 'aaa'
        bbb = '222'

        # Note that the value of 'aaa' is not an integer
        data = {'aaa': aaa, 'bbb': bbb}

        with self.assertRaises(SerialisationError):
            MyService._sio.get_output(data, DATA_FORMAT.CSV)
    def _prepare_sio_response(self, data, data_format, is_list):
        # type: (object, str, bool) -> str

        MyService = deepcopy(MyODBService)
        CySimpleIO.attach_sio(self.get_server_config(False), MyService)

        response = Response()
        response.init('abc', MyService._sio, data_format)

        if is_list:
            response.payload[:] = data
        else:
            response.payload = data

        return response.payload.getvalue()
Пример #26
0
    def test_response_all_elem_types(self):

        class MyService(Service):
            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')

        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.XML)
        self.assertEquals(result, '<response><aaa>aaa-111</aaa><bbb>bbb-222-bbb</bbb><ccc>True</ccc>' \
            '<ddd></ddd><eee>1999-12-31</eee><fff>1988-01-29T11:22:33+00:00</fff>' \
            '<ggg>123.456</ggg><jjj>111.222</jjj><mmm>9090</mmm><ooo>ZZZ-ZZZ-ZZZ</ooo>' \
            '<ppp>mytext</ppp><qqq>d011d054db4b43209e247f4c217af673</qqq></response>')
Пример #27
0
    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 = '1988-01-29T11:22:33.0000Z'
        ggg = '123.456'

        jjj = '111.222'
        mmm = '9090'

        ooo = 'ZZZ-ZZZ-ZZZ'
        ppp = 'mytext'
        qqq = '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.CSV)
        lines = result.splitlines()

        self.assertEquals(lines[0],
                          'aaa,bbb,ccc,ddd,eee,fff,ggg,jjj,mmm,ooo,ppp,qqq')
        self.assertEquals(lines[1], 'aaa-111,bbb-222-bbb,True,,1999-12-31,1988-01-29T11:22:33+00:00,123.456,111.222,9090,' \
            'ZZZ-ZZZ-ZZZ,mytext,d011d054-db4b-4320-9e24-7f4c217af673')
Пример #28
0
    def test_parse_default_all_elem_types(self):

        bbb = object()
        ccc = False
        ddd = [1, 2, 3, 4]
        eee = datetime(year=1990, month=1, day=29)
        fff = datetime(year=1990, month=1, day=29, hour=1, minute=2, second=3)
        ggg = decimal_Decimal('12.34')
        hhh = {'a': 1, 'b': 2, 'c': 3}
        iii = [{'a': 1, 'b': 2, 'c': 3}, {'a': 11, 'b': 22, 'c': 33}]
        jjj = 99.77
        mmm = 123
        nnn = ['a', 'b', 'c']
        ooo = object()
        ppp = 'mytext'
        qqq = uuid4().hex

        class MyService(Service):
            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)

        CySimpleIO.attach_sio(self.get_server_config(), MyService)

        # Note that the input document is empty
        input = MyService._sio.parse_input({}, DATA_FORMAT.JSON)
        self.assertIsInstance(input, Bunch)

        self.assertEquals(input.aaa, backward_compat_default_value)
        self.assertEquals(input.bbb, bbb)
        self.assertEquals(input.ccc, ccc)
        self.assertEquals(input.ddd, ddd)
        self.assertEquals(input.eee, eee)
        self.assertEquals(input.fff, fff)
        self.assertEquals(input.ggg, ggg)
        self.assertEquals(input.hhh, hhh)
        self.assertEquals(input.iii, iii)
        self.assertEquals(input.jjj, jjj)
        self.assertEquals(input.mmm, mmm)
        self.assertEquals(input.nnn, nnn)
        self.assertEquals(input.ooo, ooo)
        self.assertEquals(input.ppp, ppp)
        self.assertEquals(input.qqq, qqq)
Пример #29
0
    def test_top_level_skip_empty_input_true_no_force_empty_with_attribute(
            self):
        class MyService(Service):
            class SimpleIO:
                input = 'aaa', 'bbb', '-ccc', '-ddd'
                skip_empty_keys = True

        CySimpleIO.attach_sio(self.get_server_config(), MyService)

        data = Bunch()
        data.aaa = 'aaa'
        data.bbb = 'bbb'

        input = MyService._sio.parse_input(data, DATA_FORMAT.JSON)
        self.assertIsInstance(input, Bunch)
        self.assertDictEqual(input, {
            'aaa': 'aaa',
            'bbb': 'bbb',
        })
Пример #30
0
    def test_parse_nested_dict_customer_deep_defaults_elem_level(self):

        locality_default = object()

        locality = Dict('locality', '-type', '-name', default=locality_default)
        address = Dict('address', locality, '-street')
        email = Dict('email', 'personal', 'business')
        customer = Dict('customer', 'name', email, address)

        _default_input_value = 'default-input-value'

        class MyService(Service):
            class SimpleIO:
                input = customer
                default_input_value = 'default-input-value'

        CySimpleIO.attach_sio(self.get_server_config(), MyService)

        # Note that this locality has no type nor name but we expect for that Dict's default value to be used,
        # also, address has no street but since this Dict has no default value, again, SimpleIO one will be used.
        data = Bunch()
        data.customer = Bunch()
        data.customer.name = 'my-name'
        data.customer.email = Bunch()
        data.customer.email.personal = 'my-personal-email'
        data.customer.email.business = 'my-business-email'
        data.customer.address = Bunch()
        data.customer.address.locality = Bunch()

        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.personal,
                          data.customer.email.personal)
        self.assertEquals(input.customer.email.business,
                          data.customer.email.business)
        self.assertEquals(input.customer.address.street, _default_input_value)
        self.assertEquals(input.customer.address.locality.type,
                          locality_default)
        self.assertEquals(input.customer.address.locality.name,
                          locality_default)