Skip to content

Commit

Permalink
Fix Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shishirbychapur committed Oct 16, 2023
1 parent 7f42386 commit bda0ec7
Show file tree
Hide file tree
Showing 21 changed files with 115 additions and 41 deletions.
1 change: 1 addition & 0 deletions src/main/java/seedu/lovebook/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public void stop() {
logger.info("============================ [ Stopping Height Book ] =============================");
try {
storage.saveUserPrefs(model.getUserPrefs());
storage.saveDatePrefs(model.getDatePrefs());
} catch (IOException e) {
logger.severe("Failed to save preferences " + StringUtil.getDetails(e));
}
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/seedu/lovebook/model/DatePrefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,46 @@

import static java.util.Objects.requireNonNull;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.prefs.Preferences;

import seedu.lovebook.commons.util.ToStringBuilder;
import seedu.lovebook.model.person.Age;
import seedu.lovebook.model.person.Gender;
import seedu.lovebook.model.person.Height;
import seedu.lovebook.model.person.Income;

/**
* Represents the preferences of the user for a date.
*/
public class DatePrefs implements ReadOnlyDatePrefs {
private Age age;
private Gender gender;
private Height height;
private Income income;

/**
* Creates a {@code DatePrefs} with default values.
*/
public DatePrefs() {
this.age = new Age("21");
this.gender = new Gender("F");
this.height = new Height("170");
this.income = new Income("10000");
}

/**
* Creates a {@code DatePrefs} with the given values.
*/
public DatePrefs(Age age, Gender gender, Height height, Income income) {
this.age = age;
this.gender = gender;
this.height = height;
this.income = income;
}

/**
* Creates a {@code DatePrefs} with the given values.
*/
public DatePrefs(ReadOnlyDatePrefs toBeCopied) {
this();
resetData(toBeCopied);
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/seedu/lovebook/model/LoveBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public void setPersons(List<Date> dates) {
*/
public void resetData(ReadOnlyLoveBook newData) {
requireNonNull(newData);

setPersons(newData.getPersonList());
}

Expand Down Expand Up @@ -82,7 +81,6 @@ public void addPerson(Date p) {
*/
public void setPerson(Date target, Date editedDate) {
requireNonNull(editedDate);

dates.setPerson(target, editedDate);
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/seedu/lovebook/model/ReadOnlyDatePrefs.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package seedu.lovebook.model;

/**
* Unmodifiable view of date preferences.
*/
public interface ReadOnlyDatePrefs {
DatePrefs getPreferences();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import seedu.lovebook.model.person.Height;
import seedu.lovebook.model.person.Income;

/**
* A utility class containing a sample {@code DatePrefs} to be used in tests.
*/
public class SampleDatePrefUtil {
public static DatePrefs getSamplePreferences() {
Age age = new Age("20");
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/seedu/lovebook/storage/DatePrefsStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import seedu.lovebook.commons.exceptions.DataLoadingException;
import seedu.lovebook.model.ReadOnlyDatePrefs;

/**
* Represents a storage for {@link seedu.lovebook.model.DatePrefs}.
*/
public interface DatePrefsStorage {
/**
* Returns the file path of the data file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import com.fasterxml.jackson.annotation.JsonProperty;

import seedu.lovebook.commons.exceptions.IllegalValueException;
import seedu.lovebook.model.DatePrefs;
import seedu.lovebook.model.person.Age;
import seedu.lovebook.model.person.Date;
import seedu.lovebook.model.person.Gender;
import seedu.lovebook.model.person.Height;
import seedu.lovebook.model.person.Income;
import seedu.lovebook.model.DatePrefs;

/**
* Jackson-friendly version of {@link DatePrefs}.
*/
public class JsonAdaptedDatePrefs {
public static final String MISSING_FIELD_MESSAGE_FORMAT = "Date's %s field is missing!";
private String age;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import seedu.lovebook.commons.util.JsonUtil;
import seedu.lovebook.model.ReadOnlyDatePrefs;

/**
* A class to access DatePrefs data stored as a json file on the hard disk.
*/
public class JsonLoveBookDatePrefs implements DatePrefsStorage {
private static final Logger logger = LogsCenter.getLogger(JsonLoveBookDatePrefs.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
package seedu.lovebook.storage;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
import seedu.lovebook.commons.exceptions.IllegalValueException;

import seedu.lovebook.model.ReadOnlyDatePrefs;
import seedu.lovebook.commons.exceptions.IllegalValueException;
import seedu.lovebook.model.DatePrefs;
import seedu.lovebook.model.ReadOnlyDatePrefs;

/**
* An Immutable DatePrefs that is serializable to JSON format.
*/
@JsonRootName(value = "DatePrefs")
public class JsonSerializableDatePrefs {
private JsonAdaptedDatePrefs jsonDatePrefs;
private DatePrefs datePrefs;

/**
* Constructs a {@code JsonSerializableLoveBook} with the given dates.
* Constructs a {@code JsonSerializableDatePrefs} with the given preferences.
*/
@JsonCreator
public JsonSerializableDatePrefs(@JsonProperty("prefs") JsonAdaptedDatePrefs prefs) {
this.jsonDatePrefs = prefs;
}

/**
* Converts a given {@code ReadOnlyLoveBook} into this class for Jackson use.
* Converts a given {@code ReadyOnlyDatePrefs} into this class for Jackson use.
*
* @param source future changes to this will not affect the created {@code JsonSerializableLoveBook}.
* @param source future changes to this will not affect the created {@code JsonSerializableDatePrefs}.
*/
public JsonSerializableDatePrefs(ReadOnlyDatePrefs source) {
datePrefs = source.getPreferences();
}

/**
* Converts this lovebook book into the model's {@code LoveBook} object.
* Converts these preferences into the model's {@code Preferences} object.
*
* @throws IllegalValueException if there were any data constraints violated.
*/
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/seedu/lovebook/storage/StorageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ public class StorageManager implements Storage {
private DatePrefsStorage datePrefsStorage;

/**
* Creates a {@code StorageManager} with the given {@code LoveBookStorage} and {@code UserPrefStorage}.
* Creates a {@code StorageManager} with the given {@code LoveBookStorage} and {@code UserPrefStorage}
* and {@code DatePrefsStorage}.
*/
public StorageManager(LoveBookStorage loveBookStorage, UserPrefsStorage userPrefsStorage, DatePrefsStorage datePrefsStorage) {
public StorageManager(LoveBookStorage loveBookStorage, UserPrefsStorage userPrefsStorage,
DatePrefsStorage datePrefsStorage) {
this.loveBookStorage = loveBookStorage;
this.userPrefsStorage = userPrefsStorage;
this.datePrefsStorage = datePrefsStorage;
}

/**
* Creates a {@code StorageManager} with the given {@code LoveBookStorage} and {@code UserPrefStorage}.
*/
public StorageManager(LoveBookStorage loveBookStorage, UserPrefsStorage userPrefsStorage) {
this.loveBookStorage = loveBookStorage;
this.userPrefsStorage = userPrefsStorage;
Expand Down Expand Up @@ -86,7 +91,7 @@ public void saveLoveBook(ReadOnlyLoveBook loveBook, Path filePath) throws IOExce
// ================ DatePrefs Methods ==============================
@Override
public Path getDatePrefsFilePath() {
return loveBookStorage.getLoveBookFilePath();
return datePrefsStorage.getDatePrefsFilePath();
}

@Override
Expand All @@ -102,7 +107,7 @@ public Optional<ReadOnlyDatePrefs> readDatePrefs(Path filePath) throws DataLoadi

@Override
public void saveDatePrefs(ReadOnlyDatePrefs loveBook) throws IOException {
saveDatePrefs(loveBook, loveBookStorage.getLoveBookFilePath());
saveDatePrefs(loveBook, getDatePrefsFilePath());
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/seedu/lovebook/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private void assertCommandException(String inputCommand, String expectedMessage)
*/
private void assertCommandFailure(String inputCommand, Class<? extends Throwable> expectedException,
String expectedMessage) {
Model expectedModel = new ModelManager(model.getLoveBook(), new UserPrefs());
Model expectedModel = new ModelManager(model.getLoveBook(), new UserPrefs(), model.getDatePrefs());
assertCommandFailure(inputCommand, expectedException, expectedMessage, expectedModel);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static seedu.lovebook.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.lovebook.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.lovebook.testutil.TypicalDatePrefs.getTypicalDatePrefs;
import static seedu.lovebook.testutil.TypicalPersons.getTypicalLoveBook;

import org.junit.jupiter.api.BeforeEach;
Expand All @@ -23,14 +24,14 @@ public class AddCommandIntegrationTest {

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

@Test
public void execute_newPerson_success() {
Date validDate = new PersonBuilder().build();

Model expectedModel = new ModelManager(model.getLoveBook(), new UserPrefs());
Model expectedModel = new ModelManager(model.getLoveBook(), new UserPrefs(), model.getDatePrefs());
expectedModel.addPerson(validDate);

assertCommandSuccess(new AddCommand(validDate), model,
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/seedu/lovebook/logic/commands/AddCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import seedu.lovebook.commons.core.GuiSettings;
import seedu.lovebook.logic.Messages;
import seedu.lovebook.logic.commands.exceptions.CommandException;
import seedu.lovebook.model.DatePrefs;
import seedu.lovebook.model.LoveBook;
import seedu.lovebook.model.Model;
import seedu.lovebook.model.ReadOnlyDatePrefs;
import seedu.lovebook.model.ReadOnlyLoveBook;
import seedu.lovebook.model.ReadOnlyUserPrefs;
import seedu.lovebook.model.person.Date;
Expand Down Expand Up @@ -162,6 +164,26 @@ public void updateFilteredPersonList(Predicate<Date> predicate) {
public String getWelcomeMessage() {
throw new AssertionError("This method should not be called.");
}

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

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

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

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

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.lovebook.logic.commands;

import static seedu.lovebook.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.lovebook.testutil.TypicalDatePrefs.getTypicalDatePrefs;
import static seedu.lovebook.testutil.TypicalPersons.getTypicalLoveBook;

import org.junit.jupiter.api.Test;
Expand All @@ -22,8 +23,8 @@ public void execute_emptyLoveBook_success() {

@Test
public void execute_nonEmptyLoveBook_success() {
Model model = new ModelManager(getTypicalLoveBook(), new UserPrefs());
Model expectedModel = new ModelManager(getTypicalLoveBook(), new UserPrefs());
Model model = new ModelManager(getTypicalLoveBook(), new UserPrefs(), getTypicalDatePrefs());
Model expectedModel = new ModelManager(getTypicalLoveBook(), new UserPrefs(), getTypicalDatePrefs());
expectedModel.setLoveBook(new LoveBook());

assertCommandSuccess(new ClearCommand(), model, ClearCommand.MESSAGE_SUCCESS, expectedModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static seedu.lovebook.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.lovebook.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.lovebook.logic.commands.CommandTestUtil.showPersonAtIndex;
import static seedu.lovebook.testutil.TypicalDatePrefs.getTypicalDatePrefs;
import static seedu.lovebook.testutil.TypicalIndexes.INDEX_FIRST_PERSON;
import static seedu.lovebook.testutil.TypicalIndexes.INDEX_SECOND_PERSON;
import static seedu.lovebook.testutil.TypicalPersons.getTypicalLoveBook;
Expand All @@ -25,7 +26,7 @@
*/
public class DeleteCommandTest {

private Model model = new ModelManager(getTypicalLoveBook(), new UserPrefs());
private Model model = new ModelManager(getTypicalLoveBook(), new UserPrefs(), getTypicalDatePrefs());

@Test
public void execute_validIndexUnfilteredList_success() {
Expand All @@ -35,7 +36,7 @@ public void execute_validIndexUnfilteredList_success() {
String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_PERSON_SUCCESS,
Messages.format(dateToDelete));

ModelManager expectedModel = new ModelManager(model.getLoveBook(), new UserPrefs());
ModelManager expectedModel = new ModelManager(model.getLoveBook(), new UserPrefs(), model.getDatePrefs());
expectedModel.deletePerson(dateToDelete);

assertCommandSuccess(deleteCommand, model, expectedMessage, expectedModel);
Expand All @@ -59,7 +60,7 @@ public void execute_validIndexFilteredList_success() {
String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_PERSON_SUCCESS,
Messages.format(dateToDelete));

Model expectedModel = new ModelManager(model.getLoveBook(), new UserPrefs());
Model expectedModel = new ModelManager(model.getLoveBook(), new UserPrefs(), model.getDatePrefs());
expectedModel.deletePerson(dateToDelete);
showNoPerson(expectedModel);

Expand Down
Loading

0 comments on commit bda0ec7

Please sign in to comment.