Ejemplo n.º 1
0
        public void TestSoftDeleteServiceGetSoftDeletedEntriesOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>();

            using (var context = new SingleSoftDelDbContext(options))
            {
                context.Database.EnsureCreated();
                var book1 = context.AddBookWithReviewToDb("test1");
                var book2 = context.AddBookWithReviewToDb("test2");

                var config  = new ConfigSoftDeleteWithUserId(context);
                var service = new SingleSoftDeleteService <ISingleSoftDelete>(context, config);
                var status  = service.SetSoftDelete(book1);
                status.IsValid.ShouldBeTrue(status.GetAllErrors());
            }
            using (var context = new SingleSoftDelDbContext(options))
            {
                var config  = new ConfigSoftDeleteWithUserId(context);
                var service = new SingleSoftDeleteService <ISingleSoftDelete>(context, config);

                //ATTEMPT
                var softDelBooks = service.GetSoftDeletedEntries <Book>().ToList();

                //VERIFY
                softDelBooks.Count.ShouldEqual(1);
                softDelBooks.Single().Title.ShouldEqual("test1");
                context.Books.Count().ShouldEqual(1);
                context.Books.IgnoreQueryFilters().Count().ShouldEqual(2);
            }
        }
        public void TestSetSoftDeleteOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>();

            using var context = new SingleSoftDelDbContext(options);
            context.Database.EnsureCreated();
            var shadowClass = new ShadowDelClass();

            context.Add(shadowClass);
            context.SaveChanges();

            context.ChangeTracker.Clear();

            var config  = new ConfigSoftDeleteShadowDel(context);
            var service = new SingleSoftDeleteService <IShadowSoftDelete>(config);

            //ATTEMPT
            var status = service.SetSoftDeleteViaKeys <ShadowDelClass>(shadowClass.Id);

            //VERIFY
            status.IsValid.ShouldBeTrue(status.GetAllErrors());
            status.Result.ShouldEqual(1);
            context.ShadowDelClasses.Count().ShouldEqual(0);
            context.ShadowDelClasses.IgnoreQueryFilters().Count().ShouldEqual(1);
        }
Ejemplo n.º 3
0
        public void TestSoftDeleteServiceSetSoftDddDeleteViaKeysOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>();

            using (var context = new SingleSoftDelDbContext(options))
            {
                context.Database.EnsureCreated();
                var bookDdd = new BookDDD("Test");
                context.Add(bookDdd);
                context.SaveChanges();

                var config  = new ConfigSoftDeleteDDD();
                var service = new SingleSoftDeleteService <ISingleSoftDeletedDDD>(context, config);

                //ATTEMPT
                var status = service.SetSoftDeleteViaKeys <BookDDD>(bookDdd.Id);

                //VERIFY
                status.IsValid.ShouldBeTrue(status.GetAllErrors());
                status.Result.ShouldEqual(1);
            }
            using (var context = new SingleSoftDelDbContext(options))
            {
                context.BookDdds.Count().ShouldEqual(0);
                context.BookDdds.IgnoreQueryFilters().Count().ShouldEqual(1);
            }
        }
        public void TestSoftDeleteServiceResetSoftDeleteNoCallSaveChangesOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>();

            using (var context = new SingleSoftDelDbContext(options))
            {
                context.Database.EnsureCreated();
                var book = context.AddBookWithReviewToDb();

                var config  = new ConfigSoftDeleteWithUserId(context);
                var service = new SingleSoftDeleteService <ISingleSoftDelete>(config);
                service.SetSoftDelete(book);

                //ATTEMPT
                var status = service.ResetSoftDelete(book, false);
                context.Books.Count().ShouldEqual(0);
                context.SaveChanges();
                context.Books.Count().ShouldEqual(1);

                //VERIFY
                status.IsValid.ShouldBeTrue(status.GetAllErrors());
                status.Result.ShouldEqual(1);

                context.ChangeTracker.Clear();
                context.Books.IgnoreQueryFilters().Count().ShouldEqual(1);
            }
        }
Ejemplo n.º 5
0
        public void TestSoftDeleteOrderWithAddressOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>();

            using var context = new SingleSoftDelDbContext(options);
            context.Database.EnsureCreated();
            context.Add(new Order
            {
                OrderRef    = "123",
                UserAddress = new Address {
                    FullAddress = "xxx"
                }
            });
            context.SaveChanges();

            context.ChangeTracker.Clear();

            var config  = new ConfigSoftDeleteWithUserId(context);
            var service = new SingleSoftDeleteService <ISingleSoftDelete>(config);

            //ATTEMPT
            var status = service.SetSoftDeleteViaKeys <Order>(1);

            //VERIFY
            status.IsValid.ShouldBeTrue(status.GetAllErrors());
            context.Orders.Count().ShouldEqual(0);
            context.Addresses.Count().ShouldEqual(1);
        }
Ejemplo n.º 6
0
        public void TestHardDeleteViaKeysOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>();

            using (var context = new SingleSoftDelDbContext(options))
            {
                context.Database.EnsureCreated();
                var book = context.AddBookWithReviewToDb();

                var config  = new ConfigSoftDeleteWithUserId(context);
                var service = new SingleSoftDeleteService <ISingleSoftDelete>(context, config);
                var status  = service.SetSoftDelete(book);
                status.IsValid.ShouldBeTrue(status.GetAllErrors());
            }
            using (var context = new SingleSoftDelDbContext(options))
            {
                var config  = new ConfigSoftDeleteWithUserId(context);
                var service = new SingleSoftDeleteService <ISingleSoftDelete>(context, config);

                //ATTEMPT
                var status = service.HardDeleteViaKeys <Book>(context.Books.IgnoreQueryFilters().Single().Id);

                //VERIFY
                status.IsValid.ShouldBeTrue(status.GetAllErrors());
                status.Result.ShouldEqual(1);
            }
            using (var context = new SingleSoftDelDbContext(options))
            {
                context.Books.IgnoreQueryFilters().Count().ShouldEqual(0);
            }
        }
        public void TestHardDeleteViaKeysWithUserIdOk()
        {
            //SETUP
            var currentUser = Guid.NewGuid();
            int orderId;
            var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>();

            options.StopNextDispose();
            using (var context = new SingleSoftDelDbContext(options, currentUser))
            {
                context.Database.EnsureCreated();
                var order1 = new Order
                {
                    OrderRef = "Cur user Order, soft del", SoftDeleted = true, UserId = currentUser
                };
                var order2 = new Order
                {
                    OrderRef = "Cur user Order", SoftDeleted = false, UserId = currentUser
                };
                var order3 = new Order
                {
                    OrderRef = "Diff user Order", SoftDeleted = true, UserId = Guid.NewGuid()
                };
                var order4 = new Order
                {
                    OrderRef = "Diff user Order", SoftDeleted = false, UserId = Guid.NewGuid()
                };
                context.AddRange(order1, order2, order3, order4);
                context.SaveChanges();
                orderId = order1.Id;
            }
            using (var context = new SingleSoftDelDbContext(options, currentUser))
            {
                var config  = new ConfigSoftDeleteWithUserId(context);
                var service = new SingleSoftDeleteService <ISingleSoftDelete>(config);

                //ATTEMPT
                var status = service.ResetSoftDeleteViaKeys <Order>(orderId);

                //VERIFY
                status.IsValid.ShouldBeTrue(status.GetAllErrors());
                status.Result.ShouldEqual(1);

                context.ChangeTracker.Clear();
                context.Orders.IgnoreQueryFilters().Count().ShouldEqual(4);
                context.Orders.Count().ShouldEqual(2);
            }
        }
        public void TestSoftDeleteServiceGetSoftDeletedEntriesWithUserIdOk()
        {
            //SETUP
            var currentUser = Guid.NewGuid();
            var options     = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>();

            using (var context = new SingleSoftDelDbContext(options, currentUser))
            {
                context.Database.EnsureCreated();
                var order1 = new Order
                {
                    OrderRef = "Cur user Order, soft del", SoftDeleted = true, UserId = currentUser
                };
                var order2 = new Order
                {
                    OrderRef = "Cur user Order", SoftDeleted = false, UserId = currentUser
                };
                var order3 = new Order
                {
                    OrderRef = "Diff user Order", SoftDeleted = true, UserId = Guid.NewGuid()
                };
                var order4 = new Order
                {
                    OrderRef = "Diff user Order", SoftDeleted = false, UserId = Guid.NewGuid()
                };
                context.AddRange(order1, order2, order3, order4);
                context.SaveChanges();

                context.ChangeTracker.Clear();

                var config  = new ConfigSoftDeleteWithUserId(context);
                var service = new SingleSoftDeleteService <ISingleSoftDelete>(config);

                //ATTEMPT
                var orders = service.GetSoftDeletedEntries <Order>().ToList();

                //VERIFY
                orders.Count.ShouldEqual(1);

                context.ChangeTracker.Clear();
                orders.Single(x => x.UserId == currentUser).OrderRef.ShouldEqual("Cur user Order, soft del");
                context.Orders.IgnoreQueryFilters().Count().ShouldEqual(4);
                var all = context.Orders.IgnoreQueryFilters().ToList();
                context.Orders.Count().ShouldEqual(1);
            }
        }
Ejemplo n.º 9
0
        public void TestHardDeleteViaKeysWithWrongUserIdBad()
        {
            //SETUP
            var currentUser = Guid.NewGuid();
            int bookId;
            var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>();

            using (var context = new SingleSoftDelDbContext(options))
            {
                context.Database.EnsureCreated();
                var order1 = new Order
                {
                    OrderRef = "Cur user Order, soft del", SoftDeleted = true, UserId = currentUser
                };
                var order2 = new Order
                {
                    OrderRef = "Cur user Order", SoftDeleted = false, UserId = currentUser
                };
                var order3 = new Order
                {
                    OrderRef = "Diff user Order", SoftDeleted = true, UserId = Guid.NewGuid()
                };
                var order4 = new Order
                {
                    OrderRef = "Diff user Order", SoftDeleted = false, UserId = Guid.NewGuid()
                };
                context.AddRange(order1, order2, order3, order4);
                context.SaveChanges();
                bookId = order3.Id;
            }
            using (var context = new SingleSoftDelDbContext(options, currentUser))
            {
                var config  = new ConfigSoftDeleteWithUserId(context);
                var service = new SingleSoftDeleteService <ISingleSoftDelete>(context, config);

                //ATTEMPT
                var status = service.ResetSoftDeleteViaKeys <Order>(bookId);

                //VERIFY
                status.IsValid.ShouldBeFalse();
                status.GetAllErrors().ShouldEqual("Could not find the entry you ask for.");
                context.Orders.IgnoreQueryFilters().Count().ShouldEqual(4);
                context.Orders.Count().ShouldEqual(1);
            }
        }
Ejemplo n.º 10
0
        public void TestSoftDeleteServiceSetSoftDeleteViaKeysBadNumberOfKeys()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>();

            using (var context = new SingleSoftDelDbContext(options))
            {
                context.Database.EnsureCreated();
                var book = context.AddBookWithReviewToDb();

                var config  = new ConfigSoftDeleteWithUserId(context);
                var service = new SingleSoftDeleteService <ISingleSoftDelete>(context, config);

                //ATTEMPT
                var ex = Assert.Throws <ArgumentException>(() => service.SetSoftDeleteViaKeys <Book>(1, 2));

                //VERIFY
                ex.Message.ShouldEqual("Mismatch in keys: your provided 2 key(s) and the entity has 1 key(s) (Parameter 'keyValues')");
            }
        }
Ejemplo n.º 11
0
        public void TestSoftDeleteServiceSetSoftDeleteViaKeysBadKeyType()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>();

            using (var context = new SingleSoftDelDbContext(options))
            {
                context.Database.EnsureCreated();
                var book = context.AddBookWithReviewToDb();

                var config  = new ConfigSoftDeleteWithUserId(context);
                var service = new SingleSoftDeleteService <ISingleSoftDelete>(context, config);

                //ATTEMPT
                var ex = Assert.Throws <ArgumentException>(() => service.SetSoftDeleteViaKeys <Book>(book));

                //VERIFY
                ex.Message.ShouldEqual("Mismatch in keys: your provided key 1 (of 1) is of type Book but entity key's type is System.Int32 (Parameter 'keyValues')");
            }
        }
        public void TestSoftDeleteServiceSetSoftDeleteOneToOneBad()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>();

            using (var context = new SingleSoftDelDbContext(options))
            {
                context.Database.EnsureCreated();
                context.AddBookWithReviewToDb();

                var config  = new ConfigSoftDeleteWithUserId(context);
                var service = new SingleSoftDeleteService <ISingleSoftDelete>(config);

                //ATTEMPT
                var ex = Assert.Throws <InvalidOperationException>(() => service.SetSoftDeleteViaKeys <OneToOne>(1));

                //VERIFY
                ex.Message.ShouldEqual("You cannot soft delete a one-to-one relationship. It causes problems if you try to create a new version.");
            }
        }
Ejemplo n.º 13
0
        public void TestSoftDeleteServiceSetSoftDeleteViaKeysNotFoundBad()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>();

            using (var context = new SingleSoftDelDbContext(options))
            {
                context.Database.EnsureCreated();

                var config  = new ConfigSoftDeleteWithUserId(context);
                var service = new SingleSoftDeleteService <ISingleSoftDelete>(context, config);

                //ATTEMPT
                var status = service.SetSoftDeleteViaKeys <Book>(123);

                //VERIFY
                status.IsValid.ShouldBeFalse();
                status.GetAllErrors().ShouldEqual("Could not find the entry you ask for.");
                status.Result.ShouldEqual(0);
            }
        }
Ejemplo n.º 14
0
        public void TestHardDeleteViaKeysNotFoundOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>();

            using (var context = new SingleSoftDelDbContext(options))
            {
                context.Database.EnsureCreated();
                var book = context.AddBookWithReviewToDb();

                var config  = new ConfigSoftDeleteWithUserId(context);
                var service = new SingleSoftDeleteService <ISingleSoftDelete>(context, config);
                var status1 = service.SetSoftDelete(book);
                status1.IsValid.ShouldBeTrue(status1.GetAllErrors());

                //ATTEMPT
                var status = service.HardDeleteViaKeys <Book>(234);

                //VERIFY
                status.IsValid.ShouldBeFalse(status.GetAllErrors());
                status.GetAllErrors().ShouldEqual("Could not find the entry you ask for.");
            }
        }
Ejemplo n.º 15
0
        public void TestSoftDeleteServiceSetSoftDeleteViaKeysNotFoundReturnsZero()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>();

            using (var context = new SingleSoftDelDbContext(options))
            {
                context.Database.EnsureCreated();

                var config = new ConfigSoftDeleteWithUserId(context)
                {
                    NotFoundIsNotAnError = true
                };
                var service = new SingleSoftDeleteService <ISingleSoftDelete>(context, config);

                //ATTEMPT
                var status = service.SetSoftDeleteViaKeys <Book>(123);

                //VERIFY
                status.IsValid.ShouldBeTrue(status.GetAllErrors());
                status.Result.ShouldEqual(0);
            }
        }
        public void TestGetSoftDeleteOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>();

            using var context = new SingleSoftDelDbContext(options);
            context.Database.EnsureCreated();
            var shadowClass = new ShadowDelClass();

            context.Add(shadowClass);
            context.Entry(shadowClass).Property("SoftDeleted").CurrentValue = true;
            context.SaveChanges();

            context.ChangeTracker.Clear();

            var config  = new ConfigSoftDeleteShadowDel(context);
            var service = new SingleSoftDeleteService <IShadowSoftDelete>(config);

            //ATTEMPT
            var entities = service.GetSoftDeletedEntries <ShadowDelClass>().ToList();

            //VERIFY
            entities.Count().ShouldEqual(1);
        }
        public void TestResetSoftDeleteOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>();

            using var context = new SingleSoftDelDbContext(options);
            context.Database.EnsureCreated();
            var shadowClass = new ShadowDelClass();

            context.Add(shadowClass);
            context.Entry(shadowClass).Property("SoftDeleted").CurrentValue = true;
            context.SaveChanges();

            var config  = new ConfigSoftDeleteShadowDel(context);
            var service = new SingleSoftDeleteService <IShadowSoftDelete>(config);

            //ATTEMPT
            var status = service.ResetSoftDelete(shadowClass);

            //VERIFY
            status.IsValid.ShouldBeTrue(status.GetAllErrors());
            status.Result.ShouldEqual(1);
            context.ShadowDelClasses.Count().ShouldEqual(1);
        }