Spaces:
Build error
Build error
File size: 20,846 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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
from typing import List
import aiounittest
from botbuilder.core import (
ConversationState,
MemoryStorage,
TurnContext,
MessageFactory,
)
from botbuilder.core.adapters import TestAdapter
from botbuilder.dialogs import DialogSet, DialogTurnResult, DialogTurnStatus
from botbuilder.dialogs.choices import Choice, ChoiceFactoryOptions, ListStyle
from botbuilder.dialogs.prompts import (
ConfirmPrompt,
PromptCultureModel,
PromptOptions,
PromptValidatorContext,
)
from botbuilder.schema import Activity, ActivityTypes
class ConfirmPromptTest(aiounittest.AsyncTestCase):
def test_confirm_prompt_with_empty_id_should_fail(self):
empty_id = ""
with self.assertRaises(TypeError):
ConfirmPrompt(empty_id)
def test_confirm_prompt_with_none_id_should_fail(self):
none_id = None
with self.assertRaises(TypeError):
ConfirmPrompt(none_id)
async def test_confirm_prompt(self):
async def exec_test(turn_context: TurnContext):
dialog_context = await dialogs.create_context(turn_context)
results: DialogTurnResult = await dialog_context.continue_dialog()
if results.status == DialogTurnStatus.Empty:
options = PromptOptions(
prompt=Activity(type=ActivityTypes.message, text="Please confirm.")
)
await dialog_context.prompt("ConfirmPrompt", options)
elif results.status == DialogTurnStatus.Complete:
message_text = "Confirmed" if results.result else "Not confirmed"
await turn_context.send_activity(MessageFactory.text(message_text))
await convo_state.save_changes(turn_context)
# Initialize TestAdapter.
adapter = TestAdapter(exec_test)
# Create new ConversationState with MemoryStorage and register the state as middleware.
convo_state = ConversationState(MemoryStorage())
# Create a DialogState property, DialogSet, and ChoicePrompt.
dialog_state = convo_state.create_property("dialogState")
dialogs = DialogSet(dialog_state)
confirm_prompt = ConfirmPrompt("ConfirmPrompt", default_locale="English")
dialogs.add(confirm_prompt)
step1 = await adapter.send("hello")
step2 = await step1.assert_reply("Please confirm. (1) Yes or (2) No")
step3 = await step2.send("yes")
await step3.assert_reply("Confirmed")
async def test_confirm_prompt_retry(self):
async def exec_test(turn_context: TurnContext):
dialog_context = await dialogs.create_context(turn_context)
results: DialogTurnResult = await dialog_context.continue_dialog()
if results.status == DialogTurnStatus.Empty:
options = PromptOptions(
prompt=Activity(type=ActivityTypes.message, text="Please confirm."),
retry_prompt=Activity(
type=ActivityTypes.message,
text="Please confirm, say 'yes' or 'no' or something like that.",
),
)
await dialog_context.prompt("ConfirmPrompt", options)
elif results.status == DialogTurnStatus.Complete:
message_text = "Confirmed" if results.result else "Not confirmed"
await turn_context.send_activity(MessageFactory.text(message_text))
await convo_state.save_changes(turn_context)
# Initialize TestAdapter.
adapter = TestAdapter(exec_test)
# Create new ConversationState with MemoryStorage and register the state as middleware.
convo_state = ConversationState(MemoryStorage())
# Create a DialogState property, DialogSet, and ChoicePrompt.
dialog_state = convo_state.create_property("dialogState")
dialogs = DialogSet(dialog_state)
confirm_prompt = ConfirmPrompt("ConfirmPrompt", default_locale="English")
dialogs.add(confirm_prompt)
step1 = await adapter.send("hello")
step2 = await step1.assert_reply("Please confirm. (1) Yes or (2) No")
step3 = await step2.send("lala")
step4 = await step3.assert_reply(
"Please confirm, say 'yes' or 'no' or something like that. (1) Yes or (2) No"
)
step5 = await step4.send("no")
await step5.assert_reply("Not confirmed")
async def test_confirm_prompt_no_options(self):
async def exec_test(turn_context: TurnContext):
dialog_context = await dialogs.create_context(turn_context)
results: DialogTurnResult = await dialog_context.continue_dialog()
if results.status == DialogTurnStatus.Empty:
await dialog_context.prompt("ConfirmPrompt", PromptOptions())
elif results.status == DialogTurnStatus.Complete:
message_text = "Confirmed" if results.result else "Not confirmed"
await turn_context.send_activity(MessageFactory.text(message_text))
await convo_state.save_changes(turn_context)
# Initialize TestAdapter.
adapter = TestAdapter(exec_test)
# Create new ConversationState with MemoryStorage and register the state as middleware.
convo_state = ConversationState(MemoryStorage())
# Create a DialogState property, DialogSet, and ChoicePrompt.
dialog_state = convo_state.create_property("dialogState")
dialogs = DialogSet(dialog_state)
confirm_prompt = ConfirmPrompt("ConfirmPrompt", default_locale="English")
dialogs.add(confirm_prompt)
step1 = await adapter.send("hello")
step2 = await step1.assert_reply(" (1) Yes or (2) No")
step3 = await step2.send("lala")
step4 = await step3.assert_reply(" (1) Yes or (2) No")
step5 = await step4.send("no")
await step5.assert_reply("Not confirmed")
async def test_confirm_prompt_choice_options_numbers(self):
async def exec_test(turn_context: TurnContext):
dialog_context = await dialogs.create_context(turn_context)
results: DialogTurnResult = await dialog_context.continue_dialog()
if results.status == DialogTurnStatus.Empty:
options = PromptOptions(
prompt=Activity(type=ActivityTypes.message, text="Please confirm."),
retry_prompt=Activity(
type=ActivityTypes.message,
text="Please confirm, say 'yes' or 'no' or something like that.",
),
)
await dialog_context.prompt("ConfirmPrompt", options)
elif results.status == DialogTurnStatus.Complete:
message_text = "Confirmed" if results.result else "Not confirmed"
await turn_context.send_activity(MessageFactory.text(message_text))
await convo_state.save_changes(turn_context)
# Initialize TestAdapter.
adapter = TestAdapter(exec_test)
# Create new ConversationState with MemoryStorage and register the state as middleware.
convo_state = ConversationState(MemoryStorage())
# Create a DialogState property, DialogSet, and ChoicePrompt.
dialog_state = convo_state.create_property("dialogState")
dialogs = DialogSet(dialog_state)
confirm_prompt = ConfirmPrompt("ConfirmPrompt", default_locale="English")
confirm_prompt.choice_options = ChoiceFactoryOptions(include_numbers=True)
confirm_prompt.style = ListStyle.in_line
dialogs.add(confirm_prompt)
step1 = await adapter.send("hello")
step2 = await step1.assert_reply("Please confirm. (1) Yes or (2) No")
step3 = await step2.send("lala")
step4 = await step3.assert_reply(
"Please confirm, say 'yes' or 'no' or something like that. (1) Yes or (2) No"
)
step5 = await step4.send("2")
await step5.assert_reply("Not confirmed")
async def test_confirm_prompt_choice_options_multiple_attempts(self):
async def exec_test(turn_context: TurnContext):
dialog_context = await dialogs.create_context(turn_context)
results: DialogTurnResult = await dialog_context.continue_dialog()
if results.status == DialogTurnStatus.Empty:
options = PromptOptions(
prompt=Activity(type=ActivityTypes.message, text="Please confirm."),
retry_prompt=Activity(
type=ActivityTypes.message,
text="Please confirm, say 'yes' or 'no' or something like that.",
),
)
await dialog_context.prompt("ConfirmPrompt", options)
elif results.status == DialogTurnStatus.Complete:
message_text = "Confirmed" if results.result else "Not confirmed"
await turn_context.send_activity(MessageFactory.text(message_text))
await convo_state.save_changes(turn_context)
# Initialize TestAdapter.
adapter = TestAdapter(exec_test)
# Create new ConversationState with MemoryStorage and register the state as middleware.
convo_state = ConversationState(MemoryStorage())
# Create a DialogState property, DialogSet, and ChoicePrompt.
dialog_state = convo_state.create_property("dialogState")
dialogs = DialogSet(dialog_state)
confirm_prompt = ConfirmPrompt("ConfirmPrompt", default_locale="English")
confirm_prompt.choice_options = ChoiceFactoryOptions(include_numbers=True)
confirm_prompt.style = ListStyle.in_line
dialogs.add(confirm_prompt)
step1 = await adapter.send("hello")
step2 = await step1.assert_reply("Please confirm. (1) Yes or (2) No")
step3 = await step2.send("lala")
step4 = await step3.assert_reply(
"Please confirm, say 'yes' or 'no' or something like that. (1) Yes or (2) No"
)
step5 = await step4.send("what")
step6 = await step5.assert_reply(
"Please confirm, say 'yes' or 'no' or something like that. (1) Yes or (2) No"
)
step7 = await step6.send("2")
await step7.assert_reply("Not confirmed")
async def test_confirm_prompt_options_no_numbers(self):
async def exec_test(turn_context: TurnContext):
dialog_context = await dialogs.create_context(turn_context)
results: DialogTurnResult = await dialog_context.continue_dialog()
if results.status == DialogTurnStatus.Empty:
options = PromptOptions(
prompt=Activity(type=ActivityTypes.message, text="Please confirm."),
retry_prompt=Activity(
type=ActivityTypes.message,
text="Please confirm, say 'yes' or 'no' or something like that.",
),
)
await dialog_context.prompt("ConfirmPrompt", options)
elif results.status == DialogTurnStatus.Complete:
message_text = "Confirmed" if results.result else "Not confirmed"
await turn_context.send_activity(MessageFactory.text(message_text))
await convo_state.save_changes(turn_context)
# Initialize TestAdapter.
adapter = TestAdapter(exec_test)
# Create new ConversationState with MemoryStorage and register the state as middleware.
convo_state = ConversationState(MemoryStorage())
# Create a DialogState property, DialogSet, and ChoicePrompt.
dialog_state = convo_state.create_property("dialogState")
dialogs = DialogSet(dialog_state)
confirm_prompt = ConfirmPrompt("ConfirmPrompt", default_locale="English")
confirm_prompt.choice_options = ChoiceFactoryOptions(
include_numbers=False, inline_separator="~"
)
dialogs.add(confirm_prompt)
step1 = await adapter.send("hello")
step2 = await step1.assert_reply("Please confirm. Yes or No")
step3 = await step2.send("2")
step4 = await step3.assert_reply(
"Please confirm, say 'yes' or 'no' or something like that. Yes or No"
)
step5 = await step4.send("no")
await step5.assert_reply("Not confirmed")
async def test_confirm_prompt_should_default_to_english_locale(self):
async def exec_test(turn_context: TurnContext):
dialog_context = await dialogs.create_context(turn_context)
results: DialogTurnResult = await dialog_context.continue_dialog()
if results.status == DialogTurnStatus.Empty:
options = PromptOptions(
prompt=Activity(type=ActivityTypes.message, text="Please confirm."),
retry_prompt=Activity(
type=ActivityTypes.message,
text="Please confirm, say 'yes' or 'no' or something like that.",
),
)
await dialog_context.prompt("ConfirmPrompt", options)
elif results.status == DialogTurnStatus.Complete:
message_text = "Confirmed" if results.result else "Not confirmed"
await turn_context.send_activity(MessageFactory.text(message_text))
await convo_state.save_changes(turn_context)
locales = [None, "", "not-supported"]
for locale in locales:
# Initialize TestAdapter.
adapter = TestAdapter(exec_test)
# Create new ConversationState with MemoryStorage and register the state as middleware.
convo_state = ConversationState(MemoryStorage())
# Create a DialogState property, DialogSet, and ChoicePrompt.
dialog_state = convo_state.create_property("dialogState")
dialogs = DialogSet(dialog_state)
confirm_prompt = ConfirmPrompt("ConfirmPrompt")
confirm_prompt.choice_options = ChoiceFactoryOptions(include_numbers=True)
dialogs.add(confirm_prompt)
step1 = await adapter.send(
Activity(type=ActivityTypes.message, text="Hello", locale=locale)
)
step2 = await step1.assert_reply("Please confirm. (1) Yes or (2) No")
step3 = await step2.send("lala")
step4 = await step3.assert_reply(
"Please confirm, say 'yes' or 'no' or something like that. (1) Yes or (2) No"
)
step5 = await step4.send("2")
await step5.assert_reply("Not confirmed")
async def test_should_recognize_locale_variations_of_correct_locales(self):
def cap_ending(locale: str) -> str:
return f"{locale.split('-')[0]}-{locale.split('-')[1].upper()}"
def title_ending(locale: str) -> str:
return locale[:3] + locale[3].upper() + locale[4:]
def cap_two_letter(locale: str) -> str:
return locale.split("-")[0].upper()
def lower_two_letter(locale: str) -> str:
return locale.split("-")[0].upper()
async def exec_test_for_locale(valid_locale: str, locale_variations: List):
# Hold the correct answer from when a valid locale is used
expected_answer = None
def inspector(activity: Activity, description: str):
nonlocal expected_answer
assert not description
if valid_locale == test_locale:
expected_answer = activity.text
else:
# Ensure we're actually testing a variation.
assert activity.locale != valid_locale
assert activity.text == expected_answer
return True
async def exec_test(turn_context: TurnContext):
dialog_context = await dialogs.create_context(turn_context)
results: DialogTurnResult = await dialog_context.continue_dialog()
if results.status == DialogTurnStatus.Empty:
options = PromptOptions(
prompt=Activity(
type=ActivityTypes.message, text="Please confirm."
)
)
await dialog_context.prompt("prompt", options)
elif results.status == DialogTurnStatus.Complete:
confirmed = results.result
if confirmed:
await turn_context.send_activity("true")
else:
await turn_context.send_activity("false")
await convo_state.save_changes(turn_context)
async def validator(prompt: PromptValidatorContext) -> bool:
assert prompt
if not prompt.recognized.succeeded:
await prompt.context.send_activity("Bad input.")
return prompt.recognized.succeeded
test_locale = None
for test_locale in locale_variations:
adapter = TestAdapter(exec_test)
convo_state = ConversationState(MemoryStorage())
dialog_state = convo_state.create_property("dialogState")
dialogs = DialogSet(dialog_state)
choice_prompt = ConfirmPrompt("prompt", validator)
dialogs.add(choice_prompt)
step1 = await adapter.send(
Activity(
type=ActivityTypes.message, text="Hello", locale=test_locale
)
)
await step1.assert_reply(inspector)
locales = [
"zh-cn",
"nl-nl",
"en-us",
"fr-fr",
"de-de",
"it-it",
"ja-jp",
"ko-kr",
"pt-br",
"es-es",
"tr-tr",
"de-de",
]
locale_tests = []
for locale in locales:
locale_tests.append(
[
locale,
cap_ending(locale),
title_ending(locale),
cap_two_letter(locale),
lower_two_letter(locale),
]
)
# Test each valid locale
for locale_tests in locale_tests:
await exec_test_for_locale(locale_tests[0], locale_tests)
async def test_should_recognize_and_use_custom_locale_dict(
self,
):
async def exec_test(turn_context: TurnContext):
dialog_context = await dialogs.create_context(turn_context)
results: DialogTurnResult = await dialog_context.continue_dialog()
if results.status == DialogTurnStatus.Empty:
options = PromptOptions(
prompt=Activity(type=ActivityTypes.message, text="Please confirm.")
)
await dialog_context.prompt("prompt", options)
elif results.status == DialogTurnStatus.Complete:
selected_choice = results.result
await turn_context.send_activity(selected_choice.value)
await convo_state.save_changes(turn_context)
async def validator(prompt: PromptValidatorContext) -> bool:
assert prompt
if not prompt.recognized.succeeded:
await prompt.context.send_activity("Bad input.")
return prompt.recognized.succeeded
adapter = TestAdapter(exec_test)
convo_state = ConversationState(MemoryStorage())
dialog_state = convo_state.create_property("dialogState")
dialogs = DialogSet(dialog_state)
culture = PromptCultureModel(
locale="custom-locale",
no_in_language="customNo",
yes_in_language="customYes",
separator="customSeparator",
inline_or="customInlineOr",
inline_or_more="customInlineOrMore",
)
custom_dict = {
culture.locale: (
Choice(culture.yes_in_language),
Choice(culture.no_in_language),
ChoiceFactoryOptions(
culture.separator, culture.inline_or, culture.inline_or_more, True
),
)
}
confirm_prompt = ConfirmPrompt("prompt", validator, choice_defaults=custom_dict)
dialogs.add(confirm_prompt)
step1 = await adapter.send(
Activity(type=ActivityTypes.message, text="Hello", locale=culture.locale)
)
await step1.assert_reply(
"Please confirm. (1) customYescustomInlineOr(2) customNo"
)
|