Exemplo n.º 1
0
        public async Task State_UseBotStateDirectly()
        {
            var adapter = new TestAdapter();

            await new TestFlow(
                adapter,
                async(context, cancellationToken) =>
            {
                var botStateManager = new TestBotState(new MemoryStorage());

                var testProperty = botStateManager.CreateProperty <CustomState>("test");

                // read initial state object
                await botStateManager.LoadAsync(context);

                var customState = await testProperty.GetAsync(context, () => new CustomState());

                // this should be a 'new CustomState' as nothing is currently stored in storage
                Assert.IsNotNull(customState);
                Assert.IsInstanceOfType(customState, typeof(CustomState));
                Assert.IsNull(customState.CustomString);

                // amend property and write to storage
                customState.CustomString = "test";
                await botStateManager.SaveChangesAsync(context);

                customState.CustomString = "asdfsadf";

                // read into context again
                await botStateManager.LoadAsync(context, force: true);

                customState = await testProperty.GetAsync(context);

                // check object read from value has the correct value for CustomString
                Assert.AreEqual("test", customState.CustomString);
            })
            .Send(new Activity()
            {
                Type = ActivityTypes.ConversationUpdate
            })
            .StartTestAsync();
        }
        public async Task BotStateGetCachedState()
        {
            var turnContext = TestUtilities.CreateEmptyContext();

            turnContext.Activity.Conversation = new ConversationAccount {
                Id = "1234"
            };

            var storage  = new MemoryStorage(new Dictionary <string, JObject>());
            var botState = new TestBotState(storage);

            (await botState
             .CreateProperty <TestPocoState>("test-name")
             .GetAsync(turnContext, () => new TestPocoState())).Value = "test-value";

            var cache = botState.GetCachedState(turnContext);

            Assert.NotNull(cache);

            Assert.Same(cache, botState.GetCachedState(turnContext));
        }
Exemplo n.º 3
0
        public async Task BotStateGet()
        {
            var turnContext = TestUtilities.CreateEmptyContext();

            turnContext.Activity.Conversation = new ConversationAccount {
                Id = "1234"
            };

            var storage = new MemoryStorage(new Dictionary <string, JObject>());

            // This was changed from ConversationSate to TestBotState
            // because TestBotState has a context service key
            // that is different from the name of its type
            var botState = new TestBotState(storage);

            (await botState
             .CreateProperty <TestPocoState>("test-name")
             .GetAsync(turnContext, () => new TestPocoState())).Value = "test-value";

            var json = botState.Get(turnContext);

            Assert.AreEqual("test-value", json["test-name"]["Value"].ToString());
        }
Exemplo n.º 4
0
        public async Task State_UseBotStateDirectly()
        {
            var adapter = new TestAdapter();

            await new TestFlow(adapter,
                               async(context, cancellationToken) =>
            {
                var botStateManager = new TestBotState(new MemoryStorage());

                var testProperty = botStateManager.CreateProperty <CustomState>("test");

                // read initial state object
                await botStateManager.LoadAsync(context);

                var customState = await testProperty.GetAsync(context, () => new CustomState());

                // this should be a 'new CustomState' as nothing is currently stored in storage
                Assert.Equals(customState, new CustomState());

                // amend property and write to storage
                customState.CustomString = "test";
                await botStateManager.SaveChangesAsync(context);

                customState.CustomString = "asdfsadf";

                // read into context again
                await botStateManager.LoadAsync(context);

                customState = await testProperty.GetAsync(context);

                // check object read from value has the correct value for CustomString
                Assert.Equals(customState.CustomString, "test");
            }
                               )
            .StartTestAsync();
        }