def test_scalar_typed_data_decoder_not_ok(self):
        metadata = {
            'unsupported_type': protos.TypedData(stream=b'aaa'),
            'unexpected_json': protos.TypedData(json='[1, 2, 3]'),
            'unexpected_data': protos.TypedData(json='"foo"'),
        }

        cases = [
            ('unsupported_type', int, ValueError,
             "unsupported type of field 'unsupported_type' in "
             "trigger metadata: stream"),
            ('unexpected_json', int, ValueError,
             "cannot convert value of field 'unexpected_json' in "
             "trigger metadata into int"),
            ('unexpected_data', int, ValueError,
             "cannot convert value of field "
             "'unexpected_data' in trigger metadata into int: "
             "invalid literal for int"),
            ('unexpected_data', (int, float), ValueError,
             "unexpected value type in field "
             "'unexpected_data' in trigger metadata: str, "
             "expected one of: int, float"),
        ]

        for field, pytype, exc, msg in cases:
            with self.subTest(field=field):
                with self.assertRaisesRegex(exc, msg):
                    Converter._decode_trigger_metadata_field(
                        metadata, field, python_type=pytype)
    async def test_mock_generic_as_str(self):
        async with testutils.start_mockhost(
                script_root=self.generic_funcs_dir) as host:

            func_id, r = await host.load_function('foobar_as_str')

            self.assertEqual(r.response.function_id, func_id)
            self.assertEqual(r.response.result.status,
                             protos.StatusResult.Success)

            _, r = await host.invoke_function(
                'foobar_as_str', [
                    protos.ParameterBinding(
                        name='input',
                        data=protos.TypedData(
                            string='test'
                        )
                    )
                ]
            )
            self.assertEqual(r.response.result.status,
                             protos.StatusResult.Success)
            self.assertEqual(
                r.response.return_value,
                protos.TypedData(string='test')
            )
 async def call_and_check(due: bool):
     _, r = await host.invoke_function('return_pastdue', [
         protos.ParameterBinding(
             name='timer',
             data=protos.TypedData(
                 json=json.dumps({'IsPastDue': due})))
     ])
     self.assertEqual(r.response.result.status,
                      protos.StatusResult.Success)
     self.assertEqual(list(r.response.output_data), [
         protos.ParameterBinding(
             name='pastdue', data=protos.TypedData(string=str(due)))
     ])
            async def call_and_check():
                _, r = await host.invoke_function(
                    'eventhub_trigger_iot', [
                        protos.ParameterBinding(
                            name='event',
                            data=protos.TypedData(
                                json=json.dumps({'id': 'foo'})),
                        ),
                    ],
                    metadata={
                        'iothub-device-id':
                        protos.TypedData(string='mock-iothub-device-id'),
                        'iothub-auth-data':
                        protos.TypedData(string='mock-iothub-auth-data')
                    })

                self.assertEqual(r.response.result.status,
                                 protos.StatusResult.Success)
                self.assertIn('mock-iothub-device-id',
                              r.response.return_value.string)
示例#5
0
    def test_scalar_typed_data_decoder_ok(self):
        metadata = {
            'int_as_json': protos.TypedData(json='1'),
            'int_as_string': protos.TypedData(string='1'),
            'int_as_int': protos.TypedData(int=1),
            'string_as_json': protos.TypedData(json='"aaa"'),
            'string_as_string': protos.TypedData(string='aaa'),
            'dict_as_json': protos.TypedData(json='{"foo":"bar"}')
        }

        cases = [
            ('int_as_json', int, 1),
            ('int_as_string', int, 1),
            ('int_as_int', int, 1),
            ('string_as_json', str, 'aaa'),
            ('string_as_string', str, 'aaa'),
            ('dict_as_json', dict, {'foo': 'bar'}),
        ]

        for field, pytype, expected in cases:
            with self.subTest(field=field):
                value = Converter._decode_trigger_metadata_field(
                    metadata, field, python_type=pytype)

                self.assertIsInstance(value, pytype)
                self.assertEqual(value, expected)
示例#6
0
    async def test_call_sync_function_check_logs(self):
        async with testutils.start_mockhost() as host:
            await host.load_function('sync_logging')

            invoke_id, r = await host.invoke_function('sync_logging', [
                protos.ParameterBinding(
                    name='req',
                    data=protos.TypedData(http=protos.RpcHttp(method='GET')))
            ])

            self.assertEqual(r.response.result.status,
                             protos.StatusResult.Success)

            self.assertEqual(len(r.logs), 1)

            log = r.logs[0]
            self.assertEqual(log.invocation_id, invoke_id)
            self.assertTrue(
                log.message.startswith('a gracefully handled error'))

            self.assertEqual(r.response.return_value.string, 'OK-sync')
示例#7
0
    async def test_call_async_function_check_logs(self):
        async with testutils.start_mockhost() as host:
            await host.load_function('async_logging')

            invoke_id, r = await host.invoke_function('async_logging', [
                protos.ParameterBinding(
                    name='req',
                    data=protos.TypedData(http=protos.RpcHttp(method='GET')))
            ])

            self.assertEqual(r.response.result.status,
                             protos.StatusResult.Success)

            self.assertEqual(len(r.logs), 2)

            self.assertEqual(r.logs[0].invocation_id, invoke_id)
            self.assertEqual(r.logs[0].message, 'hello info')
            self.assertEqual(r.logs[0].level, protos.RpcLog.Information)

            self.assertEqual(r.logs[1].invocation_id, invoke_id)
            self.assertTrue(r.logs[1].message.startswith('and another error'))
            self.assertEqual(r.logs[1].level, protos.RpcLog.Error)

            self.assertEqual(r.response.return_value.string, 'OK-async')