public async void CanAddCourse()
        {
            DbContextOptions <SchoolDbContext> options = new DbContextOptionsBuilder <SchoolDbContext>()
                                                         .UseInMemoryDatabase("CreateCourseDb").Options;

            using (SchoolDbContext context = new SchoolDbContext(options))
            {
                // arrange
                Course course1 = new Course();
                course1.Name       = "Advanced Anger Management";
                course1.Teacher    = "Bob Saget";
                course1.CourseTerm = CourseTerm.Fall2018;

                Course course2 = new Course();
                course2.Name       = "C# and ASP.NET";
                course2.Teacher    = "Amanda Iverson";
                course2.CourseTerm = CourseTerm.Summer2018;

                CourseController testCC = new CourseController(context);

                // act
                await testCC.Create(course1);

                await testCC.Create(course2);


                var results1 = context.Courses.Where(c => c.Name == "Advanced Anger Management");
                var results2 = context.Courses.Where(c => c.Name == "C# and ASP.NET");

                // assert there is one instance of the results
                Assert.Equal(1, results1.Count());
                Assert.Equal(1, results2.Count());
            }
        }
Ejemplo n.º 2
0
        public void StudentControllerCanCreate()
        {
            DbContextOptions <Lab13StudentEnrollmentDbContext> options =
                new DbContextOptionsBuilder <Lab13StudentEnrollmentDbContext>()
                .UseInMemoryDatabase(Guid.NewGuid().ToString())
                .Options;

            using (Lab13StudentEnrollmentDbContext context = new Lab13StudentEnrollmentDbContext(options))
            {
                // Arrange
                Course course = new Course();
                course.Name        = "DotNet";
                course.Description = "Embrace the flood";

                CourseController cc = new CourseController(context);

                var x = cc.Create(course);

                Student student = new Student();
                student.Name     = "Someone";
                student.CourseID = 1;
                // Act
                StudentController sc = new StudentController(context);

                var y = sc.Create(student);

                var results = context.Students.Where(a => a.Name == "Someone");
                // Assert
                Assert.Equal(1, results.Count());
            }
        }
Ejemplo n.º 3
0
        public void StudentControllerCanDelete()
        {
            DbContextOptions <EnrollmentDbContext> options =
                new DbContextOptionsBuilder <EnrollmentDbContext>()
                .UseInMemoryDatabase(Guid.NewGuid().ToString())
                .Options;

            using (EnrollmentDbContext context = new EnrollmentDbContext(options))
            {
                Course course = new Course();
                course.ID           = 3;
                course.ClassName    = ClassName.Python;
                course.Instructor   = "Miss Snake";
                course.StartingDate = new DateTime(2012, 12, 12);
                course.EndDate      = new DateTime(2012, 12, 25);

                Student student = new Student();
                student.Name         = "Mario";
                student.CourseId     = 3;
                student.Passing      = true;
                student.HoursOfSleep = 5;

                CourseController cc = new CourseController(context);
                var y = cc.Create(course);

                StudentController sc = new StudentController(context);
                var x = sc.Create(student);
                var z = sc.Delete(1);

                var results = context.Student.Where(a => a.Name == "Mario");

                Assert.Equal(0, results.Count());
            }
        }
        public async void CanUpdateCourse()
        {
            DbContextOptions <SchoolDbContext> options = new DbContextOptionsBuilder <SchoolDbContext>()
                                                         .UseInMemoryDatabase("CourseUpdateTestDb")
                                                         .Options;

            using (SchoolDbContext context = new SchoolDbContext(options))
            {
                Course course1 = new Course();
                course1.ID         = 42;
                course1.Name       = "Advanced Anger Management";
                course1.Teacher    = "Bob Saget";
                course1.CourseTerm = CourseTerm.Fall2018;

                CourseController testCC = new CourseController(context);

                var x = await testCC.Create(course1);

                course1.Name       = "Making Unit Tests";
                course1.Teacher    = "Ron Testmaster";
                course1.CourseTerm = CourseTerm.Winter2018;

                var update1 = await testCC.Edit(42, course1);

                var result1 = context.Courses.Where(a => a.Name == "Making Unit Tests");
                var result2 = context.Courses.Where(a => a.Name == "Advanced Anger Management");

                //Assert
                Assert.IsAssignableFrom <IActionResult>(x);
                Assert.Equal(1, result1.Count());
                Assert.Equal(0, result2.Count());
            }
        }
        public async Task Delete_removes_all_related_course_elements_if_it_has_uid()
        {
            var courseView = new CourseViewModel(new List <CourseDesign>(), "http://completevocaltraining.nl")
            {
                Course = new Course()
                {
                    Name = "test"
                }
            };

            courseView.Course.CourseDesign = new CourseDesign
            {
                CourseDesignId = 2
            };
            var controller = new CourseController(Context);
            await controller.Create(courseView);

            await Controller.DeleteConfirmed(new CourseDesignViewModel(
                                                 new CourseDesign()
            {
                CourseDesignId = 4
            }, "http://www.completevocaltraining.nl"));

            Resultcontext.CourseDesigns.Should().HaveCount(3, because: "We seeded the db with 3 CourseDesigns");
            Resultcontext.Seminars.Should().HaveCount(3, because: "We seeded the db with 3 seminars");
            Resultcontext.Days.Should().HaveCount(6, because: "We seeded the db with 6 days");
            Resultcontext.Subjects.Should().HaveCount(6, because: "We seeded the db with 6 subjects");
        }
Ejemplo n.º 6
0
        public async void CanCreateCourse()
        {
            DbContextOptions <MetalDbContext> options =
                new DbContextOptionsBuilder <MetalDbContext>()
                .UseInMemoryDatabase("CanCreateCourse")
                .Options;

            using (MetalDbContext context = new MetalDbContext(options))
            {
                Course course1 = new Course
                {
                    Name        = "Shred Veg",
                    Description = "Veggie Cucumber Zucchini"
                };

                await context.Courses.AddAsync(course1);

                await context.SaveChangesAsync();

                CourseController cc = new CourseController(context);

                var addCourse = cc.Create(course1);
                var results   = context.Courses.Where(x => x.Name == "Shred Veg");

                Assert.Equal(1, results.Count());
            }
        }
Ejemplo n.º 7
0
        public async Task CanCreateCourseAction()
        {
            var options = new DbContextOptionsBuilder <StudentContext>()
                          .UseInMemoryDatabase(databaseName: "testing221")
                          .Options;

            using (var context = new StudentContext(options))
            {
                var    CController = new CourseController(context);
                Course c           = new Course()
                {
                    Department  = "LEAD",
                    Level       = 321,
                    Instructor  = "The Ceasers",
                    Description = "Leasons on leadership"
                };


                //act
                await CController.Create(c);

                Course course = await context.Course.FirstOrDefaultAsync(x => x.Department == "LEAD");

                //Assert
                Assert.NotNull(course);         //exists in db
                Assert.IsType <Course>(course); //is correct type
                Assert.NotEqual(0, course.ID);  //is automatically assigned an ID
            }
        }
Ejemplo n.º 8
0
        public void CourseControllerCanEdit()
        {
            DbContextOptions <EnrollmentDbContext> options =
                new DbContextOptionsBuilder <EnrollmentDbContext>()
                .UseInMemoryDatabase(Guid.NewGuid().ToString())
                .Options;

            using (EnrollmentDbContext context = new EnrollmentDbContext(options))
            {
                Course course = new Course();
                course.ClassName    = ClassName.Python;
                course.Instructor   = "Miss Snake";
                course.StartingDate = new DateTime(2012, 12, 12);
                course.EndDate      = new DateTime(2012, 12, 25);

                CourseController cc = new CourseController(context);
                var x = cc.Create(course);

                Course newCourse = new Course();
                course.ClassName    = ClassName.Python;
                course.Instructor   = "Mister Boa";
                course.StartingDate = new DateTime(2012, 12, 12);
                course.EndDate      = new DateTime(2012, 12, 25);

                var y = cc.Update(newCourse);

                var results = context.Course.Where(a => a.Instructor == "Miss Snake");

                Assert.Equal(0, results.Count());

                results = context.Course.Where(a => a.Instructor == "Mister Boa");
                Assert.Equal(1, results.Count());
            }
        }
        public void Get_creates_empty_course_and_gets_combobox_then_returns_create_view()
        {
            #region Arrange

            using (Mock.Record())
            {
                Expect.Call(CourseService.GetAllCourseTypes()).Return(CourseTypesList);
            }

            #endregion

            #region Act

            ViewResult view;

            using (Mock.Playback())
            {
                view = (ViewResult)CourseController.Create();
            }

            #endregion

            #region Assert
            Assert.IsEmpty(view.ViewName);
            Assert.IsNotNull(view.ViewBag.CourseTypes);
            Assert.That(view.ViewBag.CourseTypes, Is.InstanceOf(typeof(SelectList)));
            Assert.IsNotNull(view.ViewData.Model);
            Assert.That(view.ViewData.Model, Is.InstanceOf(typeof(CourseDto)));
            #endregion
        }
        public void Post_if_model_state_invalid_then_dont_add_course_and_return_error_view()
        {
            #region Arrange
            //Faking ModelState.IsValid = false
            CourseController.ModelState.Add("testError", new ModelState());
            CourseController.ModelState.AddModelError("testError", "test");

            using (Mock.Record())
            {
                Expect.Call(CourseService.AddCourse(Course)).Repeat.Never();
            }
            #endregion

            #region Act
            ViewResult view;
            using (Mock.Playback())
            {
                view = (ViewResult)CourseController.Create(Course);
            }

            #endregion

            #region Assert
            Assert.That(view.ViewName, Is.EqualTo("Error"));
            Assert.That(view.ViewBag.Error, Is.EqualTo(elearn.Common.ErrorMessages.Course.ModelUpdateError));
            #endregion
        }
        public void Create_Get_ReturnsViewResult()
        {
            // Arrange
            var controller = new CourseController(null, null, null, null, null);

            //Act
            var result = controller.Create();

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);
        }
Ejemplo n.º 12
0
        public void Create_ReturnsBadRequest_WhenCourseParameterIsNull()
        {
            //Arrange
            Course           course        = null;
            CourseService    courseService = Substitute.For <CourseService>();
            CourseController controller    = new CourseController(courseService, null, null);

            //Act
            IActionResult actual = controller.Create(course);

            //Assert
            actual.Should().BeAssignableTo <BadRequestResult>();
        }
Ejemplo n.º 13
0
 protected async Task MakeNewCourse(int courseDesignId)
 {
     var courseView = new CourseViewModel(new List <CourseDesign>(), "http://www.completevocaltraining.nl")
     {
         Course = new Course
         {
             Name         = "test",
             CourseDesign = new CourseDesign {
                 CourseDesignId = courseDesignId
             }
         }
     };
     var courseContr = new CourseController(Seedcontext);
     await courseContr.Create(courseView);
 }
Ejemplo n.º 14
0
        public void CreateMethodSetsLocationHeader()
        {
            // Arrange
            var controller = new CourseController(_inMemoryRep);

            // Act
            IHttpActionResult actionResult = controller.Create(new Course {
                Id = 11, Title = "ASP .Net Web API In Memo Testing...", Description = "ASP .Net Web API Development with Project work", Reference = "ASPNETB010", Duration = 18, Fees = 2500, Status = "open", Location = "Net Hall 1", TotalPlaces = 100, AvailablePlaces = 0
            });
            var createdResult = actionResult as CreatedAtRouteNegotiatedContentResult <Course>;

            // Assert
            Assert.IsNotNull(createdResult);
            Assert.AreEqual("DefaultApi", createdResult.RouteName);
            Assert.AreEqual(11, createdResult.RouteValues["id"]);
        }
Ejemplo n.º 15
0
        public void Create_RedirectsToCoursesAndCreatesCourses_WhenRequestIsValid()
        {
            //Arrange
            Course course = new Course {
                Name = "Test_2"
            };
            CourseService    courseService = Substitute.For <CourseService>();
            CourseController controller    = new CourseController(courseService, null, null);

            //Act
            var actionResult = controller.Create(course);

            courseService.Received().CreateCourse(course);

            // Assert
            actionResult.Should().BeOfType <RedirectToActionResult>().Which.ActionName.Should().Be("Courses");
        }
Ejemplo n.º 16
0
        public void Create_ReturnsViewResult_WhenModelStateIsInvalid()
        {
            //Arrange
            Course course = new Course {
                Name = "Test_2"
            };
            CourseService    courseService = Substitute.For <CourseService>();
            CourseController controller    = new CourseController(courseService, null, null);

            //Act
            controller.ModelState.AddModelError("test", "test");  //makes model invalid => if (!ModelState.IsValid)
            IActionResult actual = controller.Create(course);

            //Assert
            Assert.IsAssignableFrom <ViewResult>(actual);
            actual.Should().BeAssignableTo <ViewResult>();
        }
        public void Create_Post_RedirectToCourses()
        {
            // Arrange
            Course courseForCreating = new Course();

            var courseSericeMock = Substitute.For <CourseService>();

            courseSericeMock.CreateCourse(courseForCreating).Returns(true);
            var controller = new CourseController(courseSericeMock, null, null, null, null);

            // Act
            var result = controller.Create(courseForCreating);

            // Assert
            var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result);

            Assert.Null(redirectToActionResult.ControllerName);
            Assert.Equal("Courses", redirectToActionResult.ActionName);
        }
Ejemplo n.º 18
0
        public void Create_ReturnsViewResult_WhenModelStateIsInvalid()
        {
            // Arrange
            var courseServiceMock = Substitute.For <CourseService>();

            courseServiceMock.GetCourseById(Arg.Any <int>()).Returns(new Course());
            var studentService = Substitute.For <StudentService>();

            studentService.GetAllStudents().Returns(new List <Student>());
            var controller = new CourseController(courseServiceMock, studentService);

            controller.ModelState.AddModelError("s", "v");

            // Act
            var result = controller.Create(new CourseViewModel());

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.IsAssignableFrom <Course>(viewResult.ViewData.Model);
        }
Ejemplo n.º 19
0
        public static void SyncToCanvas()
        {
            try
            {
                SyncronizationDAL.SyncToCanvas();

                List <sp_get_uniCanvas_ws_cursos_Result> courseToSyncList = CourseDAL.CoursesToSync();
                List <string> createdCoursesList = new List <string>();

                foreach (sp_get_uniCanvas_ws_cursos_Result courseToSync in courseToSyncList)
                {
                    try
                    {
                        CourseController courseController = new CourseController();
                        Course           course           = new Course();
                        course.course = new CourseDTO(courseToSync);

                        CourseReturn newCourse = (CourseReturn)courseController.Create(course);

                        if (newCourse != null)
                        {
                            CourseDAL.UpdateCanvasData(courseToSync.IDAcademico.ToString(), newCourse);
                        }
                    }
                    catch (Exception e)
                    {
                        CourseReturn newCourse = new CourseReturn()
                        {
                            error_message = e.Message
                        };
                        CourseDAL.UpdateCanvasData(courseToSync.IDAcademico.ToString(), newCourse);
                    }
                }
            }
            catch (Exception e)
            {
                return;
            }
        }
        public void Post_adds_course_then_redirects_to_details_action()
        {
            #region Arrange

            using (Mock.Record())
            {
                Expect.Call(CourseService.AddCourse(Course)).IgnoreArguments().Return(1);
            }
            #endregion

            #region Act
            RedirectToRouteResult redirect;
            using (Mock.Playback())
            {
                redirect = (RedirectToRouteResult)CourseController.Create(new CourseDto());
            }

            #endregion

            #region Assert
            redirect.AssertActionRedirect().ToAction("Details").WithParameter("id", 1);
            #endregion
        }
        public void Post_if_create_in_db_course_failes_then_return_error_view()
        {
            #region Arrange
            using (Mock.Record())
            {
                Expect.Call(CourseService.AddCourse(Course)).IgnoreArguments().Return(null);
            }
            #endregion

            #region Act
            ViewResult view;
            using (Mock.Playback())
            {
                view = (ViewResult)CourseController.Create(Course);
            }

            #endregion

            #region Assert
            Assert.That(view.ViewName, Is.EqualTo("Error"));
            Assert.That(view.ViewBag.Error, Is.EqualTo(elearn.Common.ErrorMessages.Course.AddToDbError));
            #endregion
        }
Ejemplo n.º 22
0
        public void ShouldCreateCorrectly()
        {
            Mock <IGenericService <Course> >      courseServiceMock  = new Mock <IGenericService <Course> >();
            Mock <IActivityLoggerService>         loggerMock         = new Mock <IActivityLoggerService>();
            Mock <IObjectFinderService <Module> > moduleFinderMock   = new Mock <IObjectFinderService <Module> >();
            Mock <IGenericService <Module> >      moduleServiceMock  = new Mock <IGenericService <Module> >();
            Mock <IGenericService <Teacher> >     teacherServiceMock = new Mock <IGenericService <Teacher> >();
            Mock <IManyToManyMapperService <Course, CourseModule, Module> > mapperServiceMock = new Mock <IManyToManyMapperService <Course, CourseModule, Module> >();
            Mock <IGenericService <ExamProgram> > examProgramServiceMock = new Mock <IGenericService <ExamProgram> >();

            CourseCreateUpdateViewModel course = new CourseCreateUpdateViewModel()
            {
                Id          = 100,
                Name        = "Periode 2.3 Data Science",
                Description = "Alle opdrachten in deze periode zijn gekoppeld aan vakken; dit kunnen individuele of groepsopdrachten zijn; er zijn geen opdrachten die meerdere vakken betreffen. Daarom is een verdere omschrijving hier niet nodig."
            };

            ClaimsPrincipal identity = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
            {
                new Claim(ClaimTypes.NameIdentifier, "123")
            }));

            teacherServiceMock.Setup(m => m.FindById(It.IsAny <int>(), It.IsAny <string[]>())).Returns(new Teacher());

            moduleFinderMock.Setup(m => m.AreIdsValid(It.IsAny <int[]>())).Returns(true);

            moduleFinderMock.Setup(m => m.GetObjects(It.IsAny <int[]>())).Returns((int[] ids) =>
            {
                return(new List <Module>());
            });

            mapperServiceMock.Setup(m => m.GetMappedEntities(It.IsAny <Course>(), It.IsAny <ICollection <Module> >())).Returns <Course, ICollection <Module> >((model, modules) =>
            {
                return(new List <CourseModule>());
            });

            courseServiceMock.Setup(m => m.Insert(It.IsAny <Course>())).Returns((Course model) =>
            {
                if (!string.IsNullOrWhiteSpace(model.Name))
                {
                    return(1);
                }

                return(0);
            });

            loggerMock.Setup(m => m.Create(It.IsAny <string>()));

            CourseController controller = new CourseController(courseServiceMock.Object, moduleServiceMock.Object, moduleFinderMock.Object, mapperServiceMock.Object, examProgramServiceMock.Object, teacherServiceMock.Object)
            {
                ControllerContext = new ControllerContext()
                {
                    HttpContext = new DefaultHttpContext()
                    {
                        User = identity
                    }
                }
            };

            RedirectToActionResult result = controller.Create(course) as RedirectToActionResult;

            Assert.Equal("Index", result?.ActionName);
        }