public static async Task ContainsTest(IKeyValueStorage storage)
        {
            var key = "value_ContainsTest";
            await storage.Set(key, "asda");

            var isContains = await storage.ContainsKey(key);

            Assert.IsTrue(isContains);
            await storage.Set(key, null);

            isContains = await storage.ContainsKey(key);

            Assert.IsTrue(isContains);
            await storage.Remove(key);

            isContains = await storage.ContainsKey(key);

            Assert.IsFalse(isContains);
        }
Beispiel #2
0
        /// <summary>
        /// Return value or default Type instance (null for class and default value for struct).
        /// </summary>
        public static async Task <T> GetOrDefault <T>(this IKeyValueStorage @this, string key)
        {
            if (await @this.ContainsKey(key))
            {
                var value = await @this.Get(typeof(T), key);

                if (value == null)
                {
                    return(default(T));
                }
                return((T)value);
            }
            else
            {
                return(default(T));
            }
        }
        public static async Task SynchronizationTest(IKeyValueStorage storage)
        {
            await storage.Clear();

            var rd = new Random();

            for (int i = 0; i < 50; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    await storage.Set("key" + rd.Next(10000, 99999).ToString(), "qwwwwwwww");
                }


                string key     = "key" + i.ToString();
                string prevKey = "key" + (i - 1).ToString();

                //await storage.Clear();
                await storage.Set(prevKey, null);

                var prevVal = await storage.GetOrDefault <string>(prevKey);

                if (prevVal != null)
                {
                    Assert.Fail($"Not null value after cleaning. Iteration {i}.");
                }

                await storage.Set(key, "val");

                var val = await storage.Get <string>(key);

                Assert.AreEqual(val, "val");
                var isContains = await storage.ContainsKey(key);

                Assert.IsTrue(isContains);
            }

            await storage.Clear();
        }
        public static async Task TestScopes(IKeyValueStorage storage)
        {
            await storage.Set("ComplexType.Val1", 100);

            await storage.Set("ComplexType.Val2", "First scope string");

            await storage.Set("ComplexType.Val3.Val1", 200);

            await storage.Set("ComplexType.Val3.Val2", "Second scope string");

            await storage.Set("ComplexType.Val3.Val3.Val1", 300);

            await storage.Set("ComplexType.Val3.Val3.Val2", "Third scope string");

            var num = await storage.Get <int>("ComplexType.Val1");

            Assert.AreEqual(num, 100);
            num = await storage.Get <int>("ComplexType.Val3.Val1");

            Assert.AreEqual(num, 200);

            var str = await storage.Get <string>("ComplexType.Val3.Val2");

            Assert.AreEqual(str, "Second scope string");

            var complexType = await storage.Get <ComplexType>("ComplexType");

            Assert.AreEqual(complexType.Val3.Val3.Val1, 300);
            complexType = await storage.Get <ComplexType>("ComplexType.Val3");

            Assert.AreEqual(complexType.Val3.Val1, 300);

            await storage.Remove("ComplexType.Val3.Val3.Val1");

            var isContains = await storage.ContainsKey("ComplexType.Val3.Val3.Val1");

            Assert.IsFalse(isContains);
            isContains = await storage.ContainsKey("ComplexType.Val3.Val3");

            Assert.IsTrue(isContains);

            await storage.Remove("ComplexType.Val3.Val3.Val2");

            isContains = await storage.ContainsKey("ComplexType.Val3.Val3");

            Assert.IsFalse(isContains);
            isContains = await storage.ContainsKey("ComplexType");

            Assert.IsTrue(isContains);

            await storage.Set("ComplexType", 999);

            num = await storage.Get <int>("ComplexType");

            Assert.AreEqual(num, 999);
            isContains = await storage.ContainsKey("ComplexType.Val3.Val3");

            Assert.IsFalse(isContains);
            isContains = (await storage.ContainsKey("ComplexType.Val1"));
            Assert.IsFalse(isContains);
            isContains = (await storage.ContainsKey("ComplexType.Val2"));
            Assert.IsFalse(isContains);
            isContains = (await storage.ContainsKey("ComplexType.Val3"));
            Assert.IsFalse(isContains);
            isContains = (await storage.ContainsKey("ComplexType.Val3.Val1"));
            Assert.IsFalse(isContains);
            isContains = (await storage.ContainsKey("ComplexType.Val3.Val3"));
            Assert.IsFalse(isContains);
            isContains = (await storage.ContainsKey("ComplexType.Val3.Val3.Val1"));
            Assert.IsFalse(isContains);
        }