async def test_cosmos_storage_init_should_work_with_just_endpoint_and_key(self): storage = CosmosDbStorage(CosmosDbConfig(endpoint=cosmos_db_config.endpoint, masterkey=cosmos_db_config.masterkey)) await storage.write({'user': SimpleStoreItem()}) data = await storage.read(['user']) assert 'user' in data assert data['user'].counter == 1 assert len(data.keys()) == 1
async def test_creation_request_options_era_being_called(self): # pylint: disable=protected-access test_config = CosmosDbConfig( endpoint="https://localhost:8081", masterkey= "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", database="test-db", container="bot-storage", database_creation_options={"OfferThroughput": 1000}, container_creation_options={"OfferThroughput": 500}, ) test_id = "1" client = get_mock_client(identifier=test_id) storage = CosmosDbStorage(test_config, client) storage.database = test_id assert storage._get_or_create_database(doc_client=client, id=test_id), test_id client.CreateDatabase.assert_called_with( {"id": test_id}, test_config.database_creation_options) assert storage._get_or_create_container(doc_client=client, container=test_id), test_id client.CreateContainer.assert_called_with( "dbs/" + test_id, {"id": test_id}, test_config.container_creation_options)
async def test_cosmos_storage_init_should_work_with_just_endpoint_and_key( self): storage = CosmosDbStorage( CosmosDbConfig(endpoint=COSMOS_DB_CONFIG.endpoint, masterkey=COSMOS_DB_CONFIG.masterkey)) await storage.write({"user": SimpleStoreItem()}) data = await storage.read(["user"]) assert "user" in data assert data["user"].counter == 1 assert len(data.keys()) == 1
async def test_creation_request_options_era_being_called(self): test_config = CosmosDbConfig( endpoint='https://localhost:8081', masterkey= 'C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==', database='test-db', container='bot-storage', database_creation_options={'OfferThroughput': 1000}, container_creation_options={'OfferThroughput': 500}) test_id = '1' client = get_mock_client(id=test_id) storage = CosmosDbStorage(test_config, client) storage.db = test_id assert storage._get_or_create_database(doc_client=client, id=test_id), test_id client.CreateDatabase.assert_called_with( {'id': test_id}, test_config.database_creation_options) assert storage._get_or_create_container(doc_client=client, container=test_id), test_id client.CreateContainer.assert_called_with( 'dbs/' + test_id, {'id': test_id}, test_config.container_creation_options)
from bot import StateBot from botbuilder.azure import CosmosDbConfig, CosmosDbStorage app = Flask(__name__) loop = asyncio.get_event_loop() botadaptersettings = BotFrameworkAdapterSettings("", "") botadapter = BotFrameworkAdapter(botadaptersettings) memstore = MemoryStorage() constate = ConversationState(memstore) #userstate = UserState(memstore) key = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" cosconfig = CosmosDbConfig("https://*****:*****@app.route("/api/messages", methods=["POST"]) def messages(): if "application/json" in request.headers["content-type"]: jsonmessage = request.json else: return Response(status=415) activity = Activity().deserialize(jsonmessage) async def turn_call(turn_context):
async def test_cosmos_storage_init_should_error_without_cosmos_db_config( self): try: CosmosDbStorage(CosmosDbConfig()) except Exception as error: assert error
# Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. from unittest.mock import Mock import azure.cosmos.errors as cosmos_errors from azure.cosmos.cosmos_client import CosmosClient import pytest from botbuilder.core import StoreItem from botbuilder.azure import CosmosDbStorage, CosmosDbConfig # local cosmosdb emulator instance cosmos_db_config COSMOS_DB_CONFIG = CosmosDbConfig( endpoint="https://localhost:8081", masterkey= "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", database="test-db", container="bot-storage", ) EMULATOR_RUNNING = False async def reset(): storage = CosmosDbStorage(COSMOS_DB_CONFIG) try: storage.client.DeleteDatabase(database_link="dbs/" + COSMOS_DB_CONFIG.database) except cosmos_errors.HTTPFailure: pass def get_mock_client(identifier: str = "1"):
from aiohttp import web from botbuilder.schema import (Activity, ActivityTypes) from botbuilder.core import (BotFrameworkAdapter, BotFrameworkAdapterSettings, TurnContext, ConversationState) from botbuilder.azure import (CosmosDbStorage, CosmosDbConfig) APP_ID = '' APP_PASSWORD = '' PORT = 9000 SETTINGS = BotFrameworkAdapterSettings(APP_ID, APP_PASSWORD) ADAPTER = BotFrameworkAdapter(SETTINGS) CONFIG_FILE = 'sample_credentials_file.json' # Create CosmosStorage and ConversationState cosmos = CosmosDbStorage(CosmosDbConfig(filename=CONFIG_FILE)) # Commented out user_state because it's not being used. # user_state = UserState(memory) conversation_state = ConversationState(cosmos) # Register both State middleware on the adapter. # Commented out user_state because it's not being used. # ADAPTER.use(user_state) ADAPTER.use(conversation_state) async def create_reply_activity(request_activity, text) -> Activity: return Activity( type=ActivityTypes.message, channel_id=request_activity.channel_id, conversation=request_activity.conversation,
# Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. import pytest from botbuilder.core import StoreItem from botbuilder.azure import (CosmosDbStorage, CosmosDbConfig) # local cosmosdb emulator instance cosmos_db_config cosmos_db_config = CosmosDbConfig( endpoint='https://localhost:8081', masterkey= 'C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==', database='test-db', container='bot-storage') emulator_running = False async def reset(): storage = CosmosDbStorage(cosmos_db_config) storage.client.DeleteDatabase(database_link='dbs/' + cosmos_db_config.database, ignore_errors=True) class SimpleStoreItem(StoreItem): def __init__(self, counter=1, e_tag='*'): super(SimpleStoreItem, self).__init__() self.counter = counter self.e_tag = e_tag