public async Task GetUserOrdersWithData_ShouldRetunAllUserOrders()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            context.AddRange(GetTestOrders());
            await context.SaveChangesAsync();

            var userService = new Mock <IUserService>();
            var username    = "******";

            userService.Setup(u => u.GetByUsernameAsync(username))
            .Returns(context.Users.FirstOrDefaultAsync(x => x.UserName == username));

            this.shoppingCartService = new ShoppingCartService(context, userService.Object);
            this.orderService        = new OrderService(context, userService.Object, shoppingCartService);

            List <OrderListingServiceModel> expectedData = GetTestOrders()
                                                           .To <OrderListingServiceModel>().ToList();

            List <OrderListingServiceModel> actualData = await this.orderService.GetUserOrders(username).ToListAsync();

            Assert.Equal(expectedData.Count, actualData.Count);
        }
        public async Task GetAllBooks_WithData_ShouldReturnAllBooks()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.bookService = new AdminBookService(context);


            List <AdminBookListingServiceModel> expectedData = GetTestData()
                                                               .To <AdminBookListingServiceModel>().ToList();

            List <AdminBookListingServiceModel> actualData = await this.bookService.GetAllBooks().ToListAsync();

            Assert.Equal(expectedData.Count, actualData.Count);


            for (int i = 0; i < expectedData.Count; i++)
            {
                var expectedEntry = expectedData[i];
                var actualEntry   = actualData[i];

                Assert.True(expectedEntry.Title == actualEntry.Title, "Title is not returned properly.");
                Assert.True(expectedEntry.AuthorFullName == actualEntry.AuthorFullName, "AuthorFullName is not returned properly.");
                Assert.True(expectedEntry.PublisherName == actualEntry.PublisherName, "PublisherName is not returned properly.");
                Assert.True(expectedEntry.Price == actualEntry.Price, "Price is not returned properly.");
            }
        }
        public async Task GetOrderBooksAsync_ShouldReturnOrderBooks()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            var userService = new Mock <IUserService>();
            var username    = "******";

            userService.Setup(u => u.GetByUsernameAsync(username))
            .Returns(context.Users.FirstOrDefaultAsync(x => x.UserName == username));

            this.shoppingCartService = new ShoppingCartService(context, userService.Object);
            this.orderService        = new OrderService(context, userService.Object, shoppingCartService);

            var book = context.Books.First();

            await shoppingCartService.AddBookToShoppingCartAsync(book.Id, username);

            await orderService.CreateAsync(username);

            var order = context.Orders.First();
            var user  = context.Users.First(u => u.UserName == username);

            var expectedResult = context.OrderBooks.Where(ob => ob.OrderId == order.Id).ToList().Count;

            var result = await orderService.GetOrderBooksAsync(order.Id);

            List <OrderBookListingServiceModel> actualData = new List <OrderBookListingServiceModel>(result);

            var actualResult = actualData.Count();

            Assert.True(expectedResult == actualResult);
        }
Example #4
0
        public async Task GetReviewsByBook_ShouldReturnAllBookReviews()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.reviewService = new ReviewService(context);

            var book = context.Books.First().To <BookListingServiceModel>();

            var expectedData = book.Reviews
                               .OrderByDescending(r => r.CreatedOn)
                               .ToList();

            List <ReviewListingServiceModel> actualData = await this.reviewService.GetReviewsByBook(book.Id).ToListAsync();

            Assert.Equal(expectedData.Count, actualData.Count);

            for (int i = 0; i < expectedData.Count; i++)
            {
                var expectedEntry = expectedData[i];
                var actualEntry   = actualData[i];

                Assert.True(expectedEntry.CreatorUserName == actualEntry.CreatorUserName, "CreatorUserName is not returned properly.");
                Assert.True(expectedEntry.Text == actualEntry.Text, "Text is not returned properly.");
            }
        }
        public async Task RemoveCategoryAsync_WithNonExistingBookIdShouldReturnFalse()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            var category = new Category
            {
                Name = "Художествена литература"
            };

            context.Categories.Add(category);
            await context.SaveChangesAsync();

            var bookFromDb     = context.Books.First();
            var categoryFromDb = context.Categories.First();

            this.bookService = new AdminBookService(context);

            await this.bookService.AddCategoryAsync(bookFromDb.Id, categoryFromDb.Id);

            bool actualResult = await this.bookService.RemoveCategoryAsync(-1, categoryFromDb.Id);

            Assert.False(actualResult);
        }
        public async Task EditAsync_ShouldEditBook()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.bookService = new AdminBookService(context);

            var expectedData = context.Books.First();

            expectedData.Title       = "Под";
            expectedData.AuthorId    = 2;
            expectedData.PublisherId = 1;
            expectedData.Language    = "бълг";
            expectedData.Description = "опис";
            expectedData.Image       = "editedImage";
            expectedData.Price       = 4.99M;

            await this.bookService.EditAsync(expectedData.Id,
                                             expectedData.Title, expectedData.AuthorId, expectedData.PublisherId,
                                             expectedData.Language, expectedData.Description, expectedData.Image, expectedData.CreatedOn,
                                             expectedData.Price);

            var actualData = context.Books.First();

            Assert.True(actualData.Title == expectedData.Title, "Title not edited properly.");
            Assert.True(actualData.AuthorId == expectedData.AuthorId, "Author not edited properly.");
            Assert.True(actualData.PublisherId == expectedData.PublisherId, "Publisher not edited properly.");
            Assert.True(actualData.Language == expectedData.Language, "Language not edited properly.");
            Assert.True(actualData.Description == expectedData.Description, "Description not edited properly.");
            Assert.True(actualData.Image == expectedData.Image, "Image not edited properly.");
            Assert.True(actualData.Price == expectedData.Price, "Price not edited properly.");
        }
Example #7
0
        public async Task ShowAsync_ShouldShowCategory()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.categoryService = new AdminCategoryService(context);

            int id = context.Categories.First().Id;

            var category = context.Categories.First();

            category.IsDeleted = true;
            context.Categories.Update(category);
            await context.SaveChangesAsync();

            bool actualResult = await this.categoryService.ShowAsync(id);

            int expectedCount = 2;
            int actualCount   = context.Categories.Where(c => c.IsDeleted == false).Count();


            Assert.True(actualResult);
            Assert.True(expectedCount == actualCount);
        }
Example #8
0
        public async Task GetAllUsers_WithData_ShouldReturnAllUsers()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.userService = new AdminUserService(context);

            List <AdminUserListingServiceModel> expectedData = GetTestData().To <AdminUserListingServiceModel>().ToList();
            List <AdminUserListingServiceModel> actualData   = await this.userService.GetAllUsers().ToListAsync();

            Assert.Equal(expectedData.Count, actualData.Count);

            foreach (var actualUser in actualData)
            {
                Assert.True(expectedData.Any(user => actualUser.Username == user.Username &&
                                             actualUser.Email == user.Email), "AdminUserService GetAllUsers() does not work properly");
            }

            for (int i = 0; i < expectedData.Count; i++)
            {
                var expectedEntry = expectedData[i];
                var actualEntry   = actualData[i];

                Assert.True(expectedEntry.Username == actualEntry.Username, "Username is not returned properly.");
                Assert.True(expectedEntry.Email == actualEntry.Email, "Email is not returned properly.");
            }
        }
Example #9
0
        public async Task DecreaseQuantityAsync_WithNonExistingBook_ShouldNotDecreaseBookQuantity()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            var userService = new Mock <IUserService>();
            var username    = "******";
            var username1   = "user3";

            userService.Setup(u => u.GetByUsernameAsync(username))
            .Returns(context.Users.FirstOrDefaultAsync(x => x.UserName == username));

            this.shoppingCartService = new ShoppingCartService(context, userService.Object);

            var book = context.Books.First();

            await shoppingCartService.AddBookToShoppingCartAsync(book.Id, username);

            await shoppingCartService.IncreaseQuantityAsync(book.Id, username);

            var actualResult = await shoppingCartService.DecreaseQuantityAsync(-1, username1);

            var expectedQuantity = 2;
            var shoppingCartBook = context.ShoppingCartBooks.First();

            var actualQuantity = shoppingCartBook.Quantity;

            Assert.False(actualResult);
            Assert.Equal(expectedQuantity, actualQuantity);
        }
        public async Task RemoveCategoryAsync_ShouldRemoveCategoryFromBook()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            var category = new Category
            {
                Name = "История"
            };

            context.Categories.Add(category);
            await context.SaveChangesAsync();

            var bookFromDb     = context.Books.First();
            var categoryFromDb = context.Categories.First();

            this.bookService = new AdminBookService(context);

            await this.bookService.AddCategoryAsync(bookFromDb.Id, categoryFromDb.Id);

            bool actualResult = await this.bookService.RemoveCategoryAsync(bookFromDb.Id, categoryFromDb.Id);

            Assert.True(actualResult);
        }
Example #11
0
        public async Task RemoveBooksFromShoppingCartAsync_ShouldRemoveBooks()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            var userService = new Mock <IUserService>();
            var username    = "******";

            userService.Setup(u => u.GetByUsernameAsync(username))
            .Returns(context.Users.FirstOrDefaultAsync(x => x.UserName == username));

            this.shoppingCartService = new ShoppingCartService(context, userService.Object);

            var book = context.Books.First();

            await shoppingCartService.AddBookToShoppingCartAsync(book.Id, username);

            var actualResult = await shoppingCartService.RemoveBooksFromShoppingCartAsync(username);

            var shoppingCartBooks = context.ShoppingCartBooks.ToList();

            Assert.True(actualResult);
            Assert.Empty(shoppingCartBooks);
        }
Example #12
0
        public async Task GetAllActiveAuthors_WithData_ShouldReturnAllActiveAuthors()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.authorService = new AdminAuthorService(context);


            List <AdminAuthorListingServiceModel> expectedData = GetTestData()
                                                                 .Where(c => c.IsDeleted == false)
                                                                 .To <AdminAuthorListingServiceModel>().ToList();

            List <AdminAuthorListingServiceModel> actualData = await this.authorService.GetAllActiveAuthors().ToListAsync();

            Assert.Equal(expectedData.Count, actualData.Count);


            for (int i = 0; i < expectedData.Count; i++)
            {
                var expectedEntry = expectedData[i];
                var actualEntry   = actualData[i];

                Assert.True(expectedEntry.FirstName == actualEntry.FirstName, "FirstName is not returned properly.");
                Assert.True(expectedEntry.LastName == actualEntry.LastName, "LastName is not returned properly.");
                Assert.True(expectedEntry.IsDeleted == actualEntry.IsDeleted, "IsDeleted is not returned properly.");
            }
        }
Example #13
0
        public async Task GetAllReviews_WithData_ShouldReturnAllReviews()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.reviewService = new AdminReviewService(context);

            List <AdminReviewListingServiceModel> expectedData = GetTestData()
                                                                 .To <AdminReviewListingServiceModel>().ToList();

            List <AdminReviewListingServiceModel> actualData = await this.reviewService.GetAllReviews().ToListAsync();

            Assert.Equal(expectedData.Count, actualData.Count);

            for (int i = 0; i < expectedData.Count; i++)
            {
                var expectedEntry = expectedData[i];
                var actualEntry   = actualData[i];

                Assert.True(expectedEntry.Creator == actualEntry.Creator, "Creator is not returned properly.");
                Assert.True(expectedEntry.Book == actualEntry.Book, "Book is not returned properly.");
                Assert.True(expectedEntry.Text == actualEntry.Text, "Text is not returned properly.");
            }
        }
Example #14
0
        public async Task GetAllActiveCategories_WithoutData_ShouldReturnEmptyList()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            this.categoryService = new CategoryService(context);

            List <CategoryListingServiceModel> actualData = await this.categoryService.GetAllActiveCategories().ToListAsync();

            Assert.Empty(actualData);
        }
        public async Task GetAllBooks_WithoutData_ShouldReturnEmptyList()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            this.bookService = new AdminBookService(context);

            List <AdminBookListingServiceModel> actualData = await this.bookService.GetAllBooks().ToListAsync();

            Assert.Empty(actualData);
        }
Example #16
0
        public async Task FindBooksByAuthorWithoutData_ShouldReturnEmptyList()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            this.bookService = new BookService(context);

            List <BookListingServiceModel> actualData = await this.bookService.FindBooksByAuthor("Иван Вазов").ToListAsync();

            Assert.Empty(actualData);
        }
Example #17
0
        public async Task CreateAsync_ShouldCreateAuthor()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.authorService = new AdminAuthorService(context);

            bool actualResult = await this.authorService.CreateAsync("Николай", "Хайтов");

            Assert.True(actualResult);
        }
        public async Task GetByIdAsync_WithNonExistentId_ShouldReturnNull()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.bookService = new AdminBookService(context);

            AdminBookListingServiceModel actualData = await this.bookService.GetByIdAsync <AdminBookListingServiceModel>(int.MinValue);

            Assert.True(actualData == null);
        }
        public async Task AddCategoryAsync_WithNonExistingCategory_ShouldRetunFalse()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.bookService = new AdminBookService(context);

            bool actualResult = await this.bookService.AddCategoryAsync(1, 1);

            Assert.False(actualResult);
        }
Example #20
0
        public async Task GetByIdAsync_WithNonExistentId_ShouldReturnNull()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.categoryService = new CategoryService(context);

            CategoryListingServiceModel actualData = await this.categoryService.GetByIdAsync(-1);

            Assert.True(actualData == null);
        }
Example #21
0
        public async Task CreateAsync_ShouldCreateCategory()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.categoryService = new AdminCategoryService(context);

            bool actualResult = await this.categoryService.CreateAsync("Изкуство");

            Assert.True(actualResult);
        }
        public async Task GetByIdAsync_WithExistentId_ShouldReturnCorrectResult()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.bookService = new AdminBookService(context);

            AdminBookListingServiceModel expectedData = context.Books.First().To <AdminBookListingServiceModel>();
            AdminBookListingServiceModel actualData   = await this.bookService.GetByIdAsync <AdminBookListingServiceModel>(expectedData.Id);

            Assert.True(expectedData.Id == actualData.Id, "Id is not returned properly.");
        }
Example #23
0
        public async Task FindBooks_ShouldReturnAllBooksBySearchText(string searchText)
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.bookService = new BookService(context);

            int expectedCount = 2;
            List <BookListingServiceModel> actualData = await this.bookService.FindBooks(searchText).ToListAsync();

            Assert.Equal(expectedCount, actualData.Count);
        }
Example #24
0
        public async Task CreateAsync_ShouldCreateBook()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.reviewService = new ReviewService(context);

            var  book         = context.Books.First();
            var  creator      = context.Users.First();
            bool actualResult = await this.reviewService.CreateAsync(book.Id, "cool", creator.Id);

            Assert.True(actualResult);
        }
Example #25
0
        public async Task FindBooksByPublisher_ShouldReturnAllPublisherBooks()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.bookService = new BookService(context);

            var book      = context.Books.First();
            var publisher = book.Publisher.Name;

            int expectedCount = 1;
            List <BookListingServiceModel> actualData = await this.bookService.FindBooksByPublisher(publisher).ToListAsync();

            Assert.Equal(expectedCount, actualData.Count);
        }
        public async Task HideAsync_WithNonExistentBookId_ShouldReturnFalse()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.bookService = new AdminBookService(context);

            bool actualResult = await this.bookService.HideAsync(-1);

            int expectedCount = 2;
            int actualCount   = context.Books.Count();

            Assert.False(actualResult);
            Assert.True(expectedCount == actualCount);
        }
Example #27
0
        public async Task HideAsync_WithNonExistentCategoryId_ShouldReturnFalse()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.categoryService = new AdminCategoryService(context);

            bool actualResult = await this.categoryService.HideAsync(-1);

            int expectedCount = 2;
            int actualCount   = context.Categories.Where(c => c.IsDeleted == false).Count();

            Assert.False(actualResult);
            Assert.True(expectedCount == actualCount);
        }
Example #28
0
        public async Task RemoveAsync_WithNonExistingId_ShouldRetunFalse()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.reviewService = new AdminReviewService(context);

            var actualResult = await this.reviewService.RemoveAsync(-1);

            int expectedCount = 2;
            int actualCount   = context.Reviews.Count();

            Assert.False(actualResult);
            Assert.True(expectedCount == actualCount);
        }
        public async Task GetBooksByAuthorId_ShouldReturnAllAuthorBooks()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.bookService = new AdminBookService(context);

            var book   = context.Books.First();
            var author = book.Author;

            int expectedCount = 1;
            List <AdminBookListingServiceModel> actualData = await this.bookService.GetBooksByAuthorId(author.Id).ToListAsync();

            Assert.Equal(expectedCount, actualData.Count);
        }
Example #30
0
        public async Task RemoveAsync_WithCorrectData_ShouldDeleteReview()
        {
            var context = BookStoreDbContextInMemoryFactory.InitializeContext();

            await SeedData(context);

            this.reviewService = new AdminReviewService(context);

            int id = context.Reviews.First().Id;

            await this.reviewService.RemoveAsync(id);

            int expectedCount = 1;
            int actualCount   = context.Reviews.Count();

            Assert.True(expectedCount == actualCount);
        }