Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GradeAssignmentCommand test #92

Merged
merged 5 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ public class CommandTestUtil {
public static final String VALID_EMAIL_BOB = "[email protected]";
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
7 changes: 6 additions & 1 deletion src/test/java/seedu/address/testutil/StudentBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class StudentBuilder {
private Phone phone;
private Email email;
private Set<Tag> tags;
private List<Assignment> assignments = new ArrayList<Assignment>();
private List<Assignment> assignments;

/**
* Creates a {@code StudentBuilder} with the default details.
Expand All @@ -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<>();
}

Expand Down Expand Up @@ -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);
}

}
29 changes: 29 additions & 0 deletions src/test/java/seedu/address/testutil/TypicalAssignments.java
Original file line number Diff line number Diff line change
@@ -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<Assignment> getTypicalAssignments() {
return new ArrayList<>(Arrays.asList(MATH_HOMEWORK, PHYSICS_HOMEWORK));
}
}
3 changes: 2 additions & 1 deletion src/test/java/seedu/address/testutil/TypicalIndexes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
40 changes: 33 additions & 7 deletions src/test/java/seedu/address/testutil/TypicalStudents.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,20 +25,33 @@ public class TypicalStudents {
public static final Student ALICE = new StudentBuilder().withName("Alice Pauline")
.withEmail("[email protected]")
.withPhone("94351253")
.withTags("friends").build();
.withTags("friends")
.build();
public static final Student BENSON = new StudentBuilder().withName("Benson Meier")
.withEmail("[email protected]").withPhone("98765432")
.withTags("owesMoney", "friends").build();
.withTags("owesMoney", "friends")
.build();
public static final Student CARL = new StudentBuilder().withName("Carl Kurz").withPhone("95352563")
.withEmail("[email protected]").build();
.withEmail("[email protected]")
.build();
public static final Student DANIEL = new StudentBuilder().withName("Daniel Meier").withPhone("87652533")
.withEmail("[email protected]").withTags("friends").build();
.withEmail("[email protected]")
.withTags("friends").build();
public static final Student ELLE = new StudentBuilder().withName("Elle Meyer").withPhone("9482224")
.withEmail("[email protected]").build();
.withEmail("[email protected]")
.build();
public static final Student FIONA = new StudentBuilder().withName("Fiona Kunz").withPhone("9482427")
.withEmail("[email protected]").build();
.withEmail("[email protected]")
.build();
public static final Student GEORGE = new StudentBuilder().withName("George Best").withPhone("9482442")
.withEmail("[email protected]").build();
.withEmail("[email protected]")
.build();
public static final Student JAMES = new StudentBuilder().withName("James Bond").withPhone("94792042")
.withEmail("[email protected]").withAssignments(getTypicalAssignments())
.buildWithAssignment();
public static final Student NICOLAS = new StudentBuilder().withName("Nicolas Jackson").withPhone("92841092")
.withEmail("[email protected]").withAssignments(getTypicalAssignments())
.buildWithAssignment();

// Manually added
public static final Student HOON = new StudentBuilder().withName("Hoon Meier").withPhone("8482424")
Expand Down Expand Up @@ -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<Student> getTypicalStudents() {
return new ArrayList<>(Arrays.asList(ALICE, BENSON, CARL, DANIEL, ELLE, FIONA, GEORGE));
}

public static List<Student> getTypicalStudentsWithAssignments() {
return new ArrayList<>(Arrays.asList(JAMES, NICOLAS));
}
}