|
1 |
| -# Copyright (c) Microsoft Corporation. All rights reserved. |
2 |
| -# Licensed under the MIT License. |
3 |
| - |
4 |
| -from unittest.mock import patch |
5 |
| -from typing import Dict |
6 |
| -import aiounittest |
7 |
| -from botbuilder.core.adapters import TestAdapter, TestFlow |
8 |
| -from botbuilder.schema import Activity |
9 |
| -from botbuilder.core import ( |
10 |
| - ConversationState, |
11 |
| - MemoryStorage, |
12 |
| - TurnContext, |
13 |
| - NullTelemetryClient, |
14 |
| -) |
15 |
| -from botbuilder.dialogs import ( |
16 |
| - Dialog, |
17 |
| - DialogSet, |
18 |
| - WaterfallDialog, |
19 |
| - DialogTurnResult, |
20 |
| - DialogTurnStatus, |
21 |
| -) |
22 |
| - |
23 |
| -BEGIN_MESSAGE = Activity() |
24 |
| -BEGIN_MESSAGE.text = "begin" |
25 |
| -BEGIN_MESSAGE.type = "message" |
26 |
| - |
27 |
| - |
28 |
| -class TelemetryWaterfallTests(aiounittest.AsyncTestCase): |
29 |
| - def test_none_telemetry_client(self): |
30 |
| - # arrange |
31 |
| - dialog = WaterfallDialog("myId") |
32 |
| - # act |
33 |
| - dialog.telemetry_client = None |
34 |
| - # assert |
35 |
| - self.assertEqual(type(dialog.telemetry_client), NullTelemetryClient) |
36 |
| - |
37 |
| - @patch("botbuilder.applicationinsights.ApplicationInsightsTelemetryClient") |
38 |
| - async def test_execute_sequence_waterfall_steps( # pylint: disable=invalid-name |
39 |
| - self, MockTelemetry |
40 |
| - ): |
41 |
| - # arrange |
42 |
| - |
43 |
| - # Create new ConversationState with MemoryStorage and register the state as middleware. |
44 |
| - convo_state = ConversationState(MemoryStorage()) |
45 |
| - telemetry = MockTelemetry() |
46 |
| - |
47 |
| - # Create a DialogState property, DialogSet and register the WaterfallDialog. |
48 |
| - dialog_state = convo_state.create_property("dialogState") |
49 |
| - dialogs = DialogSet(dialog_state) |
50 |
| - |
51 |
| - async def step1(step) -> DialogTurnResult: |
52 |
| - await step.context.send_activity("bot responding.") |
53 |
| - return Dialog.end_of_turn |
54 |
| - |
55 |
| - async def step2(step) -> DialogTurnResult: |
56 |
| - await step.context.send_activity("ending WaterfallDialog.") |
57 |
| - return Dialog.end_of_turn |
58 |
| - |
59 |
| - # act |
60 |
| - |
61 |
| - my_dialog = WaterfallDialog("test", [step1, step2]) |
62 |
| - my_dialog.telemetry_client = telemetry |
63 |
| - dialogs.add(my_dialog) |
64 |
| - |
65 |
| - # Initialize TestAdapter |
66 |
| - async def exec_test(turn_context: TurnContext) -> None: |
67 |
| - |
68 |
| - dialog_context = await dialogs.create_context(turn_context) |
69 |
| - results = await dialog_context.continue_dialog() |
70 |
| - if results.status == DialogTurnStatus.Empty: |
71 |
| - await dialog_context.begin_dialog("test") |
72 |
| - else: |
73 |
| - if results.status == DialogTurnStatus.Complete: |
74 |
| - await turn_context.send_activity(results.result) |
75 |
| - |
76 |
| - await convo_state.save_changes(turn_context) |
77 |
| - |
78 |
| - adapt = TestAdapter(exec_test) |
79 |
| - |
80 |
| - test_flow = TestFlow(None, adapt) |
81 |
| - tf2 = await test_flow.send(BEGIN_MESSAGE) |
82 |
| - tf3 = await tf2.assert_reply("bot responding.") |
83 |
| - tf4 = await tf3.send("continue") |
84 |
| - await tf4.assert_reply("ending WaterfallDialog.") |
85 |
| - |
86 |
| - # assert |
87 |
| - |
88 |
| - telemetry_calls = [ |
89 |
| - ("WaterfallStart", {"DialogId": "test"}), |
90 |
| - ("WaterfallStep", {"DialogId": "test", "StepName": "Step1of2"}), |
91 |
| - ("WaterfallStep", {"DialogId": "test", "StepName": "Step2of2"}), |
92 |
| - ] |
93 |
| - self.assert_telemetry_calls(telemetry, telemetry_calls) |
94 |
| - |
95 |
| - @patch("botbuilder.applicationinsights.ApplicationInsightsTelemetryClient") |
96 |
| - async def test_ensure_end_dialog_called( |
97 |
| - self, MockTelemetry |
98 |
| - ): # pylint: disable=invalid-name |
99 |
| - # arrange |
100 |
| - |
101 |
| - # Create new ConversationState with MemoryStorage and register the state as middleware. |
102 |
| - convo_state = ConversationState(MemoryStorage()) |
103 |
| - telemetry = MockTelemetry() |
104 |
| - |
105 |
| - # Create a DialogState property, DialogSet and register the WaterfallDialog. |
106 |
| - dialog_state = convo_state.create_property("dialogState") |
107 |
| - dialogs = DialogSet(dialog_state) |
108 |
| - |
109 |
| - async def step1(step) -> DialogTurnResult: |
110 |
| - await step.context.send_activity("step1 response") |
111 |
| - return Dialog.end_of_turn |
112 |
| - |
113 |
| - async def step2(step) -> DialogTurnResult: |
114 |
| - await step.context.send_activity("step2 response") |
115 |
| - return Dialog.end_of_turn |
116 |
| - |
117 |
| - # act |
118 |
| - |
119 |
| - my_dialog = WaterfallDialog("test", [step1, step2]) |
120 |
| - my_dialog.telemetry_client = telemetry |
121 |
| - dialogs.add(my_dialog) |
122 |
| - |
123 |
| - # Initialize TestAdapter |
124 |
| - async def exec_test(turn_context: TurnContext) -> None: |
125 |
| - |
126 |
| - dialog_context = await dialogs.create_context(turn_context) |
127 |
| - await dialog_context.continue_dialog() |
128 |
| - if not turn_context.responded: |
129 |
| - await dialog_context.begin_dialog("test", None) |
130 |
| - await convo_state.save_changes(turn_context) |
131 |
| - |
132 |
| - adapt = TestAdapter(exec_test) |
133 |
| - |
134 |
| - test_flow = TestFlow(None, adapt) |
135 |
| - tf2 = await test_flow.send(BEGIN_MESSAGE) |
136 |
| - tf3 = await tf2.assert_reply("step1 response") |
137 |
| - tf4 = await tf3.send("continue") |
138 |
| - tf5 = await tf4.assert_reply("step2 response") |
139 |
| - await tf5.send( |
140 |
| - "Should hit end of steps - this will restart the dialog and trigger COMPLETE event" |
141 |
| - ) |
142 |
| - # assert |
143 |
| - telemetry_calls = [ |
144 |
| - ("WaterfallStart", {"DialogId": "test"}), |
145 |
| - ("WaterfallStep", {"DialogId": "test", "StepName": "Step1of2"}), |
146 |
| - ("WaterfallStep", {"DialogId": "test", "StepName": "Step2of2"}), |
147 |
| - ("WaterfallComplete", {"DialogId": "test"}), |
148 |
| - ("WaterfallStart", {"DialogId": "test"}), |
149 |
| - ("WaterfallStep", {"DialogId": "test", "StepName": "Step1of2"}), |
150 |
| - ] |
151 |
| - print(str(telemetry.track_event.call_args_list)) |
152 |
| - self.assert_telemetry_calls(telemetry, telemetry_calls) |
153 |
| - |
154 |
| - def assert_telemetry_call( |
155 |
| - self, telemetry_mock, index: int, event_name: str, props: Dict[str, str] |
156 |
| - ) -> None: |
157 |
| - # pylint: disable=unused-variable |
158 |
| - args, kwargs = telemetry_mock.track_event.call_args_list[index] |
159 |
| - self.assertEqual(args[0], event_name) |
160 |
| - |
161 |
| - for key, val in props.items(): |
162 |
| - self.assertTrue( |
163 |
| - key in args[1], |
164 |
| - msg=f"Could not find value {key} in {args[1]} for index {index}", |
165 |
| - ) |
166 |
| - self.assertTrue(isinstance(args[1], dict)) |
167 |
| - self.assertTrue(val == args[1][key]) |
168 |
| - |
169 |
| - def assert_telemetry_calls(self, telemetry_mock, calls) -> None: |
170 |
| - index = 0 |
171 |
| - for event_name, props in calls: |
172 |
| - self.assert_telemetry_call(telemetry_mock, index, event_name, props) |
173 |
| - index += 1 |
174 |
| - if index != len(telemetry_mock.track_event.call_args_list): |
175 |
| - self.assertTrue( # pylint: disable=redundant-unittest-assert |
176 |
| - False, |
177 |
| - f"Found {len(telemetry_mock.track_event.call_args_list)} calls, testing for {index + 1}", |
178 |
| - ) |
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +from unittest.mock import MagicMock |
| 5 | +from typing import Dict |
| 6 | +import aiounittest |
| 7 | +from botbuilder.core.adapters import TestAdapter, TestFlow |
| 8 | +from botbuilder.schema import Activity |
| 9 | +from botbuilder.core import ( |
| 10 | + ConversationState, |
| 11 | + MemoryStorage, |
| 12 | + TurnContext, |
| 13 | + NullTelemetryClient, |
| 14 | +) |
| 15 | +from botbuilder.dialogs import ( |
| 16 | + Dialog, |
| 17 | + DialogSet, |
| 18 | + WaterfallDialog, |
| 19 | + DialogTurnResult, |
| 20 | + DialogTurnStatus, |
| 21 | +) |
| 22 | + |
| 23 | +BEGIN_MESSAGE = Activity() |
| 24 | +BEGIN_MESSAGE.text = "begin" |
| 25 | +BEGIN_MESSAGE.type = "message" |
| 26 | + |
| 27 | +MOCK_TELEMETRY = "botbuilder.applicationinsights.ApplicationInsightsTelemetryClient" |
| 28 | + |
| 29 | + |
| 30 | +class TelemetryWaterfallTests(aiounittest.AsyncTestCase): |
| 31 | + def test_none_telemetry_client(self): |
| 32 | + # arrange |
| 33 | + dialog = WaterfallDialog("myId") |
| 34 | + # act |
| 35 | + dialog.telemetry_client = None |
| 36 | + # assert |
| 37 | + self.assertEqual(type(dialog.telemetry_client), NullTelemetryClient) |
| 38 | + |
| 39 | + async def test_execute_sequence_waterfall_steps(self): |
| 40 | + # arrange |
| 41 | + |
| 42 | + # Create new ConversationState with MemoryStorage and register the state as middleware. |
| 43 | + convo_state = ConversationState(MemoryStorage()) |
| 44 | + telemetry = MagicMock(name=MOCK_TELEMETRY) |
| 45 | + |
| 46 | + # Create a DialogState property, DialogSet and register the WaterfallDialog. |
| 47 | + dialog_state = convo_state.create_property("dialogState") |
| 48 | + dialogs = DialogSet(dialog_state) |
| 49 | + |
| 50 | + async def step1(step) -> DialogTurnResult: |
| 51 | + await step.context.send_activity("bot responding.") |
| 52 | + return Dialog.end_of_turn |
| 53 | + |
| 54 | + async def step2(step) -> DialogTurnResult: |
| 55 | + await step.context.send_activity("ending WaterfallDialog.") |
| 56 | + return Dialog.end_of_turn |
| 57 | + |
| 58 | + # act |
| 59 | + |
| 60 | + my_dialog = WaterfallDialog("test", [step1, step2]) |
| 61 | + my_dialog.telemetry_client = telemetry |
| 62 | + dialogs.add(my_dialog) |
| 63 | + |
| 64 | + # Initialize TestAdapter |
| 65 | + async def exec_test(turn_context: TurnContext) -> None: |
| 66 | + |
| 67 | + dialog_context = await dialogs.create_context(turn_context) |
| 68 | + results = await dialog_context.continue_dialog() |
| 69 | + if results.status == DialogTurnStatus.Empty: |
| 70 | + await dialog_context.begin_dialog("test") |
| 71 | + else: |
| 72 | + if results.status == DialogTurnStatus.Complete: |
| 73 | + await turn_context.send_activity(results.result) |
| 74 | + |
| 75 | + await convo_state.save_changes(turn_context) |
| 76 | + |
| 77 | + adapt = TestAdapter(exec_test) |
| 78 | + |
| 79 | + test_flow = TestFlow(None, adapt) |
| 80 | + tf2 = await test_flow.send(BEGIN_MESSAGE) |
| 81 | + tf3 = await tf2.assert_reply("bot responding.") |
| 82 | + tf4 = await tf3.send("continue") |
| 83 | + await tf4.assert_reply("ending WaterfallDialog.") |
| 84 | + |
| 85 | + # assert |
| 86 | + |
| 87 | + telemetry_calls = [ |
| 88 | + ("WaterfallStart", {"DialogId": "test"}), |
| 89 | + ("WaterfallStep", {"DialogId": "test", "StepName": "Step1of2"}), |
| 90 | + ("WaterfallStep", {"DialogId": "test", "StepName": "Step2of2"}), |
| 91 | + ] |
| 92 | + self.assert_telemetry_calls(telemetry, telemetry_calls) |
| 93 | + |
| 94 | + async def test_ensure_end_dialog_called(self): |
| 95 | + # arrange |
| 96 | + |
| 97 | + # Create new ConversationState with MemoryStorage and register the state as middleware. |
| 98 | + convo_state = ConversationState(MemoryStorage()) |
| 99 | + telemetry = MagicMock(name=MOCK_TELEMETRY) |
| 100 | + |
| 101 | + # Create a DialogState property, DialogSet and register the WaterfallDialog. |
| 102 | + dialog_state = convo_state.create_property("dialogState") |
| 103 | + dialogs = DialogSet(dialog_state) |
| 104 | + |
| 105 | + async def step1(step) -> DialogTurnResult: |
| 106 | + await step.context.send_activity("step1 response") |
| 107 | + return Dialog.end_of_turn |
| 108 | + |
| 109 | + async def step2(step) -> DialogTurnResult: |
| 110 | + await step.context.send_activity("step2 response") |
| 111 | + return Dialog.end_of_turn |
| 112 | + |
| 113 | + # act |
| 114 | + |
| 115 | + my_dialog = WaterfallDialog("test", [step1, step2]) |
| 116 | + my_dialog.telemetry_client = telemetry |
| 117 | + dialogs.add(my_dialog) |
| 118 | + |
| 119 | + # Initialize TestAdapter |
| 120 | + async def exec_test(turn_context: TurnContext) -> None: |
| 121 | + |
| 122 | + dialog_context = await dialogs.create_context(turn_context) |
| 123 | + await dialog_context.continue_dialog() |
| 124 | + if not turn_context.responded: |
| 125 | + await dialog_context.begin_dialog("test", None) |
| 126 | + await convo_state.save_changes(turn_context) |
| 127 | + |
| 128 | + adapt = TestAdapter(exec_test) |
| 129 | + |
| 130 | + test_flow = TestFlow(None, adapt) |
| 131 | + tf2 = await test_flow.send(BEGIN_MESSAGE) |
| 132 | + tf3 = await tf2.assert_reply("step1 response") |
| 133 | + tf4 = await tf3.send("continue") |
| 134 | + tf5 = await tf4.assert_reply("step2 response") |
| 135 | + await tf5.send( |
| 136 | + "Should hit end of steps - this will restart the dialog and trigger COMPLETE event" |
| 137 | + ) |
| 138 | + # assert |
| 139 | + telemetry_calls = [ |
| 140 | + ("WaterfallStart", {"DialogId": "test"}), |
| 141 | + ("WaterfallStep", {"DialogId": "test", "StepName": "Step1of2"}), |
| 142 | + ("WaterfallStep", {"DialogId": "test", "StepName": "Step2of2"}), |
| 143 | +
EE1E
("WaterfallComplete", {"DialogId": "test"}), |
| 144 | + ("WaterfallStart", {"DialogId": "test"}), |
| 145 | + ("WaterfallStep", {"DialogId": "test", "StepName": "Step1of2"}), |
| 146 | + ] |
| 147 | + print(str(telemetry.track_event.call_args_list)) |
| 148 | + self.assert_telemetry_calls(telemetry, telemetry_calls) |
| 149 | + |
| 150 | + def assert_telemetry_call( |
| 151 | + self, telemetry_mock, index: int, event_name: str, props: Dict[str, str] |
| 152 | + ) -> None: |
| 153 | + # pylint: disable=unused-variable |
| 154 | + args, kwargs = telemetry_mock.track_event.call_args_list[index] |
| 155 | + self.assertEqual(args[0], event_name) |
| 156 | + |
| 157 | + for key, val in props.items(): |
| 158 | + self.assertTrue( |
| 159 | + key in args[1], |
| 160 | + msg=f"Could not find value {key} in {args[1]} for index {index}", |
| 161 | + ) |
| 162 | + self.assertTrue(isinstance(args[1], dict)) |
| 163 | + self.assertTrue(val == args[1][key]) |
| 164 | + |
| 165 | + def assert_telemetry_calls(self, telemetry_mock, calls) -> None: |
| 166 | + index = 0 |
| 167 | + for event_name, props in calls: |
| 168 | + self.assert_telemetry_call(telemetry_mock, index, event_name, props) |
| 169 | + index += 1 |
| 170 | + if index != len(telemetry_mock.track_event.call_args_list): |
| 171 | + self.assertTrue( # pylint: disable=redundant-unittest-assert |
| 172 | + False, |
| 173 | + f"Found {len(telemetry_mock.track_event.call_args_list)} calls, testing for {index + 1}", |
| 174 | + ) |
0 commit comments