Spaces:
Build error
Build error
File size: 12,224 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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import unittest
import aiounittest
from botbuilder.dialogs.prompts import (
ActivityPrompt,
PromptOptions,
PromptValidatorContext,
)
from botbuilder.schema import Activity, ActivityTypes
from botbuilder.core import (
ConversationState,
MemoryStorage,
TurnContext,
MessageFactory,
)
from botbuilder.core.adapters import TestAdapter
from botbuilder.dialogs import DialogSet, DialogTurnStatus, DialogReason
async def validator(prompt_context: PromptValidatorContext):
tester = unittest.TestCase()
tester.assertTrue(prompt_context.attempt_count > 0)
activity = prompt_context.recognized.value
if activity.type == ActivityTypes.event:
if int(activity.value) == 2:
prompt_context.recognized.value = MessageFactory.text(str(activity.value))
return True
else:
await prompt_context.context.send_activity(
"Please send an 'event'-type Activity with a value of 2."
)
return False
class SimpleActivityPrompt(ActivityPrompt):
pass
class ActivityPromptTests(aiounittest.AsyncTestCase):
def test_activity_prompt_with_empty_id_should_fail(self):
empty_id = ""
with self.assertRaises(TypeError):
SimpleActivityPrompt(empty_id, validator)
def test_activity_prompt_with_none_id_should_fail(self):
none_id = None
with self.assertRaises(TypeError):
SimpleActivityPrompt(none_id, validator)
def test_activity_prompt_with_none_validator_should_fail(self):
none_validator = None
with self.assertRaises(TypeError):
SimpleActivityPrompt("EventActivityPrompt", none_validator)
async def test_basic_activity_prompt(self):
async def exec_test(turn_context: TurnContext):
dialog_context = await dialogs.create_context(turn_context)
results = await dialog_context.continue_dialog()
if results.status == DialogTurnStatus.Empty:
options = PromptOptions(
prompt=Activity(
type=ActivityTypes.message, text="please send an event."
)
)
await dialog_context.prompt("EventActivityPrompt", options)
elif results.status == DialogTurnStatus.Complete:
await turn_context.send_activity(results.result)
await convo_state.save_changes(turn_context)
# Initialize TestAdapter.
adapter = TestAdapter(exec_test)
# Create ConversationState with MemoryStorage and register the state as middleware.
convo_state = ConversationState(MemoryStorage())
# Create a DialogState property, DialogSet and AttachmentPrompt.
dialog_state = convo_state.create_property("dialog_state")
dialogs = DialogSet(dialog_state)
dialogs.add(SimpleActivityPrompt("EventActivityPrompt", validator))
event_activity = Activity(type=ActivityTypes.event, value=2)
step1 = await adapter.send("hello")
step2 = await step1.assert_reply("please send an event.")
step3 = await step2.send(event_activity)
await step3.assert_reply("2")
async def test_retry_activity_prompt(self):
async def exec_test(turn_context: TurnContext):
dialog_context = await dialogs.create_context(turn_context)
results = await dialog_context.continue_dialog()
if results.status == DialogTurnStatus.Empty:
options = PromptOptions(
prompt=Activity(
type=ActivityTypes.message, text="please send an event."
)
)
await dialog_context.prompt("EventActivityPrompt", options)
elif results.status == DialogTurnStatus.Complete:
await turn_context.send_activity(results.result)
await convo_state.save_changes(turn_context)
# Initialize TestAdapter.
adapter = TestAdapter(exec_test)
# Create ConversationState with MemoryStorage and register the state as middleware.
convo_state = ConversationState(MemoryStorage())
# Create a DialogState property, DialogSet and AttachmentPrompt.
dialog_state = convo_state.create_property("dialog_state")
dialogs = DialogSet(dialog_state)
dialogs.add(SimpleActivityPrompt("EventActivityPrompt", validator))
event_activity = Activity(type=ActivityTypes.event, value=2)
step1 = await adapter.send("hello")
step2 = await step1.assert_reply("please send an event.")
step3 = await step2.send("hello again")
step4 = await step3.assert_reply(
"Please send an 'event'-type Activity with a value of 2."
)
step5 = await step4.send(event_activity)
await step5.assert_reply("2")
async def test_activity_prompt_should_return_dialog_end_if_validation_failed(self):
async def exec_test(turn_context: TurnContext):
dialog_context = await dialogs.create_context(turn_context)
results = await dialog_context.continue_dialog()
if results.status == DialogTurnStatus.Empty:
options = PromptOptions(
prompt=Activity(
type=ActivityTypes.message, text="please send an event."
),
retry_prompt=Activity(
type=ActivityTypes.message, text="event not received."
),
)
await dialog_context.prompt("EventActivityPrompt", options)
elif results.status == DialogTurnStatus.Complete:
await turn_context.send_activity(results.result)
await convo_state.save_changes(turn_context)
async def aux_validator(prompt_context: PromptValidatorContext):
assert prompt_context, "Validator missing prompt_context"
return False
# Initialize TestAdapter.
adapter = TestAdapter(exec_test)
# Create ConversationState with MemoryStorage and register the state as middleware.
convo_state = ConversationState(MemoryStorage())
# Create a DialogState property, DialogSet and AttachmentPrompt.
dialog_state = convo_state.create_property("dialog_state")
dialogs = DialogSet(dialog_state)
dialogs.add(SimpleActivityPrompt("EventActivityPrompt", aux_validator))
step1 = await adapter.send("hello")
step2 = await step1.assert_reply("please send an event.")
step3 = await step2.send("test")
await step3.assert_reply("event not received.")
async def test_activity_prompt_resume_dialog_should_return_dialog_end(self):
async def exec_test(turn_context: TurnContext):
dialog_context = await dialogs.create_context(turn_context)
results = await dialog_context.continue_dialog()
if results.status == DialogTurnStatus.Empty:
options = PromptOptions(
prompt=Activity(
type=ActivityTypes.message, text="please send an event."
)
)
await dialog_context.prompt("EventActivityPrompt", options)
second_results = await event_prompt.resume_dialog(
dialog_context, DialogReason.NextCalled
)
assert (
second_results.status == DialogTurnStatus.Waiting
), "resume_dialog did not returned Dialog.EndOfTurn"
await convo_state.save_changes(turn_context)
async def aux_validator(prompt_context: PromptValidatorContext):
assert prompt_context, "Validator missing prompt_context"
return False
# Initialize TestAdapter.
adapter = TestAdapter(exec_test)
# Create ConversationState with MemoryStorage and register the state as middleware.
convo_state = ConversationState(MemoryStorage())
# Create a DialogState property, DialogSet and AttachmentPrompt.
dialog_state = convo_state.create_property("dialog_state")
dialogs = DialogSet(dialog_state)
event_prompt = SimpleActivityPrompt("EventActivityPrompt", aux_validator)
dialogs.add(event_prompt)
step1 = await adapter.send("hello")
step2 = await step1.assert_reply("please send an event.")
await step2.assert_reply("please send an event.")
async def test_activity_prompt_onerror_should_return_dialogcontext(self):
# Create ConversationState with MemoryStorage and register the state as middleware.
convo_state = ConversationState(MemoryStorage())
# Create a DialogState property, DialogSet and AttachmentPrompt.
dialog_state = convo_state.create_property("dialog_state")
dialogs = DialogSet(dialog_state)
dialogs.add(SimpleActivityPrompt("EventActivityPrompt", validator))
async def exec_test(turn_context: TurnContext):
dialog_context = await dialogs.create_context(turn_context)
results = await dialog_context.continue_dialog()
if results.status == DialogTurnStatus.Empty:
options = PromptOptions(
prompt=Activity(
type=ActivityTypes.message, text="please send an event."
)
)
try:
await dialog_context.prompt("EventActivityPrompt", options)
await dialog_context.prompt("Non existent id", options)
except Exception as err:
self.assertIsNotNone(
err.data["DialogContext"] # pylint: disable=no-member
)
self.assertEqual(
err.data["DialogContext"][ # pylint: disable=no-member
"active_dialog"
],
"EventActivityPrompt",
)
else:
raise Exception("Should have thrown an error.")
elif results.status == DialogTurnStatus.Complete:
await turn_context.send_activity(results.result)
await convo_state.save_changes(turn_context)
# Initialize TestAdapter.
adapter = TestAdapter(exec_test)
await adapter.send("hello")
async def test_activity_replace_dialog_onerror_should_return_dialogcontext(self):
# Create ConversationState with MemoryStorage and register the state as middleware.
convo_state = ConversationState(MemoryStorage())
# Create a DialogState property, DialogSet and AttachmentPrompt.
dialog_state = convo_state.create_property("dialog_state")
dialogs = DialogSet(dialog_state)
dialogs.add(SimpleActivityPrompt("EventActivityPrompt", validator))
async def exec_test(turn_context: TurnContext):
dialog_context = await dialogs.create_context(turn_context)
results = await dialog_context.continue_dialog()
if results.status == DialogTurnStatus.Empty:
options = PromptOptions(
prompt=Activity(
type=ActivityTypes.message, text="please send an event."
)
)
try:
await dialog_context.prompt("EventActivityPrompt", options)
await dialog_context.replace_dialog("Non existent id", options)
except Exception as err:
self.assertIsNotNone(
err.data["DialogContext"] # pylint: disable=no-member
)
else:
raise Exception("Should have thrown an error.")
elif results.status == DialogTurnStatus.Complete:
await turn_context.send_activity(results.result)
await convo_state.save_changes(turn_context)
# Initialize TestAdapter.
adapter = TestAdapter(exec_test)
await adapter.send("hello")
|