Skip to content

Commit

Permalink
Merge pull request #78 from avrilmohh/mark-assignment-tests
Browse files Browse the repository at this point in the history
Create test cases for MarkAssignmentCommand
  • Loading branch information
avrilmohh authored Oct 16, 2024
2 parents 429e68c + 114023b commit 227e96e
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class Assignment {
public Assignment(Student student, AssignmentName name, int maxScore) {
requireNonNull(student);
requireNonNull(name);
this.student = student;
this.assignmentName = name;
checkArgument(isValidScore(maxScore), MESSAGE_CONSTRAINTS);
this.maxScore = maxScore;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package seedu.address.logic.commands;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_ASSIGNMENT;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_STUDENT;

import java.util.ArrayList;

import org.junit.jupiter.api.Test;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.Messages;
import seedu.address.model.AddressBook;
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;
import seedu.address.testutil.AssignmentBuilder;
import seedu.address.testutil.StudentBuilder;


public class MarkAssignmentCommandTest {

private Model model = new ModelManager(new AddressBook(), new UserPrefs());

@Test
public void execute_validStudentAndAssignmentIndex_markSuccessful() {
Assignment assignment = new AssignmentBuilder()
.withAssignmentName("Math Homework")
.withMaxScore(100)
.build();
Student student = new StudentBuilder().withAssignments(new ArrayList<>()).build().addAssignment(assignment);
model.addStudent(student);
MarkAssignmentCommand markAssignmentCommand = new MarkAssignmentCommand(
INDEX_FIRST_STUDENT, INDEX_FIRST_ASSIGNMENT);
String expectedMessage = String.format(MarkAssignmentCommand.MESSAGE_MARK_SUCCESS,
"Math Homework", student.getName());
Model expectedModel = new ModelManager(new AddressBook(), model.getUserPrefs());
Assignment expectedAssignment = new AssignmentBuilder()
.withAssignmentName("Math Homework")
.withMaxScore(100)
.withHasSubmitted(true)
.build();
Student expectedStudent = new StudentBuilder().withAssignments(new ArrayList<>()).build()
.addAssignment(expectedAssignment);
expectedModel.addStudent(expectedStudent);
assertCommandSuccess(markAssignmentCommand, model, expectedMessage, expectedModel);
assertTrue(student.getAssignmentList().get(INDEX_FIRST_ASSIGNMENT.getZeroBased()).getHasSubmitted());
}

@Test
public void execute_invalidStudentIndex_markFailure() {
Assignment assignment = new AssignmentBuilder()
.withAssignmentName("Math Homework")
.withMaxScore(100)
.build();
Student student = new StudentBuilder().withAssignments(new ArrayList<>()).build().addAssignment(assignment);
model.addStudent(student);
MarkAssignmentCommand markAssignmentCommand = new MarkAssignmentCommand(
Index.fromOneBased(2),
INDEX_FIRST_ASSIGNMENT);
String expectedMessage = Messages.MESSAGE_INVALID_STUDENT_DISPLAYED_INDEX;
assertCommandFailure(markAssignmentCommand, model, expectedMessage);
}

@Test
public void execute_invalidAssignmentIndex_markFailure() {
Assignment assignment = new AssignmentBuilder()
.withAssignmentName("Math Homework")
.withMaxScore(100)
.build();
Student student = new StudentBuilder().withAssignments(new ArrayList<>()).build().addAssignment(assignment);
model.addStudent(student);
MarkAssignmentCommand markAssignmentCommand = new MarkAssignmentCommand(
INDEX_FIRST_STUDENT,
Index.fromOneBased(2));
String expectedMessage = Messages.MESSAGE_INVALID_ASSIGNMENT_INDEX;
assertCommandFailure(markAssignmentCommand, model, expectedMessage);
}
}
97 changes: 97 additions & 0 deletions src/test/java/seedu/address/testutil/AssignmentBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package seedu.address.testutil;

import seedu.address.model.assignment.Assignment;
import seedu.address.model.assignment.AssignmentName;
import seedu.address.model.student.Student;

/**
* A utility class to help build Assignment objects for testing purposes.
*/
public class AssignmentBuilder {

public static final Student DEFAULT_STUDENT = new StudentBuilder().build();
public static final String DEFAULT_ASSIGNMENT_NAME = "Math Homework";
public static final int DEFAULT_MAX_SCORE = 100;
public static final int DEFAULT_SCORE = 0;
public static final boolean DEFAULT_SUBMISSION_STATUS = false;

private Student student;
private AssignmentName assignmentName;
private int maxScore;
private int score;
private boolean hasSubmitted;

/**
* Creates an AssignmentBuilder with default values for the Assignment.
*/
public AssignmentBuilder() {
student = DEFAULT_STUDENT;
assignmentName = new AssignmentName(DEFAULT_ASSIGNMENT_NAME);
maxScore = DEFAULT_MAX_SCORE;
score = DEFAULT_SCORE;
hasSubmitted = DEFAULT_SUBMISSION_STATUS;
}

/**
* Creates an AssignmentBuilder with values copied from the specified Assignment.
*
* @param assignmentToCopy the Assignment to copy values from
*/
public AssignmentBuilder(Assignment assignmentToCopy) {
student = assignmentToCopy.getStudent();
assignmentName = new AssignmentName(assignmentToCopy.getName());
maxScore = assignmentToCopy.getMaxScore();
score = assignmentToCopy.getScore();
hasSubmitted = assignmentToCopy.getHasSubmitted();
}

/**
* Sets the {@code student} of the {@code Assignment} that we are building.
*/
public AssignmentBuilder withStudent(Student student) {
this.student = student;
return this;
}

/**
* Sets the {@code AssignmentName} of the {@code Assignment} that we are building.
*/
public AssignmentBuilder withAssignmentName(String assignmentName) {
this.assignmentName = new AssignmentName(assignmentName);
return this;
}

/**
* Sets the {@code maxScore} of the {@code Assignment} that we are building.
*/
public AssignmentBuilder withMaxScore(int maxScore) {
this.maxScore = maxScore;
return this;
}

/**
* Sets the {@code score} of the {@code Assignment} that we are building.
*/
public AssignmentBuilder withScore(int score) {
this.score = score;
return this;
}

/**
* Sets the {@code hasSubmitted} status of the {@code Assignment} that we are building.
*/
public AssignmentBuilder withHasSubmitted(boolean hasSubmitted) {
this.hasSubmitted = hasSubmitted;
return this;
}

/**
* Builds and returns an {@code Assignment}.
*/
public Assignment build() {
Assignment assignment = new Assignment(student, assignmentName, maxScore);
assignment.setScore(this.score);
assignment.setHasSubmitted(this.hasSubmitted);
return assignment;
}
}
2 changes: 2 additions & 0 deletions src/test/java/seedu/address/testutil/TypicalIndexes.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public class TypicalIndexes {
public static final Index INDEX_FIRST_STUDENT = Index.fromOneBased(1);
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);

}

0 comments on commit 227e96e

Please sign in to comment.