Esempio n. 1
0
 public void Create(T model)
 {
     if (model == null)
     {
         throw new ArgumentNullException(nameof(model));
     }
     else
     {
         _context.Set <T>().Add(model);
         _context.SaveChanges();
     }
 }
Esempio n. 2
0
        public void Access_TestEdited_Check_GivenInvalidModel()
        {
            var testData = TestModelData.TestData();
            var options  = new DbContextOptionsBuilder <TestMakerContext>()
                           .UseInMemoryDatabase(databaseName: "Access_TestEdited_Check_GivenInvalidModel")
                           .Options;

            using var _context = new TestMakerContext(options);
            _context.Tests.Add(testData[0]);
            foreach (var q in testData[0].Questions)
            {
                _context.Questions.Add(q);
            }
            foreach (var c in testData[0].Questions[0].Choices)
            {
                _context.Choices.Add(c);
            }
            _context.SaveChanges();
            ITestRepository testRepository = new TestRepository(_context);
            var             controller     = new TestController(testRepository);

            controller.ModelState.AddModelError("error", "some error");
            var view = controller.Edit(1, testData[0]) as ViewResult;

            Assert.Null(view.ViewName);
        }
Esempio n. 3
0
        public void Access_TestDetails_check_viewData()
        {
            var testData = TestModelData.TestData();
            var options  = new DbContextOptionsBuilder <TestMakerContext>()
                           .UseInMemoryDatabase(databaseName: "Access_TestDetails_check_viewData")
                           .Options;

            using var _context = new TestMakerContext(options);
            _context.Tests.Add(testData[0]);
            foreach (var q in testData[0].Questions)
            {
                _context.Questions.Add(q);
            }
            foreach (var c in testData[0].Questions[0].Choices)
            {
                _context.Choices.Add(c);
            }
            _context.SaveChanges();
            ITestRepository testRepository = new TestRepository(_context);
            var             controller     = new TestController(testRepository);
            var             view           = controller.Details(1) as ViewResult;

            Assert.Equal("Details", view.ViewData["Title"]);
            Assert.Equal("Details", view.ViewData["Action"]);
            Assert.Equal("Test", view.ViewData["Controller"]);
        }
Esempio n. 4
0
        public void Access_UserChangePassword_Check_ModelStateIsValid()
        {
            var model   = UserModelTestData.UserModelData();
            var options = new DbContextOptionsBuilder <TestMakerContext>()
                          .UseInMemoryDatabase(databaseName: "Access_UserChangePassword_Check_ModelStateIsValid")
                          .Options;

            using var _context = new TestMakerContext(options);
            _context.Users.Add(model);
            _context.SaveChanges();
            IUserRepository userRepository = new UserRepository(_context);
            var             testMapper     = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new AutoMapperConfig());
            });
            var mapper     = testMapper.CreateMapper();
            var controller = new UserController(userRepository, mapper);

            controller.ModelState.AddModelError("error", "some error");
            var view = controller.ChangePassword(model) as ViewResult;

            Assert.Equal("Change password", view.ViewData["Title"]);
            Assert.Equal("ChangePassword", view.ViewData["Action"]);
            Assert.Equal("User", view.ViewData["Controller"]);
        }
Esempio n. 5
0
        public void Access_homeIndex_check_viewData()
        {
            var viewModel = HomeViewModelTestData.HomeIndexViewModelData();
            var options   = new DbContextOptionsBuilder <TestMakerContext>()
                            .UseInMemoryDatabase(databaseName: "Access_homeIndex_check_viewData")
                            .Options;

            using var _context = new TestMakerContext(options);
            foreach (var t in viewModel.Tests)
            {
                _context.Tests.Add(t);
            }
            foreach (var u in viewModel.Users)
            {
                _context.Users.Add(u);
            }
            _context.SaveChanges();
            IHomeRepository homeRepository = new HomeRepository(_context);
            var             controller     = new HomeController(homeRepository);
            var             view           = controller.Index() as ViewResult;

            Assert.Equal("Home Page", view.ViewData["Title"]);
            Assert.Equal("Index", view.ViewData["Action"]);
            Assert.Equal("Home", view.ViewData["Controller"]);
        }
Esempio n. 6
0
        public void Access_UserIndex_db()
        {
            var model   = UserModelTestData.UserModelData();
            var options = new DbContextOptionsBuilder <TestMakerContext>()
                          .UseInMemoryDatabase(databaseName: "Access_UserIndex_db")
                          .Options;

            using var _context = new TestMakerContext(options);
            _context.Users.Add(model);
            _context.SaveChanges();
            Assert.Equal(model.LoginId, _context.Users.FirstOrDefault().LoginId);
            Assert.Equal(model.UserName, _context.Users.FirstOrDefault().UserName);
        }
Esempio n. 7
0
        public async Task Access_UserChangePassword_db()
        {
            var model   = UserModelTestData.UserModelData();
            var options = new DbContextOptionsBuilder <TestMakerContext>()
                          .UseInMemoryDatabase(databaseName: "Access_UserChangePassword_db")
                          .Options;

            using var _context = new TestMakerContext(options);
            _context.Users.Add(model);
            _context.SaveChanges();
            var user = await _context.Users.FindAsync(1);

            Assert.Equal(model.LoginId, user.LoginId);
            Assert.Equal(model.Password, user.Password);
        }
Esempio n. 8
0
        public void Access_TestDeleteConfirmed_Check_db()
        {
            var testData = TestModelData.TestData();
            var options  = new DbContextOptionsBuilder <TestMakerContext>()
                           .UseInMemoryDatabase(databaseName: "Access_TestDeleteConfirmed_Check_db")
                           .Options;

            using var _context = new TestMakerContext(options);
            _context.Tests.Add(testData[0]);
            _context.SaveChanges();
            Assert.Equal(1, _context.Tests.Count());
            ITestRepository testRepository = new TestRepository(_context);
            var             controller     = new TestController(testRepository);

            controller.DeleteConfirmed(1);
            Assert.Equal(0, _context.Tests.Count());
        }
Esempio n. 9
0
        public void Access_TestDeleteConfirmed_check_RedirectToActionResult()
        {
            var testData = TestModelData.TestData();
            var options  = new DbContextOptionsBuilder <TestMakerContext>()
                           .UseInMemoryDatabase(databaseName: "Access_TestDeleteConfirmed_check_RedirectToActionResult")
                           .Options;

            using var _context = new TestMakerContext(options);
            _context.Tests.Add(testData[0]);
            _context.SaveChanges();
            ITestRepository testRepository = new TestRepository(_context);
            var             controller     = new TestController(testRepository);
            var             actionResult   = controller.DeleteConfirmed(1) as RedirectToActionResult;

            Assert.Equal("Index", actionResult.ActionName);
            Assert.Equal("Home", actionResult.ControllerName);
        }
Esempio n. 10
0
        public async Task Access_UserDetails_db()
        {
            var model   = UserModelTestData.UserModelData();
            var options = new DbContextOptionsBuilder <TestMakerContext>()
                          .UseInMemoryDatabase(databaseName: "Access_UserDetails_db")
                          .Options;

            using var _context = new TestMakerContext(options);
            _context.Users.Add(model);
            _context.SaveChanges();
            var user = await _context.Users.FirstOrDefaultAsync(m => m.UserId == 1);

            Assert.Equal(model.UserId, user.UserId);
            Assert.Equal(model.LoginId, user.LoginId);
            Assert.Equal(model.UserName, user.UserName);
            Assert.Equal(model.Icon, user.Icon);
            Assert.Equal(model.SelfIntroduction, user.SelfIntroduction);
        }
Esempio n. 11
0
        public void Access_TestScore_Check_SingleCorrectResult()
        {
            var testData = TestModelData.TestDataForScoreSingleCorrectCheck();
            var options  = new DbContextOptionsBuilder <TestMakerContext>()
                           .UseInMemoryDatabase(databaseName: "Access_TestScore_Check_SingleCorrectResult1")
                           .Options;

            using var _context = new TestMakerContext(options);
            _context.Tests.Add(testData);
            _context.SaveChanges();
            ITestRepository testRepository = new TestRepository(_context);
            var             controller     = new TestController(testRepository);
            var             view           = controller.Score(1, testData) as ViewResult;

            Assert.Equal(1, view.ViewData["CorrectCount"]);
            Assert.Equal("Score", view.ViewData["Score"]);
            Assert.Equal("Details", view.ViewName);
        }
Esempio n. 12
0
        public void Access_homeIndex_db()
        {
            var viewModel = HomeViewModelTestData.HomeIndexViewModelData();
            var options   = new DbContextOptionsBuilder <TestMakerContext>()
                            .UseInMemoryDatabase(databaseName: "Access_homeIndex_db")
                            .Options;

            using var _context = new TestMakerContext(options);
            foreach (var t in viewModel.Tests)
            {
                _context.Tests.Add(t);
            }
            foreach (var u in viewModel.Users)
            {
                _context.Users.Add(u);
            }
            _context.SaveChanges();
            IHomeRepository homeRepository = new HomeRepository(_context);
            var             homeInfoTest   = homeRepository.GetAll();

            Assert.Equal(viewModel.Tests[0].TestId, homeInfoTest.Tests[0].TestId);
            Assert.Equal(viewModel.Tests[0].Title, homeInfoTest.Tests[0].Title);
            Assert.Equal(viewModel.Tests[0].CreatedTime, homeInfoTest.Tests[0].CreatedTime);
            Assert.Equal(viewModel.Tests[0].UpdatedTime, homeInfoTest.Tests[0].UpdatedTime);
            Assert.Equal(viewModel.Tests[0].UserId, homeInfoTest.Tests[0].UserId);
            Assert.Equal(viewModel.Tests[0].Questions[0].QuestionText, homeInfoTest.Tests[0].Questions[0].QuestionText);
            Assert.Equal(viewModel.Tests[0].Questions[0].TestId, homeInfoTest.Tests[0].Questions[0].TestId);
            Assert.Equal(viewModel.Tests[0].Questions[0].Choices[0].ChoiceId, homeInfoTest.Tests[0].Questions[0].Choices[0].ChoiceId);
            Assert.Equal(viewModel.Tests[0].Questions[0].Choices[0].ChoiceText, homeInfoTest.Tests[0].Questions[0].Choices[0].ChoiceText);
            Assert.Equal(viewModel.Tests[0].Questions[0].Choices[0].IsAnswer, homeInfoTest.Tests[0].Questions[0].Choices[0].IsAnswer);
            Assert.Equal(viewModel.Tests[0].Questions[0].Choices[0].IsUsersAnswerCheck, homeInfoTest.Tests[0].Questions[0].Choices[0].IsUsersAnswerCheck);
            Assert.Equal(viewModel.Tests[0].Questions[0].Choices[0].IsUsersAnswerRadio, homeInfoTest.Tests[0].Questions[0].Choices[0].IsUsersAnswerRadio);
            Assert.Equal(viewModel.Tests[0].Questions[0].Choices[0].QuestionId, homeInfoTest.Tests[0].Questions[0].Choices[0].QuestionId);
            Assert.Equal(viewModel.Tests[0].Questions[0].Choices[0].ChoiceText, homeInfoTest.Tests[0].Questions[0].Choices[0].ChoiceText);
            Assert.Equal(viewModel.Tests[0].Questions[0].Choices[0].ChoiceText, homeInfoTest.Tests[0].Questions[0].Choices[0].ChoiceText);
            Assert.Equal(viewModel.Users[0].LoginId, homeInfoTest.Users[0].LoginId);
            Assert.Equal(viewModel.Users[0].UserName, homeInfoTest.Users[0].UserName);
            Assert.Equal(viewModel.Users[0].Password, homeInfoTest.Users[0].Password);
            Assert.Equal(viewModel.Users[0].Salt, homeInfoTest.Users[0].Salt);
            Assert.Equal(viewModel.Users[0].SelfIntroduction, homeInfoTest.Users[0].SelfIntroduction);
            Assert.Equal(viewModel.Users[0].Icon, homeInfoTest.Users[0].Icon);
        }
Esempio n. 13
0
        public void Access_UserEdit_db()
        {
            var model   = UserModelTestData.UserModelData();
            var options = new DbContextOptionsBuilder <TestMakerContext>()
                          .UseInMemoryDatabase(databaseName: "Access_UserEdit_db")
                          .Options;

            using var _context = new TestMakerContext(options);
            _context.Users.Add(model);
            _context.SaveChanges();
            var user = _context.Users.Find(1);

            Assert.Equal(model.UserId, user.UserId);
            Assert.Equal(model.LoginId, user.LoginId);
            Assert.Equal(model.UserName, user.UserName);
            Assert.Equal(model.Password, user.Password);
            Assert.Equal(model.Salt, user.Salt);
            Assert.Equal(model.SelfIntroduction, user.SelfIntroduction);
            Assert.Equal(model.Icon, user.Icon);
        }
Esempio n. 14
0
        public void Access_UserDetails_check_whenIdNull_BeNotFound()
        {
            var model   = UserModelTestData.UserModelData();
            var options = new DbContextOptionsBuilder <TestMakerContext>()
                          .UseInMemoryDatabase(databaseName: "Access_UserDetails_check_whenIdNull_BeNotFound")
                          .Options;

            using var _context = new TestMakerContext(options);
            _context.Users.Add(model);
            _context.SaveChanges();
            IUserRepository userRepository = new UserRepository(_context);
            var             testMapper     = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new AutoMapperConfig());
            });
            var mapper       = testMapper.CreateMapper();
            var controller   = new UserController(userRepository, mapper);
            var actionResult = controller.Details(null);

            Assert.IsType <NotFoundResult>(actionResult);
        }
Esempio n. 15
0
        public void Access_UserDeleteConfirmed_Check_db()
        {
            var model   = UserModelTestData.UserModelData();
            var options = new DbContextOptionsBuilder <TestMakerContext>()
                          .UseInMemoryDatabase(databaseName: "Access_UserDeleteConfirmed_Check_db")
                          .Options;

            using var _context = new TestMakerContext(options);
            _context.Users.Add(model);
            _context.SaveChanges();
            Assert.Equal(1, _context.Users.Count());
            IUserRepository userRepository = new UserRepository(_context);
            var             testMapper     = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new AutoMapperConfig());
            });
            var mapper     = testMapper.CreateMapper();
            var controller = new UserController(userRepository, mapper);

            controller.DeleteConfirmed(1);
            Assert.Equal(0, _context.Users.Count());
        }
Esempio n. 16
0
        public void Access_UserDeleteConfirmed_check_RedirectToActionResult()
        {
            var model   = UserModelTestData.UserModelData();
            var options = new DbContextOptionsBuilder <TestMakerContext>()
                          .UseInMemoryDatabase(databaseName: "Access_UserDeleteConfirmed_check_RedirectToActionResult")
                          .Options;

            using var _context = new TestMakerContext(options);
            _context.Users.Add(model);
            _context.SaveChanges();
            IUserRepository userRepository = new UserRepository(_context);
            var             testMapper     = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new AutoMapperConfig());
            });
            var mapper       = testMapper.CreateMapper();
            var controller   = new UserController(userRepository, mapper);
            var actionResult = controller.DeleteConfirmed(1) as RedirectToActionResult;

            Assert.Equal("Index", actionResult.ActionName);
            Assert.Equal("Home", actionResult.ControllerName);
        }
Esempio n. 17
0
        public void Access_TestIndex_db()
        {
            var testData = TestModelData.TestData();
            var options  = new DbContextOptionsBuilder <TestMakerContext>()
                           .UseInMemoryDatabase(databaseName: "Access_TestIndex_db")
                           .Options;

            using var _context = new TestMakerContext(options);
            foreach (var t in testData)
            {
                _context.Tests.Add(t);
            }
            _context.Users.Add(testData[0].User);
            _context.SaveChanges();
            ITestRepository testRepository = new TestRepository(_context);
            var             testInfoTest   = testRepository.GetAll(1);

            Assert.Equal(testData[0].TestId, testInfoTest.FirstOrDefault().TestId);
            Assert.Equal(testData[0].Title, testInfoTest.FirstOrDefault().Title);
            Assert.Equal(testData[0].CreatedTime, testInfoTest.FirstOrDefault().CreatedTime);
            Assert.Equal(testData[0].UpdatedTime, testInfoTest.FirstOrDefault().UpdatedTime);
            Assert.Equal(testData[0].UserId, testInfoTest.FirstOrDefault().UserId);
            Assert.Equal(testData[0].Questions[0].QuestionText, testInfoTest.FirstOrDefault().Questions[0].QuestionText);
            Assert.Equal(testData[0].Questions[0].TestId, testInfoTest.FirstOrDefault().Questions[0].TestId);
            Assert.Equal(testData[0].Questions[0].Choices[0].ChoiceId, testInfoTest.FirstOrDefault().Questions[0].Choices[0].ChoiceId);
            Assert.Equal(testData[0].Questions[0].Choices[0].ChoiceText, testInfoTest.FirstOrDefault().Questions[0].Choices[0].ChoiceText);
            Assert.Equal(testData[0].Questions[0].Choices[0].IsAnswer, testInfoTest.FirstOrDefault().Questions[0].Choices[0].IsAnswer);
            Assert.Equal(testData[0].Questions[0].Choices[0].IsUsersAnswerCheck, testInfoTest.FirstOrDefault().Questions[0].Choices[0].IsUsersAnswerCheck);
            Assert.Equal(testData[0].Questions[0].Choices[0].IsUsersAnswerRadio, testInfoTest.FirstOrDefault().Questions[0].Choices[0].IsUsersAnswerRadio);
            Assert.Equal(testData[0].Questions[0].Choices[0].QuestionId, testInfoTest.FirstOrDefault().Questions[0].Choices[0].QuestionId);
            Assert.Equal(testData[0].Questions[0].Choices[0].ChoiceText, testInfoTest.FirstOrDefault().Questions[0].Choices[0].ChoiceText);
            Assert.Equal(testData[0].User.LoginId, testInfoTest.FirstOrDefault().User.LoginId);
            Assert.Equal(testData[0].User.UserName, testInfoTest.FirstOrDefault().User.UserName);
            Assert.Equal(testData[0].User.Password, testInfoTest.FirstOrDefault().User.Password);
            Assert.Equal(testData[0].User.Salt, testInfoTest.FirstOrDefault().User.Salt);
            Assert.Equal(testData[0].User.SelfIntroduction, testInfoTest.FirstOrDefault().User.SelfIntroduction);
            Assert.Equal(testData[0].User.Icon, testInfoTest.FirstOrDefault().User.Icon);
        }
Esempio n. 18
0
        public void Access_TestDetails_db()
        {
            var testData = TestModelData.TestData();
            var options  = new DbContextOptionsBuilder <TestMakerContext>()
                           .UseInMemoryDatabase(databaseName: "Access_TestDetails_db")
                           .Options;

            using var _context = new TestMakerContext(options);
            _context.Tests.Add(testData[0]);
            foreach (var q in testData[0].Questions)
            {
                _context.Questions.Add(q);
            }
            foreach (var c in testData[0].Questions[0].Choices)
            {
                _context.Choices.Add(c);
            }
            _context.SaveChanges();
            ITestRepository testRepository = new TestRepository(_context);
            var             testInfoTest   = testRepository.GetContent(o => o.TestId == 1);

            Assert.Equal(testData[0].TestId, testInfoTest.TestId);
            Assert.Equal(testData[0].Title, testInfoTest.Title);
            Assert.Equal(testData[0].CreatedTime, testInfoTest.CreatedTime);
            Assert.Equal(testData[0].UpdatedTime, testInfoTest.UpdatedTime);
            Assert.Equal(testData[0].UserId, testInfoTest.UserId);
            Assert.Equal(testData[0].Questions[0].QuestionText, testInfoTest.Questions[0].QuestionText);
            Assert.Equal(testData[0].Questions[0].TestId, testInfoTest.Questions[0].TestId);
            Assert.Equal(testData[0].Questions[0].Choices[0].ChoiceId, testInfoTest.Questions[0].Choices[0].ChoiceId);
            Assert.Equal(testData[0].Questions[0].Choices[0].ChoiceText, testInfoTest.Questions[0].Choices[0].ChoiceText);
            Assert.Equal(testData[0].Questions[0].Choices[0].IsAnswer, testInfoTest.Questions[0].Choices[0].IsAnswer);
            Assert.Equal(testData[0].Questions[0].Choices[0].IsUsersAnswerCheck, testInfoTest.Questions[0].Choices[0].IsUsersAnswerCheck);
            Assert.Equal(testData[0].Questions[0].Choices[0].IsUsersAnswerRadio, testInfoTest.Questions[0].Choices[0].IsUsersAnswerRadio);
            Assert.Equal(testData[0].Questions[0].Choices[0].QuestionId, testInfoTest.Questions[0].Choices[0].QuestionId);
            Assert.Equal(testData[0].Questions[0].Choices[0].ChoiceText, testInfoTest.Questions[0].Choices[0].ChoiceText);
            Assert.Equal(testData[0].Questions[0].Choices[0].QuestionId, testInfoTest.Questions[0].Choices[0].QuestionId);
            Assert.Equal(testData[0].Questions[0].Choices[0].IsAnswer, testInfoTest.Questions[0].Choices[0].IsAnswer);
        }
Esempio n. 19
0
        public void Access_UserIndex_check_viewData()
        {
            var model   = UserModelTestData.UserModelData();
            var options = new DbContextOptionsBuilder <TestMakerContext>()
                          .UseInMemoryDatabase(databaseName: "Access_UserIndex_check_viewData")
                          .Options;

            using var _context = new TestMakerContext(options);
            _context.Users.Add(model);
            _context.SaveChanges();
            IUserRepository userRepository = new UserRepository(_context);
            var             testMapper     = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new AutoMapperConfig());
            });
            var mapper     = testMapper.CreateMapper();
            var controller = new UserController(userRepository, mapper);
            var view       = controller.Index() as ViewResult;

            Assert.Equal("Index", view.ViewData["Title"]);
            Assert.Equal("Index", view.ViewData["Action"]);
            Assert.Equal("User", view.ViewData["Controller"]);
        }