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);
        }
예제 #2
0
        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);
        }