public async Task Loans_AddLoan() { // arrange const string LENDER_USERNAME = "******"; const string BORROWER_USERNAME = "******"; const int LENDER_ID = 1; const int BORROWER_ID = 2; const int OWNED_BOOK_ID = 1; using var contextFactory = new TestLooseLeafContextFactory(); using (LooseLeafContext arrangeContext = contextFactory.CreateContext()) { await contextFactory.CreateUser(arrangeContext, LENDER_USERNAME); await contextFactory.CreateUser(arrangeContext, BORROWER_USERNAME); await contextFactory.CreateBook(arrangeContext, "Book 1", "Author 1"); await contextFactory.CreateOwnedBook(arrangeContext, 1, 1); await arrangeContext.SaveChangesAsync(); } int fakeAddressId = 1; var fakeBook = new Mock <IOwnedBook>(); fakeBook.Setup(b => b.OwnerId).Returns(LENDER_ID); fakeBook.Setup(b => b.Id).Returns(1); fakeBook.Setup(b => b.Availability).Returns(Availability.Available); var ownedBooks = new List <int>() { OWNED_BOOK_ID }; // act using (LooseLeafContext context = contextFactory.CreateContext()) { var loan = new Business.Models.Loan(LENDER_ID, BORROWER_ID, "Hello", new DateTime(2000, 1, 2), new DateTime(2000, 1, 4), fakeAddressId, ownedBooks, Business.Models.LoanStatus.Requested); LoanRepository loanRepository = new LoanRepository(context); await loanRepository.AddLoanAsync(loan); await context.SaveChangesAsync(); } //assert using var assertContext = contextFactory.CreateContext(); Assert.Equal(1, assertContext.Loans.Count()); }
public async Task OwnedBookRepository_AddOwnedBook() { // arrange const int userId = 1; const string username = "******"; const string title = "The Martian"; const string author = "Beyonce"; long isbn = 9780804139038; using var contextFactory = new TestLooseLeafContextFactory(); using (LooseLeafContext arrangeContext = contextFactory.CreateContext()) { await contextFactory.CreateUser(arrangeContext, username); isbn = await contextFactory.CreateBook(arrangeContext, title, author); await arrangeContext.SaveChangesAsync(); } Mock <IIsbnData> mockIsbn = new Mock <IIsbnData>(); mockIsbn.Setup(x => x.IsbnValue).Returns(isbn); Mock <IBook> fakeBook = new Mock <IBook>(); fakeBook.Setup(x => x.Title).Returns(title); fakeBook.Setup(x => x.Author).Returns(author); fakeBook.Setup(x => x.Genres).Returns(new List <string>() { "Test" }); fakeBook.Setup(x => x.Isbn).Returns(isbn); Mock <IOwnedBook> fakeOwnedBook = new Mock <IOwnedBook>(); fakeOwnedBook.Setup(x => x.Availability).Returns(Availability.Available); fakeOwnedBook.Setup(x => x.Condition).Returns(PhysicalCondition.LikeNew); fakeOwnedBook.Setup(x => x.OwnerId).Returns(userId); fakeOwnedBook.Setup(x => x.Isbn).Returns(mockIsbn.Object); GoogleBooks googleBooks = new GoogleBooks(new HttpClient(), null); // act using (LooseLeafContext actContext = contextFactory.CreateContext()) { IOwnedBookRepository ownedBookRepo = new OwnedBookRepository(actContext); await ownedBookRepo.AddOwnedBookAsync(fakeOwnedBook.Object, googleBooks); await actContext.SaveChangesAsync(); } // assert using LooseLeafContext assertContext = contextFactory.CreateContext(); var ownedBook = await assertContext.OwnedBooks.Include(x => x.User).Include(x => x.Book).SingleAsync(); Assert.Equal(isbn, ownedBook.Book.Isbn); Assert.Equal(username, ownedBook.User.Username); }
public async Task WishlistRepository_GetWishlist_ReturnList() { // arrange const string username = "******"; const int userId = 1; using var contextFactory = new TestLooseLeafContextFactory(); List <DataAccess.Wishlist> wishlists = new List <DataAccess.Wishlist>() { new DataAccess.Wishlist() { UserId = userId, BookId = 1 }, new DataAccess.Wishlist() { UserId = userId, BookId = 2 }, new DataAccess.Wishlist() { UserId = userId, BookId = 3 } }; using (LooseLeafContext addContext = contextFactory.CreateContext()) { await contextFactory.CreateUser(addContext, username); await contextFactory.CreateBook(addContext, "Book 1", "Author 1"); await contextFactory.CreateBook(addContext, "Book 2", "Author 2"); await contextFactory.CreateBook(addContext, "Book 3", "Author 3"); await addContext.SaveChangesAsync(); await addContext.Wishlists.AddRangeAsync(wishlists); await addContext.SaveChangesAsync(); } using LooseLeafContext context = contextFactory.CreateContext(); IWishlistRepository wishlistRepository = new WishlistRepository(context); // act var books = await wishlistRepository.GetUserWishlist(userId); // assert Assert.Equal(wishlists.Count, books.Count()); }
public async Task WishlistRepository_AddBookToUserWishlist() { // arrange //Constants to compare user and book with test results. const string username = "******"; const string bookName = "Test Book"; const string authorName = "The Author"; long isbn; const int userId = 1; const int bookId = 1; using var contextFactory = new TestLooseLeafContextFactory(); // Add in foreign key / required database data. Wishlist requires a user and a book type to exist in the database. using (LooseLeafContext arrangeContext = contextFactory.CreateContext()) { await contextFactory.CreateUser(arrangeContext, username); isbn = await contextFactory.CreateBook(arrangeContext, bookName, authorName); await arrangeContext.SaveChangesAsync(); } // act using (LooseLeafContext actContext = contextFactory.CreateContext()) { // Create Repository IWishlistRepository wishlistRepository = new WishlistRepository(actContext); // Test repository method await wishlistRepository.AddBookToUserWishlist(userId, bookId); await actContext.SaveChangesAsync(); } // assert // Create a new context to ensure that data was saved to database. using LooseLeafContext assertContext = contextFactory.CreateContext(); var wishlist = await assertContext.Wishlists.Include(w => w.User).Include(w => w.Book).SingleAsync(); // assert that the book and user are the same as one added to the wishlist. Assert.Equal(username, wishlist.User.Username); Assert.Equal(bookName, wishlist.Book.Title); Assert.Equal(authorName, wishlist.Book.Author); }
public async Task WishlistRepository_RemoveBookFromUserWishlist() { // arrange const string username = "******"; const string bookName = "Test Book"; const string authorName = "The Author"; long isbn; int originalWishlistCount; const int userId = 1; const int bookId = 1; using var contextFactory = new TestLooseLeafContextFactory(); using (LooseLeafContext arrangeContext = contextFactory.CreateContext()) { await contextFactory.CreateUser(arrangeContext, username); isbn = await contextFactory.CreateBook(arrangeContext, bookName, authorName); await arrangeContext.Wishlists.AddAsync(new DataAccess.Wishlist() { UserId = 1, BookId = 1 }); await arrangeContext.SaveChangesAsync(); } // act using (LooseLeafContext actContext = contextFactory.CreateContext()) { IWishlistRepository wishlistRepository = new WishlistRepository(actContext); originalWishlistCount = actContext.Wishlists.Count(); await wishlistRepository.RemoveBookFromUserWishlist(userId, bookId); await actContext.SaveChangesAsync(); } // assert using LooseLeafContext assertContext = contextFactory.CreateContext(); Assert.Equal(0, assertContext.Wishlists.Count()); Assert.Equal(1, originalWishlistCount); }
public async Task UserRepository_GetAllUsersAsync(int numberOfUsersToCreate) { // arrange using var contextFactory = new TestLooseLeafContextFactory(); using (LooseLeafContext arrangeContext = contextFactory.CreateContext()) { for (int i = 0; i < numberOfUsersToCreate; i++) { await contextFactory.CreateUser(arrangeContext, $"User {i + 1}"); } arrangeContext.SaveChanges(); } using var context = contextFactory.CreateContext(); var repo = new UserRepository(context); // act var users = await repo.GetAllUsersAsync(); // assert Assert.Equal(numberOfUsersToCreate, users.Count()); }
public async Task Loans_GetAllAsync() { // arrange const string LENDER_USERNAME = "******"; const string BORROWER_USERNAME = "******"; const string LOAN_MESSAGE = "Hello, I would like to borrow your book."; const int LENDER_ID = 1; const int BORROWER_ID = 2; const int FIRST_OWNED_BOOK_ID = 1; const int SECOND_OWNED_BOOK_ID = 2; using var contextFactory = new TestLooseLeafContextFactory(); using (LooseLeafContext arrangeContext = contextFactory.CreateContext()) { await contextFactory.CreateAddress(arrangeContext); await contextFactory.CreateUser(arrangeContext, LENDER_USERNAME); await contextFactory.CreateUser(arrangeContext, BORROWER_USERNAME); await contextFactory.CreateBook(arrangeContext, "Book 1", "Author 1"); await contextFactory.CreateOwnedBook(arrangeContext, LENDER_ID, FIRST_OWNED_BOOK_ID); await contextFactory.CreateBook(arrangeContext, "Book 2", "Author 2"); await contextFactory.CreateOwnedBook(arrangeContext, LENDER_ID, SECOND_OWNED_BOOK_ID); var loan = new DataAccess.Loan() { LenderId = LENDER_ID, BorrowerId = BORROWER_ID, Message = LOAN_MESSAGE, LoanStatusId = (int)Business.Models.LoanStatus.Approved, AddressId = 1, DropoffDate = new DateTime(2000, 10, 1), ReturnedDate = new DateTime(2000, 10, 17), }; var loanedBooks = new List <DataAccess.LoanedBook>() { new LoanedBook() { Loan = loan, OwnedBookid = FIRST_OWNED_BOOK_ID }, new LoanedBook() { Loan = loan, OwnedBookid = SECOND_OWNED_BOOK_ID } }; await arrangeContext.AddAsync(loan); await arrangeContext.AddRangeAsync(loanedBooks); await arrangeContext.SaveChangesAsync(); } // act IEnumerable <ILoanResult> loans; using (LooseLeafContext actContext = contextFactory.CreateContext()) { LoanRepository loanRepository = new LoanRepository(actContext); ILoanSearchParams searchParams = new LoanSearchParams(); loans = await loanRepository.GetLoansAsync(searchParams); } // assert var firstLoan = loans.First(); Assert.Single(loans); Assert.Equal(LENDER_ID, firstLoan.Lender.Id); Assert.Equal(BORROWER_ID, firstLoan.Borrower.Id); Assert.Equal(LOAN_MESSAGE, firstLoan.Message); Assert.Equal(FIRST_OWNED_BOOK_ID, firstLoan.LoanedBooks.First().Id); Assert.Equal(SECOND_OWNED_BOOK_ID, firstLoan.LoanedBooks.Last().Id); }
public async Task UserRepository_GetUserRecommendations() { using var contextFactory = new TestLooseLeafContextFactory(); using (LooseLeafContext arrangeContext = contextFactory.CreateContext()) { //creates test address 1 for lender await contextFactory.CreateAddress(arrangeContext); //creates test book 1 await contextFactory.CreateBook(arrangeContext); //creates test book 2 await contextFactory.CreateBook(arrangeContext); //creates test book 3 await contextFactory.CreateBook(arrangeContext); //creates test book 4 await contextFactory.CreateBook(arrangeContext); //creates test book 5 await contextFactory.CreateBook(arrangeContext); //creates owned book 1 await contextFactory.CreateOwnedBook(arrangeContext, 1, 1); //creates owned book 2 await contextFactory.CreateOwnedBook(arrangeContext, 1, 2); //creates owned book 3 await contextFactory.CreateOwnedBook(arrangeContext, 1, 3); //creates owned book 4 await contextFactory.CreateOwnedBook(arrangeContext, 1, 4); //creates owned book 5 await contextFactory.CreateOwnedBook(arrangeContext, 1, 5); //adds inserts lender variable able as a user in the sqllite database references address 1 await contextFactory.CreateUser(arrangeContext, "damionsilver"); arrangeContext.SaveChanges();//adds inserts burrower variable able as a user in the sqllite database references address 1 await contextFactory.CreateUser(arrangeContext, "dajiabridgers"); DataAccess.Book count = arrangeContext.Books.First(); arrangeContext.SaveChanges(); await contextFactory.CreateLoan(arrangeContext, 1, 2); arrangeContext.SaveChanges(); } //creates new instance of sqllite database and name it context. using var context = contextFactory.CreateContext(); //creates a userrepository instance and insert context inside of it. var repo = new UserRepository(context); // act List <IBook> userrecommendedbooks = new List <IBook>(await repo.GetRecommendedBooksAsync(2)); // assert Assert.Contains("Test", userrecommendedbooks.First().Genres); }