8000 add BotState tests, allow TestAdapter to take no args to closer align… · umarfarook882/botbuilder-python@d0da6fe · GitHub
[go: up one dir, main page]

Skip to content

Commit d0da6fe

Browse files
committed
add BotState tests, allow TestAdapter to take no args to closer align with js
1 parent c563149 commit d0da6fe

File tree

5 files changed

+111
-4
lines changed

5 files changed

+111
-4
lines changed

libraries/botbuilder-core/botbuilder/core/bot_state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ async def clear(self, context: BotContext):
8383
"""
8484
cached = context.services.get(self.state_key)
8585
if cached is not None:
86-
cached['state'] = {}
86+
cached['state'] = StoreItem()
8787
context.services[self.state_key] = cached
8888

8989
async def get(self, context: BotContext):

libraries/botbuilder-core/botbuilder/core/test_adapter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
class TestAdapter(BotAdapter):
15-
def __init__(self, logic: Coroutine, template: ConversationReference=None):
15+
def __init__(self, logic: Coroutine=None, template: ConversationReference=None):
1616
"""
1717
Creates a new TestAdapter instance.
1818
:param logic:
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import pytest
5+
6+
from botbuilder.core import BotContext, BotState, MemoryStorage, TestAdapter
7+
from botbuilder.schema import Activity
8+
9+
RECEIVED_MESSAGE = Activity(type='message',
10+
text='received')
11+
STORAGE_KEY = 'stateKey'
12+
13+
14+
def cached_state(context, state_key):
15+
cached = context.services.get(state_key)
16+
return cached['state'] if cached is not None else None
17+
18+
19+
def key_factory(context):
20+
assert context is not None
21+
return STORAGE_KEY
22+
23+
24+
class TestBotState:
25+
storage = MemoryStorage()
26+
adapter = TestAdapter()
27+
context = BotContext(adapter, RECEIVED_MESSAGE)
28+
middleware = BotState(storage, key_factory)
29+
30+
@pytest.mark.asyncio
31+
async def test_should_return_undefined_from_get_if_nothing_cached(self):
32+
state = await self.middleware.get(self.context)
33+
assert state is None, 'state returned'
34+
35+
@pytest.mark.asyncio
36+
async def test_should_load_and_save_state_from_storage(self):
37+
38+
async def next_middleware():
39+
state = cached_state(self.context, self.middleware.state_key)
40+
assert state is not None, 'state not loaded'
41+
state.test = 'foo'
42+
43+
await self.middleware.on_process_request(self.context, next_middleware)
44+
items = await self.storage.read([STORAGE_KEY])
45+
assert STORAGE_KEY in items, 'saved state not found in storage.'
46+
assert items[STORAGE_KEY].test == 'foo', 'Missing test value in stored state.'
47+
48+
@pytest.mark.skipif(True, reason='skipping while goal of test is investigated, test currently fails')
49+
@pytest.mark.asyncio
50+
async def test_should_force_read_of_state_from_storage(self):
51+
async def next_middleware():
52+
state = cached_state(self.context, self.middleware.state_key)
53+
assert state.test == 'foo', 'invalid initial state'
54+
del state.test
55+
56+
# items will not have the attribute 'test'
57+
items = await self.middleware.read(self.context, True)
58+
# Similarly, the returned value from cached_state will also not have the attribute 'test'
59+
assert cached_state(self.context, self.middleware.state_key).test == 'foo', 'state not reloaded'
60+
61+
await self.middleware.on_process_request(self.context, next_middleware)
62+
63+
@pytest.mark.asyncio
64+
async def test_should_clear_state_storage(self):
65+
66+
async def next_middleware():
67+
assert cached_state(self.context, self.middleware.state_key).test == 'foo', 'invalid initial state'
68+
await self.middleware.clear(self.context)
69+
cached_state_data = cached_state(self.context, self.middleware.state_key)
70+
assert not hasattr(cached_state_data, 'test'), 'state not cleared on context.'
71+
72+
await self.middleware.on_process_request(self.context, next_middleware)
73+
items = await self.storage.read([STORAGE_KEY])
74+
assert not hasattr(items[STORAGE_KEY], 'test'), 'state not cleared from storage.'
75+
76+
@pytest.mark.asyncio
77+
async def test_should_force_immediate_write_of_state_to_storage(self):
78+
async def next_middleware():
79+
state = cached_state(self.context, self.middleware.state_key)
80+
assert not hasattr(state, 'test'), 'invalid initial state'
81+
state.test = 'foo'
82+
83+
await self.middleware.write(self.context, True)
84+
items = await self.storage.read([STORAGE_KEY])
85+
assert items[STORAGE_KEY].test == 'foo', 'state not immediately flushed.'
86+
await self.middleware.on_process_request(self.context, next_middleware)
87+
88+
@pytest.mark.asyncio
89+
async def test_should_read_from_storage_if_cached_state_missing(self):
90+
self.context.services[self.middleware.state_key] = None
91+
state = await self.middleware.read(self.context)
92+
assert state.test == 'foo', 'state not loaded'
93+
94+
@pytest.mark.asyncio
95+
async def test_should_read_from_cache(self):
96+
state = await self.middleware.read(self.context)
97+
assert state.test == 'foo', 'state not loaded'
98+
99+
@pytest.mark.asyncio
100+
async def test_should_force_write_to_storage_of_an_empty_state_object(self):
101+
self.context.services[self.middleware.state_key] = None
102+
await self.middleware.write(self.context, True)
103+
104+
@pytest.mark.asyncio
105+
async def test_should_noop_calls_to_clear_when_nothing_cached(self):
106+
self.context.services[self.middleware.state_key] = None
107+
await self.middleware.clear(self.context)

libraries/botbuilder-core/tests/test_conversation_state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
class TestConversationState:
2525
storage = MemoryStorage()
26-
adapter = TestAdapter(None)
26+
adapter = TestAdapter()
2727
context = BotContext(adapter, RECEIVED_MESSAGE)
2828
middleware = ConversationState(storage)
2929

libraries/botbuilder-core/tests/test_user_state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
class TestUserState:
2222
storage = MemoryStorage()
23-
adapter = TestAdapter(None)
23+
adapter = TestAdapter()
2424
context = BotContext(adapter, RECEIVED_MESSAGE)
2525
middleware = UserState(storage)
2626

0 commit comments

Comments
 (0)
0