Exemplo n.º 1
0
        public static Student Find(int id)
        {
            SqlConnection conn = DB.Connection();
              SqlDataReader rdr = null;
              conn.Open();

              SqlCommand cmd = new SqlCommand("SELECT * FROM students WHERE id = @StudentId;", conn);
              SqlParameter studentIdParameter = new SqlParameter();
              studentIdParameter.ParameterName = "@StudentId";
              studentIdParameter.Value = id.ToString();
              cmd.Parameters.Add(studentIdParameter);
              rdr = cmd.ExecuteReader();

              int foundStudentId = 0;
              string foundStudentName = null;
              DateTime? foundStudentEnrollmentDate = null;

              while(rdr.Read())
              {
            foundStudentName = rdr.GetString(0);
            foundStudentEnrollmentDate = rdr.GetDateTime(1);
            foundStudentId = rdr.GetInt32(2);
              }
              Student foundStudent = new Student(foundStudentName, foundStudentEnrollmentDate, foundStudentId);

              if (rdr != null)
              {
            rdr.Close();
              }
              if (conn != null)
              {
            conn.Close();
              }
              return foundStudent;
        }
Exemplo n.º 2
0
        public void Test_AddMajor_DisplaysAddedMajors()
        {
            Student testStudent = new Student("Bob", enrollmentDate);
              testStudent.Save();
              Department testDepartment = new Department("History");
              testDepartment.Save();

              testStudent.AddMajor(testDepartment.GetId());
              List<Department> resultList = testStudent.GetMajors();
              List<Department> expectedList= new List<Department>{testDepartment};

              Assert.Equal(expectedList, resultList);
        }
Exemplo n.º 3
0
        public void Test_AddCourse_DisplaysAddedCourses()
        {
            Student testStudent2 = new Student("Bob", enrollmentDate);
              testStudent2.Save();
              Course testCourse = new Course("CS101", 1);
              testCourse.Save();

              testStudent2.AddCourse(testCourse.GetId());
              List<Course> resultList = testStudent2.GetCourses();
              List<Course> expectedList= new List<Course>{testCourse};

              Assert.Equal(expectedList, resultList);
        }
Exemplo n.º 4
0
        public void Test_DropCourse_DropsSelectedCourse()
        {
            Student testStudent = new Student("Bob", enrollmentDate);
              testStudent.Save();
              Course testCourse1 = new Course("CS101", 1);
              testCourse1.Save();
              testStudent.AddCourse(testCourse1.GetId());
              Course testCourse2 = new Course("PHIL101", 2);
              testCourse2.Save();
              testStudent.AddCourse(testCourse2.GetId());

              testStudent.DropCourse(testCourse1.GetId());

              List<Course> resultList = testStudent.GetCourses();
              List<Course> expectedList= new List<Course>{testCourse2};

              Assert.Equal(expectedList, resultList);
        }
Exemplo n.º 5
0
        public void Test_DropMajor_DropsSelectedMajor()
        {
            Student testStudent = new Student("Bob", enrollmentDate);
              testStudent.Save();
              Department testDepartment1 = new Department("Chinese");
              testDepartment1.Save();
              testStudent.AddMajor(testDepartment1.GetId());
              Department testDepartment2 = new Department("Spanish");
              testDepartment2.Save();
              testStudent.AddMajor(testDepartment2.GetId());

              testStudent.DropMajor(testDepartment1.GetId());

              List<Department> resultList = testStudent.GetMajors();
              List<Department> expectedList= new List<Department>{testDepartment2};

              Assert.Equal(expectedList, resultList);
        }
Exemplo n.º 6
0
        public void Test_Save_SavesToDatabase()
        {
            //Arrange
              Student testStudent = new Student("Chad", enrollmentDate);

              //Act
              testStudent.Save();
              List<Student> result = Student.GetAll();
              List<Student> testList = new List<Student>{testStudent};

              //Assert
              Assert.Equal(testList, result);
        }
Exemplo n.º 7
0
        public void Test_Save_AssignsIdToObject()
        {
            //Arrange
              Student testStudent = new Student("Chad", enrollmentDate);

              //Act
              testStudent.Save();
              Student savedStudent = Student.GetAll()[0];

              int result = savedStudent.GetId();
              int testId = testStudent.GetId();

              //Assert
              Assert.Equal(testId, result);
        }
Exemplo n.º 8
0
        public void Test_Find_FindsStudentInDatabase()
        {
            //Arrange
              Student testStudent = new Student("Chad", enrollmentDate);
              testStudent.Save();

              //Act
              Student foundStudent = Student.Find(testStudent.GetId());

              //Assert
              Assert.Equal(testStudent, foundStudent);
        }
Exemplo n.º 9
0
        public void Test_Equal_ReturnsTrueIfNamesAndEnrollmentDatesAreTheSame()
        {
            //Arrange, Act
              Student firstStudent = new Student("Chad", enrollmentDate);
              Student secondStudent = new Student("Chad", enrollmentDate);

              //Assert
              Assert.Equal(firstStudent, secondStudent);
        }
Exemplo n.º 10
0
        public static List<Student> GetAll()
        {
            List<Student> allStudents = new List<Student>{};

              SqlConnection conn = DB.Connection();
              SqlDataReader rdr = null;
              conn.Open();

              SqlCommand cmd = new SqlCommand("SELECT * FROM students ORDER BY enrollment_date;", conn);
              rdr = cmd.ExecuteReader();

              while(rdr.Read())
              {
            string studentDescription = rdr.GetString(0);
            DateTime? studentDate = rdr.GetDateTime(1);
            int studentId = rdr.GetInt32(2);
            Student newStudent = new Student(studentDescription, studentDate, studentId);
            allStudents.Add(newStudent);
              }

              if (rdr != null)
              {
            rdr.Close();
              }
              if (conn != null)
              {
            conn.Close();
              }

              return allStudents;
        }