From e1e4dd55ccdb74018bf1b14778558c362d8c11c8 Mon Sep 17 00:00:00 2001 From: Jessica Wailes Date: Tue, 23 Jun 2020 13:30:43 -0700 Subject: [PATCH 1/2] added assertNoReply to TestAdapter --- .../botbuilder/core/adapters/test_adapter.py | 42 +++++++++++++++++++ .../tests/test_test_adapter.py | 25 +++++++++++ 2 files changed, 67 insertions(+) diff --git a/libraries/botbuilder-core/botbuilder/core/adapters/test_adapter.py b/libraries/botbuilder-core/botbuilder/core/adapters/test_adapter.py index fed4388b5..7f44a7384 100644 --- a/libraries/botbuilder-core/botbuilder/core/adapters/test_adapter.py +++ b/libraries/botbuilder-core/botbuilder/core/adapters/test_adapter.py @@ -652,6 +652,48 @@ async def wait_for_activity(): return TestFlow(await test_flow_previous(), self.adapter) + async def assert_no_reply( + self, + description=None, + timeout=None, # pylint: disable=unused-argument + ) -> "TestFlow": + """ + Generates an assertion if the bot responds when no response is expected. + :param description: + :param timeout: + """ + if description is None: + description = "" + + async def test_flow_previous(): + nonlocal timeout + if not timeout: + timeout = 3000 + start = datetime.now() + adapter = self.adapter + + async def wait_for_activity(): + nonlocal timeout + current = datetime.now() + + if (current - start).total_seconds() * 1000 > timeout: + # operation timed out and recieved no reply + return + + elif adapter.activity_buffer: + reply = adapter.activity_buffer.pop(0) + raise RuntimeError( + f"TestAdapter.assert_no_reply(): '{reply.text}' is responded when waiting for no reply." + ) + + else: + await asyncio.sleep(0.05) + await wait_for_activity() + + await wait_for_activity() + + return TestFlow(await test_flow_previous(), self.adapter) + def validate_activity(activity, expected) -> None: """ Helper method that compares activities diff --git a/libraries/botbuilder-core/tests/test_test_adapter.py b/libraries/botbuilder-core/tests/test_test_adapter.py index 4312ca352..4c38b01f9 100644 --- a/libraries/botbuilder-core/tests/test_test_adapter.py +++ b/libraries/botbuilder-core/tests/test_test_adapter.py @@ -245,3 +245,28 @@ async def test_get_user_token_returns_token_with_magice_code(self): assert token_response assert token == token_response.token assert connection_name == token_response.connection_name + + async def test_should_validate_no_reply_when_no_reply_expected(self): + async def logic(context: TurnContext): + await context.send_activity(RECEIVED_MESSAGE) + + adapter = TestAdapter(logic) + test_flow = await adapter.test("test", "received") + await test_flow.assert_no_reply("should be no additional replies") + + async def test_should_timeout_waiting_for_assert_no_reply_when_no_reply_expected(self): + async def logic(context: TurnContext): + await context.send_activity(RECEIVED_MESSAGE) + + adapter = TestAdapter(logic) + test_flow = await adapter.test("test", "received") + await test_flow.assert_no_reply("no reply received", 500) + + async def test_should_throw_error_with_assert_no_reply_when_no_reply_expected_but_was_recieved(self): + async def logic(context: TurnContext): + activities = [RECEIVED_MESSAGE, RECEIVED_MESSAGE] + await context.send_activities(activities) + + adapter = TestAdapter(logic) + test_flow = await adapter.test("test", "received") + await test_flow.assert_no_reply("should be no additional replies") From 2e76985a0e326cce5de21d12d540a614a6f577d7 Mon Sep 17 00:00:00 2001 From: Jessica Wailes Date: Tue, 23 Jun 2020 14:11:36 -0700 Subject: [PATCH 2/2] adding AssertNoReply to TestAdapter --- .../botbuilder/core/adapters/test_adapter.py | 8 +++----- libraries/botbuilder-core/tests/test_test_adapter.py | 8 ++++++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/libraries/botbuilder-core/botbuilder/core/adapters/test_adapter.py b/libraries/botbuilder-core/botbuilder/core/adapters/test_adapter.py index 7f44a7384..0f6792ca9 100644 --- a/libraries/botbuilder-core/botbuilder/core/adapters/test_adapter.py +++ b/libraries/botbuilder-core/botbuilder/core/adapters/test_adapter.py @@ -651,11 +651,8 @@ async def wait_for_activity(): return TestFlow(await test_flow_previous(), self.adapter) - async def assert_no_reply( - self, - description=None, - timeout=None, # pylint: disable=unused-argument + self, description=None, timeout=None, # pylint: disable=unused-argument ) -> "TestFlow": """ Generates an assertion if the bot responds when no response is expected. @@ -664,7 +661,7 @@ async def assert_no_reply( """ if description is None: description = "" - + async def test_flow_previous(): nonlocal timeout if not timeout: @@ -694,6 +691,7 @@ async def wait_for_activity(): return TestFlow(await test_flow_previous(), self.adapter) + def validate_activity(activity, expected) -> None: """ Helper method that compares activities diff --git a/libraries/botbuilder-core/tests/test_test_adapter.py b/libraries/botbuilder-core/tests/test_test_adapter.py index 4c38b01f9..0d9d88e38 100644 --- a/libraries/botbuilder-core/tests/test_test_adapter.py +++ b/libraries/botbuilder-core/tests/test_test_adapter.py @@ -254,7 +254,9 @@ async def logic(context: TurnContext): test_flow = await adapter.test("test", "received") await test_flow.assert_no_reply("should be no additional replies") - async def test_should_timeout_waiting_for_assert_no_reply_when_no_reply_expected(self): + async def test_should_timeout_waiting_for_assert_no_reply_when_no_reply_expected( + self, + ): async def logic(context: TurnContext): await context.send_activity(RECEIVED_MESSAGE) @@ -262,7 +264,9 @@ async def logic(context: TurnContext): test_flow = await adapter.test("test", "received") await test_flow.assert_no_reply("no reply received", 500) - async def test_should_throw_error_with_assert_no_reply_when_no_reply_expected_but_was_recieved(self): + async def test_should_throw_error_with_assert_no_reply_when_no_reply_expected_but_was_recieved( + self, + ): async def logic(context: TurnContext): activities = [RECEIVED_MESSAGE, RECEIVED_MESSAGE] await context.send_activities(activities)