public void Put_MockUserRepository_AndUpdateUser()
        {
            //assign
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();
            int idUser = miniAutoFixture.CreateInt();

            miniAutoFixture.Initialize();
            UserModify userModify = DomainTools.CreateUserModify(miniAutoFixture);
            UserView   userView   = new UserView
            {
                Id      = idUser,
                Name    = userModify.Name,
                Surname = userModify.Surname,
                Deleted = miniAutoFixture.CreateBoolean()
            };
            var viewRepository   = new Mock <IViewRepository>();
            var modifyRepository = new Mock <IModifyRepository>();
            var logger           = new Mock <ILogger <UserController> >();

            modifyRepository.Setup(a => a.UpdateUser(It.IsAny <int>(), It.IsAny <UserModify>())).Returns <int, UserModify>((a, b) => userView);
            UserController userController = new UserController(viewRepository.Object, modifyRepository.Object, logger.Object);


            //action
            IActionResult actionResult = userController.Put(idUser, userModify);

            //assert
            modifyRepository.Verify
            (
                a =>
                a.UpdateUser
                (
                    It.Is <int>(val => val == idUser),
                    It.Is <UserModify>
                        (val => val.Name == userModify.Name && val.Surname == userModify.Surname)
                )
                , Times.Once
            );

            var okResult   = Assert.IsType <OkObjectResult>(actionResult);
            var returnUser = Assert.IsType <UserView>(okResult.Value);

            Assert.NotNull(returnUser);
            Assert.Equal(userModify.Name, returnUser.Name);
            Assert.Equal(userModify.Surname, returnUser.Surname);
            Assert.Equal(idUser, returnUser.Id);
            Assert.Equal(userView.Deleted, returnUser.Deleted);
        }
        public void Get_MockViewRepository_AndGetTask()
        {
            //assign
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();

            var viewRepository   = new Mock <IViewRepository>();
            var modifyRepository = new Mock <IModifyRepository>();
            var logger           = new Mock <ILogger <TaskController> >();


            TaskView taskView = DomainTools.GetTask(miniAutoFixture);


            viewRepository.Setup(a => a.GetTask(It.Is <int>(val => val == taskView.Id))).Returns(taskView);
            TaskController taskController = new TaskController(viewRepository.Object, modifyRepository.Object, logger.Object);

            //action
            IActionResult actionResult = taskController.Get(taskView.Id);

            //assert
            var okResult   = Assert.IsType <OkObjectResult>(actionResult);
            var returnTask = Assert.IsType <TaskView>(okResult.Value);

            viewRepository.Verify(a => a.GetTask(It.Is <int>(val => val == taskView.Id)), Times.Once);
            Assert.NotNull(returnTask);
            Assert.Equal(taskView.Id, returnTask.Id);
            Assert.Equal(taskView.Text, returnTask.Text);
            Assert.Equal(taskView.Date, returnTask.Date);
            Assert.Equal(taskView.Deleted, returnTask.Deleted);
            Assert.Equal(taskView.Done, returnTask.Done);
        }
        public void GetUser_RunServerAddUserList_GetUser()
        {
            DoIntegrationTest(async(client, dbContextOptions) =>
            {
                //Arrange
                MiniAutoFixture miniAutoFixture = new MiniAutoFixture();
                miniAutoFixture.Initialize();
                List <User> userList  = DbSetTools.AddExampleUsersToDatabase(dbContextOptions, miniAutoFixture);
                User userExample      = userList[0];
                UserModify userModify = DomainTools.CreateUserModify(miniAutoFixture);
                var request           = new HttpRequestMessage(new HttpMethod("GET"), string.Format("/api/user/{0}", userExample.Id));

                //Action
                var response = await client.SendAsync(request);

                //Assert
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                var content       = await response.Content.ReadAsStringAsync();
                UserView userView = JsonConvert.DeserializeObject <UserView>(content);

                Assert.Equal(userExample.Id, userView.Id);
                Assert.Equal(userExample.Name, userView.Name);
                Assert.Equal(userExample.Surname, userView.Surname);
                Assert.Equal(userExample.Deleted, userView.Deleted);
            });
        }
Beispiel #4
0
        public void SaveTaskModify_WhenSetTaskModify_ChangeTaskWithoutChangeAnythingElse()
        {
            //arrange
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();
            int  id      = miniAutoFixture.CreateInt();
            bool deleted = miniAutoFixture.CreateBoolean();
            bool done    = miniAutoFixture.CreateBoolean();

            Task task = new Task
            {
                Id      = id,
                Done    = done,
                Deleted = deleted
            };

            TaskModify taskModify = new TaskModify
            {
                Date = miniAutoFixture.CreateDatetime(),
                Text = miniAutoFixture.CreateString()
            };

            //action
            ExtensionMethods.SaveTaskModify(task, taskModify);

            //assert
            Assert.Equal(id, task.Id);
            Assert.Equal(taskModify.Date, task.Date);
            Assert.Equal(taskModify.Text, task.Text);
            Assert.Equal(done, task.Done);
            Assert.Equal(deleted, task.Deleted);
        }
Beispiel #5
0
        public void SaveUserModify_WhenSetUserModify_ChangeUserWithoutChangeAnythingElse()
        {
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();

            int  id      = miniAutoFixture.CreateInt();
            bool deleted = miniAutoFixture.CreateBoolean();
            User user    = new User {
                Id      = id,
                Deleted = deleted,
            };

            UserModify userModify = new UserModify {
                Name    = miniAutoFixture.CreateString(),
                Surname = miniAutoFixture.CreateString()
            };

            // action
            ExtensionMethods.SaveUserModify(user, userModify);

            // assert
            Assert.Equal(id, user.Id);
            Assert.Equal(userModify.Name, user.Name);
            Assert.Equal(userModify.Surname, user.Surname);
            Assert.Equal(deleted, user.Deleted);
        }
Beispiel #6
0
        public void PostTaskRunServerAddUserListAndTask_ReturnTaskAdd()
        {
            DoIntegrationTest(async(client, dbContextOptions) =>
            {
                //Arrange
                MiniAutoFixture miniAutoFixture = new MiniAutoFixture();
                miniAutoFixture.Initialize();
                List <User> userList  = null;
                List <Task> taskList  = DbSetTools.AddExampleTasksToDatabase(dbContextOptions, miniAutoFixture, out userList);
                User userExample      = userList[0];
                TaskModify taskModify = DomainTools.CreateTaskModify(miniAutoFixture);

                //Action
                var response = await client.PostAsJsonAsync(string.Format("/api/task/user/{0}", userExample.Id), taskModify);

                //Assert
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                var content       = await response.Content.ReadAsStringAsync();
                TaskView taskView = JsonConvert.DeserializeObject <TaskView>(content);
                Assert.Equal(taskModify.Text, taskView.Text);
                Assert.Equal(taskModify.Date, taskView.Date);
                Assert.Equal(false, taskView.Deleted);
                Assert.Equal(false, taskView.Done);
                Assert.Equal(true, taskView.Id > 0);
            });
        }
Beispiel #7
0
        public void UnDoneTask_RunServerAddUserListAndTask_ReturnTaskDone()
        {
            DoIntegrationTest(async(client, dbContextOptions) =>
            {
                //Arrange
                bool done = false;
                MiniAutoFixture miniAutoFixture = new MiniAutoFixture();
                miniAutoFixture.Initialize();
                List <User> userList = null;
                List <Task> taskList = DbSetTools.AddExampleTasksToDatabase(dbContextOptions, miniAutoFixture, out userList);
                Task taskExample     = taskList[0];

                //Action
                var response = await client.PutAsync(string.Format("/api/task/undone/{0}", taskExample.Id), null);

                //Assert
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                var content       = await response.Content.ReadAsStringAsync();
                TaskView taskView = JsonConvert.DeserializeObject <TaskView>(content);
                Assert.Equal(taskExample.Text, taskView.Text);
                Assert.Equal(taskExample.Date, taskView.Date);
                Assert.Equal(taskExample.Deleted, taskView.Deleted);
                Assert.Equal(done, taskView.Done);
                Assert.Equal(taskExample.Id, taskView.Id);
            });
        }
        public void Get_MockUserRepository_AndGetUser()
        {
            //assign
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();
            UserView user = DomainTools.GetUser(miniAutoFixture);

            var viewRepository   = new Mock <IViewRepository>();
            var modifyRepository = new Mock <IModifyRepository>();
            var logger           = new Mock <ILogger <UserController> >();

            viewRepository.Setup(a => a.GetUser(It.Is <int>(b => b == user.Id))).Returns(user);
            UserController userController = new UserController(viewRepository.Object, modifyRepository.Object, logger.Object);

            //action
            IActionResult actionResult = userController.Get(user.Id);

            // assert
            viewRepository.Verify(a => a.GetUser(It.Is <int>(b => b == user.Id)), Times.Once);
            var okResult   = Assert.IsType <OkObjectResult>(actionResult);
            var returnUser = Assert.IsType <UserView>(okResult.Value);

            Assert.NotNull(returnUser);
            Assert.Equal(user.Id, returnUser.Id);
            Assert.Equal(user.Name, returnUser.Name);
            Assert.Equal(user.Surname, returnUser.Surname);
            Assert.Equal(user.Deleted, returnUser.Deleted);
        }
        public void InsertUser_MockDbSetUser_AndGetInsertUser()
        {
            //assign
            int             newIdUser       = 11;
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();
            List <User>       usersCollection = DbSetTools.CreateUserList(1, 10, miniAutoFixture);
            IQueryable <User> users           = usersCollection.AsQueryable();
            var mockDbSet = new Mock <DbSet <User> >();

            mockDbSet.As <IQueryable <User> >().Setup(m => m.Provider).Returns(users.Provider);
            mockDbSet.As <IQueryable <User> >().Setup(m => m.Expression).Returns(users.Expression);
            mockDbSet.As <IQueryable <User> >().Setup(m => m.ElementType).Returns(users.ElementType);
            mockDbSet.As <IQueryable <User> >().Setup(m => m.GetEnumerator()).Returns(users.GetEnumerator());
            mockDbSet.Setup(m => m.Add(It.IsAny <User>()))
            .Callback <User>(a =>
            {
                a.Id = newIdUser;
                usersCollection.Add(a);
            });

            var mockContext = new Mock <OrganizerContext>();

            mockContext.Setup(c => c.User).Returns(mockDbSet.Object);
            var logger = new Mock <ILogger <ModifyRepository> >();
            ModifyRepository modifyRepository = new ModifyRepository(mockContext.Object, logger.Object);
            UserModify       userModify       = DomainTools.CreateUserModify(miniAutoFixture);

            //action
            UserView userView = modifyRepository.InsertUser(userModify);

            //assert
            mockDbSet.Verify
            (
                a => a.Add
                (
                    It.Is <User>
                    (

                        user => user.Name == userModify.Name &&
                        user.Surname == userModify.Surname &&
                        !user.Deleted &&
                        user.Id == newIdUser
                    )
                ),
                Times.Once
            );
            mockContext.Verify(a => a.SaveChanges(), Times.Once);


            Assert.Equal(userModify.Name, userView.Name);
            Assert.Equal(userModify.Surname, userView.Surname);
            Assert.Equal(false, userView.Deleted);
            Assert.Equal(newIdUser, userView.Id);
        }
        public void GetAllTasks_MockDbSetTasksAndContext_AndGetAllTasks()
        {
            //assign
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();

            User user1 = DbSetTools.CreateUser(1, miniAutoFixture);
            User user2 = DbSetTools.CreateUser(1, miniAutoFixture);

            List <Task> tasksCollection = DbSetTools.CreateTaskList(1, user1, 10, miniAutoFixture)
                                          .Concat(DbSetTools.CreateTaskList(11, user2, 20, miniAutoFixture))
                                          .ToList();

            IQueryable <Task> tasks = tasksCollection.AsQueryable();

            var mockDbSet = new Mock <DbSet <Task> >();

            mockDbSet.As <IQueryable <Task> >().Setup(m => m.Provider).Returns(tasks.Provider);
            mockDbSet.As <IQueryable <Task> >().Setup(m => m.Expression).Returns(tasks.Expression);
            mockDbSet.As <IQueryable <Task> >().Setup(m => m.ElementType).Returns(tasks.ElementType);
            mockDbSet.As <IQueryable <Task> >().Setup(m => m.GetEnumerator()).Returns(tasks.GetEnumerator());

            var mockContext = new Mock <OrganizerContext>();

            mockContext.Setup(c => c.Tasks).Returns(mockDbSet.Object);
            var            logger         = new Mock <ILogger <ViewRepository> >();
            ViewRepository viewRepository = new ViewRepository(mockContext.Object, logger.Object);

            //action
            IEnumerable <TaskView> taskList = viewRepository.GetAllTasks();

            //assert
            Assert.Equal(tasks.Count(), taskList.Count());
            tasks.ToList().ForEach(task =>
            {
                Assert.Equal
                (
                    taskList.Count
                    (
                        taskView =>
                        taskView.Id == task.Id &&
                        taskView.Text == task.Text &&
                        taskView.Date == task.Date &&
                        taskView.Deleted == task.Deleted &&
                        taskView.Done == task.Done
                    ),
                    1
                );
            }
                                   );
        }
        public void Delete_MockUserRepository_AndDeleteUser()
        {
            //assign
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();
            int idUser = miniAutoFixture.CreateInt();

            miniAutoFixture.Initialize();

            UserView userView = new UserView
            {
                Id      = idUser,
                Name    = miniAutoFixture.CreateString(),
                Surname = miniAutoFixture.CreateString(),
                Deleted = true
            };
            var viewRepository   = new Mock <IViewRepository>();
            var modifyRepository = new Mock <IModifyRepository>();
            var logger           = new Mock <ILogger <UserController> >();

            modifyRepository.Setup(a => a.DeleteUser(It.IsAny <int>())).Returns <int>(a => userView);
            UserController userController = new UserController(viewRepository.Object, modifyRepository.Object, logger.Object);


            //action
            IActionResult actionResult = userController.Delete(idUser);

            //assert
            modifyRepository.Verify(a => a.DeleteUser(It.Is <int>(val => val == idUser)), Times.Once);
            var okResult   = Assert.IsType <OkObjectResult>(actionResult);
            var returnUser = Assert.IsType <UserView>(okResult.Value);

            Assert.NotNull(returnUser);
            Assert.Equal(userView.Name, returnUser.Name);
            Assert.Equal(userView.Surname, returnUser.Surname);
            Assert.Equal(idUser, returnUser.Id);
            Assert.Equal(true, returnUser.Deleted);
        }
        public void GetAllUsers_MockDbSetUserAndContext_AndGetAllUsers()
        {
            //assign
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();
            List <User>       usersCollection = DbSetTools.CreateUserList(1, 100, miniAutoFixture);
            IQueryable <User> users           = usersCollection.AsQueryable();
            var mockDbSet = new Mock <DbSet <User> >();

            mockDbSet.As <IQueryable <User> >().Setup(m => m.Provider).Returns(users.Provider);
            mockDbSet.As <IQueryable <User> >().Setup(m => m.Expression).Returns(users.Expression);
            mockDbSet.As <IQueryable <User> >().Setup(m => m.ElementType).Returns(users.ElementType);
            mockDbSet.As <IQueryable <User> >().Setup(m => m.GetEnumerator()).Returns(users.GetEnumerator());

            var mockContext = new Mock <OrganizerContext>();

            mockContext.Setup(c => c.User).Returns(mockDbSet.Object);
            var            logger         = new Mock <ILogger <ViewRepository> >();
            ViewRepository viewRepository = new ViewRepository(mockContext.Object, logger.Object);

            //action
            IEnumerable <UserView> userList = viewRepository.GetAllUsers();

            //assert
            Assert.Equal(users.Count(), userList.Count());
            users.ToList().ForEach(user =>
            {
                Assert.Equal
                (
                    userList.Count
                    (
                        userView =>
                        userView.Id == user.Id &&
                        userView.Name == user.Name &&
                        userView.Surname == user.Surname &&
                        userView.Deleted == user.Deleted

                    ),
                    1
                );
            }
                                   );
        }
Beispiel #13
0
        public void MapUserToView_WhenSetUser_ReturnUserView()
        {
            //assign
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();
            int id = miniAutoFixture.CreateInt();

            User user = DbSetTools.CreateUser(id, miniAutoFixture);

            //action
            UserView userView = ExtensionMethods.MapUserToView(user);

            //assert
            Assert.Equal(user.Id, userView.Id);
            Assert.Equal(user.Name, userView.Name);
            Assert.Equal(user.Surname, userView.Surname);
            Assert.Equal(user.Deleted, userView.Deleted);
        }
        public void Post_MockModifyRepository_AndInsertTask()
        {
            //assign
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();

            int idUser           = miniAutoFixture.CreateInt();
            var viewRepository   = new Mock <IViewRepository>();
            var modifyRepository = new Mock <IModifyRepository>();
            var logger           = new Mock <ILogger <TaskController> >();

            TaskModify taskModify = DomainTools.CreateTaskModify(miniAutoFixture);
            TaskView   taskView   = new TaskView
            {
                Id      = miniAutoFixture.CreateInt(),
                Text    = taskModify.Text,
                Date    = taskModify.Date,
                Deleted = false,
                Done    = false
            };

            modifyRepository.Setup(a => a.InsertTask(It.IsAny <int>(), It.IsAny <TaskModify>())).Returns <int, TaskModify>((a, b) => taskView);

            TaskController taskController = new TaskController(viewRepository.Object, modifyRepository.Object, logger.Object);

            //action
            IActionResult actionResult = taskController.Post(idUser, taskModify);

            //assert
            var okResult   = Assert.IsType <OkObjectResult>(actionResult);
            var returnTask = Assert.IsType <TaskView>(okResult.Value);

            modifyRepository.Verify(a => a.InsertTask(It.Is <int>(val => val == idUser), It.Is <TaskModify>(val => val == taskModify)), Times.Once);
            Assert.NotNull(returnTask);
            Assert.Equal(taskView.Id, returnTask.Id);
            Assert.Equal(taskModify.Text, returnTask.Text);
            Assert.Equal(taskModify.Date, returnTask.Date);
            Assert.Equal(taskView.Deleted, returnTask.Deleted);
            Assert.Equal(taskView.Done, returnTask.Done);
        }
Beispiel #15
0
        public void GetUserTask_RunServerAddUserListAndTask_ReturnTaskListJson()
        {
            DoIntegrationTest(async(client, dbContextOptions) =>
            {
                //Arrange
                MiniAutoFixture miniAutoFixture = new MiniAutoFixture();
                miniAutoFixture.Initialize();
                List <User> userList     = null;
                List <Task> taskList     = DbSetTools.AddExampleTasksToDatabase(dbContextOptions, miniAutoFixture, out userList);
                User userExample         = userList[0];
                List <Task> userTaskList = taskList.Where(a => a.User == userExample).ToList();
                var request = new HttpRequestMessage(new HttpMethod("GET"), string.Format("/api/task/user/{0}", userExample.Id));

                //Action
                var response = await client.SendAsync(request);

                //Assert
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                var content = await response.Content.ReadAsStringAsync();

                List <TaskView> taskViewList = JsonConvert.DeserializeObject <List <TaskView> >(content);

                Assert.Equal(userTaskList.Count(), taskViewList.Count());
                userTaskList.ForEach(
                    task =>
                {
                    Assert.Equal(taskViewList
                                 .Count
                                 (
                                     taskView =>
                                     task.Id == taskView.Id &&
                                     task.Text == taskView.Text &&
                                     task.Date == taskView.Date &&
                                     task.Deleted == taskView.Deleted &&
                                     task.Done == taskView.Done
                                 ),
                                 1);
                }
                    );
            });
        }
        public void GetAllTasks_MockDbSetTasksAndContext_AndGetTask()
        {
            //assign
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();

            User user          = DbSetTools.CreateUser(11, miniAutoFixture);
            Task taskSpecified = DbSetTools.CreateTask(11, user, miniAutoFixture);

            List <Task> tasksCollection = DbSetTools.CreateTaskList(1, user, 10, miniAutoFixture)
                                          .Concat(new Task[] { taskSpecified })
                                          .ToList();


            IQueryable <Task> tasks = tasksCollection.AsQueryable();

            var mockDbSet = new Mock <DbSet <Task> >();

            mockDbSet.As <IQueryable <Task> >().Setup(m => m.Provider).Returns(tasks.Provider);
            mockDbSet.As <IQueryable <Task> >().Setup(m => m.Expression).Returns(tasks.Expression);
            mockDbSet.As <IQueryable <Task> >().Setup(m => m.ElementType).Returns(tasks.ElementType);
            mockDbSet.As <IQueryable <Task> >().Setup(m => m.GetEnumerator()).Returns(tasks.GetEnumerator());

            var mockContext = new Mock <OrganizerContext>();

            mockContext.Setup(c => c.Tasks).Returns(mockDbSet.Object);
            var            logger         = new Mock <ILogger <ViewRepository> >();
            ViewRepository viewRepository = new ViewRepository(mockContext.Object, logger.Object);


            // action
            TaskView taskView = viewRepository.GetTask(taskSpecified.Id);

            // assign
            Assert.Equal(taskSpecified.Id, taskView.Id);
            Assert.Equal(taskSpecified.Text, taskView.Text);
            Assert.Equal(taskSpecified.Date, taskView.Date);
            Assert.Equal(taskSpecified.Deleted, taskView.Deleted);
            Assert.Equal(taskSpecified.Done, taskView.Done);
        }
Beispiel #17
0
        public void MapTaskToView_WhenSetTask_ReturnTaskView()
        {
            //assign
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();

            int id = miniAutoFixture.CreateInt();

            Task task = DbSetTools.CreateTask(id, null, miniAutoFixture);

            //action
            TaskView taskView = ExtensionMethods.MapTaskToView(task);

            //assert
            Assert.Equal(task.Id, taskView.Id);
            Assert.Equal(task.Date, taskView.Date);
            Assert.Equal(task.Text, taskView.Text);
            Assert.Equal(task.Done, taskView.Done);
            Assert.Equal(task.Deleted, taskView.Deleted);
        }
        public void Get_MockUserRepository_AndGetAllUsers()
        {
            //assign
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();
            IEnumerable <UserView> userList = DomainTools.GetUserList(miniAutoFixture, 10);
            var viewRepository   = new Mock <IViewRepository>();
            var modifyRepository = new Mock <IModifyRepository>();
            var logger           = new Mock <ILogger <UserController> >();


            viewRepository.Setup(a => a.GetAllUsers()).Returns(userList);
            UserController userController = new UserController(viewRepository.Object, modifyRepository.Object, logger.Object);

            //action
            IActionResult actionResult = userController.Get();

            //assert
            var okResult = Assert.IsType <OkObjectResult>(actionResult);


            var returnUserList = (IEnumerable <UserView>)okResult.Value;

            Assert.Equal(userList.Count(), returnUserList.Count());
            userList.ToList().ForEach(user =>
            {
                Assert.Equal(returnUserList.ToList()
                             .Count
                             (
                                 returnUser =>
                                 user.Id == returnUser.Id &&
                                 user.Name == returnUser.Name &&
                                 user.Surname == returnUser.Surname &&
                                 user.Deleted == returnUser.Deleted
                             ),
                             1);
            }
                                      );
        }
        public void ModifytUser_MockDbSetUser_AndGetUpdateUser()
        {
            //assign
            int             idUserUpdate    = 1;
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();
            List <User>       usersCollection = DbSetTools.CreateUserList(1, 10, miniAutoFixture);
            IQueryable <User> users           = usersCollection.AsQueryable();
            UserModify        userModify      = DomainTools.CreateUserModify(miniAutoFixture);

            var mockDbSet = new Mock <DbSet <User> >();

            mockDbSet.As <IQueryable <User> >().Setup(m => m.Provider).Returns(users.Provider);
            mockDbSet.As <IQueryable <User> >().Setup(m => m.Expression).Returns(users.Expression);
            mockDbSet.As <IQueryable <User> >().Setup(m => m.ElementType).Returns(users.ElementType);
            mockDbSet.As <IQueryable <User> >().Setup(m => m.GetEnumerator()).Returns(users.GetEnumerator());


            var mockContext = new Mock <OrganizerContext>();

            mockContext.Setup(c => c.User).Returns(mockDbSet.Object);
            var logger = new Mock <ILogger <ModifyRepository> >();
            ModifyRepository modifyRepository = new ModifyRepository(mockContext.Object, logger.Object);


            //action
            UserView userView = modifyRepository.UpdateUser(idUserUpdate, userModify);

            //assert
            User userModified = usersCollection.Where(a => a.Id == idUserUpdate).Single();

            mockContext.Verify(a => a.Attach(It.Is <User>(user => userModified == user)), Times.Once);
            mockContext.Verify(a => a.SaveChanges(), Times.Once);
            Assert.Equal(userModify.Name, userView.Name);
            Assert.Equal(userModify.Surname, userView.Surname);
            Assert.Equal(userModified.Deleted, userView.Deleted);
            Assert.Equal(idUserUpdate, userView.Id);
        }
        public void GetUserTask_MockViewRepository_AndGetAllUserTask()
        {
            //assign
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();
            int idUser           = miniAutoFixture.CreateInt();
            var viewRepository   = new Mock <IViewRepository>();
            var modifyRepository = new Mock <IModifyRepository>();
            var logger           = new Mock <ILogger <TaskController> >();

            IEnumerable <TaskView> taskList = DomainTools.GetTaskList(miniAutoFixture, 10);


            viewRepository.Setup(a => a.GetAllUserTask(It.IsAny <int>())).Returns(taskList);
            TaskController taskController = new TaskController(viewRepository.Object, modifyRepository.Object, logger.Object);

            //action
            IActionResult actionResult = taskController.GetUserTask(idUser);

            //assert
            var okResult       = Assert.IsType <OkObjectResult>(actionResult);
            var returnTaskList = (IEnumerable <TaskView>)okResult.Value;

            Assert.Equal(taskList.Count(), returnTaskList.Count());
            viewRepository.Verify(a => a.GetAllUserTask(It.Is <int>(val => val == idUser)), Times.Once);
            taskList.ToList().ForEach(task =>
            {
                Assert.Equal(returnTaskList.ToList().Count(
                                 returnTask =>
                                 task.Id == returnTask.Id &&
                                 task.Text == returnTask.Text &&
                                 task.Date == returnTask.Date &&
                                 task.Deleted == returnTask.Deleted &&
                                 task.Done == returnTask.Done
                                 ), 1);
            }
                                      );
        }
        public void DoneTask_MockDbSetTask_AndGetDoneTask()
        {
            //assign
            int             idTaskModify    = 1;
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();
            User              user            = DbSetTools.CreateUser(1, miniAutoFixture);
            List <Task>       tasksCollection = DbSetTools.CreateTaskList(1, user, 10, miniAutoFixture);
            IQueryable <Task> tasks           = tasksCollection.AsQueryable();
            var mockDbSetTask = new Mock <DbSet <Task> >();

            mockDbSetTask.As <IQueryable <Task> >().Setup(m => m.Provider).Returns(tasks.Provider);
            mockDbSetTask.As <IQueryable <Task> >().Setup(m => m.Expression).Returns(tasks.Expression);
            mockDbSetTask.As <IQueryable <Task> >().Setup(m => m.ElementType).Returns(tasks.ElementType);
            mockDbSetTask.As <IQueryable <Task> >().Setup(m => m.GetEnumerator()).Returns(tasks.GetEnumerator());

            var mockContext = new Mock <OrganizerContext>();

            mockContext.Setup(c => c.Tasks).Returns(mockDbSetTask.Object);
            var logger = new Mock <ILogger <ModifyRepository> >();
            ModifyRepository modifyRepository = new ModifyRepository(mockContext.Object, logger.Object);
            bool             done             = miniAutoFixture.CreateBoolean();

            //action
            TaskView taskView = modifyRepository.DoneTask(idTaskModify, done);

            //assert
            Task taskModified = tasksCollection.Where(a => a.Id == idTaskModify).Single();

            mockContext.Verify(a => a.Attach(It.Is <Task>(task => task == taskModified)), Times.Once);
            mockContext.Verify(a => a.SaveChanges(), Times.Once);

            Assert.Equal(taskModified.Date, taskView.Date);
            Assert.Equal(taskModified.Text, taskView.Text);
            Assert.Equal(taskModified.Deleted, taskView.Deleted);
            Assert.Equal(done, taskView.Done);
            Assert.Equal(idTaskModify, taskView.Id);
        }
        public void GetAllUser_RunServerAddUserList_ReturnUserListJson()
        {
            DoIntegrationTest(async(client, dbContextOptions) =>
            {
                //Arrange
                MiniAutoFixture miniAutoFixture = new MiniAutoFixture();
                miniAutoFixture.Initialize();

                List <User> userList = DbSetTools.AddExampleUsersToDatabase(dbContextOptions, miniAutoFixture);
                var request          = new HttpRequestMessage(new HttpMethod("GET"), "/api/user");

                //Action
                var response = await client.SendAsync(request);

                //Assert
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                var content = await response.Content.ReadAsStringAsync();

                List <UserView> userViewList = JsonConvert.DeserializeObject <List <UserView> >(content);
                Assert.Equal(userList.Count, userViewList.Count);
                userList.ForEach(
                    user =>
                {
                    Assert.Equal(userViewList
                                 .Count
                                 (
                                     userView =>
                                     user.Id == userView.Id &&
                                     user.Name == userView.Name &&
                                     user.Surname == userView.Surname &&
                                     user.Deleted == userView.Deleted
                                 ),
                                 1);
                }
                    );
            }
                              );
        }
        public void UnDone_MockModifyRepository_AndDoneTask()
        {
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();
            int      idTask           = miniAutoFixture.CreateInt();
            var      viewRepository   = new Mock <IViewRepository>();
            var      modifyRepository = new Mock <IModifyRepository>();
            var      logger           = new Mock <ILogger <TaskController> >();
            bool     done             = false;
            TaskView taskView         = new TaskView
            {
                Id      = idTask,
                Text    = miniAutoFixture.CreateString(),
                Date    = miniAutoFixture.CreateDatetime(),
                Deleted = miniAutoFixture.CreateBoolean(),
                Done    = done
            };

            modifyRepository.Setup(a => a.DoneTask(It.IsAny <int>(), It.IsAny <bool>())).Returns <int, bool>((a, b) => taskView);
            TaskController taskController = new TaskController(viewRepository.Object, modifyRepository.Object, logger.Object);


            IActionResult actionResult = taskController.UnDone(idTask);

            //assert
            var okResult   = Assert.IsType <OkObjectResult>(actionResult);
            var returnTask = Assert.IsType <TaskView>(okResult.Value);

            modifyRepository.Verify(a => a.DoneTask(It.Is <int>(val => val == idTask), It.Is <bool>(val => val == done)), Times.Once);
            Assert.NotNull(returnTask);
            Assert.Equal(idTask, returnTask.Id);
            Assert.Equal(taskView.Text, returnTask.Text);
            Assert.Equal(taskView.Date, returnTask.Date);
            Assert.Equal(taskView.Deleted, returnTask.Deleted);
            Assert.Equal(done, returnTask.Done);
        }
        public void GetUser_MockDbSetUserAndContext_AndGetUser()
        {
            //assign
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();

            User        userSpecified   = DbSetTools.CreateUser(101, miniAutoFixture);
            List <User> usersCollection =
                DbSetTools.CreateUserList(1, 100, miniAutoFixture)
                .Concat(new User[] { userSpecified }).ToList();


            IQueryable <User> users = usersCollection.AsQueryable();
            var mockDbSet           = new Mock <DbSet <User> >();

            mockDbSet.As <IQueryable <User> >().Setup(m => m.Provider).Returns(users.Provider);
            mockDbSet.As <IQueryable <User> >().Setup(m => m.Expression).Returns(users.Expression);
            mockDbSet.As <IQueryable <User> >().Setup(m => m.ElementType).Returns(users.ElementType);
            mockDbSet.As <IQueryable <User> >().Setup(m => m.GetEnumerator()).Returns(users.GetEnumerator());

            var mockContext = new Mock <OrganizerContext>();

            mockContext.Setup(c => c.User).Returns(mockDbSet.Object);
            var            logger         = new Mock <ILogger <ViewRepository> >();
            ViewRepository viewRepository = new ViewRepository(mockContext.Object, logger.Object);

            //action
            UserView userView = viewRepository.GetUser(userSpecified.Id);

            //assert
            Assert.Equal(userSpecified.Id, userView.Id);
            Assert.Equal(userSpecified.Name, userView.Name);
            Assert.Equal(userSpecified.Surname, userView.Surname);
            Assert.Equal(userSpecified.Deleted, userView.Deleted);
        }
        public void PostUser_RunServerAddUserList_GetAddUser()
        {
            DoIntegrationTest(async(client, dbContextOptions) =>
            {
                //Arrange
                MiniAutoFixture miniAutoFixture = new MiniAutoFixture();
                miniAutoFixture.Initialize();
                List <User> userList  = DbSetTools.AddExampleUsersToDatabase(dbContextOptions, miniAutoFixture);
                UserModify userModify = DomainTools.CreateUserModify(miniAutoFixture);

                //Action
                var response = await client.PostAsJsonAsync("/api/user", userModify);


                //Assert
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                var content       = await response.Content.ReadAsStringAsync();
                UserView userView = JsonConvert.DeserializeObject <UserView>(content);
                Assert.Equal(userModify.Name, userView.Name);
                Assert.Equal(userModify.Surname, userView.Surname);
                Assert.Equal(false, userView.Deleted);
                Assert.Equal(true, userView.Id > 0);
            });
        }
        public void InsertTask_MockDbSetUserDbSetTask_AndGetInsertTask()
        {
            //assign
            int             newIdTask       = 41;
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();

            User        userSpecified   = DbSetTools.CreateUser(1, miniAutoFixture);
            User        user2           = DbSetTools.CreateUser(2, miniAutoFixture);
            User        user3           = DbSetTools.CreateUser(3, miniAutoFixture);
            List <Task> tasksCollection = DbSetTools.CreateTaskList(1, userSpecified, 20, miniAutoFixture)
                                          .Concat(DbSetTools.CreateTaskList(21, user2, 10, miniAutoFixture))
                                          .Concat(DbSetTools.CreateTaskList(31, user3, 10, miniAutoFixture))
                                          .ToList();

            IQueryable <Task> tasks = tasksCollection.AsQueryable();
            List <User>       userCollection = new User[] { userSpecified, user2, user3 }.ToList();
            IQueryable <User> users = userCollection.AsQueryable();

            var mockDbSetUser = new Mock <DbSet <User> >();
            var mockDbSetTask = new Mock <DbSet <Task> >();

            mockDbSetUser.As <IQueryable <User> >().Setup(m => m.Provider).Returns(users.Provider);
            mockDbSetUser.As <IQueryable <User> >().Setup(m => m.Expression).Returns(users.Expression);
            mockDbSetUser.As <IQueryable <User> >().Setup(m => m.ElementType).Returns(users.ElementType);
            mockDbSetUser.As <IQueryable <User> >().Setup(m => m.GetEnumerator()).Returns(users.GetEnumerator());

            mockDbSetTask.As <IQueryable <Task> >().Setup(m => m.Provider).Returns(tasks.Provider);
            mockDbSetTask.As <IQueryable <Task> >().Setup(m => m.Expression).Returns(tasks.Expression);
            mockDbSetTask.As <IQueryable <Task> >().Setup(m => m.ElementType).Returns(tasks.ElementType);
            mockDbSetTask.As <IQueryable <Task> >().Setup(m => m.GetEnumerator()).Returns(tasks.GetEnumerator());

            var mockContext = new Mock <OrganizerContext>();

            mockContext.Setup(c => c.User).Returns(mockDbSetUser.Object);
            mockContext.Setup(c => c.Tasks).Returns(mockDbSetTask.Object);
            mockDbSetTask.Setup(a => a.Add(It.IsAny <Task>()))
            .Callback <Task>(
                task => {
                task.Id = newIdTask;
                tasksCollection.Add(task);
            });

            var logger = new Mock <ILogger <ModifyRepository> >();
            ModifyRepository modifyRepository = new ModifyRepository(mockContext.Object, logger.Object);
            TaskModify       taskModify       = DomainTools.CreateTaskModify(miniAutoFixture);


            // action
            TaskView taskView = modifyRepository.InsertTask(userSpecified.Id, taskModify);

            //assert
            mockDbSetTask.Verify
            (
                a => a.Add
                (
                    It.Is <Task>
                    (
                        task =>
                        task.Id == newIdTask &&
                        task.Date == task.Date &&
                        !task.Deleted &&
                        !task.Done &&
                        task.Text == taskModify.Text &&
                        task.User == userSpecified
                    )
                ),
                Times.Once
            );

            mockContext.Verify(a => a.SaveChanges(), Times.Once);

            Assert.Equal(taskModify.Text, taskView.Text);
            Assert.Equal(taskModify.Date, taskView.Date);
            Assert.Equal(false, taskView.Done);
            Assert.Equal(false, taskView.Deleted);
            Assert.Equal(newIdTask, taskView.Id);
        }