public async Task PopulateHasManyAndBelongsToWithoutCache() { Parent[] allParent = new[] { new Parent() { id = 1, colour = "red" }, new Parent() { id = 2, colour = "green" } }; foreach (var p in allParent) { await parentOrigin.Store(p.id, p); } Child[] allChildren = new[] { new Child() { id = "5", parentId = 1, age = 2 }, new Child() { id = "6", parentId = 1, age = 4 }, new Child() { id = "7", parentId = 2, age = 5 }, new Child() { id = "8", parentId = 2, age = 7 }, }; foreach (var c in allChildren) { await childOrigin.Store(c.id, c); } var cascade = new CascadeDataLayer(origin, new ICascadeCache[] { }, new CascadeConfig()); var parent = await cascade.Get <Parent>(1); Assert.AreEqual(1, parent !.id); Assert.IsNull(parent.Children); await cascade.Populate(parent, "Children"); Assert.AreEqual(2, parent.Children !.Count()); Assert.IsTrue(parent.Children !.Any(c => c.id == "5")); Assert.IsTrue(parent.Children !.Any(c => c.id == "6")); Assert.IsFalse(parent.Children !.Any(c => c.id == "7")); var child = parent.Children !.FirstOrDefault() !; await cascade.Populate(child, "Parent"); Assert.AreSame(parent, child.Parent); }
public async Task QueryWithSqliteCache() { var path = System.IO.Path.GetTempFileName(); var conn = new SQLite.SQLiteAsyncConnection(path); var db = new TestDatabase(conn); await db.Reset(); Child[] allGadgets = new[] { new Child() { id = "aaa", power = 1, weight = 100 }, new Child() { id = "bbb", power = 2, weight = 123 }, new Child() { id = "ccc", power = 3, weight = 456 }, new Child() { id = "ddd", power = 4, weight = 100 } }; foreach (var t in allGadgets) { await gadgetOrigin.Store(t.id, t); } var memoryGadgetCache = new ModelClassCache <Child, string>(); var memoryCache = new ModelCache(aClassCache: new Dictionary <Type, IModelClassCache>() { { typeof(Child), memoryGadgetCache } }); var sqliteGadgetCache = new SqliteClassCache <Child, string>(db); await sqliteGadgetCache.Setup(); var sqliteCache = new ModelCache(aClassCache: new Dictionary <Type, IModelClassCache>() { { typeof(Child), sqliteGadgetCache } }); var cascade = new CascadeDataLayer(origin, new ICascadeCache[] { memoryCache, sqliteCache }, new CascadeConfig()); var gadgets100 = await cascade.Query <Child>("gadgets100", new JsonObject { ["weight"] = 100 }); var gadgets100Ids = gadgets100.Select(t => t.id).ToImmutableArray(); Assert.That(gadgets100Ids, Is.EqualTo(new string[] { "aaa", "ddd" })); }
public async Task Simple() { Parent[] allThings = new[] { new Parent() { id = 1, colour = "red" }, new Parent() { id = 2, colour = "green" }, new Parent() { id = 3, colour = "red" }, new Parent() { id = 4, colour = "yellow" }, }; foreach (var t in allThings) { await thingOrigin.Store(t.id, t); } var thingModelStore1 = new ModelClassCache <Parent, long>(); var cache1 = new ModelCache(aClassCache: new Dictionary <Type, IModelClassCache>() { { typeof(Parent), thingModelStore1 } }); var cascade = new CascadeDataLayer(origin, new ICascadeCache[] { cache1 }, new CascadeConfig()); var redThings = await cascade.Query <Parent>("red_things", new JsonObject { ["colour"] = "red" }); var redIds = redThings.Select(t => t.id).ToImmutableArray(); Assert.That(redIds, Is.EqualTo(new long[] { 1, 3 })); // check that collection & models are in cache Parent?thing; thing = await thingModelStore1.Fetch <Parent>(1); Assert.AreEqual(1, thing.id); Assert.AreEqual("red", thing.colour); thing = await thingModelStore1.Fetch <Parent>(3); Assert.AreEqual(3, thing.id); Assert.AreEqual("red", thing.colour); thing = await thingModelStore1.Fetch <Parent>(2); Assert.AreEqual(null, thing); var rcBefore = origin.RequestCount; origin.IncNowMs(); // request with freshness=2 var redThings2 = await cascade.Query <Parent>("red_things", new JsonObject { ["colour"] = "red" }, freshnessSeconds : 2); Assert.That(redThings2.Select(t => t.id).ToImmutableArray(), Is.EqualTo(new long[] { 1, 3 })); // same response Assert.AreEqual(rcBefore, origin.RequestCount); // didn't use origin rcBefore = origin.RequestCount; origin.IncNowMs(); redThings2 = (await cascade.Query <Parent>("red_things", new JsonObject { ["colour"] = "red" }, freshnessSeconds: 0)).ToImmutableArray(); Assert.That(redThings2.Select(t => t.id).ToImmutableArray(), Is.EqualTo(new long[] { 1, 3 })); // same response Assert.AreEqual(rcBefore + 1, origin.RequestCount); // did use origin }