public async Task OwnedBookRepository_GetOwnedBooks() { // arrange const int userId = 1; const int bookId = 1; const int bookId2 = 2; using var contextFactory = new TestLooseLeafContextFactory(); using (LooseLeafContext arrangeContext = contextFactory.CreateContext()) { await contextFactory.CreateBook(arrangeContext); await contextFactory.CreateBook(arrangeContext); await contextFactory.CreateOwnedBook(arrangeContext, userId, bookId); await contextFactory.CreateOwnedBook(arrangeContext, userId, bookId2); await arrangeContext.SaveChangesAsync(); } // act IEnumerable <IOwnedBookResult> ownedBooks; using (LooseLeafContext actContext = contextFactory.CreateContext()) { IOwnedBookRepository ownedBookRepo = new OwnedBookRepository(actContext); ownedBooks = await ownedBookRepo.GetOwnedBooksAsync(new OwnedBookSearchParams()); } // assert Assert.Equal(2, ownedBooks.Count()); }
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_UpdateStatusOfOwnedBook() { // arrange const int ownedBookId = 1; const int userId = 1; const int bookId = 1; Availability availability = Availability.CheckedOut; PhysicalCondition condition = PhysicalCondition.Fair; using var contextFactory = new TestLooseLeafContextFactory(); using (LooseLeafContext arrangeContext = contextFactory.CreateContext()) { await contextFactory.CreateOwnedBook(arrangeContext, userId, bookId); await arrangeContext.SaveChangesAsync(); } // act using (LooseLeafContext actContext = contextFactory.CreateContext()) { IOwnedBookRepository ownedBookRepo = new OwnedBookRepository(actContext); await ownedBookRepo.UpdateOwnedBookStatus(ownedBookId, availability, condition); 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(userId, ownedBook.UserId); Assert.Equal(bookId, ownedBook.BookId); Assert.Equal((int)condition, ownedBook.ConditionId); Assert.Equal((int)availability, ownedBook.AvailabilityStatusId); }
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); }