Exemplo n.º 1
0
        public async Task AddAsyncShouldAddBook()
        {
            var db = DbInfrastructure.GetDatabase();

            var bookService = new BookService(db);

            var author1 = new User
            {
                UserName = "******"
            };

            var author2 = new User
            {
                UserName = "******"
            };

            await db.AddRangeAsync(author1, author2);

            const string Title       = "Some title";
            const string Description = "Description of the book";

            await bookService.AddAsync(Title, Description, author1.Id);

            var book = await db.Books.FirstOrDefaultAsync();

            book.Title.Should().Be(Title);

            book.Description.Should().Be(Description);

            book.AuthorId.Should().Be(author1.Id);
        }
Exemplo n.º 2
0
        public async Task AddShouldSaveCorrectData()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
            var dbContext = new ApplicationDbContext(options);

            var repository         = new EfDeletableEntityRepository <Book>(dbContext);
            var categoryRepository = new EfDeletableEntityRepository <Category>(dbContext);
            var service            = new BookService(repository, categoryRepository);
            var title        = "Title";
            var shortContent = "ShortContent";
            var imgUrl       = "ImgUrl";
            var fileUrl      = "fileUrl";
            var pages        = 20;
            var authorId     = 1;
            var categoryId   = 1;

            await service.AddAsync(title, shortContent, imgUrl, fileUrl, pages, categoryId, authorId);

            var actualResult = repository.All().FirstOrDefault();

            Assert.Equal(title, actualResult.Title);
            Assert.Equal(shortContent, actualResult.ShortContent);
            Assert.Equal(imgUrl, actualResult.ImgUrl);
            Assert.Equal(fileUrl, actualResult.FileName);
            Assert.Equal(pages, actualResult.Pages);
            Assert.Equal(authorId, actualResult.AutorId);
            Assert.Equal(categoryId, actualResult.CategoryId);
        }
Exemplo n.º 3
0
        public void BookService_AddAsync_ThrowsLibraryExceptionWithInvalidYear()
        {
            var mockUnitOfWork = new Mock <IUnitOfWork>();

            mockUnitOfWork.Setup(x => x.BookRepository.AddAsync(It.IsAny <Book>()));
            var bookService = new BookService(mockUnitOfWork.Object, UnitTestHelper.CreateMapperProfile());
            var book        = new BookModel {
                Id = 100, Author = "Honore de Balzac", Title = "The Splendors and Miseries of Courtesans", Year = 9999
            };

            Assert.ThrowsAsync <LibraryException>(() => bookService.AddAsync(book));
        }
Exemplo n.º 4
0
        public void BookService_AddAsync_ThrowsLibraryExceptionWithEmptyTitle()
        {
            var mockUnitOfWork = new Mock <IUnitOfWork>();

            mockUnitOfWork.Setup(x => x.BookRepository.AddAsync(It.IsAny <Book>()));
            var bookService = new BookService(mockUnitOfWork.Object, UnitTestHelper.CreateMapperProfile());
            var book        = new BookModel {
                Id = 100, Author = "Honore de Balzac", Title = ""
            };

            Assert.ThrowsAsync <LibraryException>(() => bookService.AddAsync(book));
        }
Exemplo n.º 5
0
        public async Task GetNewBooksShouldReturnCorrectCount()
        {
            this.InitializeMapper();
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
            var dbContext = new ApplicationDbContext(options);

            await this.SeedBooksAsync(dbContext);

            var repository         = new EfDeletableEntityRepository <Book>(dbContext);
            var categoryRepository = new EfDeletableEntityRepository <Category>(dbContext);
            var service            = new BookService(repository, categoryRepository);
            await service.AddAsync("NewBook1", "Content", "ImgUrl", "fileUrl", 20, 1, 1);

            await service.AddAsync("NewBook2", "Content", "ImgUrl", "fileUrl", 20, 1, 1);

            await service.AddAsync("NewBook3", "Content", "ImgUrl", "fileUrl", 20, 1, 1);

            var actualResult = service.GetNewBooks <BookDetailViewModel>().ToList().Count;

            Assert.Equal(3, actualResult);
        }
Exemplo n.º 6
0
        public async Task AddShouldSaveCorrect()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
            var dbContext = new ApplicationDbContext(options);

            var repository         = new EfDeletableEntityRepository <Book>(dbContext);
            var categoryRepository = new EfDeletableEntityRepository <Category>(dbContext);
            var service            = new BookService(repository, categoryRepository);

            await service.AddAsync("Title", "ShortContent", "ImgUrl", "FileUrl", 30, 1, 1);

            Assert.Equal(1, repository.All().Count());
        }
Exemplo n.º 7
0
        public async Task BookService_AddAsync_AddsModel()
        {
            //Arrange
            var mockUnitOfWork = new Mock <IUnitOfWork>();

            mockUnitOfWork.Setup(x => x.BookRepository.AddAsync(It.IsAny <Book>()));
            var bookService = new BookService(mockUnitOfWork.Object, UnitTestHelper.CreateMapperProfile());
            var book        = new BookModel {
                Id = 100, Author = "Honore de Balzac", Title = "The Splendors and Miseries of Courtesans"
            };

            //Act
            await bookService.AddAsync(book);

            //Assert
            mockUnitOfWork.Verify(x => x.BookRepository.AddAsync(It.Is <Book>(b => b.Author == book.Author && b.Id == book.Id)), Times.Once);
            mockUnitOfWork.Verify(x => x.SaveAsync(), Times.Once);
        }
Exemplo n.º 8
0
 public async Task <Book> Add([FromBody] Book author)
 {
     return(await _bookService.AddAsync(author));
 }