Exemplo n.º 1
0
        public async Task DeleteStudent()
        {
            var options = new DbContextOptionsBuilder <BackendTestDbContext>()
                          .UseInMemoryDatabase(databaseName: "UpdateStudentDatabase")
                          .Options;

            var savedMasterClass = new ClassMaster();
            var savedDetailClass = new ClassDetail();

            #region SaveMasterClass

            using (var context = new BackendTestDbContext(options))
            {
                var classMasterRepository        = new ClassMasterRepository(context);
                var classDetailRepository        = new ClassDetailRepository(context);
                var studentRepository            = new StudentRepository(context);
                var classDetailStudentRepository = new ClassDetailStudentRepository(context);
                var classController = new ClassController(classMasterRepository, classDetailRepository,
                                                          studentRepository, classDetailStudentRepository);

                var toBeSavedEntity = new ClassMasterDto
                {
                    Classname   = "Computer Engineering",
                    Location    = "Some where",
                    TeacherName = "Dr. Demberg"
                };

                var rawJson = await classController.SaveMasterClass(toBeSavedEntity);

                var res = (WebApiSimpleResponse <ClassMaster>)rawJson.Value;
                savedMasterClass = res.Result;

                Assert.IsFalse(res.ErrorFlag);
            }

            #endregion

            #region SaveDetailClass

            using (var context = new BackendTestDbContext(options))
            {
                var classMasterRepository        = new ClassMasterRepository(context);
                var classDetailRepository        = new ClassDetailRepository(context);
                var studentRepository            = new StudentRepository(context);
                var classDetailStudentRepository = new ClassDetailStudentRepository(context);
                var classController = new ClassController(classMasterRepository, classDetailRepository,
                                                          studentRepository, classDetailStudentRepository);

                var toBeSavedEntity = new ClassDetailDto
                {
                    ClassMasterId = savedMasterClass.Id
                };

                var rawJson = await classController.SaveDetailClass(toBeSavedEntity);

                var res = (WebApiSimpleResponse <ClassDetail>)rawJson.Value;
                savedDetailClass = res.Result;

                Assert.IsFalse(res.ErrorFlag);
            }

            #endregion


            var student = new Student();

            #region SaveStudent

            using (var context = new BackendTestDbContext(options))
            {
                var _studentRepository            = new StudentRepository(context);
                var _classDetailStudentRepository = new ClassDetailStudentRepository(context);
                var studentController             = new StudentController(_studentRepository, _classDetailStudentRepository);

                var toBeSavedStudent = new StudentDto
                {
                    FirstName = "John",
                    LastName  = "Smith",
                    Gpa       = (decimal)15.2,
                    Age       = 20
                };

                var rawJsonRes1 = await studentController.SaveStudent(toBeSavedStudent);

                student = ((WebApiSimpleResponse <Student>)rawJsonRes1.Value).Result;
                Assert.IsFalse(((WebApiSimpleResponse <Student>)rawJsonRes1.Value).ErrorFlag);
            }

            #endregion

            #region AddStudentToDetailClass

            using (var context = new BackendTestDbContext(options))
            {
                var classMasterRepository        = new ClassMasterRepository(context);
                var classDetailRepository        = new ClassDetailRepository(context);
                var studentRepository            = new StudentRepository(context);
                var classDetailStudentRepository = new ClassDetailStudentRepository(context);
                var classController = new ClassController(classMasterRepository, classDetailRepository,
                                                          studentRepository, classDetailStudentRepository);

                var toBeSavedEntity = new ClassDetailDto
                {
                    ClassDetailId = savedDetailClass.Id,
                    StudentDtos   = new List <StudentDto>
                    {
                        new StudentDto
                        {
                            Id = student.Id
                        }
                    }
                };

                var rawJson = await classController.AddStudentsToDetailClass(toBeSavedEntity);

                var res = (WebApiSimpleResponse <ClassDetailStudent>)rawJson.Value;

                Assert.IsFalse(res.ErrorFlag);
            }

            #endregion

            #region DeleteWithNullValue

            using (var context = new BackendTestDbContext(options))
            {
                var _studentRepository            = new StudentRepository(context);
                var _classDetailStudentRepository = new ClassDetailStudentRepository(context);
                var studentController             = new StudentController(_studentRepository, _classDetailStudentRepository);

                var rawJsonDelete = await studentController.DeleteStudent(null);

                var deleteStudentRes = (WebApiSimpleResponse <Student>)rawJsonDelete.Value;
                Assert.IsTrue(deleteStudentRes.ErrorFlag);
            }

            #endregion

            #region DeleteAStudentEnrolledInAClass

            using (var context = new BackendTestDbContext(options))
            {
                var _studentRepository            = new StudentRepository(context);
                var _classDetailStudentRepository = new ClassDetailStudentRepository(context);
                var studentController             = new StudentController(_studentRepository, _classDetailStudentRepository);

                var toBeDeletedStudent = new StudentDto
                {
                    Id = student.Id,
                };

                var rawJsonDelete = await studentController.DeleteStudent(toBeDeletedStudent);

                var deleteStudentRes = (WebApiSimpleResponse <Student>)rawJsonDelete.Value;
                Assert.IsTrue(deleteStudentRes.ErrorFlag);
            }

            #endregion

            #region RemoveStudentFromDetailClass

            using (var context = new BackendTestDbContext(options))
            {
                var classMasterRepository        = new ClassMasterRepository(context);
                var classDetailRepository        = new ClassDetailRepository(context);
                var studentRepository            = new StudentRepository(context);
                var classDetailStudentRepository = new ClassDetailStudentRepository(context);
                var classController = new ClassController(classMasterRepository, classDetailRepository,
                                                          studentRepository, classDetailStudentRepository);

                var toBeSavedEntity = new ClassDetailDto
                {
                    ClassDetailId = savedDetailClass.Id,
                    StudentDtos   = new List <StudentDto>
                    {
                        new StudentDto
                        {
                            Id = student.Id
                        }
                    }
                };

                var rawJson = await classController.RemoveStudentsFromDetailClass(toBeSavedEntity);

                var res = (WebApiSimpleResponse <ClassMasterDto>)rawJson.Value;

                Assert.IsFalse(res.ErrorFlag);
            }

            #endregion

            #region NiceStudentDelete

            using (var context = new BackendTestDbContext(options))
            {
                var _studentRepository            = new StudentRepository(context);
                var _classDetailStudentRepository = new ClassDetailStudentRepository(context);
                var studentController             = new StudentController(_studentRepository, _classDetailStudentRepository);

                var toBeDeletedStudent = new StudentDto
                {
                    Id = student.Id,
                };

                var rawJsonDelete = await studentController.DeleteStudent(toBeDeletedStudent);

                var deleteStudentRes = (WebApiSimpleResponse <Student>)rawJsonDelete.Value;
                Assert.IsFalse(deleteStudentRes.ErrorFlag);

                var rawJsonGetStudentById = studentController.GetStudentById(toBeDeletedStudent.Id);
                var getStudentByIdRes     = (WebApiSimpleResponse <StudentDto>)rawJsonGetStudentById.Value;

                Assert.IsTrue(getStudentByIdRes.ErrorFlag);
            }

            #endregion

            #region InvalidId

            using (var context = new BackendTestDbContext(options))
            {
                var _studentRepository            = new StudentRepository(context);
                var _classDetailStudentRepository = new ClassDetailStudentRepository(context);
                var studentController             = new StudentController(_studentRepository, _classDetailStudentRepository);

                var toBeDeletedStudent = new StudentDto
                {
                    Id = student.Id,
                };

                var rawJsonDelete = await studentController.DeleteStudent(toBeDeletedStudent);

                var deleteStudentRes = (WebApiSimpleResponse <Student>)rawJsonDelete.Value;
                Assert.IsTrue(deleteStudentRes.ErrorFlag);
            }

            #endregion
        }