Пример #1
0
        public void TestNestedDtosV2Ok()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

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

                var utData = context.SetupSingleDtoAndEntities <BookListNestedV2Dto>();
                utData.AddSingleDto <AuthorNestedV2Dto>();
                var service = new CrudServices(context, utData.Wrapped);

                //ATTEMPT
                var dto = service.ReadSingle <BookListNestedV2Dto>(1);

                //VERIFY
                service.IsValid.ShouldBeTrue(service.GetAllErrors());
                dto.AuthorsLink.Count.ShouldEqual(2);
                dto.AuthorsLink.First().AStringToHoldAuthorName.ShouldStartWith("Author");
                dto.AuthorsLink.First().Order.ShouldEqual((byte)0);
                dto.AuthorsLink.Last().AStringToHoldAuthorName.ShouldEqual("CommonAuthor");
                dto.AuthorsLink.Last().Order.ShouldEqual((byte)1);
            }
        }
        public void TestCheckoutListOneBookDatabaseOneBook()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseDummyBooks(1);
            }
            //two line items: BookId:1 NumBooks:1
            var mockCookieRequests = new MockHttpCookieAccess(BasketCookie.BasketCookieName, $"{Guid.NewGuid()},1,1");

            //ATTEMPT
            using (var context = new EfCoreContext(options))
            {
                var service = new CheckoutListService(context, mockCookieRequests.CookiesIn);
                var list    = service.GetCheckoutList();

                //VERIFY
                list.Count.ShouldEqual(1);
                list.First().BookId.ShouldEqual(1);
                list.First().BookPrice.ShouldEqual(1);
            }
        }
        public void TestCheckoutListTwoBooksSqLite()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseDummyBooks(10);

                //two line items: BookId:1 NumBooks:2, BookId:2 NumBooks:3
                var mockCookieRequests = new MockHttpCookieAccess(BasketCookie.BasketCookieName, $"{Guid.NewGuid()},1,2,2,3");

                //ATTEMPT

                var service = new CheckoutListService(context, mockCookieRequests.CookiesIn);
                var list    = service.GetCheckoutList();

                //VERIFY
                for (int i = 0; i < list.Count(); i++)
                {
                    list[i].BookId.ShouldEqual(i + 1);
                    list[i].NumBooks.ShouldEqual((short)(i + 2));
                    list[i].BookPrice.ShouldEqual((i + 1));
                }
            }
        }
Пример #4
0
        public void TestRemoveLinkByRemovingFromCollectionToAuthorOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using var context = new EfCoreContext(options);
            context.Database.EnsureCreated();
            context.SeedDatabaseDummyBooks(1);
            var bookId = context.Books.First().BookId;

            context.ChangeTracker.Clear();

            //ATTEMPT
            var existingBook = context.Books
                               .Include(book => book.AuthorsLink.OrderBy(x => x.Order))
                               .Single(book => book.BookId == bookId);

            var linkToRemove = existingBook.AuthorsLink.Last();

            existingBook.AuthorsLink.Remove(linkToRemove);
            context.SaveChanges();

            context.ChangeTracker.Clear();

            //VERIFY
            var bookAgain = context.Books
                            .Include(p => p.AuthorsLink).ThenInclude(p => p.Author)
                            .Single(p => p.BookId == bookId);

            bookAgain.AuthorsLink.ShouldNotBeNull();
            bookAgain.AuthorsLink.Count.ShouldEqual(1);
            bookAgain.AuthorsLink.First().Author.Name.ShouldEqual("Author0000");
            context.Set <BookAuthor>().Count().ShouldEqual(1);
        }
Пример #5
0
        public void SetupRestOfDto(BooksFilterBy filterBy, string filterValue, int pageSize,
                                   int expectedPageNum, int expectedNumPages)
        {
            //SETUP
            var numBooks = 12;
            var options  = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseDummyBooks(numBooks);

                var sfpDto = new SortFilterPageOptions
                {
                    FilterBy    = BooksFilterBy.ByVotes,
                    FilterValue = "Dummy",
                    PageNum     = 2
                };

                //need to do this to to setup PrevCheckState
                sfpDto.SetupRestOfDto(context.Books);

                //ATTEMPT
                sfpDto.PageNum     = 2;
                sfpDto.FilterBy    = filterBy;
                sfpDto.FilterValue = filterValue;
                sfpDto.PageSize    = pageSize;
                sfpDto.SetupRestOfDto(context.Books);

                //VERIFY
                sfpDto.PageNum.ShouldEqual(expectedPageNum);
                sfpDto.NumPages.ShouldEqual(expectedNumPages);
            }
        }
Пример #6
0
        public void TestPerformanceReadMultipleToDto()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseDummyBooks(100);
            }

            using (var context = new EfCoreContext(options))
            {
                var utData  = context.SetupEntitiesDirect();
                var service = new CrudServices <EfCoreContext>(context, utData.ConfigAndMapper);

                using (new TimeThings(_output, "RunHandCoded ReadMany", 1))
                {
                    context.Books.Select(x => new BookTitleAndCount {
                        BookId       = x.BookId,
                        Title        = x.Title,
                        ReviewsCount = x.Reviews.Count()
                    }).ToList();
                }

                using (new TimeThings(_output, "RunGenericService ReadManyNoTracked", 1))
                {
                    service.ReadManyNoTracked <BookTitleAndCount>().ToList();
                }

                using (new TimeThings(_output, "RunGenericService ReadMany", 100))
                {
                    for (int i = 0; i < 100; i++)
                    {
                        context.Books.Select(x => new BookTitleAndCount
                        {
                            BookId       = x.BookId,
                            Title        = x.Title,
                            ReviewsCount = x.Reviews.Count()
                        }).ToList();
                    }
                }

                using (new TimeThings(_output, "RunHandCoded Find", 100))
                {
                    for (int i = 0; i < 100; i++)
                    {
                        service.ReadManyNoTracked <BookTitleAndCount>().ToList();
                    }
                }
            }
        }
Пример #7
0
        public void Setup()
        {
            var con = this.GetUniqueDatabaseConnectionString(nameof(PerfListMany));

            _options = new DbContextOptionsBuilder <EfCoreContext>().UseSqlServer(con).Options;
            using (var context = new EfCoreContext(_options))
            {
                context.Database.EnsureCreated();
                if (!context.Books.Any())
                {
                    context.SeedDatabaseDummyBooks(100);
                }
                _utData = context.SetupSingleDtoAndEntities <BookListDto>();
            }
        }
Пример #8
0
        public void ExampleCodeForBook()
        {
            //SETUP
            var userId  = Guid.NewGuid();
            var options = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options, new FakeUserIdService(userId)))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseDummyBooks();
                var lineItems = new List <OrderLineItem>
                {
                    new OrderLineItem {
                        BookId = 1, NumBooks = 4
                    },
                    new OrderLineItem {
                        BookId = 2, NumBooks = 5
                    },
                    new OrderLineItem {
                        BookId = 3, NumBooks = 6
                    }
                };
                //ATTEMPT

                var dbAccess = new PlaceOrderDbAccess(context);
                var action1  = new PlaceOrderPart1(dbAccess);
                var action2  = new PlaceOrderPart2(dbAccess);
                var runner   = new RunnerTransact2WriteDb <
                    PlaceOrderInDto,
                    Part1ToPart2Dto,
                    Order>(context, action1, action2);
                var order = runner.RunAction(new PlaceOrderInDto(true, userId, lineItems.ToImmutableList()));

                //VERIFY
                runner.HasErrors.ShouldBeFalse();
                context.Orders.Count().ShouldEqual(1);
            }
        }
Пример #9
0
        public void PageBooks(int pageSize)
        {
            //SETUP
            var numBooks = 12;
            var options  = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseDummyBooks(numBooks);

                //ATTEMPT
                var service     = new ListBooksService(context);
                var listOptions = new SortFilterPageOptions()
                {
                    PageSize = pageSize
                };
                var dtos = service.SortFilterPage(listOptions).ToList();

                //VERIFY
                dtos.Count.ShouldEqual(pageSize);
            }
        }
Пример #10
0
        public void TestQueryPerformanceOk()
        {
            //SETUP
            int numBooks = 100;
            var options  = SqliteInMemory.CreateOptions <EfCoreContext>();

            using (var context = new EfCoreContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseDummyBooks(numBooks);
            }
            //ATTEMPT
            using (var context = new EfCoreContext(options))
                using (new TimeThings(_output, "Normal"))
                {
                    var books = context.Books
                                .Include(x => x.Reviews)
                                .Include(x => x.AuthorsLink).ThenInclude(x => x.Author)
                                .ToList();
                }
            using (var context = new EfCoreContext(options))
                using (new TimeThings(_output, "AsNoTracking"))
                {
                    var books = context.Books
                                .AsNoTracking()
                                .Include(x => x.Reviews)
                                .Include(x => x.AuthorsLink).ThenInclude(x => x.Author)
                                .ToList();
                }
            using (var context = new EfCoreContext(options))
                using (new TimeThings(_output, "AsNoTrackingWithIdentityResolution"))
                {
                    var books = context.Books
                                .AsNoTrackingWithIdentityResolution()
                                .Include(x => x.Reviews)
                                .Include(x => x.AuthorsLink).ThenInclude(x => x.Author)
                                .ToList();
                }

            using (var context = new EfCoreContext(options))
                using (new TimeThings(_output, "Normal2"))
                {
                    var books = context.Books
                                .Include(x => x.Reviews)
                                .Include(x => x.AuthorsLink).ThenInclude(x => x.Author)
                                .ToList();
                }

            using (var context = new EfCoreContext(options))
                using (new TimeThings(_output, "AsNoTracking2"))
                {
                    var books = context.Books
                                .AsNoTracking()
                                .Include(x => x.Reviews)
                                .Include(x => x.AuthorsLink).ThenInclude(x => x.Author)
                                .ToList();
                }

            using (var context = new EfCoreContext(options))
                using (new TimeThings(_output, "AsNoTrackingWithIdentityResolution2"))
                {
                    var books = context.Books
                                .AsNoTrackingWithIdentityResolution()
                                .Include(x => x.Reviews)
                                .Include(x => x.AuthorsLink).ThenInclude(x => x.Author)
                                .ToList();
                }
        }