public void Add_Valid_ShouldReturnStatusCode200AndNotNullContent() { var mockUnitOfWork = Mock.Create<IUnitOfWork>(); School school = new School() { Location = "Location", Name = "Name" }; Student studentEntity = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = school, Marks = new List<Mark>() { new Mark() { Subject = "math", Value = 6 } } }; bool isItemAdded = false; Mock.Arrange(() => mockUnitOfWork.SchoolsRepository.Get(Arg.IsAny<int>())) .Returns(school); Mock.Arrange(() => mockUnitOfWork.StudentsRepository.Add(Arg.IsAny<Student>())) .DoInstead(() => isItemAdded = true) .Returns(studentEntity); var server = new InMemoryHttpServer<Student>("http://localhost/", mockUnitOfWork); var response = server.CreatePostRequest("api/students", studentEntity); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); Assert.IsTrue(isItemAdded); Assert.IsNotNull(response.Content); }
public static Student CreateOrLoadStudent(StudentModel studentModel, IUnitOfWork unitOfWork) { Student student = unitOfWork.StudentsRepository.Get(studentModel.ID); if (student != null) { return student; } Student newStudent = new Student() { FirstName = studentModel.FirstName, LastName = studentModel.LastName, Age = studentModel.Age, Grade = studentModel.Grade, School = CreateOrLoadSchool(studentModel.School, unitOfWork) }; foreach (MarkModel mark in studentModel.Marks) { newStudent.Marks.Add(MarksMapper.ToMarkEntity(mark)); } unitOfWork.StudentsRepository.Add(newStudent); unitOfWork.Save(); return newStudent; }
public void AddStudentWithSchoolAndNoMarks_ShouldAddTheStudentCorrectly() { Student student = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = new School() { Name = "Name", Location = "Location" } }; Student studentEntity = this.studentsRepository.Add(student); this.unitOfWork.Save(); // Check entity Assert.IsNotNull(studentEntity); Assert.IsTrue(studentEntity.ID > 0); // Check entity info Assert.AreEqual(student.FirstName, studentEntity.FirstName); Assert.AreEqual(student.LastName, studentEntity.LastName); Assert.AreEqual(student.Age, studentEntity.Age); Assert.AreEqual(student.Grade, studentEntity.Grade); // Check entity school Assert.IsNotNull(studentEntity.School); Assert.AreEqual(student.School.Name, studentEntity.School.Name); Assert.AreEqual(student.School.Location, studentEntity.School.Location); // Check entity marks Assert.IsNotNull(studentEntity.Marks); Assert.IsTrue(studentEntity.Marks.Count == 0); }
public static StudentModel ToStudentModel(Student studentEntity) { StudentModel studentModel = new StudentModel(); studentModel.ID = studentEntity.ID; studentModel.FirstName = studentEntity.FirstName; studentModel.LastName = studentEntity.LastName; studentModel.Age = studentEntity.Age; studentModel.Grade = studentEntity.Grade; studentModel.School = new SchoolDetails() { ID = studentEntity.School.ID, Name = studentEntity.School.Name, Location = studentEntity.School.Location }; foreach (Mark mark in studentEntity.Marks) { studentModel.Marks.Add(new MarkModel() { ID = mark.ID, Subject = mark.Subject, Value = mark.Value }); } return studentModel; }
public static Student ToStudentEntity(StudentModel studentModel, IUnitOfWork unitOfWork) { Student student = new Student(); student.Age = studentModel.Age; student.FirstName = studentModel.FirstName; student.LastName = studentModel.LastName; student.Grade = studentModel.Grade; student.School = Extensions.CreateOrLoadSchool(studentModel.School, unitOfWork); foreach (MarkModel mark in studentModel.Marks) { student.Marks.Add(new Mark() { Subject = mark.Subject, Value = mark.Value }); } return student; }
public void AddValidStudent_ShouldBeAddedCorrectly() { var unitOfWork = Mock.Create<IUnitOfWork>(); School school = new School() { Location = "Location", Name = "Name" }; Student studentEntity = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = school, Marks = new List<Mark>() { new Mark() { Subject = "math", Value = 6 } } }; bool isItemAdded = false; Mock.Arrange(() => unitOfWork.SchoolsRepository.Get(Arg.IsAny<int>())) .Returns(school); Mock.Arrange(() => unitOfWork.StudentsRepository.Add(Arg.IsAny<Student>())) .DoInstead(() => isItemAdded = true) .Returns(studentEntity); var studentsController = new StudentsController(unitOfWork); SetupController(studentsController); StudentModel studentModel = new StudentModel() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = new SchoolDetails() { Location = school.Location, Name = school.Name }, Marks = new List<MarkModel>() { new MarkModel() { Subject = "math", Value = 6 } } }; studentsController.Add(studentModel); Assert.IsTrue(isItemAdded); }
public void Add_StudentWithoutSchool_ShouldReturnStatusCode500() { var mockUnitOfWork = Mock.Create<IUnitOfWork>(); Student studentEntity = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, Marks = new List<Mark>() { new Mark() { Subject = "math", Value = 6 } } }; Mock.Arrange(() => mockUnitOfWork.StudentsRepository .Add(Arg.Matches<Student>(st => st.School == null))) .Throws<ArgumentNullException>(); var server = new InMemoryHttpServer<Student>("http://localhost/", mockUnitOfWork); var response = server.CreatePostRequest("api/students", studentEntity); Assert.AreEqual(HttpStatusCode.InternalServerError, response.StatusCode); }
public void GetStudentsWithMarkAboveValueOnASubject_ShouldReturnZeroStudents() { Student firstStudentToAdd = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = new School { Name = "Name", Location = "Location" }, Marks = new List<Mark>() { new Mark() { Value = 5.5, Subject = "math" }, new Mark() { Value = 4.9, Subject = "Biology" } } }; this.studentsRepository.Add(firstStudentToAdd); this.unitOfWork.Save(); var students = this.studentsRepository.GetBySubjectAndMark("math", 6); Assert.IsTrue(students.Count() == 0); }
public void GetAllStudents_ShouldReturnAllStudents() { Student firstStudentToAdd = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = new School { Name = "Name", Location = "Location" }, Marks = new List<Mark>() { new Mark() { Value = 5.5, Subject = "math" }, new Mark() { Value = 4.9, Subject = "Biology" } } }; Student secondStudentToAdd = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = new School { Name = "Name", Location = "Location" } }; this.studentsRepository.Add(firstStudentToAdd); this.studentsRepository.Add(secondStudentToAdd); this.unitOfWork.Save(); foreach (Student studentEntity in this.studentsRepository.GetAll()) { Assert.IsTrue(studentEntity.ID > 0); } }
public void GetAll_WhenOneStudent_ShouldReturnStatusCode200AndCorrectStudent() { var mockUnitOfWork = Mock.Create<IUnitOfWork>(); School school = new School() { Location = "Location", Name = "Name" }; Student studentEntity = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = school, Marks = new List<Mark>() { new Mark() { Subject = "math", Value = 6 } } }; var studentEntities = new List<Student>(); studentEntities.Add(studentEntity); Mock.Arrange(() => mockUnitOfWork.StudentsRepository.GetAll()) .Returns(() => studentEntities.AsQueryable<Student>()); var server = new InMemoryHttpServer<Student>("http://localhost/", mockUnitOfWork); var response = server.CreateGetRequest("api/students"); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); // Check personal information Assert.AreEqual(studentEntity.FirstName, studentEntity.FirstName); Assert.AreEqual(studentEntity.LastName, studentEntity.LastName); Assert.AreEqual(studentEntity.Age, studentEntity.Age); Assert.AreEqual(studentEntity.Grade, studentEntity.Grade); Assert.AreEqual(school.Name, studentEntity.School.Name); Assert.AreEqual(school.Location, studentEntity.School.Location); // Check marks Assert.AreEqual(studentEntity.Marks.First().Subject, studentEntity.Marks.First().Subject); Assert.AreEqual(studentEntity.Marks.First().Value, studentEntity.Marks.First().Value); }
public void GetBySubjectAndMark_WithMarkValueAboveExisting_ShoulReturn200() { var mockUnitOfWork = Mock.Create<IUnitOfWork>(); School school = new School() { Location = "Location", Name = "Name" }; Student studentEntity = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = school, Marks = new List<Mark>() { new Mark() { Subject = "math", Value = 6 } } }; var studentEntities = new List<Student>(); studentEntities.Add(studentEntity); string subject = "math"; double mark = 6.2; Mock.Arrange(() => mockUnitOfWork.StudentsRepository.GetBySubjectAndMark(subject, mark)) .Returns(() => studentEntities .Where(st => st.Marks.Any(m => m.Subject == subject && m.Value > mark)) .AsQueryable<Student>()); var server = new InMemoryHttpServer<Student>("http://localhost/", mockUnitOfWork); var response = server.CreateGetRequest("api/students?subject=" + subject + "&value=" + mark); var studentModels = response.Content .ReadAsAsync<IQueryable<StudentModel>>().Result; Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); Assert.AreEqual(0, studentModels.Count()); }
public void GetByValidID_ShouldReturnCorrectStudent() { var unitOfWork = Mock.Create<IUnitOfWork>(); School school = new School() { Location = "Location", Name = "Name" }; Student studentEntity = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = school, Marks = new List<Mark>() { new Mark() { Subject = "math", Value = 6 } } }; IList<Student> studentEntities = new List<Student>(); studentEntities.Add(studentEntity); int id = 1; // Database IDs starts with 1 id--; Mock.Arrange(() => unitOfWork.StudentsRepository.Get(id)) .Returns(() => id >= 0 ? studentEntities[id] : null); var studentsController = new StudentsController(unitOfWork); SetupController(studentsController); var httpResponseWithStudentModels = studentsController.Get(id); var studentModel = httpResponseWithStudentModels.Content.ReadAsAsync<StudentModel>().Result; Assert.AreEqual(httpResponseWithStudentModels.StatusCode, HttpStatusCode.OK); // Check personal information Assert.AreEqual(studentEntity.FirstName, studentModel.FirstName); Assert.AreEqual(studentEntity.LastName, studentModel.LastName); Assert.AreEqual(studentEntity.Age, studentModel.Age); Assert.AreEqual(studentEntity.Grade, studentModel.Grade); Assert.AreEqual(school.Name, studentModel.School.Name); Assert.AreEqual(school.Location, studentModel.School.Location); // Check marks Assert.AreEqual(studentEntity.Marks.First().Subject, studentModel.Marks.First().Subject); Assert.AreEqual(studentEntity.Marks.First().Value, studentModel.Marks.First().Value); }
public void GetByID_WithInvalidID_ShouldReturnStatusCode500() { var mockUnitOfWork = Mock.Create<IUnitOfWork>(); School school = new School() { Location = "Location", Name = "Name" }; Student studentEntity = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = school, Marks = new List<Mark>() { new Mark() { Subject = "math", Value = 6 } } }; var studentEntities = new List<Student>(); studentEntities.Add(studentEntity); int id = -1; id--; // Database IDs starts with 1 Mock.Arrange(() => mockUnitOfWork.StudentsRepository.Get(id)) .Returns(() => id >= 0 ? studentEntities[id] : null); var server = new InMemoryHttpServer<Student>("http://localhost/", mockUnitOfWork); var response = server.CreateGetRequest("api/students/" + id); Assert.AreEqual(HttpStatusCode.InternalServerError, response.StatusCode); }
public void GetAll_WhenMultipleStudents_ShouldReturnStatusCode200AndNotNullContent() { var mockUnitOfWork = Mock.Create<IUnitOfWork>(); School school = new School() { Location = "Location", Name = "Name" }; Student studentEntity = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = school, Marks = new List<Mark>() { new Mark() { Subject = "math", Value = 6 } } }; var studentEntities = new List<Student>(); studentEntities.Add(studentEntity); Mock.Arrange(() => mockUnitOfWork.StudentsRepository.GetAll()) .Returns(() => studentEntities.AsQueryable<Student>()); var server = new InMemoryHttpServer<Student>("http://localhost/", mockUnitOfWork); var response = server.CreateGetRequest("api/students"); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); Assert.IsNotNull(response.Content); }
public void AddStudentWithoutSchool_ShouldNotBeAdded() { var unitOfWork = Mock.Create<IUnitOfWork>(); Student studentEntity = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, Marks = new List<Mark>() { new Mark() { Subject = "math", Value = 6 } } }; bool isItemAdded = false; Mock.Arrange(() => unitOfWork.StudentsRepository.Add(Arg.IsAny<Student>())) .DoInstead(() => isItemAdded = true) .Returns(studentEntity); var studentsController = new StudentsController(unitOfWork); SetupController(studentsController); StudentModel studentModel = new StudentModel() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, Marks = new List<MarkModel>() { new MarkModel() { Subject = "math", Value = 6 } } }; studentsController.Add(studentModel); Assert.IsFalse(isItemAdded); }
public void GetBySubjectAndMark_WithMarkValueAboveThanExisting_ShouldReturnEmptyCollection() { var unitOfWork = Mock.Create<IUnitOfWork>(); School school = new School() { Location = "Location", Name = "Name" }; Student studentEntity = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = school, Marks = new List<Mark>() { new Mark() { Subject = "math", Value = 6 } } }; IList<Student> studentEntities = new List<Student>(); studentEntities.Add(studentEntity); string subject = "math"; double mark = 6.2; Mock.Arrange(() => unitOfWork.StudentsRepository.GetBySubjectAndMark(subject, mark)) .Returns(() => studentEntities .Where(st => st.Marks.Any(m => m.Subject == subject && m.Value > mark)) .AsQueryable<Student>()); var studentsController = new StudentsController(unitOfWork); SetupController(studentsController); var httpResponseWithStudentModels = studentsController.Get(subject, mark); var studentModels = httpResponseWithStudentModels.Content .ReadAsAsync<IQueryable<StudentModel>>().Result; Assert.AreEqual(httpResponseWithStudentModels.StatusCode, HttpStatusCode.OK); Assert.AreEqual(0, studentModels.Count()); }
public void GetBySubjectAndMark_WithSingleStudent_ShouldReturnCorrectStudent() { var unitOfWork = Mock.Create<IUnitOfWork>(); School school = new School() { Location = "Location", Name = "Name" }; Student studentEntity = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = school, Marks = new List<Mark>() { new Mark() { Subject = "math", Value = 6 } } }; IList<Student> studentEntities = new List<Student>(); studentEntities.Add(studentEntity); string subject = "math"; double mark = 5.9; Mock.Arrange(() => unitOfWork.StudentsRepository.GetBySubjectAndMark(subject, mark)) .Returns(() => studentEntities .Where(st => st.Marks.Any(m => m.Subject == subject && m.Value > mark)) .AsQueryable<Student>()); var studentsController = new StudentsController(unitOfWork); SetupController(studentsController); var httpResponseWithStudentModels = studentsController.Get(subject, mark); var studentModel = httpResponseWithStudentModels.Content .ReadAsAsync<IQueryable<StudentModel>>().Result.First(); Assert.AreEqual(httpResponseWithStudentModels.StatusCode, HttpStatusCode.OK); // Check personal information Assert.AreEqual(studentEntity.FirstName, studentModel.FirstName); Assert.AreEqual(studentEntity.LastName, studentModel.LastName); Assert.AreEqual(studentEntity.Age, studentModel.Age); Assert.AreEqual(studentEntity.Grade, studentModel.Grade); Assert.AreEqual(school.Name, studentModel.School.Name); Assert.AreEqual(school.Location, studentModel.School.Location); // Check marks Assert.AreEqual(studentEntity.Marks.First().Subject, studentModel.Marks.First().Subject); Assert.AreEqual(studentEntity.Marks.First().Value, studentModel.Marks.First().Value); }
public void AddStudentWithSchoolAndMarks_ShouldAddStudentCorrectly() { Student student = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = new School { Name = "Name", Location = "Location" }, Marks = new List<Mark>() { new Mark() { Value = 5.5, Subject = "math" }, new Mark() { Value = 4.9, Subject = "Biology" } } }; Student studentEntity = this.studentsRepository.Add(student); this.unitOfWork.Save(); // Check entity Assert.IsNotNull(studentEntity); Assert.IsTrue(studentEntity.ID > 0); // Check entity info Assert.AreEqual(student.FirstName, studentEntity.FirstName); Assert.AreEqual(student.LastName, studentEntity.LastName); Assert.AreEqual(student.Age, studentEntity.Age); Assert.AreEqual(student.Grade, studentEntity.Grade); // Check entity school Assert.IsNotNull(studentEntity.School); Assert.AreEqual(student.School.Name, studentEntity.School.Name); Assert.AreEqual(student.School.Location, studentEntity.School.Location); // Check entity marks Assert.IsNotNull(studentEntity.Marks); Assert.IsTrue(studentEntity.Marks.Count == 2); Assert.AreEqual(student.Marks.First().Subject, studentEntity.Marks.First().Subject); Assert.AreEqual(student.Marks.First().Value, studentEntity.Marks.First().Value); Assert.AreEqual(student.Marks.Skip(1).First().Subject, studentEntity.Marks.Skip(1).First().Subject); Assert.AreEqual(student.Marks.Skip(1).First().Value, studentEntity.Marks.Skip(1).First().Value); }
public void GetAllFromACollectionWithMultipleStudents_ShouldReturnMultipleStudents() { var unitOfWork = Mock.Create<IUnitOfWork>(); School school = new School() { Location = "Location", Name = "Name" }; Student studentEntity1 = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = school, Marks = new List<Mark>() { new Mark() { Subject = "math", Value = 6 } } }; Student studentEntity2 = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = school, Marks = new List<Mark>() { new Mark() { Subject = "math", Value = 6 } } }; IList<Student> studentEntities = new List<Student>(); studentEntities.Add(studentEntity1); studentEntities.Add(studentEntity2); Mock.Arrange(() => unitOfWork.StudentsRepository.GetAll()) .Returns(() => studentEntities.AsQueryable<Student>()); var studentsController = new StudentsController(unitOfWork); SetupController(studentsController); var httpResponseWithStudentModels = studentsController.GetAll(); var studentModels = httpResponseWithStudentModels.Content.ReadAsAsync<IQueryable<StudentModel>>().Result; Assert.AreEqual(2, studentModels.Count()); }
public void GetByInvalidId_ShoulReturnInternalServerErrorResponse() { var unitOfWork = Mock.Create<IUnitOfWork>(); School school = new School() { Location = "Location", Name = "Name" }; Student studentEntity = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = school, Marks = new List<Mark>() { new Mark() { Subject = "math", Value = 6 } } }; IList<Student> studentEntities = new List<Student>(); studentEntities.Add(studentEntity); int id = -1; // Database IDs starts with 1 id--; Mock.Arrange(() => unitOfWork.StudentsRepository.Get(id)) .Returns(() => id >= 0 ? studentEntities[id] : null); var studentsController = new StudentsController(unitOfWork); SetupController(studentsController); var httpResponseWithStudentModels = studentsController.Get(id); Assert.AreEqual(httpResponseWithStudentModels.StatusCode, HttpStatusCode.InternalServerError); }
public void GetByID_WithValidID_ShouldReturnStatusCode200AndCorrectStudent() { var mockUnitOfWork = Mock.Create<IUnitOfWork>(); School school = new School() { Location = "Location", Name = "Name" }; Student studentEntity = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = school, Marks = new List<Mark>() { new Mark() { Subject = "math", Value = 6 } } }; var studentEntities = new List<Student>(); studentEntities.Add(studentEntity); int id = 1; id--; // Database IDs starts with 1 Mock.Arrange(() => mockUnitOfWork.StudentsRepository.Get(id)) .Returns(() => id >= 0 ? studentEntities[id] : null); var server = new InMemoryHttpServer<Student>("http://localhost/", mockUnitOfWork); var response = server.CreateGetRequest("api/students/" + id); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); // Check personal information Assert.AreEqual(studentEntity.FirstName, studentEntity.FirstName); Assert.AreEqual(studentEntity.LastName, studentEntity.LastName); Assert.AreEqual(studentEntity.Age, studentEntity.Age); Assert.AreEqual(studentEntity.Grade, studentEntity.Grade); Assert.AreEqual(school.Name, studentEntity.School.Name); Assert.AreEqual(school.Location, studentEntity.School.Location); // Check marks Assert.AreEqual(studentEntity.Marks.First().Subject, studentEntity.Marks.First().Subject); Assert.AreEqual(studentEntity.Marks.First().Value, studentEntity.Marks.First().Value); }
public void GetAllFromACollectionOfOne_ShouldSingleStudent() { var unitOfWork = Mock.Create<IUnitOfWork>(); School school = new School() { Location = "Location", Name = "Name" }; Student studentEntity = new Student() { FirstName = "Pesho", LastName = "Peshov", Age = 16, Grade = 10, School = school, Marks = new List<Mark>() { new Mark() { Subject = "math", Value = 6 } } }; IList<Student> studentEntities = new List<Student>(); studentEntities.Add(studentEntity); Mock.Arrange(() => unitOfWork.StudentsRepository.GetAll()) .Returns(() => studentEntities.AsQueryable<Student>()); var studentsController = new StudentsController(unitOfWork); SetupController(studentsController); var httpResponseWithStudentModels = studentsController.GetAll(); var studentModels = httpResponseWithStudentModels.Content.ReadAsAsync<IQueryable<StudentModel>>().Result; // Check count Assert.AreEqual(1, studentModels.Count()); // Check personal information Assert.AreEqual(studentEntity.FirstName, studentModels.First().FirstName); Assert.AreEqual(studentEntity.LastName, studentModels.First().LastName); Assert.AreEqual(studentEntity.Age, studentModels.First().Age); Assert.AreEqual(studentEntity.Grade, studentModels.First().Grade); Assert.AreEqual(school.Name, studentModels.First().School.Name); Assert.AreEqual(school.Location, studentModels.First().School.Location); // Check marks Assert.AreEqual(studentEntity.Marks.First().Subject, studentModels.First().Marks.First().Subject); Assert.AreEqual(studentEntity.Marks.First().Value, studentModels.First().Marks.First().Value); }