-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from southworkscom/add-date-time-prompt-test
Add tests for DateTimePrompt and NumberPrompt
- Loading branch information
Showing
4 changed files
with
107 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
libraries/botbuilder-dialogs/tests/test_dateTime_prompt.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
import aiounittest | ||
from botbuilder.dialogs.prompts import (DateTimePrompt, PromptOptions) | ||
from botbuilder.core import MessageFactory | ||
from botbuilder.core import (ConversationState, MemoryStorage, TurnContext) | ||
from botbuilder.dialogs import (DialogSet, DialogTurnStatus) | ||
from botbuilder.core.adapters import (TestAdapter, TestFlow) | ||
|
||
|
||
class DatetimePromptTests(aiounittest.AsyncTestCase): | ||
|
||
async def test_date_time_prompt(self): | ||
# Create new ConversationState with MemoryStorage and register the state as middleware. | ||
conver_state = ConversationState(MemoryStorage()) | ||
|
||
# Create a DialogState property | ||
dialog_state = conver_state.create_property('dialogState') | ||
|
||
#Create new DialogSet. | ||
dialogs = DialogSet(dialog_state) | ||
|
||
#Create and add DateTime prompt to DialogSet. | ||
dateTimePrompt = DateTimePrompt('DateTimePrompt') | ||
|
||
dialogs.add(dateTimePrompt) | ||
|
||
# Initialize TestAdapter | ||
async def exec_test(turn_context: TurnContext) -> None: | ||
prompt_msg = 'What date would you like?' | ||
dc = await dialogs.create_context(turn_context) | ||
|
||
results = await dc.continue_dialog() | ||
if results.status == DialogTurnStatus.Empty: | ||
|
||
options = PromptOptions( | ||
prompt=MessageFactory.text(prompt_msg) | ||
) | ||
await dc.begin_dialog('DateTimePrompt', options) | ||
else: | ||
if results.status == DialogTurnStatus.Complete: | ||
resolution = results.result[0] | ||
reply = MessageFactory.text(f"Timex: '{resolution.timex}' Value: '{resolution.value}'") | ||
await turn_context.send_activity(reply) | ||
await conver_state.save_changes(turn_context) | ||
|
||
adapt = TestAdapter(exec_test) | ||
|
||
tf = TestFlow(None, adapt) | ||
tf2 = await tf.send('hello') | ||
tf3 = await tf2.assert_reply('What date would you like?') | ||
tf4 = await tf3.send('5th December 2018 at 9am') | ||
tf5 = await tf4.assert_reply("Timex: '2018-12-05T09' Value: '2018-12-05 09:00:00'") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,48 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
import aiounittest | ||
from botbuilder.dialogs.prompts import NumberPrompt | ||
from botbuilder.dialogs.prompts import NumberPrompt, PromptOptions | ||
from botbuilder.core import MemoryStorage, ConversationState, TurnContext, MessageFactory | ||
from botbuilder.core.adapters import TestAdapter, TestFlow | ||
from botbuilder.dialogs import DialogSet, DialogTurnStatus | ||
|
||
class NumberPromptTests(aiounittest.AsyncTestCase): | ||
def test_empty_should_fail(self): | ||
empty_id = '' | ||
self.assertRaises(TypeError, lambda:NumberPrompt(empty_id)) | ||
|
||
|
||
async def test_number_prompt(self): | ||
# Create new ConversationState with MemoryStorage and register the state as middleware. | ||
conver_state = ConversationState(MemoryStorage()) | ||
|
||
# Create a DialogState property, DialogSet and register the WaterfallDialog. | ||
dialog_state = conver_state.create_property('dialogState') | ||
|
||
dialogs = DialogSet(dialog_state) | ||
|
||
# Create and add number prompt to DialogSet. | ||
numberPrompt = NumberPrompt('NumberPrompt', None, 'English') | ||
dialogs.add(numberPrompt) | ||
|
||
async def exec_test(turn_context: TurnContext) -> None: | ||
|
||
dialogContext = await dialogs.create_context(turn_context) | ||
results = await dialogContext.continue_dialog() | ||
|
||
if results.status == DialogTurnStatus.Empty: | ||
await dialogContext.begin_dialog('NumberPrompt', PromptOptions(prompt = MessageFactory.text('Enter quantity of cable'))) | ||
else: | ||
if results.status == DialogTurnStatus.Complete: | ||
numberResult = results.result | ||
await turn_context.send_activity(MessageFactory.text(f"You asked me for '{numberResult}' meters of cable.")) | ||
|
||
await conver_state.save_changes(turn_context) | ||
|
||
adapter = TestAdapter(exec_test) | ||
|
||
test_flow = TestFlow(None, adapter) | ||
|
||
test_flow2 = await test_flow.send('Hello') | ||
test_flow3 = await test_flow2.assert_reply('Enter quantity of cable') | ||
test_flow4 = await test_flow3.send('Give me twenty meters of cable') | ||
test_flow5 = await test_flow4.assert_reply("You asked me for '20' meters of cable.") |