Skip to content

Commit

Permalink
Merge pull request #69 from kohkakohla/fixTests
Browse files Browse the repository at this point in the history
Add addTuteeCommand, Tutor, Tutee tests
  • Loading branch information
estellelim authored Oct 21, 2024
2 parents a1314b7 + f230725 commit 54263f1
Show file tree
Hide file tree
Showing 10 changed files with 547 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/person/Tutee.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public boolean equals(Object other) {
*/
@Override
public String toString() {
return "Tutee: " + super.toString();
return super.toString();
}

}
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/person/Tutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public boolean equals(Object other) {
*/
@Override
public String toString() {
return "Tutor: " + super.toString();
return super.toString();
}

}
9 changes: 3 additions & 6 deletions src/main/java/seedu/address/storage/JsonAdaptedPerson.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ public Person toModelType() throws IllegalValueException {
List<JsonAdaptedTag> tags = this.getTags();
String role = this.getRole();

final List<Tag> tuteeTags = new ArrayList<>();
final List<Tag> personTags = new ArrayList<>();
for (JsonAdaptedTag tag : tags) {
tuteeTags.add(tag.toModelType());
personTags.add(tag.toModelType());
}

final List<Subject> personSubjects = new ArrayList<>();
Expand Down Expand Up @@ -217,13 +217,10 @@ public Person toModelType() throws IllegalValueException {
}
final Hours modelHours = new Hours(hours);

//final Set<Tag> modelTags = new HashSet<>(personTags);
final Set<Subject> modelSubjects = new HashSet<>(personSubjects);

//return new Person(modelName, modelPhone, modelEmail, modelAddress, modelHours, modelTags, modelSubjects);
final Set<Tag> modelTags = new HashSet<>(personTags);


final Set<Tag> modelTags = new HashSet<>(tuteeTags);
if (Objects.equals(role, "Tutor")) {
return new Tutor(modelName, modelPhone, modelEmail, modelAddress, modelHours, modelTags, modelSubjects);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package seedu.address.logic.commands;

import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import seedu.address.logic.CommandHistory;
import seedu.address.logic.Messages;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.person.Tutee;
import seedu.address.testutil.TuteeBuilder;

/**
* Contains integration tests (interaction with the Model) for {@code AddTuteeCommand}.
*/
public class AddTuteeCommandIntegrationTest {

private Model model;
private CommandHistory commandHistory = new CommandHistory();


@BeforeEach
public void setUp() {
model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
}

@Test
public void execute_newPerson_success() {
Tutee validTutee = new TuteeBuilder().build();

Model expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs());
expectedModel.addPerson(validTutee);
expectedModel.commitAddressBook();

assertCommandSuccess(new AddTuteeCommand(validTutee), model, commandHistory,
String.format(AddTuteeCommand.MESSAGE_SUCCESS, Messages.format(validTutee)),
expectedModel);
}

@Test
public void execute_duplicatePerson_throwsCommandException() {
Tutee tuteeInList = (Tutee) model.getAddressBook().getPersonList().get(4);
assertCommandFailure(new AddTuteeCommand(tuteeInList), model, commandHistory,
AddTuteeCommand.MESSAGE_DUPLICATE_PERSON);
}

}
242 changes: 242 additions & 0 deletions src/test/java/seedu/address/logic/commands/AddTuteeCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
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.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalPersons.DANIEL;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Predicate;

import org.junit.jupiter.api.Test;

import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.logic.CommandHistory;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.AddressBook;
import seedu.address.model.Model;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.ReadOnlyUserPrefs;
import seedu.address.model.person.Person;
import seedu.address.model.person.Tutee;
import seedu.address.testutil.TuteeBuilder;

public class AddTuteeCommandTest {

private static final CommandHistory EMPTY_COMMAND_HISTORY = new CommandHistory();

private CommandHistory commandHistory = new CommandHistory();

@Test
public void constructor_nullPerson_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> new AddTuteeCommand(null));
}

@Test
public void execute_personAcceptedByModel_addSuccessful() throws Exception {
ModelStubAcceptingPersonAdded modelStub = new ModelStubAcceptingPersonAdded();
Tutee validTutee = new TuteeBuilder().build();

CommandResult commandResult = new AddTuteeCommand(validTutee).execute(modelStub, commandHistory);

assertEquals(String.format(AddTuteeCommand.MESSAGE_SUCCESS, Messages.format(validTutee)),
commandResult.getFeedbackToUser());
assertEquals(Arrays.asList(validTutee), modelStub.personsAdded);
assertEquals(EMPTY_COMMAND_HISTORY, commandHistory);
}

@Test
public void execute_duplicatePerson_throwsCommandException() {
Tutee validTutee = new TuteeBuilder().build();
AddTuteeCommand addTuteeCommand = new AddTuteeCommand(validTutee);
ModelStub modelStub = new ModelStubWithPerson(validTutee);

assertThrows(CommandException.class, AddTuteeCommand.MESSAGE_DUPLICATE_PERSON, () ->
addTuteeCommand.execute(modelStub, commandHistory));
}

@Test
public void equals() {
Tutee alice = new TuteeBuilder().withName("Alice").build();
Tutee bob = new TuteeBuilder().withName("Bob").build();
AddTuteeCommand addAliceCommand = new AddTuteeCommand(alice);
AddTuteeCommand addBobCommand = new AddTuteeCommand(bob);

// same object -> returns true
assertTrue(addAliceCommand.equals(addAliceCommand));

// same values -> returns true
AddTuteeCommand addAliceCommandCopy = new AddTuteeCommand(alice);
assertTrue(addAliceCommand.equals(addAliceCommandCopy));

// different types -> returns false
assertFalse(addAliceCommand.equals(1));

// null -> returns false
assertFalse(addAliceCommand.equals(null));

// different person -> returns false
assertFalse(addAliceCommand.equals(addBobCommand));
}

@Test
public void toStringMethod() {
AddTuteeCommand addTuteeCommand = new AddTuteeCommand(DANIEL);
String expected = AddTuteeCommand.class.getCanonicalName() + "{toAddTutee=" + DANIEL + "}";
assertEquals(expected, addTuteeCommand.toString());
}

/**
* A default model stub that have all of the methods failing.
*/
private class ModelStub implements Model {
@Override
public void setUserPrefs(ReadOnlyUserPrefs userPrefs) {
throw new AssertionError("This method should not be called.");
}

@Override
public ReadOnlyUserPrefs getUserPrefs() {
throw new AssertionError("This method should not be called.");
}

@Override
public GuiSettings getGuiSettings() {
throw new AssertionError("This method should not be called.");
}

@Override
public void setGuiSettings(GuiSettings guiSettings) {
throw new AssertionError("This method should not be called.");
}

@Override
public Path getAddressBookFilePath() {
throw new AssertionError("This method should not be called.");
}

@Override
public void setAddressBookFilePath(Path addressBookFilePath) {
throw new AssertionError("This method should not be called.");
}

@Override
public void addPerson(Person person) {
throw new AssertionError("This method should not be called.");
}

@Override
public void setAddressBook(ReadOnlyAddressBook newData) {
throw new AssertionError("This method should not be called.");
}

@Override
public ReadOnlyAddressBook getAddressBook() {
throw new AssertionError("This method should not be called.");
}

@Override
public boolean hasPerson(Person person) {
throw new AssertionError("This method should not be called.");
}

@Override
public void deletePerson(Person target) {
throw new AssertionError("This method should not be called.");
}

@Override
public void setPerson(Person target, Person editedPerson) {
throw new AssertionError("This method should not be called.");
}

@Override
public ObservableList<Person> getFilteredPersonList() {
throw new AssertionError("This method should not be called.");
}

@Override
public void updateFilteredPersonList(Predicate<Person> predicate) {
throw new AssertionError("This method should not be called.");
}

@Override
public boolean canUndoAddressBook() {
throw new AssertionError("This method should not be called.");
}

@Override
public boolean canRedoAddressBook() {
throw new AssertionError("This method should not be called.");
}

@Override
public void undoAddressBook() {
throw new AssertionError("This method should not be called.");
}

@Override
public void redoAddressBook() {
throw new AssertionError("This method should not be called.");
}

@Override
public void commitAddressBook() {
throw new AssertionError("This method should not be called.");
}
}

/**
* A Model stub that contains a single person.
*/
private class ModelStubWithPerson extends ModelStub {
private final Person person;

ModelStubWithPerson(Person person) {
requireNonNull(person);
this.person = person;
}

@Override
public boolean hasPerson(Person person) {
requireNonNull(person);
return this.person.isSamePerson(person);
}
}

/**
* A Model stub that always accept the person being added.
*/
private class ModelStubAcceptingPersonAdded extends ModelStub {
final ArrayList<Person> personsAdded = new ArrayList<>();

@Override
public boolean hasPerson(Person person) {
requireNonNull(person);
return personsAdded.stream().anyMatch(person::isSamePerson);
}

@Override
public void addPerson(Person person) {
requireNonNull(person);
personsAdded.add(person);
}

@Override
public void commitAddressBook() {
// called by {@code AddCommand#execute()}
}

@Override
public ReadOnlyAddressBook getAddressBook() {
return new AddressBook();
}
}

}
13 changes: 13 additions & 0 deletions src/test/java/seedu/address/logic/commands/CommandTestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,22 @@ public class CommandTestUtil {
public static final String VALID_HOURS_BOB = "10";
public static final String VALID_TAG_HUSBAND = "husband";
public static final String VALID_TAG_FRIEND = "friend";
public static final String VALID_NAME_CLARA = "Clara Doe";
public static final String VALID_NAME_DEACON = "Deacon Smith";
public static final String VALID_PHONE_CLARA = "33333333";
public static final String VALID_PHONE_DEACON = "44444444";
public static final String VALID_EMAIL_CLARA = "[email protected]";
public static final String VALID_EMAIL_DEACON = "[email protected]";
public static final String VALID_ADDRESS_CLARA = "Block 456, Clara Street 5";
public static final String VALID_ADDRESS_DEACON = "Block 789, Deacon Avenue 8";
public static final String VALID_HOURS_CLARA = "15";
public static final String VALID_HOURS_DEACON = "25";
public static final String VALID_TAG_COLLEAGUE = "colleague";
public static final String VALID_TAG_MENTOR = "mentor";
public static final String VALID_SUBJECT_MATH = "math";
public static final String VALID_SUBJECT_SCIENCE = "science";


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
8 changes: 3 additions & 5 deletions src/test/java/seedu/address/model/person/PersonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import static seedu.address.testutil.TypicalPersons.ALICE;
import static seedu.address.testutil.TypicalPersons.BOB;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import seedu.address.testutil.PersonBuilder;
Expand Down Expand Up @@ -53,7 +52,7 @@ public void isSamePerson() {
assertFalse(BOB.isSamePerson(editedBob));
}

@Disabled

@Test
public void equals() {
// same values -> returns true
Expand Down Expand Up @@ -97,12 +96,11 @@ public void equals() {
assertFalse(ALICE.equals(editedAlice));
}

@Disabled
@Test
public void toStringMethod() {
String expected = Person.class.getCanonicalName() + "{name=" + ALICE.getName() + ", phone=" + ALICE.getPhone()
String expected = Tutor.class.getCanonicalName() + "{name=" + ALICE.getName() + ", phone=" + ALICE.getPhone()
+ ", email=" + ALICE.getEmail() + ", address=" + ALICE.getAddress() + ", hours=" + ALICE.getHours()
+ ", tags=" + ALICE.getTags() + "}";
+ ", tags=" + ALICE.getTags() + ", subjects=" + ALICE.getSubjects() + "}";
assertEquals(expected, ALICE.toString());
}
}
Loading

0 comments on commit 54263f1

Please sign in to comment.