コード例 #1
0
        public void AddSchoolWithStudent()
        {
            //arrange
            var student = new Student()
            {
                Age = 10,
                FirstName = "Pesho",
                LastName = "Trojkata",
                Grade = 4,
            };

            var school = new School()
            {
                Location = "Меден Рудник",
                Name = "Петко Росен",
                Students = new HashSet<Student>() { student },
            };

            dbContext.Set<Student>().Add(student);
            dbContext.SaveChanges();
            //act
            dbContext.Set<School>().Add(school);
            dbContext.SaveChanges();
            //assert
            Assert.AreEqual(student, school.Students.ToList<Student>()[0]);
        }
コード例 #2
0
        public void StudentControllerGetSingleRecordTest()
        {
            //arrange
            var mockRepository = Mock.Create<IRepository<Student>>();
            var studentToAdd = new Student()
            {
                Age = 10,
                Grade = 4,
                FirstName = "Nikolay",
                LastName = "Kostadinov",
            };

            IList<Student> studentEntities = new List<Student>();
            studentEntities.Add(studentToAdd);

            Mock.Arrange(() => mockRepository.All())
                .Returns(() => studentEntities.AsQueryable());

            var studentController = new StudentController(mockRepository);
            var studentModels = studentEntities.AsQueryable().Select(StudentDetailedModel.FormStudent).First();
            var expected = studentModels;
            //act
            var actual = studentController.Get(0);

            // assert
            Assert.AreEqual(expected.FirstName, actual.FirstName);
        }
コード例 #3
0
        public void AddSimpleStudentTest()
        {
            bool isItemAdded = false;
            var repository = Mock.Create<IRepository<Student>>();

            var studentModel = new StudentModel()
            {
                FirstName = "Petko"
            };

            var studentEntity = new Student()
            {
                StudentId = 1,
                FirstName = studentModel.FirstName
            };

            Mock.Arrange(() => repository.Add(Arg.IsAny<Student>()))
                .DoInstead(() => isItemAdded = true);
                //.Returns(studentEntity);

            var controller = new StudentsController(repository);
            SetupController(controller);
            controller.PostStudent(studentModel);

            Assert.IsTrue(isItemAdded);
        }
コード例 #4
0
        public void StudentControllerGetAllRecordsTest()
        {
            //arrange
            var mockRepository = Mock.Create<IRepository<Student>>();
            var studentToAdd = new Student()
            {
                Age = 10,
                Grade = 4,
                FirstName = "Nikolay",
                LastName = "Kostadinov",
            };

            IList<Student> studentEntities = new List<Student>();
            studentEntities.Add(studentToAdd);

            Mock.Arrange(() => mockRepository.All())
                .Returns(() => studentEntities.AsQueryable());

            var studentController = new StudentController(mockRepository);

            var expected = 1;
            //act
            var actual = studentController.Get();

            // assert
            Assert.AreEqual(expected, actual.Count());
        }
コード例 #5
0
        static void Main()
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<SchoolSystemContext, Configuration>());

            var db = new SchoolSystemContext();

            var newStudent = new Student { Name = "Random Person", Number = "99999", };
            db.Students.Add(newStudent);

            db.SaveChanges();
        }
コード例 #6
0
        public void GetAllStudents_ReturnOnlyOneTest()
        {
            var repository = new FakeRepository<Student>();

            var studentToAdd = new Student()
            {
                FirstName = "Test student"
            };
            repository.entities.Add(studentToAdd);

            var controller = new StudentsController(repository);

            var studentsModels = controller.Get();
            Assert.IsTrue(studentsModels.Count() == 1);
            Assert.AreEqual(studentToAdd.FirstName, studentsModels.First().FirstName);
        }
コード例 #7
0
        public HttpResponseMessage AddStudent(Student student)
        {
            var response = this.HandleExceptions(() =>
            {
                if (!this.ModelState.IsValid)
                {
                    throw new ArgumentNullException(
                        "student", string.Format("The student model is not valid. {0}", this.ModelState));
                }

                student = this.DataStore.Create(student);
                return this.Request.CreateResponse(HttpStatusCode.Created, student);
            });

            return response;
        }
コード例 #8
0
        public void GetAllStudentsTest()
        {
            var repository = Mock.Create<IRepository<Student>>();
            var studentToAdd = new Student()
            {
                FirstName = "Petko"
            };
            IList<Student> studentEntities = new List<Student>();
            studentEntities.Add(studentToAdd);
            Mock.Arrange(() => repository.All()).Returns(studentEntities.AsQueryable);

            var controller = new StudentsController(repository);

            var studentModels = controller.Get();
            Assert.IsTrue(studentModels.Count() == 1);
            Assert.AreEqual(studentToAdd.FirstName, studentModels.First().FirstName);
        }
コード例 #9
0
        public void GetAllStudents_WhenASingleStudentInRepository_ShouldReturnSingleStudent()
        {
            // Arrange
            var repository = new FakeStudentsRepository();
            var student = new Student
            {
                FirstName = "Gosho"
            };
            repository.Students.Add(student);

            var controller = SetupController(repository, HttpMethod.Get);

            // Act
            var students = controller.GetAllStudents();

            // Assert
            Assert.IsNotNull(students);
            Assert.IsTrue(students.Count() == 1);
            Assert.AreEqual(student.FirstName, students.First().FirstName);
        }
コード例 #10
0
        public void AddingSingleStudentEntryTest()
        {
            //arrange
            const string Subject = "Math";
            const decimal Value = 2.00m;
            const int Age = 10;
            const string FirstName = "Nikolay";
            const string LastName = "Kostadinov";
            const int Grade = 4;
            var mark = new Mark()
            {
                Subject = Subject,
                Value = Value
            };

            var student = new Student()
            {
                Age = Age,
                FirstName = FirstName,
                LastName = LastName,
                Grade = Grade,
                Marks = new List<Mark>() { mark },
            };

            //act
            dbContext.Set<Student>().Add(student);
            dbContext.SaveChanges();

            //assert
            var studentMarks = student.Marks.ToList();
            var actual =
                student.Age == Age &&
                student.Grade == Grade &&
                student.FirstName == FirstName &&
                student.LastName == LastName &&
                student.Marks.Count == 1 &&
                studentMarks[0].Subject == mark.Subject &&
                studentMarks[0].Value == mark.Value;
            Assert.IsTrue(actual);
        }
コード例 #11
0
        public void UpdateStudent(Student student)
        {
            if (this.FirstName != null)
            {
                student.FirstName = this.FirstName;
            }

            if (this.LastName != null)
            {
                student.LastName = this.LastName;
            }

            //if (this.Age != null)
            //{
                student.Age = this.Age;
            //}

            //if (this.Grade != null)
            //{
                student.Grade = this.Grade;
            //}
        }
コード例 #12
0
        public void GetAllStudents_WhenASingleStudentInRepository_ShouldReturnSingleStudent()
        {
            // Arrange
            var student = new Student
            {
                FirstName = "Gosho",
                LastName = "Petkov",
                Age = 15,
                Grade = 9
            };

            AddStudent(this.httpServer, student);

            // Act
            var response = this.httpServer.Get("api/students");

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.IsNotNull(response.Content);
            var responseStudents = GetStudentCollection(response.Content).ToList();
            Assert.AreEqual(1, responseStudents.Count());
            Assert.AreEqual(student.FirstName, responseStudents.First().FirstName);
        }
コード例 #13
0
        public void Add_WhenStudentIsInValid_ShouldAddTheStudent()
        {
            bool isItemAdded = false;
            var repository = Mock.Create<IRepository<Student>>();

            var studentModel = new Student()
            {
                Age = 10,
                Grade = 4,
                FirstName = "Nikolay",
                LastName = "Kostadinov",
            };

            var studentEntity = new Student()
            {
                StudentId = 1,
                Grade = 4,
                FirstName = "Nikolay",
                LastName = "Kostadinov",
            };

            Mock.Arrange(() => repository.Add(Arg.IsAny<Student>()))
                .Throws<ArgumentException>("Grade");

            var controller = new StudentController(repository);
            controller.SetupControllerForTest(HttpMethod.Post, BaseUrl, ControllerName);
            
            var expected = HttpStatusCode.BadRequest;

            //act
            var httpResponse = controller.Post(studentModel);
            var actual = httpResponse.StatusCode;

            ///assert
            Assert.AreEqual(expected, actual);
        }
コード例 #14
0
        public void Update_WhenStudentIsValid_ShouldUpdateTheStudent()
        {
            bool isItemUpdated = false;
            var repository = Mock.Create<IRepository<Student>>();

            var studentModel = new Student()
            {
                Age = 10,
                Grade = 4,
                FirstName = "Nikolay",
                LastName = "Kostadinov",
            };

            Mock.Arrange(() => repository.Update(Arg.AnyInt, Arg.IsAny<Student>()))
                .DoInstead(() => { isItemUpdated = true; });

            var controller = new StudentController(repository);
            controller.SetupControllerForTest(HttpMethod.Put, BaseUrl, ControllerName);

            //act
            var httpResponse = controller.Put(1, studentModel);

            var actual = httpResponse.StatusCode;
            var expected = HttpStatusCode.Accepted;

            Assert.IsTrue(isItemUpdated);
            Assert.AreEqual(expected, actual);
        }
コード例 #15
0
        public void StudentControllerGetAllWithMarksGreaterThan5RecordTest()
        {
            //arrange
            var mockRepository = Mock.Create<IRepository<Student>>();
            
            var studentToAdd2 = new Student()
            {
                Age = 10,
                Grade = 4,
                FirstName = "Pencho",
                LastName = "Penchev",
                Marks = new List<Mark>
                {
                    new Mark
                    {
                        Subject = "Math",
                        Value = 4,
                    }
                },
            };

            IList<Student> studentEntities = new List<Student>();
            studentEntities.Add(studentModel);
            studentEntities.Add(studentToAdd2);

            Mock.Arrange(() => mockRepository.All())
                .Returns(() => studentEntities.AsQueryable());

            var studentController = new StudentController(mockRepository);
            var studentModels = studentEntities.AsQueryable().Select(StudentDetailedModel.FormStudent).First();
            var expected = studentModels;
            
            //act
            var result = studentController.Get("Math", 5);
            var actual = result.First();

            // assert
            Assert.IsTrue(result.Count() == 1);
            Assert.AreEqual(expected.FirstName, actual.FirstName);
        }
コード例 #16
0
        public void UpdateStudentTest()
        {
            bool isItemUpdated = false;
            var repository = Mock.Create<IRepository<Student>>();

            var studentModel = new StudentModel()
            {
                FirstName = "Petko"
            };
            var studentEntity = new Student()
            {
                StudentId = 1,
                FirstName = studentModel.FirstName
            };

            var controller = new StudentsController(repository);
            SetupController(controller);
            controller.PostStudent(studentModel);

            Mock.Arrange(() => repository.Update(Arg.IsAny<int>(), Arg.IsAny<Student>()))
              .DoInstead(() => isItemUpdated = true)
              .Returns(studentEntity);

            var newStudent = new Student()
            {
                StudentId = 1,
                FirstName = "New first name"
            };

            controller.Put(1, newStudent);

            Assert.IsTrue(isItemUpdated);
        }
コード例 #17
0
        public void GetAllStudentsBySubjectAndValue_WhenNotMatchAnyStudent_ShouldReturnEmptyCollection()
        {
            // Arrange
            string subject = "Math";
            decimal value = 6.00m;

            var student1 = new Student
            {
                FirstName = "Gosho",
                LastName = "Petkov",
                Age = 14,
                Grade = 8,
                Marks = new HashSet<Mark> { new Mark { Subject = "Math", Value = 5.5m } }
            };

            var student2 = new Student
            {
                FirstName = "Pesho",
                LastName = "Georgiev",
                Age = 15,
                Grade = 9,
                Marks = new HashSet<Mark> { new Mark { Subject = "Math", Value = 5.0m } }
            };

            var student3 = new Student
            {
                FirstName = "Ivan",
                LastName = "Ivanov",
                Age = 16,
                Grade = 10,
                Marks = new HashSet<Mark> { new Mark { Subject = "Math", Value = 4.5m } }
            };

            student1 = AddStudent(this.httpServer, student1);
            student2 = AddStudent(this.httpServer, student2);
            student3 = AddStudent(this.httpServer, student3);

            // Act
            var response = this.httpServer.Get(string.Format("api/students?subject={0}&value={1}", subject, value));

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.IsNotNull(response.Content);
            var responseStudents = GetStudentCollection(response.Content).ToList();
            Assert.AreEqual(0, responseStudents.Count());
            Assert.IsFalse(responseStudents.Any(s => s.Marks.Any(m => m.Subject != subject && m.Value < value)));
            Assert.IsFalse(responseStudents.Any(s => s.FirstName == student1.FirstName));
            Assert.IsFalse(responseStudents.Any(s => s.FirstName == student2.FirstName));
            Assert.IsFalse(responseStudents.Any(s => s.FirstName == student3.FirstName));
        }
コード例 #18
0
        public void AddingSingleStudentEntryWithSchoolTest()
        {
            //arrange
            const string Subject = "Math";
            const decimal Value = 2.00m;
            const int Age = 10;
            const string FirstName = "Nikolay";
            const string LastName = "Kostadinov";
            const int Grade = 4;
            var mark = new Mark()
            {
                Subject = Subject,
                Value = Value
            };

            var school = new School()
            {
                Name = "Petko Rosen",
                Location = "Meden Rudnik",
            };

            var student = new Student()
            {
                Age = Age,
                FirstName = FirstName,
                LastName = LastName,
                Grade = Grade,
                Marks = new List<Mark>() { mark },
                School = school,
            };

            school.Students.Add(student);

            //act
            dbContext.Set<School>().Add(school);
            dbContext.Set<Student>().Add(student);
            dbContext.SaveChanges();

            //assert
            var studentMarks = student.Marks.ToList();
            var actual =
                student.Age == Age &&
                student.Grade == Grade &&
                student.FirstName == FirstName &&
                student.LastName == LastName &&
                student.Marks.Count == 1 &&
                studentMarks[0].Subject == mark.Subject &&
                studentMarks[0].Value == mark.Value;
            var expected = true;
            Assert.AreEqual(expected, actual);
        }
コード例 #19
0
        public void AddStudent_WhenAddOneNotValidStudent_ShouldReturBadRequestStatusCode()
        {
            // Arrange
            var student = new Student
            {
                FirstName = "Gosho",
            };

            // Act
            var response = this.httpServer.Post("api/students", student);

            // Assert
            Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
        }
コード例 #20
0
        public void AddingSingleStudentEntryWithSchoolThanDeleteStudentTest()
        {
            //arrange
            const string Subject = "Math";
            const decimal Value = 2.00m;
            const int Age = 10;
            const string FirstName = "Nikolay";
            const string LastName = "Kostadinov";
            const int Grade = 4;
            var mark = new Mark()
            {
                Subject = Subject,
                Value = Value
            };

            var school = new School()
            {
                Name = "Petko Rosen",
                Location = "Meden Rudnik",
            };

            var student = new Student()
            {
                Age = Age,
                FirstName = FirstName,
                LastName = LastName,
                Grade = Grade,
                Marks = new List<Mark>() { mark },
                School = school,
            };

            school.Students.Add(student);

            dbContext.Set<School>().Add(school);
            dbContext.Set<Student>().Add(student);
            dbContext.Set<Mark>().Add(mark);
            dbContext.SaveChanges();
            
            //act

            var actual = dbContext.Set<Student>().Where(x => x.StudentId == student.StudentId).FirstOrDefault();

            Assert.AreEqual(student, actual);

            dbContext.Set<Student>().Remove(student);
            dbContext.SaveChanges();
            
            //assert


            actual = dbContext.Set<Student>().Where(x => x.StudentId == student.StudentId).FirstOrDefault();   
            Assert.AreEqual(null, actual);
        }
コード例 #21
0
        public void Add_WhenStudentIsValid_ShouldAddTheStudent()
        {
            bool isItemAdded = false;
            var repository = Mock.Create<IRepository<Student>>();

            var studentModel = new Student()
            {
                Age = 10,
                Grade = 4,
                FirstName = "Nikolay",
                LastName = "Kostadinov",
            };

            var studentEntity = new Student()
            {
                StudentId = 1,
                Age = 10,
                Grade = 4,
                FirstName = "Nikolay",
                LastName = "Kostadinov",
            };

            Mock.Arrange(() => repository.Add(Arg.IsAny<Student>()))
                .DoInstead(() => { isItemAdded = true; })
                .Returns(studentEntity);

            var controller = new StudentController(repository);
            controller.SetupControllerForTest(HttpMethod.Post, BaseUrl, ControllerName);

            //act
            var httpResponse = controller.Post(studentModel);

            var header = httpResponse.Headers.Location.AbsoluteUri;
            var expected = BaseUrl + ControllerName + "/1";
            Assert.IsTrue(isItemAdded);
            Assert.AreEqual(expected, header);
        }
コード例 #22
0
        // PUT api/Students/5
        public HttpResponseMessage Put(int id, Student Student)
        {
            //var studentEntity = db.Get(id);
            this.db.Update(id, Student);

            return Request.CreateResponse(HttpStatusCode.OK);
        }
コード例 #23
0
 private static Student AddStudent(InMemoryHttpServer httpServer, Student student)
 {
     var response = httpServer.Post("api/students", student);
     return GetStudent(response.Content);
 }
コード例 #24
0
        public void TestGetByIdStudent()
        {
            var repository = Mock.Create<IRepository<Student>>();
            var student = new Student()
            {
                StudentId = 1,
                FirstName = "Pesho",
                LastName = "Peshov",
                Grade = 3
            };

            Mock.Arrange<Student>(
                () => repository.Get(student.StudentId))
                .IgnoreArguments()
                .Returns(student)
                .MustBeCalled();

            var controller = new StudentsController(repository);
            SetupController(controller);
            var result = controller.Get(student.StudentId);

            Assert.AreEqual(result.StudentId, student.StudentId);
            Assert.AreEqual(result.FirstName, student.FirstName);
            Assert.AreEqual(result.LastName, student.LastName);
            Assert.AreEqual(result.Grade, student.Grade);
        }
コード例 #25
0
        public void GetStudentById_WhenExistStudentWithThisId_ShouldReturnSingleStudentWithThisId()
        {
            // Arrange
            var student = new Student
            {
                FirstName = "Gosho",
                LastName = "Petkov",
                Age = 15,
                Grade = 9
            };

            var addedStudent = AddStudent(this.httpServer, student);

            // Act
            var response = this.httpServer.Get("api/students/" + addedStudent.Id);

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.IsNotNull(response.Content);
            var responseStudent = GetStudent(response.Content);
            Assert.AreEqual(addedStudent.Id, responseStudent.Id);
        }
コード例 #26
0
        public void AddStudent_WhenAddOneValidStudent_ShouldReturAddedStudent()
        {
            // Arrange
            var student = new Student
            {
                FirstName = "Gosho",
                LastName = "Petkov",
                Age = 15,
                Grade = 9
            };

            // Act
            var response = this.httpServer.Post("api/students", student);

            // Assert
            Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
            Assert.IsNotNull(response.Content);
            var responseStudent = GetStudent(response.Content);
            Assert.AreEqual(student.FirstName, responseStudent.FirstName);
        }
コード例 #27
0
        public void Update_WhenUpdateExistingStudent_ShouldBeModified()
        {
            // Arrange
            var student = new Student { FirstName = "Gosho", LastName = "Petkov", Age = 14, Grade = 8 };
            this.dbContext.Students.Add(student);
            this.dbContext.SaveChanges();

            // Act
            string lastName = "Georgiev";
            student.LastName = lastName;
            var updatedStudentsCount = this.repository.Update(student);

            // Assert
            Assert.IsTrue(updatedStudentsCount == 1);
            Assert.IsTrue(this.dbContext.Students.Any(s => s.LastName == lastName));
        }
コード例 #28
0
        public void StudentControllerGetStudentsBySchoolRecordTest()
        {
            //arrange
            var mockRepository = Mock.Create<IRepository<Student>>();
            var studentToAdd = new Student()
            {
                Age = 10,
                Grade = 4,
                FirstName = "Nikolay",
                LastName = "Kostadinov",
                School = new School()
                {
                    Location = "Lazur",
                    Name = "Wasil aprilov",
                    SchoolId = 1,
                }
            };

            IList<Student> studentEntities = new List<Student>();
            studentEntities.Add(studentToAdd);

            Mock.Arrange(() => mockRepository.All())
                .Returns(() => studentEntities.AsQueryable());

            var studentController = new StudentController(mockRepository);
            //act
            var actual = studentController.GetStudentBySchool(1);

            // assert
            Assert.AreEqual(1, actual.Count());
        }