protected async Task _handleCrazyKeys(IStorage storage)
        {
            var    storeItems = new StoreItems();
            string key        = "!@#$%^&*()~/\\><,.?';\"`~";

            storeItems[key] = new PocoStoreItem()
            {
                Id = "1"
            };

            await storage.Write(storeItems);

            dynamic result = await storage.Read(key);

            Assert.IsNotNull(result[key], $"result['{key}'] should not be null");
            Assert.AreEqual(result[key].Id, "1", "strong .id should be 1");
        }
Ejemplo n.º 2
0
        protected async Task HandleCrazyKeys(IStorage storage)
        {
            var key       = "!@#$%^&*()~/\\><,.?';\"`~";
            var storeItem = new PocoStoreItem()
            {
                Id = "1"
            };

            var dict = new Dictionary <string, object>()
            {
                { key, storeItem }
            };

            await storage.WriteAsync(dict);

            var storeItems = await storage.ReadAsync(new[] { key });

            storeItem = storeItems.FirstOrDefault(si => si.Key == key).Value as PocoStoreItem;

            Assert.IsNotNull(storeItem);
            Assert.AreEqual("1", storeItem.Id);
        }
Ejemplo n.º 3
0
        protected async Task UpdateObjectTest(IStorage storage)
        {
            var originalPocoItem = new PocoItem()
            {
                Id = "1", Count = 1
            };
            var originalPocoStoreItem = new PocoStoreItem()
            {
                Id = "1", Count = 1
            };

            // first write should work
            var dict = new Dictionary <string, object>()
            {
                { "pocoItem", originalPocoItem },
                { "pocoStoreItem", originalPocoStoreItem },
            };

            await storage.WriteAsync(dict);

            var loadedStoreItems = new Dictionary <string, object>(await storage.ReadAsync(new[] { "pocoItem", "pocoStoreItem" }));

            var updatePocoItem      = loadedStoreItems["pocoItem"] as PocoItem;
            var updatePocoStoreItem = loadedStoreItems["pocoStoreItem"] as PocoStoreItem;

            Assert.IsNotNull(updatePocoStoreItem.ETag, "updatePocoItem.eTag  should not be null");

            // 2nd write should work, because we have new etag, or no etag
            updatePocoItem.Count++;
            updatePocoStoreItem.Count++;

            await storage.WriteAsync(loadedStoreItems);

            var reloadedStoreItems = new Dictionary <string, object>(await storage.ReadAsync(new[] { "pocoItem", "pocoStoreItem" }));

            var reloeadedUpdatePocoItem     = reloadedStoreItems["pocoItem"] as PocoItem;
            var reloadedUpdatePocoStoreItem = reloadedStoreItems["pocoStoreItem"] as PocoStoreItem;

            Assert.IsNotNull(reloadedUpdatePocoStoreItem.ETag, "updatePocoItem.eTag  should not be null");
            Assert.AreNotEqual(updatePocoStoreItem.ETag, reloadedUpdatePocoStoreItem.ETag, "updatePocoItem.eTag  should not be different");
            Assert.AreEqual(2, reloeadedUpdatePocoItem.Count, "updatePocoItem.Count should be 2");
            Assert.AreEqual(2, reloadedUpdatePocoStoreItem.Count, "updatePocoStoreItem.Count should be 2");

            // write with old etag should succeed for non-storeitem
            try
            {
                updatePocoItem.Count = 123;

                await storage.WriteAsync(
                    new Dictionary <string, object>() { { "pocoItem", updatePocoItem } });
            }
            catch
            {
                Assert.Fail("Should not throw exception on write with pocoItem");
            }

            // write with old etag should FAIL for storeitem
            try
            {
                updatePocoStoreItem.Count = 123;

                await storage.WriteAsync(
                    new Dictionary <string, object>() { { "pocoStoreItem", updatePocoStoreItem } });

                Assert.Fail("Should have thrown exception on write with store item because of old etag");
            }
            catch
            {
            }

            var reloadedStoreItems2 = new Dictionary <string, object>(await storage.ReadAsync(new[] { "pocoItem", "pocoStoreItem" }));

            var reloadedPocoItem2      = reloadedStoreItems2["pocoItem"] as PocoItem;
            var reloadedPocoStoreItem2 = reloadedStoreItems2["pocoStoreItem"] as PocoStoreItem;

            Assert.AreEqual(123, reloadedPocoItem2.Count);
            Assert.AreEqual(2, reloadedPocoStoreItem2.Count);

            // write with wildcard etag should work
            reloadedPocoItem2.Count      = 100;
            reloadedPocoStoreItem2.Count = 100;
            reloadedPocoStoreItem2.ETag  = "*";

            var wildcardEtagedict = new Dictionary <string, object>()
            {
                { "pocoItem", reloadedPocoItem2 },
                { "pocoStoreItem", reloadedPocoStoreItem2 },
            };

            await storage.WriteAsync(wildcardEtagedict);

            var reloadedStoreItems3 = new Dictionary <string, object>(await storage.ReadAsync(new[] { "pocoItem", "pocoStoreItem" }));

            Assert.AreEqual(100, (reloadedStoreItems3["pocoItem"] as PocoItem).Count);
            Assert.AreEqual(100, (reloadedStoreItems3["pocoStoreItem"] as PocoStoreItem).Count);

            // write with empty etag should not work
            try
            {
                var reloadedStoreItems4 = await storage.ReadAsync(new[] { "pocoStoreItem" });

                var reloadedStoreItem4 = reloadedStoreItems4["pocoStoreItem"] as PocoStoreItem;

                Assert.IsNotNull(reloadedStoreItem4);

                reloadedStoreItem4.ETag = string.Empty;
                var dict2 = new Dictionary <string, object>()
                {
                    { "pocoStoreItem", reloadedStoreItem4 },
                };

                await storage.WriteAsync(dict2);

                Assert.Fail("Should have thrown exception on write with storeitem because of empty etag");
            }
            catch
            {
            }

            var finalStoreItems = new Dictionary <string, object>(await storage.ReadAsync(new[] { "pocoItem", "pocoStoreItem" }));

            Assert.AreEqual(100, (finalStoreItems["pocoItem"] as PocoItem).Count);
            Assert.AreEqual(100, (finalStoreItems["pocoStoreItem"] as PocoStoreItem).Count);
        }
        protected async Task UpdateObjectTest <T>(IStorage storage)
            where T : Exception
        {
            var originalPocoItem = new PocoItem()
            {
                Id = "1", Count = 1
            };
            var originalPocoStoreItem = new PocoStoreItem()
            {
                Id = "1", Count = 1
            };

            // first write should work
            var dict = new Dictionary <string, object>()
            {
                { "pocoItem", originalPocoItem },
                { "pocoStoreItem", originalPocoStoreItem },
            };

            await storage.WriteAsync(dict);

            var loadedStoreItems = new Dictionary <string, object>(await storage.ReadAsync(new[] { "pocoItem", "pocoStoreItem" }));

            var updatePocoItem      = loadedStoreItems["pocoItem"] as PocoItem;
            var updatePocoStoreItem = loadedStoreItems["pocoStoreItem"] as PocoStoreItem;

            Assert.NotNull(updatePocoStoreItem.ETag);

            // 2nd write should work, because we have new etag, or no etag
            updatePocoItem.Count++;
            updatePocoStoreItem.Count++;

            await storage.WriteAsync(loadedStoreItems);

            var reloadedStoreItems = new Dictionary <string, object>(await storage.ReadAsync(new[] { "pocoItem", "pocoStoreItem" }));

            var reloeadedUpdatePocoItem     = reloadedStoreItems["pocoItem"] as PocoItem;
            var reloadedUpdatePocoStoreItem = reloadedStoreItems["pocoStoreItem"] as PocoStoreItem;

            Assert.NotNull(reloadedUpdatePocoStoreItem.ETag);
            Assert.NotEqual(updatePocoStoreItem.ETag, reloadedUpdatePocoStoreItem.ETag);
            Assert.Equal(2, reloeadedUpdatePocoItem.Count);
            Assert.Equal(2, reloadedUpdatePocoStoreItem.Count);

            // write with old etag should succeed for non-storeitem
            try
            {
                updatePocoItem.Count = 123;

                await storage.WriteAsync(
                    new Dictionary <string, object>() { { "pocoItem", updatePocoItem } });
            }
            catch
            {
                Assert.True(false); // This should not be hit
            }

            // write with old etag should FAIL for storeitem

            updatePocoStoreItem.Count = 123;

            await Assert.ThrowsAnyAsync <Exception>(() => storage.WriteAsync(
                                                        new Dictionary <string, object>()
            {
                { "pocoStoreItem", updatePocoStoreItem }
            }));

            var reloadedStoreItems2 = new Dictionary <string, object>(await storage.ReadAsync(new[] { "pocoItem", "pocoStoreItem" }));

            var reloadedPocoItem2      = reloadedStoreItems2["pocoItem"] as PocoItem;
            var reloadedPocoStoreItem2 = reloadedStoreItems2["pocoStoreItem"] as PocoStoreItem;

            Assert.Equal(123, reloadedPocoItem2.Count);
            Assert.Equal(2, reloadedPocoStoreItem2.Count);

            // write with wildcard etag should work
            reloadedPocoItem2.Count      = 100;
            reloadedPocoStoreItem2.Count = 100;
            reloadedPocoStoreItem2.ETag  = "*";

            var wildcardEtagedict = new Dictionary <string, object>()
            {
                { "pocoItem", reloadedPocoItem2 },
                { "pocoStoreItem", reloadedPocoStoreItem2 },
            };

            await storage.WriteAsync(wildcardEtagedict);

            var reloadedStoreItems3 = new Dictionary <string, object>(await storage.ReadAsync(new[] { "pocoItem", "pocoStoreItem" }));

            Assert.Equal(100, (reloadedStoreItems3["pocoItem"] as PocoItem).Count);
            Assert.Equal(100, (reloadedStoreItems3["pocoStoreItem"] as PocoStoreItem).Count);

            // write with empty etag should not work
            try
            {
                var reloadedStoreItems4 = await storage.ReadAsync(new[] { "pocoStoreItem" });

                var reloadedStoreItem4 = reloadedStoreItems4["pocoStoreItem"] as PocoStoreItem;

                Assert.NotNull(reloadedStoreItem4);

                reloadedStoreItem4.ETag = string.Empty;
                var dict2 = new Dictionary <string, object>()
                {
                    { "pocoStoreItem", reloadedStoreItem4 },
                };

                await storage.WriteAsync(dict2);

                Assert.True(false); // "Should have thrown exception on write with storeitem because of empty etag"
            }
            catch
            {
            }

            var finalStoreItems = new Dictionary <string, object>(await storage.ReadAsync(new[] { "pocoItem", "pocoStoreItem" }));

            Assert.Equal(100, (finalStoreItems["pocoItem"] as PocoItem).Count);
            Assert.Equal(100, (finalStoreItems["pocoStoreItem"] as PocoStoreItem).Count);
        }