public void ReplaceLoan() { // Arrange int userID = 1; int bookID = 1; var loanDate = new DateTime(2000, 1, 1); DateTime?returnDate = null; var patchViewModel = MockDataGenerator.CreatePatchLoanViewModel(loanDate, returnDate); var viewModel = MockDataGenerator.CreateLoanViewModel(loanDate, returnDate); var mockBookRepo = new Mock <IBookRepository>(); var mockLoanRepo = new Mock <ILoanRepository>(); var mockMapper = new Mock <IMapper>(); mockLoanRepo.Setup(f => f.UpdateLoan(userID, bookID, patchViewModel)); mockMapper.Setup(x => x.Map <LoanViewModel, PatchLoanViewModel>(viewModel)).Returns(patchViewModel); var service = new BookService(mockBookRepo.Object, mockLoanRepo.Object, mockMapper.Object); // Act service.ReplaceLoan(userID, bookID, viewModel); // Assert mockLoanRepo.Verify(f => f.UpdateLoan(userID, bookID, patchViewModel), Times.Once()); mockMapper.Verify(f => f.Map <LoanViewModel, PatchLoanViewModel>(viewModel), Times.Once()); }
public void GetRecommendationsByUserID2Pages() { // Arrange var userID = 1; var pageNumber = 1; int?pageSize = 5; var numberOfRecommendations = 10; var recommendations = MockDataGenerator.CreateRecommendations(numberOfRecommendations); var envelope = MockDataGenerator.CreateRecommendationsEnvelope(recommendations, pageNumber, pageSize); var mockReviewRepo = new Mock <IReviewRepository>(); var mockMapper = new Mock <IMapper>(); mockReviewRepo.Setup(f => f.GetRecommendationsByUserID(userID, pageNumber, pageSize)).Returns(envelope); var service = new RecommendationService(mockReviewRepo.Object, mockMapper.Object); // Act var returnedRecommendations = service.GetRecommendationsByUserID(userID, pageNumber, pageSize); // Assert mockReviewRepo.Verify(f => f.GetRecommendationsByUserID(userID, pageNumber, pageSize), Times.Once()); Assert.IsNotNull(returnedRecommendations); Assert.AreEqual(returnedRecommendations.Paging.PageNumber, pageNumber); Assert.AreEqual(returnedRecommendations.Paging.PageMaxSize, pageSize); Assert.AreEqual(returnedRecommendations.Paging.PageCount, 2); Assert.AreEqual(returnedRecommendations.Paging.TotalNumberOfItems, numberOfRecommendations); Assert.AreEqual(returnedRecommendations.Items.First().Book.ID, recommendations.First().Book.ID); }
public void UpdateUserEmpty() { // Arrange var userID = 1; var newUser = new PatchUserViewModel(); var existingUser = MockDataGenerator.CreateUserViewModel(userID); var user = MockDataGenerator.CreateUser(userID); var mockUserRepo = new Mock <IUserRepository>(); var mockLoanRepo = new Mock <ILoanRepository>(); var mockMapper = new Mock <IMapper>(); mockUserRepo.Setup(f => f.UpdateUser(userID, It.IsAny <UserViewModel>())); mockUserRepo.Setup(f => f.GetUserByID(userID)).Returns(user); mockMapper.Setup(x => x.Map <PatchUserViewModel, UserViewModel>(It.IsAny <PatchUserViewModel>())).Returns(existingUser); var service = new UserService(mockUserRepo.Object, mockLoanRepo.Object, mockMapper.Object); // Act service.UpdateUser(userID, newUser); // Assert mockUserRepo.Verify(f => f.UpdateUser(userID, It.IsAny <UserViewModel>()), Times.Once()); mockUserRepo.Verify(f => f.GetUserByID(userID), Times.Once()); mockMapper.Verify(f => f.Map <PatchUserViewModel, UserViewModel>(It.IsAny <PatchUserViewModel>()), Times.Once()); }
public void UpdateUserAlreadyExists() { // Arrange var userID = 5; var userEntities = MockDataGenerator.CreateUserEntities(10); var userEntitiesQueryable = userEntities.AsQueryable(); var viewModel = MockDataGenerator.CreateUserViewModel(2); var userEntity = MockDataGenerator.CreateUserEntity(userID); var mockDbContext = new Mock <DatabaseContext>(); var mockUserSet = new Mock <DbSet <UserEntity> >(); var mockLoanRepo = new Mock <ILoanRepository>(); var mockMapper = new Mock <IMapper>(); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.Provider).Returns(userEntitiesQueryable.Provider); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.Expression).Returns(userEntitiesQueryable.Expression); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.ElementType).Returns(userEntitiesQueryable.ElementType); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.GetEnumerator()).Returns(userEntitiesQueryable.GetEnumerator()); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.GetEnumerator()).Returns(userEntitiesQueryable.GetEnumerator()); mockDbContext.Setup(f => f.Users).Returns(mockUserSet.Object); var repository = new UserRepository(mockDbContext.Object, mockMapper.Object, mockLoanRepo.Object); // Act repository.UpdateUser(userID, viewModel); }
public void AddReview() { // Arrange int userID = 1; int bookID = 1; int rating = 4; var review = MockDataGenerator.CreateReviewViewModel(rating); var reviewDTO = MockDataGenerator.CreateReview(userID, bookID, rating); var mockReviewRepo = new Mock <IReviewRepository>(); var mockMapper = new Mock <IMapper>(); mockReviewRepo.Setup(f => f.AddReview(userID, bookID, review)).Returns(reviewDTO); var service = new ReviewService(mockReviewRepo.Object, mockMapper.Object); // Act var returnedReview = service.AddReview(userID, bookID, review); // Assert mockReviewRepo.Verify(f => f.AddReview(userID, bookID, review), Times.Once()); Assert.IsNotNull(returnedReview); Assert.AreEqual(returnedReview.User.ID, userID); Assert.AreEqual(returnedReview.Book.ID, bookID); Assert.AreEqual(returnedReview.Rating, rating); }
public void UpdateBookEmpty() { // Arrange int bookID = 1; var patchViewModel = new PatchBookViewModel(); var viewModel = MockDataGenerator.CreateBookViewModel(bookID); var oldDTO = MockDataGenerator.CreateBook(bookID); var mockBookRepo = new Mock <IBookRepository>(); var mockLoanRepo = new Mock <ILoanRepository>(); var mockMapper = new Mock <IMapper>(); mockBookRepo.Setup(f => f.GetBookByID(bookID)).Returns(oldDTO); mockBookRepo.Setup(f => f.UpdateBook(bookID, viewModel)); mockMapper.Setup(x => x.Map <PatchBookViewModel, BookViewModel>(patchViewModel)).Returns(viewModel); var service = new BookService(mockBookRepo.Object, mockLoanRepo.Object, mockMapper.Object); // Act service.UpdateBook(bookID, patchViewModel); // Assert mockBookRepo.Verify(f => f.GetBookByID(bookID), Times.Once()); mockBookRepo.Verify(f => f.UpdateBook(bookID, viewModel), Times.Once()); mockMapper.Verify(f => f.Map <PatchBookViewModel, BookViewModel>(patchViewModel), Times.Once()); }
public void GetUserByID() { // Arrange var userID = 1; var userEntities = MockDataGenerator.CreateUserEntities(10); var userEntitiesQueryable = userEntities.AsQueryable(); var userEntity = MockDataGenerator.CreateUserEntity(userID); var userDTO = MockDataGenerator.CreateUser(userID); var mockDbContext = new Mock <DatabaseContext>(); var mockUserSet = new Mock <DbSet <UserEntity> >(); var mockLoanRepo = new Mock <ILoanRepository>(); var mockMapper = new Mock <IMapper>(); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.Provider).Returns(userEntitiesQueryable.Provider); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.Expression).Returns(userEntitiesQueryable.Expression); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.ElementType).Returns(userEntitiesQueryable.ElementType); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.GetEnumerator()).Returns(userEntitiesQueryable.GetEnumerator()); mockDbContext.Setup(f => f.Users).Returns(mockUserSet.Object); mockMapper.Setup(f => f.Map <UserEntity, UserDTO>(It.Is <UserEntity>(data => data.ID == userID))).Returns(userDTO); var repository = new UserRepository(mockDbContext.Object, mockMapper.Object, mockLoanRepo.Object); // Act var result = repository.GetUserByID(userID); // Assert mockMapper.Verify(f => f.Map <UserEntity, UserDTO>(It.Is <UserEntity>(data => data.ID == userID)), Times.Once()); Assert.IsNotNull(result); Assert.AreEqual(result.ID, userID); }
public void GetUsersBasic() { // Arrange var pageNumber = 1; int?pageSize = null; var users = MockDataGenerator.CreateUsers(10); var envelope = MockDataGenerator.CreateUsersEnvelope(users, pageNumber, pageSize); var mockUserRepo = new Mock <IUserRepository>(); var mockLoanRepo = new Mock <ILoanRepository>(); var mockMapper = new Mock <IMapper>(); mockUserRepo.Setup(f => f.GetUsers(pageNumber, pageSize)).Returns(envelope); var service = new UserService(mockUserRepo.Object, mockLoanRepo.Object, mockMapper.Object); // Act var returnedUsers = service.GetUsers(pageNumber, pageSize); // Assert mockUserRepo.Verify(f => f.GetUsers(pageNumber, pageSize), Times.Once()); Assert.IsNotNull(returnedUsers); Assert.AreEqual(returnedUsers.Paging.PageNumber, pageNumber); Assert.AreEqual(returnedUsers.Paging.PageMaxSize, 50); Assert.AreEqual(returnedUsers.Paging.PageCount, 1); Assert.AreEqual(returnedUsers.Paging.TotalNumberOfItems, 10); Assert.AreEqual(returnedUsers.Items.First().ID, users.First().ID); Assert.AreEqual(returnedUsers.Items.Last().ID, users.Last().ID); }
public void GetBooks2Pages() { // Arrange var pageNumber = 1; int?pageSize = 5; int numberOfBooks = 10; var books = MockDataGenerator.CreateBooks(numberOfBooks); var envelope = MockDataGenerator.CreateBooksEnvelope(books, pageNumber, pageSize); var mockBookRepo = new Mock <IBookRepository>(); var mockLoanRepo = new Mock <ILoanRepository>(); var mockMapper = new Mock <IMapper>(); mockBookRepo.Setup(f => f.GetBooks(pageNumber, pageSize)).Returns(envelope); var service = new BookService(mockBookRepo.Object, mockLoanRepo.Object, mockMapper.Object); // Act var returnedUsers = service.GetBooks(pageNumber, pageSize); // Assert mockBookRepo.Verify(f => f.GetBooks(pageNumber, pageSize), Times.Once()); Assert.IsNotNull(returnedUsers); Assert.AreEqual(returnedUsers.Paging.PageNumber, pageNumber); Assert.AreEqual(returnedUsers.Paging.PageMaxSize, pageSize); Assert.AreEqual(returnedUsers.Paging.PageCount, 2); Assert.AreEqual(returnedUsers.Paging.TotalNumberOfItems, numberOfBooks); Assert.AreEqual(returnedUsers.Items.First().ID, books.First().ID); }
public void AddUserException() { // Arrange var userID = 11; var userEntities = MockDataGenerator.CreateUserEntities(10); var userEntitiesQueryable = userEntities.AsQueryable(); var viewModel = MockDataGenerator.CreateUserViewModel(userID); var userEntity = MockDataGenerator.CreateUserEntity(userID); var mockDbContext = new Mock <DatabaseContext>(); var mockUserSet = new Mock <DbSet <UserEntity> >(); var mockLoanRepo = new Mock <ILoanRepository>(); var mockMapper = new Mock <IMapper>(); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.Provider).Returns(userEntitiesQueryable.Provider); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.Expression).Returns(userEntitiesQueryable.Expression); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.ElementType).Returns(userEntitiesQueryable.ElementType); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.GetEnumerator()).Returns(userEntitiesQueryable.GetEnumerator()); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.GetEnumerator()).Returns(userEntitiesQueryable.GetEnumerator()); mockUserSet.Setup(f => f.Add(It.Is <UserEntity>(data => data.Email == userEntity.Email))).Throws(new Exception()); mockDbContext.Setup(f => f.Users).Returns(mockUserSet.Object); mockMapper.Setup(f => f.Map <UserViewModel, UserEntity>(It.Is <UserViewModel>(data => data.Email == viewModel.Email))).Returns(userEntity); var repository = new UserRepository(mockDbContext.Object, mockMapper.Object, mockLoanRepo.Object); // Act repository.AddUser(viewModel); }
public void AddUser() { // Arrange var userID = 1; var newUser = MockDataGenerator.CreateUserViewModel(userID); var expectedUser = MockDataGenerator.CreateUser(1); var mockUserRepo = new Mock <IUserRepository>(); var mockLoanRepo = new Mock <ILoanRepository>(); var mockMapper = new Mock <IMapper>(); mockUserRepo.Setup(f => f.AddUser(newUser)).Returns(expectedUser); var service = new UserService(mockUserRepo.Object, mockLoanRepo.Object, mockMapper.Object); // Act var returnedUser = service.AddUser(newUser); // Assert mockUserRepo.Verify(f => f.AddUser(newUser), Times.Once()); Assert.IsNotNull(returnedUser); Assert.AreEqual(returnedUser.ID, userID); Assert.AreEqual(returnedUser.Name, newUser.Name); Assert.AreEqual(returnedUser.Email, newUser.Email); Assert.AreEqual(returnedUser.Address, newUser.Address); }
public void GetUsersReport2Pages() { // Arrange var pageNumber = 1; int? pageSize = 5; DateTime?loanDate = new DateTime(2000, 1, 1); int? duration = 30; var numberOfReports = 10; var reports = MockDataGenerator.CreateUserReports(numberOfReports); var envelope = MockDataGenerator.CreateUserReportsEnvelope(reports, pageNumber, pageSize); var mockLoanRepo = new Mock <ILoanRepository>(); var mockMapper = new Mock <IMapper>(); mockLoanRepo.Setup(f => f.GetUsersReport(pageNumber, pageSize, loanDate, duration)).Returns(envelope); var service = new ReportingService(mockLoanRepo.Object, mockMapper.Object); // Act var returnedReports = service.GetUsersReport(pageNumber, pageSize, loanDate, duration); // Assert mockLoanRepo.Verify(f => f.GetUsersReport(pageNumber, pageSize, loanDate, duration), Times.Once()); Assert.IsNotNull(returnedReports); Assert.AreEqual(returnedReports.Paging.PageNumber, pageNumber); Assert.AreEqual(returnedReports.Paging.PageMaxSize, pageSize); Assert.AreEqual(returnedReports.Paging.PageCount, 2); Assert.AreEqual(returnedReports.Paging.TotalNumberOfItems, numberOfReports); Assert.AreEqual(returnedReports.Items.First().User.ID, reports.First().User.ID); }
public void AddBook() { // Arrange var bookID = 11; var entites = MockDataGenerator.CreateBookEntities(10); var entitiesQueryable = entites.AsQueryable(); var viewModel = MockDataGenerator.CreateBookViewModel(bookID); var BookEntity = MockDataGenerator.CreateBookEntity(bookID); var BookDTO = MockDataGenerator.CreateBook(bookID); var mockDbContext = new Mock <DatabaseContext>(); var mockBookSet = new Mock <DbSet <BookEntity> >(); var mockMapper = new Mock <IMapper>(); mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.Provider).Returns(entitiesQueryable.Provider); mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.Expression).Returns(entitiesQueryable.Expression); mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.ElementType).Returns(entitiesQueryable.ElementType); mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.GetEnumerator()).Returns(entitiesQueryable.GetEnumerator()); mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.GetEnumerator()).Returns(entitiesQueryable.GetEnumerator()); mockBookSet.Setup(f => f.Add(It.Is <BookEntity>(data => data.Title == BookEntity.Title))).Callback <BookEntity>(u => u.ID = BookEntity.ID); mockDbContext.Setup(f => f.Books).Returns(mockBookSet.Object); mockMapper.Setup(f => f.Map <BookViewModel, BookEntity>(It.Is <BookViewModel>(data => data.Title == viewModel.Title))).Returns(BookEntity); mockMapper.Setup(f => f.Map <BookEntity, BookDTO>(It.Is <BookEntity>(data => data.ID == BookDTO.ID))).Returns(BookDTO); var repository = new BookRepository(mockDbContext.Object, mockMapper.Object); // Act var result = repository.AddBook(viewModel); // Assert mockMapper.Verify(f => f.Map <BookViewModel, BookEntity>(It.Is <BookViewModel>(data => data.Title == viewModel.Title)), Times.Once()); mockMapper.Verify(f => f.Map <BookEntity, BookDTO>(It.Is <BookEntity>(data => data.ID == BookDTO.ID)), Times.Once()); mockBookSet.Verify(f => f.Add(It.Is <BookEntity>(data => data.Title == BookEntity.Title)), Times.Once()); Assert.IsNotNull(result); Assert.AreEqual(result.ID, bookID); }
public void DeleteUserException() { // Arrange var userID = 5; var userEntities = MockDataGenerator.CreateUserEntities(10); var userEntitiesQueryable = userEntities.AsQueryable(); var userEntity = MockDataGenerator.CreateUserEntity(userID); var mockDbContext = new Mock <DatabaseContext>(); var mockUserSet = new Mock <DbSet <UserEntity> >(); var mockLoanRepo = new Mock <ILoanRepository>(); var mockMapper = new Mock <IMapper>(); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.Provider).Returns(userEntitiesQueryable.Provider); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.Expression).Returns(userEntitiesQueryable.Expression); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.ElementType).Returns(userEntitiesQueryable.ElementType); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.GetEnumerator()).Returns(userEntitiesQueryable.GetEnumerator()); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.GetEnumerator()).Returns(userEntitiesQueryable.GetEnumerator()); mockUserSet.Setup(f => f.Remove(It.Is <UserEntity>(data => data.ID == userEntity.ID))).Throws(new Exception()); mockDbContext.Setup(f => f.Users).Returns(mockUserSet.Object); var repository = new UserRepository(mockDbContext.Object, mockMapper.Object, mockLoanRepo.Object); // Act repository.DeleteUserByID(userID); }
public void GetBooksInLoanByUserIDBasic() { // Arrange var pageNumber = 1; int?pageSize = null; int numberOfLoans = 10; int userID = 1; var userLoans = MockDataGenerator.CreateUserLoans(numberOfLoans); var envelope = MockDataGenerator.CreateUsersLoansEnvelope(userLoans, pageNumber, pageSize); var mockBookRepo = new Mock <IBookRepository>(); var mockLoanRepo = new Mock <ILoanRepository>(); var mockMapper = new Mock <IMapper>(); mockLoanRepo.Setup(f => f.GetLoansByUserID(userID, true, pageNumber, pageSize)).Returns(envelope); var service = new BookService(mockBookRepo.Object, mockLoanRepo.Object, mockMapper.Object); // Act var returnedLoans = service.GetBooksInLoanByUserID(userID, pageNumber, pageSize); // Assert mockLoanRepo.Verify(f => f.GetLoansByUserID(userID, true, pageNumber, pageSize), Times.Once()); Assert.IsNotNull(returnedLoans); Assert.AreEqual(returnedLoans.Paging.PageNumber, pageNumber); Assert.AreEqual(returnedLoans.Paging.PageMaxSize, 50); Assert.AreEqual(returnedLoans.Paging.PageCount, 1); Assert.AreEqual(returnedLoans.Paging.TotalNumberOfItems, numberOfLoans); Assert.AreEqual(returnedLoans.Items.First().Book.ID, userLoans.First().Book.ID); Assert.AreEqual(returnedLoans.Items.Last().Book.ID, userLoans.Last().Book.ID); }
public void DeleteBook() { // Arrange var bookID = 5; var entites = MockDataGenerator.CreateBookEntities(10); var entitiesQueryable = entites.AsQueryable(); var BookEntity = MockDataGenerator.CreateBookEntity(bookID); var mockDbContext = new Mock <DatabaseContext>(); var mockBookSet = new Mock <DbSet <BookEntity> >(); var mockMapper = new Mock <IMapper>(); mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.Provider).Returns(entitiesQueryable.Provider); mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.Expression).Returns(entitiesQueryable.Expression); mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.ElementType).Returns(entitiesQueryable.ElementType); mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.GetEnumerator()).Returns(entitiesQueryable.GetEnumerator()); mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.GetEnumerator()).Returns(entitiesQueryable.GetEnumerator()); mockBookSet.Setup(f => f.Remove(It.Is <BookEntity>(data => data.ID == BookEntity.ID))); mockDbContext.Setup(f => f.Books).Returns(mockBookSet.Object); var repository = new BookRepository(mockDbContext.Object, mockMapper.Object); // Act repository.DeleteBookByID(bookID); // Assert mockBookSet.Verify(f => f.Remove(It.Is <BookEntity>(data => data.ID == BookEntity.ID)), Times.Once()); }
public void UpdateBookException() { // Arrange var bookID = 5; var entites = MockDataGenerator.CreateBookEntities(10); var entitiesQueryable = entites.AsQueryable(); var viewModel = MockDataGenerator.CreateBookViewModel(20); var BookEntity = MockDataGenerator.CreateBookEntity(bookID); var mockDbContext = new Mock <DatabaseContext>(); var mockBookSet = new Mock <DbSet <BookEntity> >(); var mockMapper = new Mock <IMapper>(); mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.Provider).Returns(entitiesQueryable.Provider); mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.Expression).Returns(entitiesQueryable.Expression); mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.ElementType).Returns(entitiesQueryable.ElementType); mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.GetEnumerator()).Returns(entitiesQueryable.GetEnumerator()); mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.GetEnumerator()).Returns(entitiesQueryable.GetEnumerator()); mockBookSet.Setup(f => f.Update(It.Is <BookEntity>(data => data.ID == BookEntity.ID))).Throws(new Exception()); mockDbContext.Setup(f => f.Books).Returns(mockBookSet.Object); var repository = new BookRepository(mockDbContext.Object, mockMapper.Object); // Act repository.UpdateBook(bookID, viewModel); }
public void GetBookReviewsBasic() { // Arrange var pageNumber = 1; int?pageSize = null; int numberOfBookReviews = 10; var reviews = MockDataGenerator.CreateBookReviewsForBook(numberOfBookReviews); var envelope = MockDataGenerator.CreateBookReviewsEnvelope(reviews, pageNumber, pageSize); var mockReviewRepo = new Mock <IReviewRepository>(); var mockMapper = new Mock <IMapper>(); mockReviewRepo.Setup(f => f.GetBookReviews(pageNumber, pageSize)).Returns(envelope); var service = new ReviewService(mockReviewRepo.Object, mockMapper.Object); // Act var returnedReviews = service.GetBookReviews(pageNumber, pageSize); // Assert mockReviewRepo.Verify(f => f.GetBookReviews(pageNumber, pageSize), Times.Once()); Assert.IsNotNull(returnedReviews); Assert.AreEqual(returnedReviews.Paging.PageNumber, pageNumber); Assert.AreEqual(returnedReviews.Paging.PageMaxSize, 50); Assert.AreEqual(returnedReviews.Paging.PageCount, 1); Assert.AreEqual(returnedReviews.Paging.TotalNumberOfItems, numberOfBookReviews); Assert.AreEqual(returnedReviews.Items.First().Book.ID, reviews.First().Book.ID); Assert.AreEqual(returnedReviews.Items.Last().Book.ID, reviews.Last().Book.ID); }
public void GetBookByIDExceptionBookRepo() { // Arrange var pageNumber = 1; int?pageSize = null; int numberOfLoans = 10; int bookID = 1; var bookDTO = MockDataGenerator.CreateBook(bookID); var bookDetailsBasicDTO = MockDataGenerator.CreateBasicBookDetails(bookID); var bookLoans = MockDataGenerator.CreateBookLoans(numberOfLoans); var envelope = MockDataGenerator.CreateBookLoansEnvelope(bookLoans, pageNumber, pageSize); var bookDetailsDTO = MockDataGenerator.CreateBasicBookDetails(bookID); bookDetailsDTO.LoanHistory = envelope; var mockBookRepo = new Mock <IBookRepository>(); var mockLoanRepo = new Mock <ILoanRepository>(); var mockMapper = new Mock <IMapper>(); mockBookRepo.Setup(f => f.GetBookByID(bookID)).Throws(new Exception()); mockLoanRepo.Setup(f => f.GetLoansByBookID(bookID, false, pageNumber, pageSize)).Returns(envelope); mockMapper.Setup(x => x.Map <BookDTO, BookDetailsDTO>(bookDTO)).Returns(bookDetailsBasicDTO); var service = new BookService(mockBookRepo.Object, mockLoanRepo.Object, mockMapper.Object); // Act service.GetBookByID(bookID, pageNumber, pageSize); }
public void AutoIncrement_ShoulBeIncrement() { var data = new MockDataGenerator <MyType>() .Register(x => x.Value, new AutoIncrementDataGenerator(start: 10, increment: 10)) .Generate(count: 5); Assert.Equal(10, data[0].Value); Assert.Equal(20, data[1].Value); Assert.Equal(30, data[2].Value); Assert.Equal(40, data[3].Value); Assert.Equal(50, data[4].Value); }
public BannerModel CreateBannerData(string from, string to, DateTime date) { var mockData = new MockDataGenerator(); var fromPlace = mockData.Places.FirstOrDefault(p => p.AirportCode == from); var toPlace = mockData.Places.FirstOrDefault(p => p.AirportCode == to); var departureAirportName = fromPlace.AirportName; var destinationAirportName = toPlace.AirportName; var destinationCityName = toPlace.CityName; var monthName = date.ToString("MMMM"); var flightData = mockData.FlightData.FirstOrDefault(f => f.FromPlaceId == fromPlace.PlaceId && f.ToPlaceId == toPlace.PlaceId); var cheapestFlight = flightData.Prices.Where(d => d.Date.Month == date.Month).Min(p => p.Price); var averageTemperature = mockData.WeatherForecasts.Where(w => w.PlaceId == toPlace.PlaceId && w.Date.Month == date.Month).Average(t => t.Temperature); var imageUrls = new List <string>(); mockData.Images.ToList().ForEach((image) => { if (averageTemperature > (decimal)20.0) { if (image.IsSepicific) { imageUrls.Add($"https://i.ibb.co/{image.Name}.jpg"); return; } } else if (!image.IsSepicific) { imageUrls.Add($"https://i.ibb.co/{image.Name}.jpg"); } }); return(new BannerModel() { CityName = destinationCityName, DepartureAirport = departureAirportName, DestinationAirport = destinationAirportName, Images = imageUrls, MonthName = monthName, Price = cheapestFlight, Temperature = averageTemperature }); }
public void GetUsers2Pages() { // Arrange var pageNumber = 1; int?pageSize = 5; var userEntities = MockDataGenerator.CreateUserEntities(10); var expectedUserEntities = MockDataGenerator.CreateUserEntities(5); var userEntitiesQueryable = userEntities.AsQueryable(); var userDTOs = MockDataGenerator.CreateUsers(10); var envelope = MockDataGenerator.CreateUsersEnvelope(userDTOs, pageNumber, pageSize); var mockDbContext = new Mock <DatabaseContext>(); var mockUserSet = new Mock <DbSet <UserEntity> >(); var mockLoanRepo = new Mock <ILoanRepository>(); var mockMapper = new Mock <IMapper>(); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.Provider).Returns(userEntitiesQueryable.Provider); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.Expression).Returns(userEntitiesQueryable.Expression); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.ElementType).Returns(userEntitiesQueryable.ElementType); mockUserSet.As <IQueryable <UserEntity> >().Setup(m => m.GetEnumerator()).Returns(userEntitiesQueryable.GetEnumerator()); mockDbContext.Setup(f => f.Users).Returns(mockUserSet.Object); mockMapper.Setup(f => f.Map <IList <UserEntity>, IList <UserDTO> >(It.Is <IList <UserEntity> >(data => data.Count == expectedUserEntities.Count && data.First().ID == expectedUserEntities.First().ID && data.Last().ID == expectedUserEntities.Last().ID))) .Returns(userDTOs); var repository = new UserRepository(mockDbContext.Object, mockMapper.Object, mockLoanRepo.Object); // Act var result = repository.GetUsers(pageNumber, pageSize); // Assert mockMapper.Verify(f => f.Map <IList <UserEntity>, IList <UserDTO> >(It.Is <IList <UserEntity> >(data => data.Count == expectedUserEntities.Count && data.First().ID == expectedUserEntities.First().ID && data.Last().ID == expectedUserEntities.Last().ID)), Times.Once()); Assert.IsNotNull(result); Assert.AreEqual(result.Paging.PageNumber, pageNumber); Assert.AreEqual(result.Paging.PageMaxSize, pageSize); Assert.AreEqual(result.Paging.PageCount, 2); Assert.AreEqual(result.Paging.TotalNumberOfItems, 10); Assert.AreEqual(result.Items.First().ID, userDTOs.First().ID); }
public void ReplaceBookException() { // Arrange int bookID = 1; var viewModel = MockDataGenerator.CreateBookViewModel(bookID); var mockBookRepo = new Mock <IBookRepository>(); var mockLoanRepo = new Mock <ILoanRepository>(); var mockMapper = new Mock <IMapper>(); mockBookRepo.Setup(f => f.UpdateBook(bookID, viewModel)).Throws(new Exception()); var service = new BookService(mockBookRepo.Object, mockLoanRepo.Object, mockMapper.Object); // Act service.ReplaceBook(bookID, viewModel); }
public static void BuildWellness(this IServiceCollection services, bool useMock = true) { if (useMock) { services.AddSingleton <IActivityManagementService, ActivityManagementMock>(); services.AddSingleton <IActivityParticipationService, ActivityParticipationMock>(); services.AddSingleton <IEventManagementService, EventManagmentMock>(); services.AddSingleton <IEventParticipationService, EventParticipationMock>(); services.AddSingleton <IFrequentlyAskedQuestionService, FAQManagmentMock>(); services.AddSingleton <IProfileService>(MockDataGenerator.CreateProfile()); } services.AddScoped <HomeViewModel>(); services.AddScoped <ActivityManagementViewModel>(); services.AddScoped <EventManagementViewModel>(); services.AddScoped <MainViewModel>(); services.AddScoped <FAQManagementViewModel>(); }
public void UpdateReviewException() { // Arrange int userID = 1; int bookID = 1; int rating = 4; var patchReview = MockDataGenerator.CreatePatchReviewViewModel(rating); var mockReviewRepo = new Mock <IReviewRepository>(); var mockMapper = new Mock <IMapper>(); mockReviewRepo.Setup(f => f.UpdateReview(userID, bookID, patchReview)).Throws(new Exception()); var service = new ReviewService(mockReviewRepo.Object, mockMapper.Object); // Act service.UpdateReview(userID, bookID, patchReview); }
public void GetBookByIDBasic() { // Arrange var pageNumber = 1; int?pageSize = null; int numberOfLoans = 10; int bookID = 1; var bookDTO = MockDataGenerator.CreateBook(bookID); var bookDetailsBasicDTO = MockDataGenerator.CreateBasicBookDetails(bookID); var bookLoans = MockDataGenerator.CreateBookLoans(numberOfLoans); var envelope = MockDataGenerator.CreateBookLoansEnvelope(bookLoans, pageNumber, pageSize); var bookDetailsDTO = MockDataGenerator.CreateBasicBookDetails(bookID); bookDetailsDTO.LoanHistory = envelope; var mockBookRepo = new Mock <IBookRepository>(); var mockLoanRepo = new Mock <ILoanRepository>(); var mockMapper = new Mock <IMapper>(); mockBookRepo.Setup(f => f.GetBookByID(bookID)).Returns(bookDTO); mockLoanRepo.Setup(f => f.GetLoansByBookID(bookID, false, pageNumber, pageSize)).Returns(envelope); mockMapper.Setup(x => x.Map <BookDTO, BookDetailsDTO>(bookDTO)).Returns(bookDetailsBasicDTO); var service = new BookService(mockBookRepo.Object, mockLoanRepo.Object, mockMapper.Object); // Act var returnedBook = service.GetBookByID(bookID, pageNumber, pageSize); // Assert mockBookRepo.Verify(f => f.GetBookByID(bookID), Times.Once()); mockLoanRepo.Verify(f => f.GetLoansByBookID(bookID, false, pageNumber, pageSize), Times.Once()); mockMapper.Verify(f => f.Map <BookDTO, BookDetailsDTO>(bookDTO), Times.Once()); Assert.IsNotNull(returnedBook); Assert.AreEqual(returnedBook.ID, bookID); Assert.AreEqual(returnedBook.LoanHistory.Paging.PageNumber, pageNumber); Assert.AreEqual(returnedBook.LoanHistory.Paging.PageMaxSize, 50); Assert.AreEqual(returnedBook.LoanHistory.Paging.PageCount, 1); Assert.AreEqual(returnedBook.LoanHistory.Paging.TotalNumberOfItems, numberOfLoans); Assert.AreEqual(returnedBook.LoanHistory.Items.First().User.ID, bookLoans.First().User.ID); Assert.AreEqual(returnedBook.LoanHistory.Items.Last().User.ID, bookLoans.Last().User.ID); }
public void UpdateLoanException() { // Arrange int userID = 1; int bookID = 1; var loanDate = new DateTime(2000, 1, 1); DateTime?returnDate = null; var patchViewModel = MockDataGenerator.CreatePatchLoanViewModel(loanDate, returnDate); var mockBookRepo = new Mock <IBookRepository>(); var mockLoanRepo = new Mock <ILoanRepository>(); var mockMapper = new Mock <IMapper>(); mockLoanRepo.Setup(f => f.UpdateLoan(userID, bookID, patchViewModel)).Throws(new Exception()); var service = new BookService(mockBookRepo.Object, mockLoanRepo.Object, mockMapper.Object); // Act service.UpdateLoan(userID, bookID, patchViewModel); }
public void UpdateBookExceptionGet() { // Arrange int bookID = 1; var patchViewModel = MockDataGenerator.CreatePatchBookViewModel(bookID); var viewModel = MockDataGenerator.CreateBookViewModel(bookID); var mockBookRepo = new Mock <IBookRepository>(); var mockLoanRepo = new Mock <ILoanRepository>(); var mockMapper = new Mock <IMapper>(); mockBookRepo.Setup(f => f.GetBookByID(bookID)).Throws(new Exception()); mockBookRepo.Setup(f => f.UpdateBook(bookID, viewModel)); mockMapper.Setup(x => x.Map <PatchBookViewModel, BookViewModel>(patchViewModel)).Returns(viewModel); var service = new BookService(mockBookRepo.Object, mockLoanRepo.Object, mockMapper.Object); // Act service.UpdateBook(bookID, patchViewModel); }
public void ReplaceUser() { // Arrange var userID = 1; var newUser = MockDataGenerator.CreateUserViewModel(userID); var mockUserRepo = new Mock <IUserRepository>(); var mockLoanRepo = new Mock <ILoanRepository>(); var mockMapper = new Mock <IMapper>(); mockUserRepo.Setup(f => f.UpdateUser(userID, newUser)); var service = new UserService(mockUserRepo.Object, mockLoanRepo.Object, mockMapper.Object); // Act service.ReplaceUser(userID, newUser); // Assert mockUserRepo.Verify(f => f.UpdateUser(userID, newUser), Times.Once()); }
public void GetBookByIDException() { // Arrange var bookID = 20; var entites = MockDataGenerator.CreateBookEntities(10); var entitiesQueryable = entites.AsQueryable(); var mockDbContext = new Mock <DatabaseContext>(); var mockBookSet = new Mock <DbSet <BookEntity> >(); var mockMapper = new Mock <IMapper>(); mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.Provider).Returns(entitiesQueryable.Provider); mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.Expression).Returns(entitiesQueryable.Expression); mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.ElementType).Returns(entitiesQueryable.ElementType); mockBookSet.As <IQueryable <BookEntity> >().Setup(m => m.GetEnumerator()).Returns(entitiesQueryable.GetEnumerator()); mockDbContext.Setup(f => f.Books).Throws(new Exception()); var repository = new BookRepository(mockDbContext.Object, mockMapper.Object); // Act repository.GetBookByID(bookID); }