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 CreateGenre(LooseLeafContext context, Book book, string genre = "Test") { await context.Genres.AddAsync(new DataAccess.Genre() { GenreName = genre, Book = book }); }
public async Task CreateLoan(LooseLeafContext context, int _LenderId, int _BorrowerId) { var loan = new DataAccess.Loan() { LenderId = _LenderId, BorrowerId = _BorrowerId, Message = "Test loan message", LoanStatusId = 1, DropoffDate = new DateTime(2021, 3, 20), ReturnedDate = new DateTime(2021, 3, 23), AddressId = 1 }; var loanbooks = new List <LoanedBook> { new LoanedBook() { Loan = loan, OwnedBookid = 1 }, new LoanedBook() { Loan = loan, OwnedBookid = 2 }, new LoanedBook() { Loan = loan, OwnedBookid = 3 } }; await context.Loans.AddAsync(loan); await context.LoanedBooks.AddRangeAsync(loanbooks); }
public async Task UserRepository_GetUserAsync() { // arrange var insertedUser = new DataAccess.User { Username = "******", Email = "*****@*****.**", AuthId = "123Id" }; using var contextFactory = new TestLooseLeafContextFactory(); using (LooseLeafContext arrangeContext = contextFactory.CreateContext()) { await contextFactory.CreateAddress(arrangeContext); arrangeContext.SaveChanges(); await arrangeContext.Users.AddAsync(insertedUser); arrangeContext.SaveChanges(); } using var context = contextFactory.CreateContext(); var repo = new UserRepository(context); // act IUser user = await repo.GetUserAsync(insertedUser.Id); // assert Assert.Equal(insertedUser.Username, user.UserName); Assert.Equal(insertedUser.Email, user.Email); }
private void CreateEnumData(LooseLeafContext context) { foreach (string name in Enum.GetNames(typeof(Business.Models.LoanStatus))) { context.LoanStatuses.Add(new LoanStatus() { StatusName = name }); } foreach (string name in Enum.GetNames(typeof(Business.Models.AvailabilityStatus))) { context.AvailabilityStatuses.Add(new AvailabilityStatus() { StatusName = name }); } foreach (string name in Enum.GetNames(typeof(Business.Models.PhysicalCondition))) { context.ConditionStatuses.Add(new ConditionStatus() { StatusName = name }); } }
public async Task GetBookById_Returns_Book() { // arrange var insertedBook = new DataAccess.Book { Title = "Gone with the Wind", Author = "JoJo", Isbn = 1234567892581, }; using var contextFactory = new TestLooseLeafContextFactory(); using (LooseLeafContext arrangeContext = contextFactory.CreateContext()) { arrangeContext.Genres.Add(new Genre() { GenreName = "Test Genre", Book = insertedBook }); arrangeContext.Books.Add(insertedBook); arrangeContext.SaveChanges(); } using var context = contextFactory.CreateContext(); var repo = new BookRepository(context); // act IBook book = await repo.GetBook(insertedBook.Id); // assert Assert.Equal(insertedBook.Id, book.Id); Assert.Equal(insertedBook.Title, book.Title); Assert.Equal(insertedBook.Author, book.Author); Assert.Equal(insertedBook.Isbn, book.Isbn); Assert.Single(book.Genres); }
public async Task CreateAddress(LooseLeafContext context) { int id = context.Addresses.Count() + 1; await context.Addresses.AddAsync(new DataAccess.Address() { Address1 = $"Street {id}", City = "City", State = "State", Country = "Country", Zipcode = 123456 }); }
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 CreateUser(LooseLeafContext context, string username = "******") { if (!context.Addresses.Any()) { await CreateAddress(context); await context.SaveChangesAsync(); } await context.Users.AddAsync(new DataAccess.User() { Username = username, Email = $"{username}@website.com", AuthId = $"{username}Id" }); }
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 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()); }
/// <summary> /// Creates a new book along with a genre if one doesn't exist. /// </summary> /// <param name="context">The DBContext</param> /// <param name="bookName">The name of the book</param> /// <param name="authorName">The name of the author</param> /// <returns>Returns the ISBN of the book.</returns> public async Task <long> CreateBook(LooseLeafContext context, string bookName = "Book", string authorName = "Author") { int id = context.Books.Count() + 1; long newIsbn = 9784567890123 + id; var book = new DataAccess.Book() { Title = bookName, Author = authorName, Isbn = newIsbn }; await CreateGenre(context, book); await context.Books.AddAsync(book); return(newIsbn); }
public async Task GetAllBooks_ReturnList() { // arrange const string title1 = "Brave Little Toaster"; const string author1 = "Lil Wayne"; long isbn1 = 1234567890123; const string title2 = "Three Blind Mice"; const string author2 = "Celine Dion"; long isbn2 = 1234567890124; using var contextFactory = new TestLooseLeafContextFactory(); using (LooseLeafContext arrangeContext = contextFactory.CreateContext()) { List <DataAccess.Book> books = new List <DataAccess.Book>() { new DataAccess.Book() { Title = title1, Author = author1, Isbn = isbn1 }, new DataAccess.Book() { Title = title2, Author = author2, Isbn = isbn2 } }; await contextFactory.CreateGenre(arrangeContext, books[0]); await contextFactory.CreateGenre(arrangeContext, books[1]); await arrangeContext.AddRangeAsync(books); await arrangeContext.SaveChangesAsync(); } using (LooseLeafContext actContext = contextFactory.CreateContext()) { IBookRepository bookRepo = new BookRepository(actContext); await bookRepo.GetAllBooks(new BookSearchParams()); await actContext.SaveChangesAsync(); }; // assert using LooseLeafContext assertContext = contextFactory.CreateContext(); var book = await assertContext.Books.ToListAsync(); Assert.Equal(2, book.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 LooseLeafContext CreateContext() { if (_conn == null) { _conn = new SqliteConnection("DataSource=:memory:"); _conn.Open(); DbContextOptions <LooseLeafContext> options = CreateOptions(); using var tempContext = new LooseLeafContext(options); tempContext.Database.EnsureCreated(); } var context = new LooseLeafContext(CreateOptions()); CreateEnumData(context); return(context); }
public async Task UserRepository_AddUserAsync() { // arrange using var contextFactory = new TestLooseLeafContextFactory(); using (LooseLeafContext context = contextFactory.CreateContext()) { UserRepository userRepository = new UserRepository(context); var fakeUser = new Mock <INewUser>(); fakeUser.Setup(u => u.AuthId).Returns("123Id"); fakeUser.Setup(u => u.Username).Returns("username"); fakeUser.Setup(u => u.Email).Returns("*****@*****.**"); // act await userRepository.AddUserAsync(fakeUser.Object); } // assert using var assertContext = contextFactory.CreateContext(); Assert.True(assertContext.Users.Single() != null); }
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 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 <OwnedBook> CreateOwnedBook(LooseLeafContext context, int userId, int bookId) { if (!context.Books.Any()) { await CreateBook(context); await context.SaveChangesAsync(); } if (!context.Users.Any()) { await CreateUser(context); await context.SaveChangesAsync(); } var ownedBook = new DataAccess.OwnedBook() { UserId = userId, BookId = bookId, ConditionId = 1, AvailabilityStatusId = 1 }; await context.OwnedBooks.AddAsync(ownedBook); return(ownedBook); }
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 LoanRepository(LooseLeafContext context) { _context = context; }
public BookRepository(LooseLeafContext context) { _context = context; }
public WishlistRepository(LooseLeafContext context) { _context = context; }
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); }
public UserRepository(LooseLeafContext context) { _context = context; }