|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | +import aiounittest |
| 4 | +from botbuilder.dialogs.prompts import (DateTimePrompt, PromptOptions) |
| 5 | +from botbuilder.core import MessageFactory |
| 6 | +from botbuilder.core import (ConversationState, MemoryStorage, TurnContext) |
| 7 | +from botbuilder.dialogs import (DialogSet, DialogTurnStatus) |
| 8 | +from botbuilder.core.adapters import (TestAdapter, TestFlow) |
| 9 | + |
| 10 | + |
| 11 | +class DatetimePromptTests(aiounittest.AsyncTestCase): |
| 12 | + |
| 13 | + async def test_date_time_prompt(self): |
| 14 | + # Create new ConversationState with MemoryStorage and register the state as middleware. |
| 15 | + conver_state = ConversationState(MemoryStorage()) |
| 16 | + |
| 17 | + # Create a DialogState property |
| 18 | + dialog_state = conver_state.create_property('dialogState') |
| 19 | + |
| 20 | + #Create new DialogSet. |
| 21 | + dialogs = DialogSet(dialog_state) |
| 22 | + |
| 23 | + #Create and add DateTime prompt to DialogSet. |
| 24 | + dateTimePrompt = DateTimePrompt('DateTimePrompt') |
| 25 | + |
| 26 | + dialogs.add(dateTimePrompt) |
| 27 | + |
| 28 | + # Initialize TestAdapter |
| 29 | + async def exec_test(turn_context: TurnContext) -> None: |
| 30 | + prompt_msg = 'What date would you like?' |
| 31 | + dc = await dialogs.create_context(turn_context) |
| 32 | + |
| 33 | + results = await dc.continue_dialog() |
| 34 | + if results.status == DialogTurnStatus.Empty: |
| 35 | + |
| 36 | + options = PromptOptions( |
| 37 | + prompt=MessageFactory.text(prompt_msg) |
| 38 | + ) |
| 39 | + await dc.begin_dialog('DateTimePrompt', options) |
| 40 | + else: |
| 41 | + if results.status == DialogTurnStatus.Complete: |
| 42 | + resolution = results.result[0] |
| 43 | + reply = MessageFactory.text(f"Timex: '{resolution.timex}' Value: '{resolution.value}'") |
| 44 | + await turn_context.send_activity(reply) |
| 45 | + await conver_state.save_changes(turn_context) |
| 46 | + |
| 47 | + adapt = TestAdapter(exec_test) |
| 48 | + |
| 49 | + tf = TestFlow(None, adapt) |
| 50 | + tf2 = await tf.send('hello') |
| 51 | + tf3 = await tf2.assert_reply('What date would you like?') |
| 52 | + tf4 = await tf3.send('5th December 2018 at 9am') |
| 53 | + tf5 = await tf4.assert_reply("Timex: '2018-12-05T09' Value: '2018-12-05 09:00:00'") |
0 commit comments