Skip to content

Commit

Permalink
Sprint3: Add test cases (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam-zzZ authored Nov 10, 2024
1 parent d1fe8fd commit fc82108
Show file tree
Hide file tree
Showing 2 changed files with 254 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,51 @@ public void testAddDestinationWithDuplicateLocation() {

@Test
public void testGetDestinationsWithExistingDestinations() {
mainViewModel.userSignIn("testUser", "testPassword", Assert::assertTrue);
mainViewModel.getDestination(result -> assertTrue(result.containsKey("Paris")));
CountDownLatch latch = new CountDownLatch(1);
mainViewModel.userSignIn("testUser", "testPassword", signInResult -> {
assertTrue(signInResult);
mainViewModel.getDestination(result -> {
assertNotNull(result);
boolean found = result.values().stream().anyMatch(destination ->
"Paris".equals(destination.get("travelLocation"))
);
assertTrue(found);
latch.countDown();
});
});
}

@Test
public void testGetDestinationsWithNoDestinations() {
mainViewModel.userSignIn("testUser", "testPassword", Assert::assertTrue);
mainViewModel.getDestination(Assert::assertNull);
CountDownLatch latch = new CountDownLatch(1);
mainViewModel.userSignUp("testUserNoDest", "testPassword", signUpResult -> {
assertTrue(signUpResult);
mainViewModel.userSignIn("testUserNoDest", "testPassword", signInResult -> {
assertTrue( signInResult);
mainViewModel.getDestination(result -> {
assertNotNull(result);
assertTrue(result.isEmpty());
latch.countDown();
});
});
});
}

@Test
public void testSetVacationWithValidDatesAndDuration() {
mainViewModel.userSignIn("testUser", "testPassword", Assert::assertTrue);
mainViewModel.setVacation("01/01/2023", "01/10/2023", "10", Assert::assertTrue);
mainViewModel.getVacation(result -> {
assertEquals("01/01/2023", result.get("startDate"));
assertEquals("01/10/2023", result.get("endDate"));
assertEquals("10", result.get("duration"));
CountDownLatch latch = new CountDownLatch(1);
mainViewModel.userSignIn("testUser", "testPassword", signInResult -> {
assertTrue(signInResult);
mainViewModel.setVacation("01/01/2023", "01/10/2023", "10", setResult -> {
assertTrue(setResult);
mainViewModel.getVacation(result -> {
assertNotNull(result);
assertEquals("01/01/2023", result.get("startDate"));
assertEquals("01/10/2023", result.get("endDate"));
assertEquals("10", result.get("duration"));
latch.countDown();
});
});
});
}

Expand All @@ -92,22 +119,38 @@ public void testSetVacationWithMismatchedDuration() {

@Test
public void testCalculateOccupiedDaysWithValidVacationAndDestinations() {
mainViewModel.userSignIn("testUser", "testPassword", Assert::assertTrue);
mainViewModel.setVacation("01/01/2023", "01/21/2023", "21", Assert::assertTrue);
mainViewModel.addDestination("Paris", "01/01/2023", "01/11/2023", Assert::assertTrue);
CountDownLatch latch = new CountDownLatch(1);
mainViewModel.userSignIn("testUser", "testPassword", signInResult -> {
assertTrue(signInResult);
mainViewModel.setVacation("01/01/2023", "01/21/2023", "21", setResult -> {
assertTrue(setResult);
mainViewModel.addDestination("Paris", "01/01/2023", "01/11/2023", addResult -> {
assertTrue(addResult);
mainViewModel.calVacation(occupiedDays -> {
assertEquals("11", occupiedDays);
latch.countDown();
});
});
});
});
}

@Test
public void testCalculateOccupiedDaysWithNoOverlap() throws InterruptedException {
public void testCalculateOccupiedDaysWithNoOverlap() {
CountDownLatch latch = new CountDownLatch(1);
mainViewModel.userSignIn("testUser", "testPassword", result -> {
assertTrue(result);
latch.countDown();
mainViewModel.userSignIn("testUser", "testPassword", signInResult -> {
assertTrue(signInResult);
mainViewModel.setVacation("01/01/2023", "01/30/2023", "30", setResult -> {
assertTrue(setResult);
mainViewModel.addDestination("Paris", "02/15/2023", "02/20/2023", addResult -> {
assertTrue(addResult);
mainViewModel.calVacation(occupiedDays -> {
assertEquals("0", occupiedDays);
latch.countDown();
});
});
});
});
assertTrue(latch.await(5, TimeUnit.SECONDS));

mainViewModel.setVacation("01/01/2023", "01/30/2023", "30", Assert::assertTrue);
mainViewModel.addDestination("Paris", "01/15/2023", "01/20/2023", Assert::assertTrue);
}

@Test
Expand All @@ -117,10 +160,20 @@ public void testLogTravelWithEmptyLocation() {

@Test
public void testLogTravelEntryStoredInDatabase() {
mainViewModel.userSignIn("testUser", "testPassword", Assert::assertTrue);
mainViewModel.getDestination(result -> {
assertNotNull(result);
assertTrue(result.containsKey("TestLocation"));
CountDownLatch latch = new CountDownLatch(1);
mainViewModel.userSignIn("testUser", "testPassword", signInResult -> {
assertTrue(signInResult);
mainViewModel.addDestination("TestLocation", "01/01/2023", "01/05/2023", addResult -> {
assertTrue(addResult);
mainViewModel.getDestination(result -> {
assertNotNull(result);
boolean found = result.values().stream().anyMatch(destination ->
"TestLocation".equals(destination.get("travelLocation"))
);
assertTrue(found);
latch.countDown();
});
});
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package com.example.sprint1;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import android.content.Context;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import com.example.sprint1.viewmodel.MainViewModel;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.CountDownLatch;


@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTestSprint3 {
private static final MainViewModel mainViewModel = new MainViewModel();

@Test
public void useAppContext() {
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.example.sprint1", appContext.getPackageName());
}
@Test
public void testUserSignUpWithExistingUsername() {
mainViewModel.userSignUp("sprint3Test", "test", Assert::assertTrue);
}
@Test
public void testUserSignInWithCorrectCredentials() {
mainViewModel.userSignIn("sprint3Test", "test", Assert::assertTrue);
}



// Tianqin Yu
@Test
public void testAddDiningWithEmptyLocation() {
mainViewModel.addDining("", "test.com", "11/10/2023", Assert::assertFalse);
}

// Tianqin Yu
@Test
public void testAddDiningWithEmptyWebsite() {
mainViewModel.addDining("Atlanta", "", "11/10/2023", Assert::assertFalse);
}

// Tianrui Qi
@Test
public void testAddDiningWithEmptyTime() {
mainViewModel.addDining("Atlanta", "test.com", "", Assert::assertFalse);
}

// Tianrui Qi
@Test
public void testAddValidDining(){
CountDownLatch latch = new CountDownLatch(1);
mainViewModel.userSignIn("sprint3Test", "test", signInResult -> {
assertTrue(signInResult);
mainViewModel.addDining("Atlanta", "test.com", "11/10/2023", addResult -> {
assertTrue(addResult);
latch.countDown();
});
});

}

// Yuwen Ding
@Test
public void testGetDiningEntrySaved() {
CountDownLatch latch = new CountDownLatch(1);
mainViewModel.userSignIn("sprint3Test", "test", signInResult -> {
assertTrue(signInResult);
mainViewModel.getDining(result -> {
assertNotNull(result);
boolean found = result.values().stream().anyMatch(dining ->
"Atlanta".equals(dining.get("location")) &&
"test.com".equals(dining.get("website")) &&
"11/10/2023".equals(dining.get("time"))
);
assertTrue(found);
latch.countDown();
});
});
}

// Yuwen Ding
@Test
public void testAddDiningDuplicateEntry() {
CountDownLatch latch = new CountDownLatch(1);
mainViewModel.userSignIn("sprint3Test", "test", signInResult -> {
assertTrue(signInResult);
mainViewModel.addDining("Atlanta", "test.com", "11/10/2023", addResult -> {
assertFalse(addResult);
latch.countDown();
});
});
}

/* Test Cases for Accommodation Reservations */

// Yuxuan Li
@Test
public void testAddAccommodationWithEmptyLocation() {
mainViewModel.addAccommodation("", "101", "Single", "11/10/2023", "11/15/2023", Assert::assertFalse);
}

// Yuxuan Li
@Test
public void testAddAccommodationWithEmptyCheckInDate() {
mainViewModel.addAccommodation("Hotel", "101", "Single", "", "11/15/2023", Assert::assertFalse);
}

// Zishuo Wang
@Test
public void testAddValidAccommodation() {
CountDownLatch latch = new CountDownLatch(1);
mainViewModel.userSignIn("sprint3Test", "test", signInResult -> {
assertTrue(signInResult);
mainViewModel.addAccommodation("Hotel", "101", "Single", "11/10/2023", "11/15/2023", addResult -> {
assertTrue(addResult);
latch.countDown();
});
});


}

// Zishuo Wang
@Test
public void testGetAccommodationEntrySaved() {
CountDownLatch latch = new CountDownLatch(1);
mainViewModel.userSignIn("sprint3Test", "test", signInResult -> {
assertTrue(signInResult);
mainViewModel.getAccommodation(result -> {
assertNotNull(result);
boolean found = result.values().stream().anyMatch(accommodation ->
"Hotel".equals(accommodation.get("location")) &&
"101".equals(accommodation.get("roomNum")) &&
"Single".equals(accommodation.get("roomType")) &&
"11/10/2023".equals(accommodation.get("checkIn")) &&
"11/15/2023".equals(accommodation.get("checkOut"))
);
assertTrue(found);
latch.countDown();
});
});
}

// Yuang Zhang
@Test
public void testAddAccommodationWithInvalidDateRange() {
mainViewModel.addAccommodation("Hotel", "101", "Single", "11/15/2023", "11/10/2023", Assert::assertFalse);
}

// Yuang Zhang
@Test
public void testAddAccommodationDuplicateEntry() {
CountDownLatch latch = new CountDownLatch(1);
mainViewModel.userSignIn("sprint3Test", "test", signInResult -> {
assertTrue(signInResult);
mainViewModel.addAccommodation("Hotel", "101", "Single", "11/10/2023", "11/15/2023", addResult -> {
assertFalse(addResult);
latch.countDown();
});
});
}

}

0 comments on commit fc82108

Please sign in to comment.