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 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():
                _, 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)
コード例 #4
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')
コード例 #5
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')