public async Task TestSoftDeleteServiceSetSoftDeleteOk()
        {
            //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 SingleSoftDeleteServiceAsync <ISingleSoftDelete>(config);

                //ATTEMPT
                var status = await service.SetSoftDeleteAsync(book);

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

            using (var context = new SingleSoftDelDbContext(options))
            {
                context.Database.EnsureCreated();
                var book = new Book {
                    Title = "test", SoftDeleted = true
                };
                context.Add(book);
                context.SaveChanges();

                var config = new ConfigSoftDeleteWithUserId(context);

                //ATTEMPT
                var getSoftValue = config.GetSoftDeleteValue.Compile().Invoke(book);
                getSoftValue.ShouldBeTrue();
                var query = context.Books.IgnoreQueryFilters().Where(config.GetSoftDeleteValue).Cast <Book>()
                            .Select(x => x.Title.Length);
                var result = query.ToList();

                //VERIFY
                _output.WriteLine(query.ToQueryString());
                result.Count.ShouldEqual(1);
            }
        }
Exemple #3
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);
            }
        }
Exemple #4
0
        public void TestHardDeleteSoftDeletedEntryNoCallSaveChangesOk()
        {
            //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.HardDeleteSoftDeletedEntry(context.Books.IgnoreQueryFilters().Single(), false);
                context.Books.IgnoreQueryFilters().Count().ShouldEqual(1);
                context.SaveChanges();
                context.Books.IgnoreQueryFilters().Count().ShouldEqual(0);

                //VERIFY
                status.IsValid.ShouldBeTrue(status.GetAllErrors());
                status.Result.ShouldEqual(1);
            }
        }
Exemple #5
0
        public void TestSoftDeleteServiceResetSoftDeleteViaKeysOk()
        {
            //SETUP
            int bookId;
            var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>();

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

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

                //ATTEMPT
                var status2 = service.ResetSoftDeleteViaKeys <Book>(bookId);

                //VERIFY
                status2.IsValid.ShouldBeTrue(status2.GetAllErrors());
                status2.Result.ShouldEqual(1);
            }
            using (var context = new SingleSoftDelDbContext(options))
            {
                context.Books.Count().ShouldEqual(1);
                context.Books.IgnoreQueryFilters().Count().ShouldEqual(1);
            }
        }
Exemple #6
0
        public async Task TestSoftDeleteAsyncOrderWithAddressOk()
        {
            //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 SingleSoftDeleteServiceAsync <ISingleSoftDelete>(config);

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

            //VERIFY
            status.IsValid.ShouldBeTrue(status.GetAllErrors());
            context.Orders.Count().ShouldEqual(0);
            context.Addresses.Count().ShouldEqual(1);
        }
        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);
            }
        }
Exemple #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);
            }
        }
        public async Task 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 SingleSoftDeleteServiceAsync <ISingleSoftDelete>(config);

                //ATTEMPT
                var ex = await Assert.ThrowsAsync <ArgumentException>(async() => await service.SetSoftDeleteViaKeysAsync <Book>(1, 2));

                //VERIFY
                ex.Message.ShouldEqual("Mismatch in keys: your provided 2 key(s) and the entity has 1 key(s) (Parameter 'keyValues')");
            }
        }
        public async Task 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 SingleSoftDeleteServiceAsync <ISingleSoftDelete>(config);

                //ATTEMPT
                var ex = await Assert.ThrowsAsync <ArgumentException>(async() => await service.SetSoftDeleteViaKeysAsync <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')");
            }
        }
Exemple #12
0
        public async Task 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 SingleSoftDeleteServiceAsync <ISingleSoftDelete>(config);

                //ATTEMPT
                var ex = await Assert.ThrowsAsync <InvalidOperationException>(async() => await service.SetSoftDeleteViaKeysAsync <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.");
            }
        }
        public async Task TestSoftDeleteServiceSetSoftDeleteViaKeysNotFoundBad()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <SingleSoftDelDbContext>();

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

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

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

                //VERIFY
                status.IsValid.ShouldBeFalse();
                status.GetAllErrors().ShouldEqual("Could not find the entry you ask for.");
                status.Result.ShouldEqual(0);
            }
        }
        public async Task 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 SingleSoftDeleteServiceAsync <ISingleSoftDelete>(config);

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

                //VERIFY
                status.IsValid.ShouldBeTrue(status.GetAllErrors());
                status.Result.ShouldEqual(0);
            }
        }
Exemple #15
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.");
            }
        }
        public void TestExpressionBuilderFormOtherFiltersOnlyWithUserIdOk()
        {
            //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()
                };
                context.AddRange(order1, order2, order3);
                context.SaveChanges();

                var config = new ConfigSoftDeleteWithUserId(context);

                //ATTEMPT
                var query = context.Orders.IgnoreQueryFilters().Where(
                    config.FilterToGetValueSingleSoftDeletedEntities <Order, ISingleSoftDelete>())
                            .Select(x => x.OrderRef);
                var result = query.ToList();

                //VERIFY
                _output.WriteLine(query.ToQueryString());
                result.Count.ShouldEqual(1);
            }
        }