Ejemplo n.º 1
0
        public void DataAccess()
        {
            //add two new items
            var item1 = new CacheableTypeOk(1, 1001, "aaa", new DateTime(2010, 10, 10), 1500);

            _client.PutOne(item1);

            var item2 = new CacheableTypeOk(2, 1002, "aaa", new DateTime(2010, 10, 10), 1600);

            _client.PutOne(item2);

            //reload the first one by primary key
            var item1Reloaded = _client.GetOne <CacheableTypeOk>(i => i.PrimaryKey == 1);

            Assert.AreEqual(item1, item1Reloaded);

            //try to load nonexistent object by primary key
            var itemNull = _client.GetOne <CacheableTypeOk>(i => i.PrimaryKey == 2055);

            Assert.IsNull(itemNull);

            //try to load nonexistent object by unique key
            itemNull = _client.GetOne <CacheableTypeOk>(i => i.UniqueKey == 55);
            Assert.IsNull(itemNull);

            //reload both items by folder name
            IList <CacheableTypeOk> itemsInAaa =
                _client.GetMany <CacheableTypeOk>(i => i.IndexKeyFolder == "aaa").ToList();

            Assert.AreEqual(itemsInAaa.Count, 2);

            //change the folder of the first item and put it back into the cache
            item1.IndexKeyFolder = "bbb";
            _client.PutOne(item1);

            //now it should be only one item left in aaa
            itemsInAaa = _client.GetMany <CacheableTypeOk>(i => i.IndexKeyFolder == "aaa").ToList();
            Assert.AreEqual(itemsInAaa.Count, 1);

            //get both of them again by date
            IList <CacheableTypeOk> allItems = _client
                                               .GetMany <CacheableTypeOk>(i => i.IndexKeyDate == new DateTime(2010, 10, 10)).ToList();

            Assert.AreEqual(allItems.Count, 2);


            //get both of them using an In query

            var folders = new[] { "aaa", "bbb" };

            allItems = _client.GetMany <CacheableTypeOk>(i => folders.Contains(i.IndexKeyFolder)).ToList();
            Assert.AreEqual(allItems.Count, 2);

            //remove the first one
            _client.RemoveMany <CacheableTypeOk>(i => i.PrimaryKey == 1);

            //removing non existent item should not throw exception
            var removed = _client.RemoveMany <CacheableTypeOk>(i => i.PrimaryKey == 46546);

            Assert.AreEqual(0, removed);

            //COUNT should also return 1
            var count = _client.Count <CacheableTypeOk>(i => folders.Contains(i.IndexKeyFolder));

            Assert.AreEqual(count, 1);
        }