8000 PrivateConversationState implemented with basic unit test, more testi… · sherlock666/botbuilder-python@8ce8c17 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8ce8c17

Browse files
authored
PrivateConversationState implemented with basic unit test, more testing pending. (microsoft#290)
1 parent 2d5b9d1 commit 8ce8c17

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

libraries/botbuilder-core/botbuilder/core/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from .message_factory import MessageFactory
2424
from .middleware_set import AnonymousReceiveMiddleware, Middleware, MiddlewareSet
2525
from .null_telemetry_client import NullTelemetryClient
26+
from .private_conversation_state import PrivateConversationState
2627
from .recognizer import Recognizer
2728
from .recognizer_result import RecognizerResult, TopIntent
2829
from .show_typing_middleware import ShowTypingMiddleware
@@ -55,6 +56,7 @@
5556
"Middleware",
5657
"MiddlewareSet",
5758
"NullTelemetryClient",
59+
"PrivateConversationState",
5860
"Recognizer",
5961
"RecognizerResult",
6062
"Severity",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from .bot_state import BotState
2+
from .turn_context import TurnContext
3+
from .storage import Storage
4+
5+
6+
class PrivateConversationState(BotState):
7+
def __init__(self, storage: Storage, namespace: str = ""):
8+
async def aux_func(context: TurnContext) -> str:
9+
nonlocal self
10+
return await self.get_storage_key(context)
11+
12+
self.namespace = namespace
13+
super().__init__(storage, aux_func)
14+
15+
def get_storage_key(self, turn_context: TurnContext) -> str:
16+
activity = turn_context.activity
17+
channel_id = activity.channel_id if activity is not None else None
18+
19+
if not channel_id:
20+
raise Exception("missing activity.channel_id")
21+
22+
if activity and activity.conversation and activity.conversation.id is not None:
23+
conversation_id = activity.conversation.id
24+
else:
25+
raise Exception("missing activity.conversation.id")
26+
27+
if (
28+
activity
29+
and activity.from_property
30+
and activity.from_property.id is not None
31+
):
32+
user_id = activity.from_property.id
33+
else:
34+
raise Exception("missing activity.from_property.id")
35+
36+
return f"{channel_id}/conversations/{ conversation_id }/users/{ user_id }/{ self.namespace }"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import aiounittest
2+
3+
from botbuilder.core import MemoryStorage, TurnContext, PrivateConversationState
4+
from botbuilder.core.adapters import TestAdapter
5+
from botbuilder.schema import Activity, ChannelAccount, ConversationAccount
6+
7+
RECEIVED_MESSAGE = Activity(
8+
text="received",
9+
type="message",
10+
channel_id="test",
11+
conversation=ConversationAccount(id="convo"),
12+
from_property=ChannelAccount(id="user"),
13+
)
14+
15+
16+
class TestPrivateConversationState(aiounittest.AsyncTestCase):
17+
async def test_should_load_and_save_state_from_storage(self):
18+
storage = MemoryStorage()
19+
adapter = TestAdapter()
20+
context = TurnContext(adapter, RECEIVED_MESSAGE)
21+
private_conversation_state = PrivateConversationState(storage)
22+
23+
# Simulate a "Turn" in a conversation by loading the state,
24+
# changing it and then saving the changes to state.
25+
await private_conversation_state.load(context)
26+
key = private_conversation_state.get_storage_key(context)
27+
state = private_conversation_state.get(context)
28+
assert state == {}, "State not loaded"
29+
assert key, "Key not found"
30+
state["test"] = "foo"
31+
await private_conversation_state.save_changes(context)
32+
33+
# Check the storage to see if the changes to state were saved.
34+
items = await storage.read([key])
35+
assert key in items, "Saved state not found in storage."
36+
assert items[key]["test"] == "foo", "Missing test value in stored state."

0 commit comments

Comments
 (0)
0