public void SQLiteStorageTest_SimpleTest() { var db = new SQLiteStorage <Item1> (filename); db.Clear(); db.Set("2", new Item1 { name = "luis", counter = 2, address = "raphael" }); db.Set("1", new Item1 { name = "xpto", counter = 1, address = "xpto" }); db.Set("3", new Item1 { name = "xpto", counter = 1, address = "xpto" }); db.Set("12", new Item1 { name = "name1", counter = 12, address = "address" }); var obj = db.Find(new { counter = 1, name = "xpto" }).FirstOrDefault(); // db.Get ("1"); Assert.IsNotNull(obj, "item not found!"); Assert.IsTrue(obj.name == "xpto", "wrong item!"); Assert.IsTrue(obj.counter == 1, "wrong item!"); var obj2 = db.Get("1").Where(i => i.counter == 1 && i.name == "xpto").First(); Assert.IsNotNull(obj2, "item not found!"); Assert.IsTrue(obj2.name == "xpto", "wrong item!"); Assert.IsTrue(obj2.counter == 1, "wrong item!"); Assert.IsTrue(db.Get("1").Count() == 1, "wrong item count!"); Assert.IsTrue(db.Get(new string[] { "1", "2" }).Count() == 2, "wrong item count!"); var details = db.GetDetails("1").First(); Assert.IsTrue(details.Date.Hour == DateTime.UtcNow.Hour, "wrong date format!"); db.Clear(); db.Set("1", new Item1 { name = "luis", counter = 1, address = "raphael" }); db.Set("2", new Item1 { name = "xpto", counter = 1, address = "xpto" }); db.Set("3", new Item1 { name = "xpto", counter = 1, address = "xpto" }); db.Set("4", new Item1 { name = "name1", counter = 1, address = "address" }); var list1 = db.Get().ToList(); var list2 = db.GetAndModify(i => { i.counter = 2; return(true); }).ToList(); var list3 = db.Get().ToList(); Assert.IsTrue(list2.Count == 4, "wrong item count!"); Assert.IsTrue(list2.Sum(i => i.counter) == 8, "wrong item counter (1)!"); Assert.IsTrue(list3.Sum(i => i.counter) == list2.Sum(i => i.counter), "wrong item counter (2)!"); db.GetAndModify(i => { i.counter = 0; return(true); }).ToList(); db.GetAndModify("1", i => { i.counter = 4; return(true); }).Count(); db.GetAndModify("4", i => { i.counter = 5; return(true); }).Count(); Assert.IsTrue(db.Get().Sum(i => i.counter) == 9, "wrong item counter (3)!"); db = new SQLiteStorage <Item1> (filename, SQLiteStorageOptions.KeepItemsHistory()); db.Clear(); db.Set("1", new Item1 { name = "xpto", counter = 1, address = "xpto" }); db.Set("1", new Item1 { name = "xpto", counter = 1, address = "xpto" }); db.Set("1", new Item1 { name = "xpto", counter = 1, address = "xpto" }); db.Set("1", new Item1 { name = "xpto", counter = 1, address = "xpto" }); db.GetAndModify("1", i => { return(true); }).Count(); Assert.IsTrue(db.Get().Sum(i => i.counter) == 8, "wrong item count (4)!"); // parallel tests db = new SQLiteStorage <Item1> (filename, SQLiteStorageOptions.UniqueKeys()); db.Clear(); // populate db for (int i = 0; i < 100; i++) { db.Set(i.ToString(), new Item1 { name = i.ToString(), counter = 1, address = "xpto" }); } // parallel changes int loopUpperBound = 10; var expectedTotal = db.Get().Count(); var expectedValue = db.Get().Sum(i => i.counter) + loopUpperBound; // thread warmup System.Threading.Tasks.Parallel.For(0, loopUpperBound, i => System.Threading.Thread.Sleep(0)); // execute parallel operation var pr = System.Threading.Tasks.Parallel.For(0, loopUpperBound, i => db.GetAndModify("10", m => { m.counter += 1; return(true); }).Count()); Assert.IsTrue(pr.IsCompleted, "parallel error"); // check results var total = db.Get().Count(); var newSum = db.Get().Sum(i => i.counter); var item = db.Get("10").FirstOrDefault(); Assert.IsTrue(total == expectedTotal, "wrong item expectedTotal (Parallel)!"); Assert.IsTrue(newSum == expectedValue, "wrong item expectedValue (Parallel)! {0} != {1}", newSum, expectedValue); Assert.IsTrue(item.counter == (loopUpperBound + 1), "wrong item counter (Parallel)!{0} != {1}", item.counter, (loopUpperBound + 1)); // final cleanup db.Clear(); db.Shrink(); }
public SQLiteStorageTest() { logger.Info("Initialize"); logDb = new SQLiteStorage <string> (filename, "Log", SQLiteStorageOptions.KeepItemsHistory()); }
public void Initialize() { System.Diagnostics.Debug.WriteLine("SQLiteStorageTest.Initialize"); logDb = new SQLiteStorage <string> (filename, "Log", SQLiteStorageOptions.KeepItemsHistory()); }
public SQLiteStorageTest() { Common.Logging.LogManager.GetCurrentClassLogger().Info("Initialize"); logDb = new SQLiteStorage <string> (filename, "Log", SQLiteStorageOptions.KeepItemsHistory()); }