示例#1
0
        public async Task CreateReadUpdateDeleteTest()
        {
            var path = System.IO.Path.GetTempFileName();
            var conn = new SQLite.SQLiteAsyncConnection(path);
            var db   = new TestDatabase(conn);
            await db.Reset();

            var thing1 = new Parent()
            {
                colour = "red"
            };

            thing1 = await db.Create <Parent>(thing1);

            Assert.Greater(thing1.id, 0);

            var loaded = await db.Get <Parent>(thing1.id);

            Assert.AreEqual(thing1.id, loaded.id);

            thing1.colour = "green";
            await db.Update(thing1);

            var thing2 = await db.Get <Parent>(thing1.id);

            Assert.AreEqual(thing1.colour, thing2.colour);

            await db.Delete(thing2);

            var thing3 = await db.Get <Parent>(thing2.id);

            Assert.IsNull(thing3);
        }
示例#2
0
        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);
        }