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);
        }
Ejemplo n.º 2
0
        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 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 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);
        }