Skip to content

Commit

Permalink
add unit tests so that the coverage is 93% and fixed some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
lumeixu1 committed Sep 12, 2024
1 parent 48f285f commit f1dbc73
Show file tree
Hide file tree
Showing 7 changed files with 319 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ public Course(String instructorName, String courseLocation, String timeSlot, int
* @return true if the student is successfully enrolled, false otherwise.
*/
public boolean enrollStudent() {
enrolledStudentCount++;
if(enrolledStudentCount < enrollmentCapacity) {
enrolledStudentCount++;
return true;
}
return false;
}

Expand All @@ -43,7 +46,10 @@ public boolean enrollStudent() {
* @return true if the student is successfully dropped, false otherwise.
*/
public boolean dropStudent() {
enrolledStudentCount--;
if (enrolledStudentCount > 0){
enrolledStudentCount--;
return true;
}
return false;
}

Expand Down Expand Up @@ -88,7 +94,11 @@ public void setEnrolledStudentCount(int count) {
this.enrolledStudentCount = count;
}


/**
* Check if the course is full or not.
*
* @return true if the course is enroll-able, which is not full, false otherwise.
*/
public boolean isCourseFull() {
return enrollmentCapacity > enrolledStudentCount;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public String toString() {
result.append(deptCode).append(" ").append(key).append(": ").append(value.toString())
.append("\n");
}
return "result.toString()";
return result.toString();
}

@Serial
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public ResponseEntity<?> retrieveDepartment(@RequestParam(value = "deptCode") St
departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping();

if (!departmentMapping.containsKey(deptCode.toUpperCase())) {
return new ResponseEntity<>("Department Not Found", HttpStatus.OK);
return new ResponseEntity<>("Department Not Found", HttpStatus.NOT_FOUND);
} else {
return new ResponseEntity<>(departmentMapping.get(deptCode.toUpperCase()).toString(),
HttpStatus.NOT_FOUND);
HttpStatus.OK);
}

} catch (Exception e) {
Expand Down Expand Up @@ -83,7 +83,7 @@ public ResponseEntity<?> retrieveCourse(@RequestParam(value = "deptCode") String
return new ResponseEntity<>("Course Not Found", HttpStatus.NOT_FOUND);
} else {
return new ResponseEntity<>(coursesMapping.get(Integer.toString(courseCode)).toString(),
HttpStatus.FORBIDDEN);
HttpStatus.OK);
}

}
Expand Down Expand Up @@ -147,10 +147,10 @@ public ResponseEntity<?> getMajorCtFromDept(@RequestParam(value = "deptCode") St
if (doesDepartmentExists) {
HashMap<String, Department> departmentMapping;
departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping();
return new ResponseEntity<>("There are: " + -departmentMapping.get(deptCode)
return new ResponseEntity<>("There are: " + departmentMapping.get(deptCode)
.getNumberOfMajors() + " majors in the department", HttpStatus.OK);
}
return new ResponseEntity<>("Department Not Found", HttpStatus.FORBIDDEN);
return new ResponseEntity<>("Department Not Found", HttpStatus.NOT_FOUND);
} catch (Exception e) {
return handleException(e);
}
Expand Down Expand Up @@ -286,7 +286,7 @@ public ResponseEntity<?> findCourseTime(@RequestParam(value = "deptCode") String
coursesMapping = departmentMapping.get(deptCode).getCourseSelection();

Course requestedCourse = coursesMapping.get(Integer.toString(courseCode));
return new ResponseEntity<>("The course meets at: " + "some time ",
return new ResponseEntity<>("The course meets at: " + requestedCourse.getCourseTimeSlot() + ".",
HttpStatus.OK);
} else {
return new ResponseEntity<>("Course Not Found", HttpStatus.NOT_FOUND);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@ public void toStringTest() {

@Test
public void enrollStudentTest() {
testCourse.setEnrolledStudentCount(10);
boolean result = testCourse.enrollStudent();
assertFalse(result);
assertTrue(result);
}

@Test
public void dropStudentTest() {
testCourse.enrollStudent(); // First, enroll one student to have at least one student
testCourse.setEnrolledStudentCount(10);
boolean result = testCourse.dropStudent();
assertFalse(result);
assertTrue(result);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public void constructorTest() {

@Test
public void toStringTest() {
String expectedResult = "result.toString()";
String expectedResult = "CS CS101: \n" +
"Instructor: Griffin Newbold; Location: 417 IAB; Time: 11:40-12:55\n";
assertEquals(expectedResult, testDepartment.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ public void getDepartmentMappingTest() {

@Test
public void toStringTest() {
String expectedString = "For the CS department: \n"
+ "Instructor: Dr. Smith; Location: ; Time: \n";
String expectedString = "For the CS department: \n"; // no course case
assertEquals(expectedString, database.toString());
}

Expand Down
Loading

0 comments on commit f1dbc73

Please sign in to comment.