public async Task TestCollections() { var path = System.IO.Path.GetTempFileName(); var conn = new SQLite.SQLiteAsyncConnection(path); var db = new TestDatabase(conn); await db.Reset(); var sqliteThingCache = new SqliteClassCache <Parent, long>(db); await sqliteThingCache.Setup(); var sqliteGadgetCache = new SqliteClassCache <Child, string>(db); await sqliteGadgetCache.Setup(); var cache = new ModelCache(aClassCache: new Dictionary <Type, IModelClassCache>() { { typeof(Parent), sqliteThingCache }, { typeof(Child), sqliteGadgetCache } }); var cascade = new CascadeDataLayer(origin, new ICascadeCache[] { cache }, new CascadeConfig() { DefaultFreshnessSeconds = 1 }); var collection_key = "my_things"; var ids = ImmutableArray.Create <object>(1, 2, 3); var response = await cache.Fetch(RequestOp.QueryOp <Parent>(collection_key, new JsonObject(), 0)); Assert.AreEqual(false, response.Exists); Assert.AreEqual(null, response.Result); await cache.StoreCollection(typeof(Parent), collection_key, ids, 0); response = await cache.Fetch(RequestOp.QueryOp <Parent>(collection_key, null, 0)); Assert.IsTrue(CascadeTypeUtils.IsEqualEnumerable(ids, response.ResultIds)); response = await cache.Fetch(RequestOp.QueryOp <Parent>("not_my_key", null, 0)); Assert.IsFalse(response.Exists); response = await cache.Fetch(RequestOp.QueryOp <Child>(collection_key, null, 0)); Assert.IsFalse(response.Exists); }
public async Task TestMetadata() { var path = System.IO.Path.GetTempFileName(); var conn = new SQLite.SQLiteAsyncConnection(path); var db = new TestDatabase(conn); await db.Reset(); var sqliteThingCache = new SqliteClassCache <Parent, long>(db); await sqliteThingCache.Setup(); var sqliteGadgetCache = new SqliteClassCache <Child, string>(db); await sqliteGadgetCache.Setup(); var cache1 = new ModelCache(aClassCache: new Dictionary <Type, IModelClassCache>() { { typeof(Parent), sqliteThingCache }, { typeof(Child), sqliteGadgetCache } }); // read from origin var cascade = new CascadeDataLayer(origin, new ICascadeCache[] { cache1 }, new CascadeConfig() { DefaultFreshnessSeconds = 1 }); var thing5 = new Parent() { id = 5, colour = "red" }; var thing5ArrivedAt = cascade.NowMs; await sqliteThingCache.Store(thing5.id, thing5, thing5ArrivedAt); origin.IncNowMs(); var gadget6 = new Child() { id = "abc", weight = 2.5, power = 9.2 }; var gadget6ArrivedAt = cascade.NowMs; await sqliteGadgetCache.Store(gadget6.id, gadget6, gadget6ArrivedAt); origin.IncNowMs(); var opResponse = await sqliteThingCache.Fetch(RequestOp.GetOp <Parent>(thing5.id, cascade.NowMs)); var loaded5 = (opResponse.Result as Parent) !; Assert.AreEqual(thing5ArrivedAt, opResponse.ArrivedAtMs); Assert.AreEqual(thing5.colour, loaded5.colour); opResponse = await sqliteGadgetCache.Fetch(RequestOp.GetOp <Child>(gadget6.id, cascade.NowMs)); var loaded6 = (opResponse.Result as Child) !; Assert.AreEqual(gadget6ArrivedAt, opResponse.ArrivedAtMs); Assert.AreEqual(gadget6.weight, loaded6.weight); await sqliteGadgetCache.Clear(); // thing unaffected opResponse = await sqliteThingCache.Fetch(RequestOp.GetOp <Parent>(thing5.id, cascade.NowMs)); Assert.NotNull(opResponse.Result); Assert.AreEqual(thing5ArrivedAt, opResponse.ArrivedAtMs); // gadget cleared including metadata opResponse = await sqliteGadgetCache.Fetch(RequestOp.GetOp <Child>(gadget6.id, cascade.NowMs)); Assert.IsNull(opResponse.Result); Assert.IsNull(opResponse.ArrivedAtMs); var meta6 = await db.Get <CascadeModelMeta>(CascadeModelMeta.GenerateId <Child>(6)); Assert.IsNull(meta6); }