diff --git a/src/test/java/seedu/address/logic/commands/CommandTestUtil.java b/src/test/java/seedu/address/logic/commands/CommandTestUtil.java index b826ddf2779..cfd40d77e11 100644 --- a/src/test/java/seedu/address/logic/commands/CommandTestUtil.java +++ b/src/test/java/seedu/address/logic/commands/CommandTestUtil.java @@ -33,7 +33,13 @@ public class CommandTestUtil { public static final String VALID_EMAIL_BOB = "bob@example.com"; public static final String VALID_TAG_HUSBAND = "husband"; public static final String VALID_TAG_FRIEND = "friend"; - + public static final String VALID_ASSIGNMENT_NAME_MATH = "Math Homework"; + public static final int VALID_MAX_SCORE_MATH = 100; + public static final int VALID_SCORE_MATH = 100; + public static final String VALID_ASSIGNMENT_NAME_PHYSICS = "Physics Homework"; + public static final int VALID_MAX_SCORE_PHYSICS = 60; + public static final int VALID_SCORE_PHYSICS = 45; + public static final int VALID_SCORE = 0; public static final String NAME_DESC_AMY = " " + PREFIX_NAME + VALID_NAME_AMY; public static final String NAME_DESC_BOB = " " + PREFIX_NAME + VALID_NAME_BOB; public static final String PHONE_DESC_AMY = " " + PREFIX_PHONE + VALID_PHONE_AMY; diff --git a/src/test/java/seedu/address/logic/commands/GradeAssignmentCommandTest.java b/src/test/java/seedu/address/logic/commands/GradeAssignmentCommandTest.java new file mode 100644 index 00000000000..bd6d37f0068 --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/GradeAssignmentCommandTest.java @@ -0,0 +1,124 @@ +package seedu.address.logic.commands; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.logic.commands.CommandTestUtil.VALID_SCORE; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; +import static seedu.address.logic.commands.GradeAssignmentCommand.MESSAGE_GRADE_SUCCESS; +import static seedu.address.model.Model.PREDICATE_SHOW_ALL_STUDENTS; +import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_ASSIGNMENT; +import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_STUDENT; +import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_STUDENT; +import static seedu.address.testutil.TypicalStudents.getTypicalAddressBookWithAssignments; + +import org.junit.jupiter.api.Test; + +import seedu.address.commons.core.index.Index; +import seedu.address.logic.Messages; +import seedu.address.model.Model; +import seedu.address.model.ModelManager; +import seedu.address.model.UserPrefs; +import seedu.address.model.assignment.Assignment; +import seedu.address.model.student.Student; + +public class GradeAssignmentCommandTest { + + private Model model = new ModelManager(getTypicalAddressBookWithAssignments(), new UserPrefs()); + + @Test + public void execute_validIndexScore_success() { + System.out.println(model.getFilteredStudentList()); + Student studentToGrade = model.getFilteredStudentList().get(INDEX_FIRST_STUDENT.getZeroBased()); + Assignment assignmentToGrade = studentToGrade.getAssignmentList().get(INDEX_FIRST_ASSIGNMENT.getZeroBased()); + int score = assignmentToGrade.getMaxScore(); + GradeAssignmentCommand gradeAssignmentCommand = new GradeAssignmentCommand(INDEX_FIRST_STUDENT, + INDEX_FIRST_ASSIGNMENT, score); + + String expectedMessage = String.format(MESSAGE_GRADE_SUCCESS, assignmentToGrade.getName(), + studentToGrade.getName().fullName, assignmentToGrade.getMaxScore()); + + ModelManager expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs()); + expectedModel.updateFilteredStudentList(PREDICATE_SHOW_ALL_STUDENTS);; + + assertCommandSuccess(gradeAssignmentCommand, model, expectedMessage, expectedModel); + } + + @Test + public void execute_invalidStudentIndex_throwsCommandException() { + Index outOfBoundIndex = Index.fromOneBased(model.getFilteredStudentList().size() + 1); + GradeAssignmentCommand gradeAssignmentCommand = new GradeAssignmentCommand(outOfBoundIndex, + INDEX_FIRST_ASSIGNMENT, VALID_SCORE); + + assertCommandFailure(gradeAssignmentCommand, model, Messages.MESSAGE_INVALID_STUDENT_DISPLAYED_INDEX); + } + + @Test + public void execute_invalidAssignmentIndex_throwsCommandException() { + Student studentToGrade = model.getFilteredStudentList().get(INDEX_FIRST_STUDENT.getZeroBased()); + Index outOfBoundIndex = Index.fromOneBased(studentToGrade.getAssignmentList().size() + 1); + GradeAssignmentCommand gradeAssignmentCommand = new GradeAssignmentCommand(INDEX_FIRST_STUDENT, + outOfBoundIndex, VALID_SCORE); + + assertCommandFailure(gradeAssignmentCommand, model, Messages.MESSAGE_INVALID_ASSIGNMENT_INDEX); + } + + @Test + public void execute_invalidScore_throwsCommandException() { + Student studentToGrade = model.getFilteredStudentList().get(INDEX_FIRST_STUDENT.getZeroBased()); + Assignment assignmentToGrade = studentToGrade.getAssignmentList().get(INDEX_FIRST_ASSIGNMENT.getZeroBased()); + int outOfBoundScore = assignmentToGrade.getMaxScore() + 1; + GradeAssignmentCommand gradeAssignmentCommand = new GradeAssignmentCommand(INDEX_FIRST_STUDENT, + INDEX_FIRST_ASSIGNMENT, outOfBoundScore); + + assertCommandFailure(gradeAssignmentCommand, model, Messages.MESSAGE_SCORE_EXCEEDS_MAX_SCORE); + } + + @Test + public void equals() { + GradeAssignmentCommand gradeFirstCommand = new GradeAssignmentCommand(INDEX_FIRST_STUDENT, + INDEX_FIRST_ASSIGNMENT, VALID_SCORE); + GradeAssignmentCommand gradeSecondCommand = new GradeAssignmentCommand(INDEX_SECOND_STUDENT, + INDEX_FIRST_ASSIGNMENT, VALID_SCORE); + + // same object -> returns true + assertTrue(gradeFirstCommand.equals(gradeFirstCommand)); + + // same values -> returns true + GradeAssignmentCommand gradeFirstCommandCopy = new GradeAssignmentCommand(INDEX_FIRST_STUDENT, + INDEX_FIRST_ASSIGNMENT, VALID_SCORE); + assertTrue(gradeFirstCommand.equals(gradeFirstCommandCopy)); + + // different types -> returns false + assertFalse(gradeFirstCommand.equals(1)); + + // null -> returns false + assertFalse(gradeFirstCommand.equals(null)); + + // different student -> returns false + assertFalse(gradeFirstCommand.equals(gradeSecondCommand)); + } + + @Test + public void toStringMethod() { + Index studentIndex = Index.fromOneBased(1); + Index assignmentIndex = Index.fromOneBased(1); + int score = VALID_SCORE; + GradeAssignmentCommand gradeAssignmentCommand = new GradeAssignmentCommand(studentIndex, + assignmentIndex, + score); + String expected = GradeAssignmentCommand.class.getCanonicalName() + "{studentIndex=" + studentIndex + + ", assignmentIndex=" + assignmentIndex + ", score=" + score + "}"; + assertEquals(expected, gradeAssignmentCommand.toString()); + } + + /** + * Updates {@code model}'s filtered list to show no one. + */ + private void showNoStudent(Model model) { + model.updateFilteredStudentList(p -> false); + + assertTrue(model.getFilteredStudentList().isEmpty()); + } +} diff --git a/src/test/java/seedu/address/testutil/StudentBuilder.java b/src/test/java/seedu/address/testutil/StudentBuilder.java index bf3c489a0fd..471491823f3 100644 --- a/src/test/java/seedu/address/testutil/StudentBuilder.java +++ b/src/test/java/seedu/address/testutil/StudentBuilder.java @@ -27,7 +27,7 @@ public class StudentBuilder { private Phone phone; private Email email; private Set tags; - private List assignments = new ArrayList(); + private List assignments; /** * Creates a {@code StudentBuilder} with the default details. @@ -36,6 +36,7 @@ public StudentBuilder() { name = new Name(DEFAULT_NAME); phone = new Phone(DEFAULT_PHONE); email = new Email(DEFAULT_EMAIL); + assignments = new ArrayList<>(); tags = new HashSet<>(); } @@ -94,4 +95,8 @@ public Student build() { return new Student(name, phone, email, tags); } + public Student buildWithAssignment() { + return new Student(name, phone, email, tags, assignments); + } + } diff --git a/src/test/java/seedu/address/testutil/TypicalAssignments.java b/src/test/java/seedu/address/testutil/TypicalAssignments.java new file mode 100644 index 00000000000..6a5bb05f2ea --- /dev/null +++ b/src/test/java/seedu/address/testutil/TypicalAssignments.java @@ -0,0 +1,29 @@ +package seedu.address.testutil; + +import static seedu.address.logic.commands.CommandTestUtil.VALID_ASSIGNMENT_NAME_MATH; +import static seedu.address.logic.commands.CommandTestUtil.VALID_ASSIGNMENT_NAME_PHYSICS; +import static seedu.address.logic.commands.CommandTestUtil.VALID_MAX_SCORE_MATH; +import static seedu.address.logic.commands.CommandTestUtil.VALID_MAX_SCORE_PHYSICS; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import seedu.address.model.assignment.Assignment; + +/** + * A utility class containing a list of {@code Assignment} objects to be used in tests. + */ +public class TypicalAssignments { + + public static final Assignment MATH_HOMEWORK = new AssignmentBuilder() + .withAssignmentName(VALID_ASSIGNMENT_NAME_MATH) + .withMaxScore(VALID_MAX_SCORE_MATH).build(); + public static final Assignment PHYSICS_HOMEWORK = new AssignmentBuilder() + .withAssignmentName(VALID_ASSIGNMENT_NAME_PHYSICS) + .withMaxScore(VALID_MAX_SCORE_PHYSICS).build(); + + public static List getTypicalAssignments() { + return new ArrayList<>(Arrays.asList(MATH_HOMEWORK, PHYSICS_HOMEWORK)); + } +} diff --git a/src/test/java/seedu/address/testutil/TypicalIndexes.java b/src/test/java/seedu/address/testutil/TypicalIndexes.java index 476856975d5..d750e41748a 100644 --- a/src/test/java/seedu/address/testutil/TypicalIndexes.java +++ b/src/test/java/seedu/address/testutil/TypicalIndexes.java @@ -10,5 +10,6 @@ public class TypicalIndexes { public static final Index INDEX_SECOND_STUDENT = Index.fromOneBased(2); public static final Index INDEX_THIRD_STUDENT = Index.fromOneBased(3); public static final Index INDEX_FIRST_ASSIGNMENT = Index.fromOneBased(1); - + public static final Index INDEX_SECOND_ASSIGNMENT = Index.fromOneBased(2); + public static final Index INDEX_THIRD_ASSIGNMENT = Index.fromOneBased(3); } diff --git a/src/test/java/seedu/address/testutil/TypicalStudents.java b/src/test/java/seedu/address/testutil/TypicalStudents.java index fc4213db721..f721f913abb 100644 --- a/src/test/java/seedu/address/testutil/TypicalStudents.java +++ b/src/test/java/seedu/address/testutil/TypicalStudents.java @@ -8,6 +8,7 @@ import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_FRIEND; import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_HUSBAND; +import static seedu.address.testutil.TypicalAssignments.getTypicalAssignments; import java.util.ArrayList; import java.util.Arrays; @@ -24,20 +25,33 @@ public class TypicalStudents { public static final Student ALICE = new StudentBuilder().withName("Alice Pauline") .withEmail("alice@example.com") .withPhone("94351253") - .withTags("friends").build(); + .withTags("friends") + .build(); public static final Student BENSON = new StudentBuilder().withName("Benson Meier") .withEmail("johnd@example.com").withPhone("98765432") - .withTags("owesMoney", "friends").build(); + .withTags("owesMoney", "friends") + .build(); public static final Student CARL = new StudentBuilder().withName("Carl Kurz").withPhone("95352563") - .withEmail("heinz@example.com").build(); + .withEmail("heinz@example.com") + .build(); public static final Student DANIEL = new StudentBuilder().withName("Daniel Meier").withPhone("87652533") - .withEmail("cornelia@example.com").withTags("friends").build(); + .withEmail("cornelia@example.com") + .withTags("friends").build(); public static final Student ELLE = new StudentBuilder().withName("Elle Meyer").withPhone("9482224") - .withEmail("werner@example.com").build(); + .withEmail("werner@example.com") + .build(); public static final Student FIONA = new StudentBuilder().withName("Fiona Kunz").withPhone("9482427") - .withEmail("lydia@example.com").build(); + .withEmail("lydia@example.com") + .build(); public static final Student GEORGE = new StudentBuilder().withName("George Best").withPhone("9482442") - .withEmail("anna@example.com").build(); + .withEmail("anna@example.com") + .build(); + public static final Student JAMES = new StudentBuilder().withName("James Bond").withPhone("94792042") + .withEmail("james@example.com").withAssignments(getTypicalAssignments()) + .buildWithAssignment(); + public static final Student NICOLAS = new StudentBuilder().withName("Nicolas Jackson").withPhone("92841092") + .withEmail("nicolas@example.com").withAssignments(getTypicalAssignments()) + .buildWithAssignment(); // Manually added public static final Student HOON = new StudentBuilder().withName("Hoon Meier").withPhone("8482424") @@ -67,7 +81,19 @@ public static AddressBook getTypicalAddressBook() { return ab; } + public static AddressBook getTypicalAddressBookWithAssignments() { + AddressBook ab = new AddressBook(); + for (Student student : getTypicalStudentsWithAssignments()) { + ab.addStudent(student); + } + return ab; + } + public static List getTypicalStudents() { return new ArrayList<>(Arrays.asList(ALICE, BENSON, CARL, DANIEL, ELLE, FIONA, GEORGE)); } + + public static List getTypicalStudentsWithAssignments() { + return new ArrayList<>(Arrays.asList(JAMES, NICOLAS)); + } }