22# Licensed under the MIT License.
33
44import aiounittest
5- from botbuilder .dialogs .prompts import ActivityPrompt , NumberPrompt , PromptOptions , PromptRecognizerResult
6- from botbuilder .schema import Activity , InputHints
5+ import unittest
76
8- from botbuilder .core .turn_context import TurnContext
7+ from typing import Callable
8+ from botbuilder .dialogs .prompts import (ActivityPrompt , NumberPrompt , PromptOptions , PromptRecognizerResult ,
9+ PromptValidatorContext )
10+ from botbuilder .schema import Activity , InputHints , ActivityTypes
11+
12+ from botbuilder .core import ConversationState , MemoryStorage , TurnContext , MessageFactory
913from botbuilder .core .adapters import TestAdapter
14+ from botbuilder .dialogs import DialogSet , DialogTurnStatus , DialogReason
15+
16+
17+ async def validator (prompt_context : PromptValidatorContext ):
18+ tester = unittest .TestCase ()
19+ tester .assertTrue (prompt_context .attempt_count > 0 )
20+
21+ activity = prompt_context .recognized .value
22+
23+ if activity .type == ActivityTypes .event :
24+ if int (activity .value ) == 2 :
25+ prompt_context .recognized .value = MessageFactory .text (str (activity .value ))
26+ return True
27+ else :
28+ await prompt_context .context .send_activity ("Please send an 'event'-type Activity with a value of 2." )
29+
30+ return False
31+
1032
1133class SimpleActivityPrompt (ActivityPrompt ):
12- pass
34+ def __init__ (self , dialog_id : str , validator : Callable [[PromptValidatorContext ], bool ]):
35+ super ().__init__ (dialog_id , validator )
36+
1337
1438class ActivityPromptTests (aiounittest .AsyncTestCase ):
15- async def test_does_the_things (self ):
16- my_activity = Activity (type = 'message' , text = 'I am activity message!' )
17- my_retry_prompt = Activity (type = 'message' , id = 'ididretry' , text = 'retry text hurrr' )
18- options = PromptOptions (prompt = my_activity , retry_prompt = my_retry_prompt )
19- activity_promptyy = ActivityPrompt ('myId' , 'validator thing' )
20-
21- my_context = TurnContext (TestAdapter (), my_activity )
22- my_state = {'stringy' : {'nestedkey' : 'nestedvalue' } }
23-
24- await activity_promptyy .on_prompt (my_context , state = my_state , options = options , isRetry = True )
25-
26- print ('placeholder print' )
27-
28- pass
29-
30- # def test_activity_prompt_with_empty_id_should_fail(self):
31- # empty_id = ''
32- # text_prompt = SimpleActivityPrompt(empty_id, self.validator)
33-
34- # async def validator(self):
35- # return True
36-
39+
40+ def test_activity_prompt_with_empty_id_should_fail (self ):
41+ empty_id = ''
42+ with self .assertRaises (TypeError ):
43+ SimpleActivityPrompt (empty_id , validator )
44+
45+ def test_activity_prompt_with_none_id_should_fail (self ):
46+ none_id = None
47+ with self .assertRaises (TypeError ):
48+ SimpleActivityPrompt (none_id , validator )
49+
50+ def test_activity_prompt_with_none_validator_should_fail (self ):
51+ none_validator = None
52+ with self .assertRaises (TypeError ):
53+ SimpleActivityPrompt ('EventActivityPrompt' , none_validator )
54+
55+ async def test_basic_activity_prompt (self ):
56+ async def exec_test (turn_context : TurnContext ):
57+ dc = await dialogs .create_context (turn_context )
58+
59+ results = await dc .continue_dialog ()
60+ if results .status == DialogTurnStatus .Empty :
61+ options = PromptOptions (prompt = Activity (type = ActivityTypes .message , text = 'please send an event.' ))
62+ await dc .prompt ('EventActivityPrompt' , options )
63+ elif results .status == DialogTurnStatus .Complete :
64+ await turn_context .send_activity (results .result )
65+
66+ await convo_state .save_changes (turn_context )
67+
68+ # Initialize TestAdapter.
69+ adapter = TestAdapter (exec_test )
70+
71+ # Create ConversationState with MemoryStorage and register the state as middleware.
72+ convo_state = ConversationState (MemoryStorage ())
73+
74+ # Create a DialogState property, DialogSet and AttachmentPrompt.
75+ dialog_state = convo_state .create_property ('dialog_state' )
76+ dialogs = DialogSet (dialog_state )
77+ dialogs .add (SimpleActivityPrompt ('EventActivityPrompt' , validator ))
78+
79+ event_activity = Activity (type = ActivityTypes .event , value = 2 )
80+
81+ step1 = await adapter .send ('hello' )
82+ step2 = await step1 .assert_reply ('please send an event.' )
83+ step3 = await step2 .send (event_activity )
84+ await step3 .assert_reply ('2' )
85+
86+ async def test_retry_activity_prompt (self ):
87+ async def exec_test (turn_context : TurnContext ):
88+ dc = await dialogs .create_context (turn_context )
89+
90+ results = await dc .continue_dialog ()
91+ if results .status == DialogTurnStatus .Empty :
92+ options = PromptOptions (prompt = Activity (type = ActivityTypes .message , text = 'please send an event.' ))
93+ await dc .prompt ('EventActivityPrompt' , options )
94+ elif results .status == DialogTurnStatus .Complete :
95+ await turn_context .send_activity (results .result )
96+
97+ await convo_state .save_changes (turn_context )
98+
99+ # Initialize TestAdapter.
100+ adapter = TestAdapter (exec_test )
101+
102+ # Create ConversationState with MemoryStorage and register the state as middleware.
103+ convo_state = ConversationState (MemoryStorage ())
104+
105+ # Create a DialogState property, DialogSet and AttachmentPrompt.
106+ dialog_state = convo_state .create_property ('dialog_state' )
107+ dialogs = DialogSet (dialog_state )
108+ dialogs .add (SimpleActivityPrompt ('EventActivityPrompt' , validator ))
109+
110+ event_activity = Activity (type = ActivityTypes .event , value = 2 )
111+
112+ step1 = await adapter .send ('hello' )
113+ step2 = await step1 .assert_reply ('please send an event.' )
114+ step3 = await step2 .send ('hello again' )
115+ step4 = await step3 .assert_reply ("Please send an 'event'-type Activity with a value of 2." )
116+ step5 = await step4 .send (event_activity )
117+ await step5 .assert_reply ('2' )
118+
119+ async def test_activity_prompt_should_return_dialog_end_if_validation_failed (self ):
120+ async def exec_test (turn_context : TurnContext ):
121+ dc = await dialogs .create_context (turn_context )
122+
123+ results = await dc .continue_dialog ()
124+ if results .status == DialogTurnStatus .Empty :
125+ options = PromptOptions (
126+ prompt = Activity (type = ActivityTypes .message , text = 'please send an event.' ),
127+ retry_prompt = Activity (type = ActivityTypes .message , text = 'event not received.' )
128+ )
129+ await dc .prompt ('EventActivityPrompt' , options )
130+ elif results .status == DialogTurnStatus .Complete :
131+ await turn_context .send_activity (results .result )
132+
133+ await convo_state .save_changes (turn_context )
134+
135+ async def aux_validator (prompt_context : PromptValidatorContext ):
136+ assert prompt_context , 'Validator missing prompt_context'
137+ return False
138+
139+ # Initialize TestAdapter.
140+ adapter = TestAdapter (exec_test )
141+
142+
143+
144+ # Create ConversationState with MemoryStorage and register the state as middleware.
145+ convo_state = ConversationState (MemoryStorage ())
146+
147+ # Create a DialogState property, DialogSet and AttachmentPrompt.
148+ dialog_state = convo_state .create_property ('dialog_state' )
149+ dialogs = DialogSet (dialog_state )
150+ dialogs .add (SimpleActivityPrompt ('EventActivityPrompt' , aux_validator ))
151+
152+ step1 = await adapter .send ('hello' )
153+ step2 = await step1 .assert_reply ('please send an event.' )
154+ step3 = await step2 .send ('test' )
155+ await step3 .assert_reply ('event not received.' )
156+
157+ async def test_activity_prompt_resume_dialog_should_return_dialog_end (self ):
158+ async def exec_test (turn_context : TurnContext ):
159+ dc = await dialogs .create_context (turn_context )
160+
161+ results = await dc .continue_dialog ()
162+ if results .status == DialogTurnStatus .Empty :
163+ options = PromptOptions (prompt = Activity (type = ActivityTypes .message , text = 'please send an event.' ))
164+ await dc .prompt ('EventActivityPrompt' , options )
165+
166+ second_results = await event_prompt .resume_dialog (dc , DialogReason .NextCalled )
167+
168+ assert second_results .status == DialogTurnStatus .Waiting , 'resume_dialog did not returned Dialog.EndOfTurn'
169+
170+ await convo_state .save_changes (turn_context )
171+
172+ async def aux_validator (prompt_context : PromptValidatorContext ):
173+ assert prompt_context , 'Validator missing prompt_context'
174+ return False
175+
176+ # Initialize TestAdapter.
177+ adapter = TestAdapter (exec_test )
178+
179+ # Create ConversationState with MemoryStorage and register the state as middleware.
180+ convo_state = ConversationState (MemoryStorage ())
181+
182+ # Create a DialogState property, DialogSet and AttachmentPrompt.
183+ dialog_state = convo_state .create_property ('dialog_state' )
184+ dialogs = DialogSet (dialog_state )
185+ event_prompt = SimpleActivityPrompt ('EventActivityPrompt' , aux_validator )
186+ dialogs .add (event_prompt )
187+
188+ step1 = await adapter .send ('hello' )
189+ step2 = await step1 .assert_reply ('please send an event.' )
190+ await step2 .assert_reply ('please send an event.' )
0 commit comments