Skip to content

Commit

Permalink
Refactor tests to accomodate module storage
Browse files Browse the repository at this point in the history
  • Loading branch information
juliantayyc committed Oct 17, 2024
1 parent f78db0f commit 93dfaff
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 24 deletions.
28 changes: 24 additions & 4 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -22,6 +23,7 @@
import seedu.address.model.person.Address;
import seedu.address.model.person.Course;
import seedu.address.model.person.Email;
import seedu.address.model.person.Module;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
Expand Down Expand Up @@ -103,8 +105,11 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript
Address updatedAddress = editPersonDescriptor.getAddress().orElse(personToEdit.getAddress());
Course updatedCourse = editPersonDescriptor.getCourse().orElse(personToEdit.getCourse());
Tag updatedTag = editPersonDescriptor.getTag().orElse(personToEdit.getTag());
return new Person(updatedStudentId, updatedName, updatedPhone, updatedEmail, updatedAddress,
updatedCourse, updatedTag);
ArrayList<Module> updatedModules = editPersonDescriptor.getModules().orElse(personToEdit.getModules());

Person editedPerson = new Person(updatedStudentId, updatedName, updatedPhone, updatedEmail, updatedAddress,
updatedCourse, updatedTag, updatedModules);
return editedPerson;
}

@Override
Expand Down Expand Up @@ -143,6 +148,7 @@ public static class EditPersonDescriptor {
private Address address;
private Course course;
private Tag tag;
private ArrayList<Module> modules;

public EditPersonDescriptor() {}

Expand All @@ -158,13 +164,14 @@ public EditPersonDescriptor(EditPersonDescriptor toCopy) {
setAddress(toCopy.address);
setCourse(toCopy.course);
setTag(toCopy.tag);
setModules(toCopy.modules);
}

/**
* Returns true if at least one field is edited.
*/
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(studentId, name, phone, email, address, course, tag);
return CollectionUtil.isAnyNonNull(studentId, name, phone, email, address, course, tag, modules);
}

public void setStudentId(StudentId studentId) {
Expand Down Expand Up @@ -232,6 +239,17 @@ public Optional<Tag> getTag() {
return Optional.ofNullable(tag);
}

public void setModules(ArrayList<Module> modules) {
this.modules = (modules != null) ? new ArrayList<>(modules) : null;
}
public Optional<ArrayList<Module>> getModules() {
return (modules != null) ? Optional.of(modules) : Optional.empty();
}

public void addModule(Module module) {
this.modules.add(module);
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand All @@ -250,7 +268,8 @@ public boolean equals(Object other) {
&& Objects.equals(email, otherEditPersonDescriptor.email)
&& Objects.equals(address, otherEditPersonDescriptor.address)
&& Objects.equals(course, otherEditPersonDescriptor.course)
&& Objects.equals(tag, otherEditPersonDescriptor.tag);
&& Objects.equals(tag, otherEditPersonDescriptor.tag)
&& Objects.equals(modules, otherEditPersonDescriptor.modules);
}

@Override
Expand All @@ -263,6 +282,7 @@ public String toString() {
.add("address", address)
.add("course", course)
.add("tags", tag)
.add("modules", modules)
.toString();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_COURSE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENTID;
Expand All @@ -30,7 +31,7 @@ public EditCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_STUDENTID, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_ADDRESS, PREFIX_COURSE, PREFIX_TAG);
PREFIX_ADDRESS, PREFIX_COURSE, PREFIX_TAG, PREFIX_MODULE);

StudentId studentId;

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Person {
private final Address address;
private final Course course;
private final Tag tag;
private final ArrayList<Module> modules;
private ArrayList<Module> modules = new ArrayList<>();

/**
* Every field must be present and not null.
Expand All @@ -41,7 +41,6 @@ public Person(StudentId studentId, Name name, Phone phone, Email email, Address
this.address = address;
this.course = course;
this.tag = tag;
this.modules = new ArrayList<>();
}

/**
Expand Down Expand Up @@ -129,7 +128,7 @@ public Person setModuleGrade(Module module, Grade grade) {
* if modification is attempted.
*/
public ArrayList<Module> getModules() {
return modules;
return new ArrayList<>(modules);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/seedu/address/storage/JsonAdaptedModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;

import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.person.Grade;
Expand Down Expand Up @@ -67,4 +66,4 @@ public Module toModelType() throws IllegalValueException {
}
return module;
}
}
}
1 change: 0 additions & 1 deletion src/main/java/seedu/address/storage/JsonAdaptedPerson.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,26 @@
"email" : "[email protected]",
"address" : "123, Jurong West Ave 6, #08-111",
"course": "Math",
"tag" : "Student"
"tag" : "Student",
"modules" : [ {
"module" : "MA1100",
"grade" : "A"
}, {
"module" : "MA2202",
"grade" : null
} ]
}, {
"studentId": "19191919",
"name" : "Benson Meier",
"phone" : "98765432",
"email" : "[email protected]",
"address" : "311, Clementi Ave 2, #02-25",
"course": "Medicine",
"tag" : "Student"
"tag" : "Student",
"modules" : [ {
"module" : "GEC1044",
"grade" : null
} ]
}, {
"studentId": "21212121",
"name" : "Carl Kurz",
Expand All @@ -31,15 +42,29 @@
"email" : "[email protected]",
"address" : "10th street",
"course": "Dentistry",
"tag" : "Student"
"tag" : "Student",
"modules" : [ {
"module" : "DI5100",
"grade" : null
}, {
"module" : "DI5200",
"grade" : null
} ]
}, {
"studentId": "25252525",
"name" : "Elle Meyer",
"phone" : "9482224",
"email" : "[email protected]",
"address" : "michegan ave",
"course": "biomedical engineering",
"tag" : "Student"
"tag" : "Student",
"modules" : [ {
"module" : "BN1111",
"grade" : "D"
}, {
"module" : "BN2102",
"grade" : "C+"
} ]
}, {
"studentId": "98989898",
"name" : "Fiona Kunz",
Expand All @@ -55,6 +80,13 @@
"email" : "[email protected]",
"address" : "4th street",
"course": "Dentistry",
"tag" : "Student"
"tag" : "Student",
"modules" : [ {
"module" : "DI5100",
"grade" : null
}, {
"module" : "DI5200",
"grade" : null
} ]
} ]
}
2 changes: 1 addition & 1 deletion src/test/java/seedu/address/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void saveAddressBook(ReadOnlyAddressBook addressBook, Path filePath)
// Triggers the saveAddressBook method by executing an add command
String addCommand = AddCommand.COMMAND_WORD + STUDENTID_DESC_AMY + NAME_DESC_AMY + PHONE_DESC_AMY
+ EMAIL_DESC_AMY + ADDRESS_DESC_AMY + COURSE_DESC_AMY + TAG_DESC_STUDENT;
Person expectedPerson = new PersonBuilder(AMY).build();
Person expectedPerson = new PersonBuilder(AMY).emptyModuleList().build();
ModelManager expectedModel = new ModelManager();
expectedModel.addPerson(expectedPerson);
assertCommandFailure(addCommand, CommandException.class, expectedMessage, expectedModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public class CommandTestUtil {
.withAddress(VALID_ADDRESS_AMY)
.withCourse(VALID_COURSE_AMY)
.withTag(VALID_TAG_STUDENT)
.addGradedModule(VALID_MODULE_AMY, VALID_GRADE_AMY)
.build();
DESC_BOB = new EditPersonDescriptorBuilder()
.withStudentId(VALID_STUDENTID_BOB)
Expand All @@ -103,6 +104,7 @@ public class CommandTestUtil {
.withAddress(VALID_ADDRESS_BOB)
.withCourse(VALID_COURSE_BOB)
.withTag(VALID_TAG_STUDENT)
.addGradedModule(VALID_MODULE_BOB, VALID_GRADE_BOB)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public void toStringMethod() {
+ editPersonDescriptor.getEmail().orElse(null) + ", address="
+ editPersonDescriptor.getAddress().orElse(null) + ", course="
+ editPersonDescriptor.getCourse().orElse(null) + ", tags="
+ editPersonDescriptor.getTag().orElse(null) + "}";
+ editPersonDescriptor.getTag().orElse(null) + ", modules="
+ editPersonDescriptor.getModules().orElse(null) + "}";
assertEquals(expected, editPersonDescriptor.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class AddCommandParserTest {

@Test
public void parse_allFieldsPresent_success() {
Person expectedPerson = new PersonBuilder(BOB).withTag(VALID_TAG_STUDENT).build();
Person expectedPerson = new PersonBuilder(BOB).withTag(VALID_TAG_STUDENT).emptyModuleList().build();

// whitespace only preamble
assertParseSuccess(parser, PREAMBLE_WHITESPACE + STUDENTID_DESC_BOB + NAME_DESC_BOB
Expand Down Expand Up @@ -139,7 +139,7 @@ public void parse_repeatedNonTagValue_failure() {
@Test
public void parse_optionalFieldsMissing_success() {
// zero tags
Person expectedPerson = new PersonBuilder(AMY).withTag(VALID_TAG_STUDENT).build();
Person expectedPerson = new PersonBuilder(AMY).withTag(VALID_TAG_STUDENT).emptyModuleList().build();
assertParseSuccess(parser, STUDENTID_DESC_AMY + NAME_DESC_AMY + PHONE_DESC_AMY + EMAIL_DESC_AMY
+ ADDRESS_DESC_AMY + COURSE_DESC_AMY, new AddCommand(expectedPerson));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,13 @@ public void parseCommand_delete() throws Exception {
@Test
public void parseCommand_edit() throws Exception {
Person person = new PersonBuilder().build();
EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder(person).build();
EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder(person).setEmptyModuleList().build();
StudentId studentId = person.getStudentId();
EditCommand command = (EditCommand) parser.parseCommand(EditCommand.COMMAND_WORD + " "
+ studentId + " " + PersonUtil.getEditPersonDescriptorDetails(descriptor));

EditCommand expected = new EditCommand(studentId, descriptor);

assertEquals(new EditCommand(studentId, descriptor), command);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Module;
import seedu.address.model.person.Name;
import seedu.address.model.person.Phone;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package seedu.address.testutil;

import java.util.ArrayList;

import seedu.address.logic.commands.EditCommand.EditPersonDescriptor;
import seedu.address.model.person.Address;
import seedu.address.model.person.Course;
import seedu.address.model.person.Email;
import seedu.address.model.person.Grade;
import seedu.address.model.person.Module;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
Expand Down Expand Up @@ -37,6 +41,12 @@ public EditPersonDescriptorBuilder(Person person) {
descriptor.setAddress(person.getAddress());
descriptor.setCourse(person.getCourse());
descriptor.setTag(person.getTag());
descriptor.setModules(person.getModules());
}

public EditPersonDescriptorBuilder setEmptyModuleList() {
descriptor.setModules(null);
return this;
}

/**
Expand Down Expand Up @@ -95,6 +105,33 @@ public EditPersonDescriptorBuilder withTag(String tag) {
descriptor.setTag(new Tag(tag));
return this;
}
/**
* Parses the ungraded {@code module} into a {@code ArrayList<Module>} and
* set it to the {@code EditPersonDescriptor} that we are building.
*/
public EditPersonDescriptorBuilder addUngradedModule(String module) {
Module personModule = new Module(module);
if (descriptor.getModules().isEmpty()) {
descriptor.setModules(new ArrayList<>());
}
descriptor.addModule(personModule);
return this;
}
/**
* Parses the graded {@code module} into a {@code ArrayList<Module>} and set it to the {@code EditPersonDescriptor}
* that we are building.
*/
public EditPersonDescriptorBuilder addGradedModule(String module, String grade) {
Module personModule = new Module(module);
Grade moduleGrade = new Grade(grade);
personModule.setGrade(moduleGrade);
if (descriptor.getModules().isEmpty()) {
descriptor.setModules(new ArrayList<>());
}
descriptor.addModule(personModule);
return this;
}


public EditPersonDescriptor build() {
return descriptor;
Expand Down
Loading

0 comments on commit 93dfaff

Please sign in to comment.