public void TestSmartContext_DeleteEntity_DeleteShouldBeBackedUp()
        {
            using (SmartContext sCtx = new SmartContext())
            {
                User userToDelete = sCtx.GetSingle<User>();

                int userToDeleteId = userToDelete.UserId;

                sCtx.Delete(userToDelete);
                sCtx.SaveChanges();

                User deletedUsserFromOffline = this.offlineContext.Users.FirstOrDefault(usr => usr.UserId == userToDelete.UserId);

                object deleteOperation = this.offlineContext.GetAll(DeleteOperationDefinition.DeleteOperationFullTypeName)
                    .Cast<object>()
                    .FirstOrDefault(o => o.FieldValue<int>(DeleteOperationDefinition.EntityToDeleteId) == userToDeleteId);

                int deletedEntityId = deleteOperation.FieldValue<int>(DeleteOperationDefinition.EntityToDeleteId);
                string deletedEntityType = deleteOperation.FieldValue<string>((DeleteOperationDefinition.EntityToDeleteType));

                Assert.IsNull(deletedUsserFromOffline);
                Assert.AreEqual(userToDeleteId, deletedEntityId);
                Assert.AreEqual("Model.User", deletedEntityType);
            }
        }
        public void TestAutoSwitchSmartContext_BackupDeletes_SwitchBackToOnlineMode_SyncDeletes_ObjectsShouldBeDeletedFromOnlineStorage()
        {
            using (SmartContext sCtx = new SmartContext())
            {
                int usersInOnlineCountBefore = this.onlineContext.Users.Count();

                int usrToDeleteId = 1;
                User usrToDelete = sCtx.GetSingle<User>(usr => usr.UserId == usrToDeleteId);
                sCtx.Delete(usrToDelete);

                this.LockDbTable("Users");
                sCtx.SaveChanges();

                this.ReleaseLock();
                Thread.Sleep(20000);

                User usrToDeleteFromOnline = this.onlineContext.Users.FirstOrDefault(usr => usr.UserId == usrToDeleteId);
                int usersInOnlineCountAfter = this.onlineContext.Users.Count();
                Assert.AreNotEqual(usersInOnlineCountBefore, usersInOnlineCountAfter);
                Assert.AreEqual(usersInOnlineCountBefore - 1, usersInOnlineCountAfter);
                Assert.IsNull(usrToDeleteFromOnline);
            }
        }
        public void TestAutoSwitchSmartContext_SwitchOnDelete_ObjectShouldBeDeletedFromBackup()
        {
            using (SmartContext sCtx = new SmartContext())
            {
                User userToDelete = sCtx.GetSingle<User>();
                int userToDeleteId = userToDelete.UserId;
                sCtx.Delete(userToDelete);

                //Simulate that the online storage is not available
                this.LockDbTable("Users");

                //Upon saving the chanegs the SmartContext will switch to the backup storage
                //and delete the user from there,
                //it will also save the delete operation
                sCtx.SaveChanges();

                User deletedUserFromBackup = sCtx.GetSingle<User>(usr => usr.UserId == userToDelete.UserId);
                this.ReleaseLock();
                User userFromOnline = this.onlineContext.Users.FirstOrDefault(usr => usr.UserId == userToDelete.UserId);

                object deleteOperation = this.offlineContext.GetAll(DeleteOperationDefinition.DeleteOperationFullTypeName)
                    .Cast<object>()
                    .FirstOrDefault(o => o.FieldValue<int>(DeleteOperationDefinition.EntityToDeleteId) == userToDeleteId);

                int deletedEntityId = deleteOperation.FieldValue<int>(DeleteOperationDefinition.EntityToDeleteId);
                string deletedEntityType = deleteOperation.FieldValue<string>((DeleteOperationDefinition.EntityToDeleteType));

                //Deleted from the backup storage
                Assert.IsNull(deletedUserFromBackup);
                //Still present in the online storage
                Assert.IsNotNull(userFromOnline);
                //verify that the delete operation has been backed up
                Assert.AreEqual(userToDeleteId, deletedEntityId);
                Assert.AreEqual("Model.User", deletedEntityType);
            }
        }