Skip to content

Commit

Permalink
Merge pull request #333 from ayushi0803/J-unit
Browse files Browse the repository at this point in the history
health profile tests
  • Loading branch information
Zackermax authored Nov 11, 2024
2 parents 04dfc5b + 160eb23 commit c1a0082
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 14 deletions.
7 changes: 0 additions & 7 deletions docs/team/Ayushi.md

This file was deleted.

2 changes: 2 additions & 0 deletions docs/team/ayushi0803.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ I contributed to multiple core packages in the FitTrackCLI system, collaborating
with Avjay, enabling users to track their nutrition and hydration.
- Mood Logs: Added mood tracking functionality to the TrainingSession class, allowing users to
log their emotional and physical states during training sessions.
- Unit Testing: Wrote unit tests for the HealthProfile and FitnessGoals components to ensure
- the reliability and accuracy of the system.
- Code Quality: Enhanced code readability and maintainability by adding comments to several key
modules, including:
- Fitness Goal package (AddFitnessGoal, DeleteFitnessGoal, Goal)
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/fittrack/healthprofile/FoodEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import fittrack.storage.Saveable;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;

import static fittrack.storage.Storage.DATA_DELIMITER;
import static fittrack.storage.Storage.DATA_DELIMITER_REGEX;
Expand Down Expand Up @@ -48,15 +48,14 @@ public String getFormattedDateTime() {
}

/**
* Returns the date part of the timestamp, excluding the time.
* Returns the date-time part of the timestamp, truncated to milliseconds.
*
* @return LocalDate representing the date of the food entry.
* @return LocalDateTime representing the date and time of the food entry, truncated to milliseconds.
*/
public LocalDate getLocalDate() {
return dateTime.toLocalDate();
public LocalDateTime getTruncatedDateTime() {
return dateTime.truncatedTo(ChronoUnit.MILLIS);
}


/**
* Provides a string representation of the food entry, including its name, calories,
* and formatted timestamp.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/fittrack/healthprofile/FoodWaterIntake.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void listDailyFoodIntake() {

// Print each food entry if it was added on the current date
for (FoodEntry entry : foodList) {
if (entry.getLocalDate().equals(currentDate)) {
if (entry.getTruncatedDateTime().equals(currentDate)) {
System.out.print(listIndex + ". ");
System.out.println(entry.getFoodName() + " - " + entry.getCalories()
+ " calories, added on " + entry.getFormattedDateTime());
Expand Down
155 changes: 155 additions & 0 deletions src/test/java/fittrack/healthprofile/FoodEntryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package fittrack.healthprofile;

import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.time.LocalDateTime;

public class FoodEntryTest {

@Test
void testFoodEntryConstructor() {
LocalDateTime dateTime = LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS);
FoodEntry foodEntry = new FoodEntry("Apple", 95, dateTime);

assertEquals("Apple", foodEntry.getFoodName());
assertEquals(95, foodEntry.getCalories());
assertEquals(dateTime, foodEntry.getTruncatedDateTime().truncatedTo(ChronoUnit.SECONDS));
}

@Test
void testGetFormattedDateTime() {
LocalDateTime dateTime = LocalDateTime.of(2024, 11, 10, 15, 30);
FoodEntry foodEntry = new FoodEntry("Apple", 95, dateTime);
assertEquals("10/11/2024 15:30", foodEntry.getFormattedDateTime());
}

@Test
void testToSaveString() {
LocalDateTime dateTime = LocalDateTime.of(2024, 11, 10, 15, 30);
FoodEntry foodEntry = new FoodEntry("Apple", 95, dateTime);
assertEquals("Food|Apple|95|10/11/2024 15:30", foodEntry.toSaveString());
}

@Test
void testFromSaveStringValid() {
String saveString = "Food|Apple|95|10/11/2024 15:30";
FoodEntry foodEntry = FoodEntry.fromSaveString(saveString);

assertNotNull(foodEntry);
assertEquals("Apple", foodEntry.getFoodName());
assertEquals(95, foodEntry.getCalories());
}

@Test
void testFromSaveStringInvalidFormat() {
String invalidSaveString = "Invalid|Apple|95|10/11/2024 15:30";
assertThrows(IllegalArgumentException.class, () -> {
FoodEntry.fromSaveString(invalidSaveString);
});
}
}

class WaterEntryTest {

@Test
void testWaterEntryConstructor() {
LocalDateTime dateTime = LocalDateTime.now();
WaterEntry waterEntry = new WaterEntry(250, dateTime);

assertEquals(250, waterEntry.getAmount());
assertEquals(dateTime, waterEntry.getDateTime());
}

@Test
void testGetFormattedDateTime() {
LocalDateTime dateTime = LocalDateTime.of(2024, 11, 10, 15, 30);
WaterEntry waterEntry = new WaterEntry(250, dateTime);
assertEquals("10/11/2024 15:30", waterEntry.getFormattedDateTime());
}

@Test
void testToSaveString() {
LocalDateTime dateTime = LocalDateTime.of(2024, 11, 10, 15, 30);
WaterEntry waterEntry = new WaterEntry(250, dateTime);
assertEquals("Water|250|10/11/2024 15:30", waterEntry.toSaveString());
}

@Test
void testFromSaveStringValid() {
String saveString = "Water|250|10/11/2024 15:30";
WaterEntry waterEntry = WaterEntry.fromSaveString(saveString);

assertNotNull(waterEntry);
assertEquals(250, waterEntry.getAmount());
}

@Test
void testFromSaveStringInvalidFormat() {
String invalidSaveString = "Invalid|250|10/11/2024 15:30";
assertThrows(IllegalArgumentException.class, () -> {
WaterEntry.fromSaveString(invalidSaveString);
});
}
}

class FoodWaterIntakeTest {

@Test
void testAddFood() {
FoodWaterIntake intake = new FoodWaterIntake();
FoodEntry foodEntry = new FoodEntry("Apple", 95, LocalDateTime.now());

intake.addFood(foodEntry);
assertEquals(1, intake.getFoodList().size());
assertEquals(foodEntry, intake.getFoodList().get(0));
}

@Test
void testDeleteFood() {
FoodWaterIntake intake = new FoodWaterIntake();
FoodEntry foodEntry = new FoodEntry("Apple", 95, LocalDateTime.now());
intake.addFood(foodEntry);

intake.deleteFood(0);
assertTrue(intake.getFoodList().isEmpty());
}

@Test
void testDeleteFoodInvalidIndex() {
FoodWaterIntake intake = new FoodWaterIntake();
intake.deleteFood(0); // Expect no exception, just a message
assertTrue(intake.getFoodList().isEmpty());
}

@Test
void testAddWater() {
FoodWaterIntake intake = new FoodWaterIntake();
WaterEntry waterEntry = new WaterEntry(250, LocalDateTime.now());

intake.addWater(waterEntry);
assertEquals(1, intake.getWaterList().size());
assertEquals(waterEntry, intake.getWaterList().get(0));
}

@Test
void testDeleteWater() {
FoodWaterIntake intake = new FoodWaterIntake();
WaterEntry waterEntry = new WaterEntry(250, LocalDateTime.now());
intake.addWater(waterEntry);

intake.deleteWater(0);
assertTrue(intake.getWaterList().isEmpty());
}

@Test
void testDeleteWaterInvalidIndex() {
FoodWaterIntake intake = new FoodWaterIntake();
intake.deleteWater(0); // Expect no exception, just a message
assertTrue(intake.getWaterList().isEmpty());
}
}

0 comments on commit c1a0082

Please sign in to comment.