Beispiel #1
0
        public async Task Delete_Existing_User_Multiple_Users()
        {
            var users         = UserSeed.CreateUsers(10);
            var userGenerated = UserSeed.CreateUserTest();

            users.Add(userGenerated);
            var dbName = nameof(Delete_Existing_User_Multiple_Users);

            using (var context = InMemoryDatabaseHelper.CreateContext(dbName))
            {
                InMemoryDatabaseHelper.Save(users, context);
            }

            using (var context = InMemoryDatabaseHelper.CreateContext(dbName))
            {
                var controller = CreateUserControllerObject(context);

                var result = await controller.Delete(userGenerated.Id);

                Assert.IsType <OkResult>(result);
            }

            using (var context = InMemoryDatabaseHelper.CreateContext(dbName))
            {
                Assert.False(context.User.Any(u => u.Id == userGenerated.Id));
                Assert.True(context.User.Count() == users.Count() - 1);
            }
        }
Beispiel #2
0
        private async Task Execute_Success()
        {
            var userCount = 10;

            // Arrange
            using (var context = InMemoryDatabaseHelper.CreateContext(nameof(Execute_Success)))
            {
                InMemoryDatabaseHelper.Save(UserSeed.CreateUsers(userCount), context);
            }

            using (var context = InMemoryDatabaseHelper.CreateContext(nameof(Execute_Success)))
            {
                var user       = context.User.FirstOrDefault();
                var userDelete = CreateUserDeleteObject(context);
                var userDto    = new UserDto {
                    Id = user.Id, Username = user.Username, Password = user.Password
                };

                // Act
                var result = await userDelete.Execute(userDto);

                // Assert
                Assert.DoesNotContain(user.Id, context.User.Select(u => u.Id));
                Assert.Equal(context.User.Count(), userCount - 1);
                Assert.IsType <UserDto>(result);
            }
        }
        public async Task Delete_Is_Exist_Value(int userCount)
        {
            var userGenerated = UserSeed.CreateUserTest();
            var users         = UserSeed.CreateUsers(userCount);

            users.Add(userGenerated);

            using (var context = InMemoryDatabaseHelper.CreateContext(CreateDatabaseName(nameof(Delete_Is_Exist_Value))))
            {
                InMemoryDatabaseHelper.Save(users, context);
            }

            using (var context = InMemoryDatabaseHelper.CreateContext(CreateDatabaseName(nameof(Delete_Is_Exist_Value))))
            {
                var repository = new UserRepository(context);
                await repository.Delete(userGenerated.Id);
            }

            using (var context = InMemoryDatabaseHelper.CreateContext(CreateDatabaseName(nameof(Delete_Is_Exist_Value))))
            {
                Assert.NotNull(context.User);
                Assert.False(context.User.Any(u => u.Id == userGenerated.Id));
                Assert.True(context.User.Count() == userCount);
            }
        }
Beispiel #4
0
        public async Task Delete_No_Existing_User()
        {
            var dbName = nameof(Delete_No_Existing_User);

            using (var context = InMemoryDatabaseHelper.CreateContext(dbName))
            {
                InMemoryDatabaseHelper.Save(UserSeed.CreateUsers(), context);

                var controller = CreateUserControllerObject(context);
                Assert.IsType <NotFoundObjectResult>(await controller.Delete(Guid.NewGuid().ToString()));
            }
        }
        public async Task Delete_Is_Not_Exist_Value()
        {
            using (var context = InMemoryDatabaseHelper.CreateContext(CreateDatabaseName(nameof(Delete_Is_Not_Exist_Value))))
            {
                InMemoryDatabaseHelper.Save(UserSeed.CreateUsers(), context);
            }

            using (var context = InMemoryDatabaseHelper.CreateContext(CreateDatabaseName(nameof(Delete_Is_Not_Exist_Value))))
            {
                var repository = new UserRepository(context);
                await Assert.ThrowsAsync <InvalidOperationException>(() => repository.Delete(UserSeed.CreateUserTest().Id));
            }
        }
Beispiel #6
0
        public async Task GetByUsername_404_Code()
        {
            using (var context = InMemoryDatabaseHelper.CreateContext(nameof(GetByUsername_404_Code)))
            {
                InMemoryDatabaseHelper.Save(UserSeed.CreateUsers(), context);
                var controller = CreateUserControllerObject(context);

                var actionResult = await controller.GetByUsername("user");

                // Assert
                Assert.IsType <NotFoundObjectResult>(actionResult.Result);
                Assert.IsNotType <OkResult>(actionResult);
            }
        }
Beispiel #7
0
        public void GetAll_ReturnValues()
        {
            using (var context = InMemoryDatabaseHelper.CreateContext(nameof(GetAll_ReturnValues)))
            {
                InMemoryDatabaseHelper.Save(UserSeed.CreateUsers(), context);
                var controller = CreateUserControllerObject(context);

                var actionResult = controller.GetAll();

                // Assert
                Assert.IsType <ActionResult <IEnumerable <UserFindDto> > >(actionResult);
                Assert.True(actionResult.Value.Any());
            }
        }
        public void GetAll_Return_Value()
        {
            using (var context = InMemoryDatabaseHelper.CreateContext(CreateDatabaseName(nameof(GetAll_Return_Value))))
            {
                InMemoryDatabaseHelper.Save(UserSeed.CreateUsers(), context);
            }

            using (var context = InMemoryDatabaseHelper.CreateContext(CreateDatabaseName(nameof(GetAll_Return_Value))))
            {
                var repository = new UserRepository(context);
                var userList   = repository.GetAll();

                Assert.NotNull(userList);
                Assert.True(userList.Any());
            }
        }
        public async Task GetById_Is_Not_Exist_Value()
        {
            var dbName = nameof(GetById_Is_Not_Exist_Value);

            using (var context = InMemoryDatabaseHelper.CreateContext(CreateDatabaseName(dbName)))
            {
                InMemoryDatabaseHelper.Save(UserSeed.CreateUsers(), context);
            }

            using (var context = InMemoryDatabaseHelper.CreateContext(CreateDatabaseName(dbName)))
            {
                var repository = new UserRepository(context);
                var user       = await repository.GetById(Guid.NewGuid().ToString());

                Assert.Null(user);
            }
        }
        public void GetAll_Is_Correct_Count(int userCount)
        {
            var dbName = $"{nameof(GetAll_Is_Correct_Count)}_{userCount}";

            using (var context = InMemoryDatabaseHelper.CreateContext(CreateDatabaseName(dbName)))
            {
                InMemoryDatabaseHelper.Save(UserSeed.CreateUsers(userCount), context);
            }

            using (var context = InMemoryDatabaseHelper.CreateContext(CreateDatabaseName(dbName)))
            {
                var repository = new UserRepository(context);
                var userList   = repository.GetAll();

                Assert.NotNull(userList);
                Assert.Equal(userCount, userList.Count());
            }
        }
Beispiel #11
0
        public async Task Get_Id_404_Code()
        {
            var id = Guid.NewGuid().ToString();

            using (var context = InMemoryDatabaseHelper.CreateContext(nameof(Get_Id_404_Code)))
            {
                InMemoryDatabaseHelper.Save(UserSeed.CreateUsers(), context);
                var controller = CreateUserControllerObject(context);

                var actionResult = await controller.Get(id);

                Assert.IsType <NotFoundObjectResult>(actionResult.Result);
            }

            using (var context = InMemoryDatabaseHelper.CreateContext(nameof(Get_Id_404_Code)))
            {
                Assert.False(context.User.Any(u => u.Id == id));
            }
        }
Beispiel #12
0
        private async Task Execute_Does_Not_Exist()
        {
            var userCount = 10;

            // Arrange
            using (var context = InMemoryDatabaseHelper.CreateContext(nameof(Execute_Does_Not_Exist)))
            {
                InMemoryDatabaseHelper.Save(UserSeed.CreateUsers(userCount), context);
            }

            using (var context = InMemoryDatabaseHelper.CreateContext(nameof(Execute_Does_Not_Exist)))
            {
                var userDelete = CreateUserDeleteObject(context);
                var userDto    = new UserDto();

                // Act && Assert
                await Assert.ThrowsAsync <Exception>(async() => await userDelete.Execute(userDto));
            }
        }
Beispiel #13
0
        public void GetAll_Return_Value()
        {
            // Arrange
            using (var context = InMemoryDatabaseHelper.CreateContext(nameof(GetAll_Return_Value)))
            {
                InMemoryDatabaseHelper.Save(UserSeed.CreateUsers(), context);
            }

            using (var context = InMemoryDatabaseHelper.CreateContext(nameof(GetAll_Return_Value)))
            {
                // Act
                var userFinder = CreateUserFindObject(context);
                var users      = userFinder.GetAll();

                // Assert
                Assert.NotNull(users);
                Assert.True(users.Any());
            }
        }
Beispiel #14
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            User[]       users       = UserSeed.CreateUsers();
            Restaurant[] restaurants = RestaurantSeed.CreateRestaurants();
            Product[]    products    = ProductSeed.CreateProducts(restaurants);
            Booking[]    bookings    = BookingSeed.CreateBookings(users, restaurants, products);

            modelBuilder.Entity <Restaurant>().HasMany(r => r.Products).WithOne(p => p.Restaurant);
            modelBuilder.Entity <Product>().HasOne(p => p.Restaurant).WithMany(r => r.Products);
            modelBuilder.Entity <User>().HasMany(u => u.Bookings).WithOne(b => b.User);
            modelBuilder.Entity <Booking>().HasOne(b => b.User).WithMany(u => u.Bookings);
            modelBuilder.Entity <Booking>().HasMany(b => b.Products);
            modelBuilder.Entity <Product>().HasMany(b => b.Bookings);



            modelBuilder.Entity <User>().HasData(users);
            modelBuilder.Entity <Restaurant>().HasData(restaurants);
            modelBuilder.Entity <Product>().HasData(products);
            modelBuilder.Entity <Booking>().HasData(bookings);
        }
Beispiel #15
0
        public async Task GetByUsername_No_Return_Value()
        {
            // Arrange
            var userGenerated = UserSeed.CreateUserTest();

            using (var context = InMemoryDatabaseHelper.CreateContext(nameof(GetByUsername_No_Return_Value)))
            {
                InMemoryDatabaseHelper.Save(UserSeed.CreateUsers(), context);
            }

            using (var context = InMemoryDatabaseHelper.CreateContext(nameof(GetByUsername_No_Return_Value)))
            {
                // Act
                var userFinder = CreateUserFindObject(context);
                var user       = await userFinder.GetByUsername(userGenerated.Username);

                // Assert
                Assert.Null(user);
                Assert.DoesNotContain(userGenerated.Username, context.User.Select(u => u.Username));
            }
        }
Beispiel #16
0
        public async Task Put_Create_Not_Existing_User()
        {
            using (var context = InMemoryDatabaseHelper.CreateContext(nameof(Put_Create_Not_Existing_User)))
            {
                InMemoryDatabaseHelper.Save(UserSeed.CreateUsers(), context);
            }

            using (var context = InMemoryDatabaseHelper.CreateContext(nameof(Put_Create_Not_Existing_User)))
            {
                var controller = CreateUserControllerObject(context);
                var dto        = new UserDto
                {
                    Id       = UserSeed.Id,
                    Username = UserSeed.Username,
                    Password = UserSeed.Password
                };

                var response = await controller.Put(dto);

                Assert.IsType <BadRequestObjectResult>(response.Result);
                Assert.Null(context.User.FirstOrDefault(u => u.Id == UserSeed.Id));
            }
        }
        public async Task Add_New_Value()
        {
            var userCount     = 10;
            var userGenerated = UserSeed.CreateUserTest();

            using (var context = InMemoryDatabaseHelper.CreateContext(CreateDatabaseName(nameof(Add_New_Value))))
            {
                InMemoryDatabaseHelper.Save(UserSeed.CreateUsers(userCount), context);
                var repository = new UserRepository(context);
                await repository.Add(userGenerated);
            }

            using (var context = InMemoryDatabaseHelper.CreateContext(CreateDatabaseName(nameof(Add_New_Value))))
            {
                var repository = new UserRepository(context);
                var userList   = repository.GetAll().ToList();

                Assert.NotNull(userList);
                Assert.True(userList.Count() == userCount + 1);
                Assert.Contains(userGenerated.Id, userList.Select(u => u.Id));
                Assert.Contains(userGenerated.Username, userList.Select(u => u.Username));
            }
        }
        public async Task GetById_Is_Exist_Value(int userCount)
        {
            var dbName        = $"{nameof(GetById_Is_Exist_Value)}_{userCount}";
            var userGenerated = UserSeed.CreateUserTest();
            var users         = UserSeed.CreateUsers(userCount);

            users.Add(userGenerated);

            using (var context = InMemoryDatabaseHelper.CreateContext(CreateDatabaseName(dbName)))
            {
                InMemoryDatabaseHelper.Save(users, context);
            }

            using (var context = InMemoryDatabaseHelper.CreateContext(CreateDatabaseName(dbName)))
            {
                var repository = new UserRepository(context);
                var user       = await repository.GetById(userGenerated.Id);

                Assert.NotNull(user);
                Assert.True(user.Id == userGenerated.Id);
                Assert.Equal(user.Password, userGenerated.Password);
            }
        }