Skip to content

Commit

Permalink
Merge pull request #230 from e1121208/pushing-code-coverage-to-the-limit
Browse files Browse the repository at this point in the history
Pushing code coverage to the limit
  • Loading branch information
e1121208 authored Nov 5, 2024
2 parents 07ab60c + 5a82e82 commit 85a5ec2
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(String.format(MESSAGE_HAS_ACTIVE_LISTINGS, personToDelete.getName()));
}

if (model.hasListingsForBuyer(personToDelete)) {
throw new CommandException(String.format(MESSAGE_HAS_ACTIVE_LISTINGS, personToDelete.getName()));
}

model.deletePerson(personToDelete);
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS,
personToDelete.getName(), personToDelete.getPhone(), personToDelete.getEmail()));
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ public interface Model {
* Checks if the seller has a listing associated with it
*/
boolean hasListingsForSeller(Person seller);
/**
* Checks if the buyer has a listing associated with it
*/
boolean hasListingsForBuyer(Person buyer);


// Returns the listing with the same name as {@code listing} exists in the address book.
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,20 @@ public boolean hasListingsForSeller(Person seller) {
.anyMatch(listing -> listing.getSeller().equals(seller));
}

/**
* Checks if there are any listings associated with the specified {@code buyer}.
*
* @param buyer The seller whose listings are to be checked.
* @return {@code true} if there is at least one listing associated with the buyer;
* {@code false} otherwise.
*/
@Override
public boolean hasListingsForBuyer(Person buyer) {
requireNonNull(buyer);
return listings.getListingList().stream()
.anyMatch(listing -> listing.getBuyers().contains(buyer));
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static seedu.address.logic.commands.CommandTestUtil.showPersonWithName;
import static seedu.address.testutil.TypicalPersons.ALICE;
import static seedu.address.testutil.TypicalPersons.BENSON;
import static seedu.address.testutil.TypicalPersons.DANIEL;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;
import static seedu.address.testutil.TypicalPersons.getTypicalNames;

Expand All @@ -23,6 +24,7 @@
import seedu.address.model.UserPrefs;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.testutil.TypicalListings;

/**
* Contains integration tests (interaction with the Model) and unit tests for
Expand Down Expand Up @@ -91,6 +93,32 @@ public void execute_invalidNameFilteredList_throwsCommandException() {
assertCommandFailure(deleteCommand, model, Messages.MESSAGE_INVALID_PERSON_INPUT);
}

@Test
public void execute_buyerInListing_throwsCommandException() {
Model model =
new ModelManager(getTypicalAddressBook(), new UserPrefs(), TypicalListings.getTypicalListings());
Person personToDelete = DANIEL;
DeleteClientProfileCommand deleteCommand = new DeleteClientProfileCommand(personToDelete.getName());

String expectedMessage = String.format(DeleteClientProfileCommand.MESSAGE_HAS_ACTIVE_LISTINGS,
personToDelete.getName(), personToDelete.getPhone(), personToDelete.getEmail());

assertCommandFailure(deleteCommand, model, expectedMessage);
}

@Test
public void execute_sellerInListing_throwsCommandException() {
Model model =
new ModelManager(getTypicalAddressBook(), new UserPrefs(), TypicalListings.getTypicalListings());
Person personToDelete = ALICE;
DeleteClientProfileCommand deleteCommand = new DeleteClientProfileCommand(personToDelete.getName());

String expectedMessage = String.format(DeleteClientProfileCommand.MESSAGE_HAS_ACTIVE_LISTINGS,
personToDelete.getName(), personToDelete.getPhone(), personToDelete.getEmail());

assertCommandFailure(deleteCommand, model, expectedMessage);
}

@Test
public void equals() {
DeleteClientProfileCommand deleteFirstCommand = new DeleteClientProfileCommand(ALICE.getName());
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/seedu/address/model/ModelStub.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ public boolean hasListingsForSeller(Person seller) {
throw new AssertionError("This method should not be called.");
}

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

@Override
public ObservableList<Listing> getFilteredListingList() {
throw new AssertionError("This method should not be called.");
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/seedu/address/ui/MainWindowUiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package seedu.address.ui;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.testfx.api.FxRobot;
import org.testfx.api.FxToolkit;
import org.testfx.framework.junit5.ApplicationTest;
import org.testfx.util.WaitForAsyncUtils;

import javafx.scene.input.KeyCode;
import seedu.address.MainApp;

class MainWindowUiTest extends ApplicationTest {
private MainApp app;
@BeforeEach
public void setUp() throws Exception {
FxToolkit.registerPrimaryStage();
app = new MainApp();
FxToolkit.setupApplication(() -> app);
FxToolkit.showStage();
WaitForAsyncUtils.waitForFxEvents(20);
}

@Test
void handleHelp_handlesHelpCommandCorrectly() {
FxRobot robot = new FxRobot();
robot.clickOn("#commandTextField");
robot.write("help");
robot.type(KeyCode.ENTER);
assertTrue(robot.lookup("#helpMessage").query().isVisible());
}
}

0 comments on commit 85a5ec2

Please sign in to comment.