Spaces:
Build error
Build error
File size: 5,451 Bytes
0827183 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import logging
from aiounittest import AsyncTestCase
from botbuilder.core import MessageFactory
from botbuilder.dialogs import (
ComponentDialog,
DialogContext,
DialogTurnResult,
DialogTurnStatus,
PromptOptions,
TextPrompt,
WaterfallDialog,
WaterfallStepContext,
)
from botbuilder.schema import Activity
from botbuilder.testing import DialogTestClient, DialogTestLogger
class DialogTestClientTest(AsyncTestCase):
"""Tests for dialog test client."""
def __init__(self, *args, **kwargs):
super(DialogTestClientTest, self).__init__(*args, **kwargs)
logging.basicConfig(format="", level=logging.INFO)
def test_init(self):
client = DialogTestClient(channel_or_adapter="test", target_dialog=None)
self.assertIsInstance(client, DialogTestClient)
def test_init_with_custom_channel_id(self):
client = DialogTestClient(channel_or_adapter="custom", target_dialog=None)
self.assertEqual("custom", client.test_adapter.template.channel_id)
async def test_single_turn_waterfall_dialog(self):
async def step1(step: DialogContext) -> DialogTurnResult:
await step.context.send_activity("hello")
return await step.end_dialog()
dialog = WaterfallDialog("waterfall", [step1])
client = DialogTestClient("test", dialog)
reply = await client.send_activity("hello")
self.assertEqual("hello", reply.text)
self.assertEqual("test", reply.channel_id)
self.assertEqual(DialogTurnStatus.Complete, client.dialog_turn_result.status)
async def test_single_turn_waterfall_dialog_with_logger(self):
"""
Test for single turn waterfall dialog with logger with test client.
To view the console output:
* unittest
```bash
python -m unittest -v -k logger
```
* pytest
```bash
pytest --log-cli-level=INFO --log-format="%(message)s" -k logger
```
The results are similar to:
```
User: Text = hello
-> ts: 13:39:59
Bot: Text = hello
Speak = None
InputHint = acceptingInput
-> ts: 13:39:59 elapsed 8 ms
```
:return: None
:rtype: None
"""
async def step1(step: DialogContext) -> DialogTurnResult:
await step.context.send_activity("hello")
return await step.end_dialog()
dialog = WaterfallDialog("waterfall", [step1])
client = DialogTestClient(
"test",
dialog,
initial_dialog_options=None,
middlewares=[DialogTestLogger()],
)
reply = await client.send_activity("hello")
self.assertEqual("hello", reply.text)
self.assertEqual("test", reply.channel_id)
self.assertEqual(DialogTurnStatus.Complete, client.dialog_turn_result.status)
async def test_two_turn_waterfall_dialog(self):
async def step1(step: WaterfallStepContext) -> DialogTurnResult:
await step.context.send_activity("hello")
await step.context.send_activity(Activity(type="typing"))
return await step.next(result=None)
async def step2(step: WaterfallStepContext) -> DialogTurnResult:
await step.context.send_activity("hello 2")
return await step.end_dialog()
dialog = WaterfallDialog("waterfall", [step1, step2])
client = DialogTestClient(
"test",
dialog,
initial_dialog_options=None,
middlewares=[DialogTestLogger()],
)
reply = await client.send_activity("hello")
self.assertEqual("hello", reply.text)
reply = client.get_next_reply()
self.assertEqual("typing", reply.type)
reply = client.get_next_reply()
self.assertEqual("hello 2", reply.text)
self.assertEqual(DialogTurnStatus.Complete, client.dialog_turn_result.status)
async def test_component_dialog(self):
component = MainDialog("component")
client = DialogTestClient(
"test",
component,
initial_dialog_options=None,
middlewares=[DialogTestLogger()],
)
reply = await client.send_activity("hello")
self.assertEqual("Tell me something", reply.text)
reply = await client.send_activity("foo")
self.assertEqual("you said: foo", reply.text)
self.assertEqual(DialogTurnStatus.Complete, client.dialog_turn_result.status)
class MainDialog(ComponentDialog):
def __init__(self, dialog_id: str):
super().__init__(dialog_id)
dialog = WaterfallDialog("waterfall", [self.step1, self.step2])
self.add_dialog(TextPrompt(TextPrompt.__name__))
self.add_dialog(dialog)
self.initial_dialog_id = dialog.id
@staticmethod
async def step1(step: WaterfallStepContext) -> DialogTurnResult:
options = PromptOptions(prompt=MessageFactory.text("Tell me something"))
return await step.prompt(TextPrompt.__name__, options)
@staticmethod
async def step2(step: WaterfallStepContext) -> DialogTurnResult:
await step.context.send_activity(
MessageFactory.text(f"you said: {step.result}")
)
return await step.end_dialog()
|