finished tests for Plant, added fix for Plant

This commit is contained in:
Elias Csomor 2022-11-26 11:54:47 +01:00
parent 77541c282c
commit 4e720c2ddc
2 changed files with 100 additions and 3 deletions

View File

@ -66,16 +66,16 @@ public record Plant(
public int timeToHarvest(int group) { public int timeToHarvest(int group) {
List<GrowthPhase> activeLifecycle = lifecycleForGroup(group); List<GrowthPhase> activeLifecycle = lifecycleForGroup(group);
GrowthPhase sow = activeLifecycle.stream() GrowthPhase sow = activeLifecycle.stream()
.filter(growthPhase -> !growthPhase.type().equals(GrowthPhaseType.SOW)) .filter(growthPhase -> growthPhase.type().equals(GrowthPhaseType.SOW))
.findFirst() .findFirst()
.orElseThrow(); .orElseThrow();
GrowthPhase harvest = activeLifecycle.stream() GrowthPhase harvest = activeLifecycle.stream()
.filter(growthPhase -> !growthPhase.type().equals(GrowthPhaseType.HARVEST)) .filter(growthPhase -> growthPhase.type().equals(GrowthPhaseType.HARVEST))
.findFirst() .findFirst()
.orElseThrow(); .orElseThrow();
int currentYear = LocalDate.now().getYear(); int currentYear = LocalDate.now().getYear();
return (int) DAYS.between(harvest.startDate().atYear(currentYear), sow.startDate().atYear(currentYear)); return (int) DAYS.between(sow.startDate().atYear(currentYear),harvest.startDate().atYear(currentYear));
} }
public int lifecycleGroupFromHarvestDate(LocalDate harvestDate) { public int lifecycleGroupFromHarvestDate(LocalDate harvestDate) {

View File

@ -0,0 +1,97 @@
package ch.zhaw.gartenverwaltung.types;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.time.LocalDate;
import java.time.MonthDay;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class PlantTest {
Plant testPlant;
@BeforeEach
void setUp() {
List<GrowthPhase> growthPhases = new ArrayList<>();
growthPhases.add(new GrowthPhase(MonthDay.of(2, 1), MonthDay.of(4, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.SOW, HardinessZone.ZONE_8A, new ArrayList<>()));
growthPhases.add(new GrowthPhase(MonthDay.of(4, 2), MonthDay.of(6, 5), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()));
growthPhases.add(new GrowthPhase(MonthDay.of(6, 3), MonthDay.of(8, 6), 0, new WateringCycle(0, 0, null), GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, new ArrayList<>()));
growthPhases.add(new GrowthPhase(MonthDay.of(3, 1), MonthDay.of(5, 4), 1, new WateringCycle(0, 0, null), GrowthPhaseType.SOW, HardinessZone.ZONE_8A, new ArrayList<>()));
growthPhases.add(new GrowthPhase(MonthDay.of(5, 2), MonthDay.of(7, 5), 1, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()));
growthPhases.add(new GrowthPhase(MonthDay.of(7, 3), MonthDay.of(9, 6), 1, new WateringCycle(0, 0, null), GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, new ArrayList<>()));
growthPhases.add(new GrowthPhase(MonthDay.of(4, 1), MonthDay.of(6, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.SOW, HardinessZone.ZONE_1A, new ArrayList<>()));
growthPhases.add(new GrowthPhase(MonthDay.of(6, 2), MonthDay.of(8, 5), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_1A, new ArrayList<>()));
growthPhases.add(new GrowthPhase(MonthDay.of(7, 2), MonthDay.of(9, 5), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_1A, new ArrayList<>()));
growthPhases.add(new GrowthPhase(MonthDay.of(8, 3), MonthDay.of(10, 6), 0, new WateringCycle(0, 0, null), GrowthPhaseType.HARVEST, HardinessZone.ZONE_1A, new ArrayList<>()));
testPlant = new Plant(
20,
"summertime onion",
"Onion, (Allium cepa), herbaceous biennial plant in the amaryllis family (Amaryllidaceae) grown for its edible bulb. The onion is likely native to southwestern Asia but is now grown throughout the world, chiefly in the temperate zones. Onions are low in nutrients but are valued for their flavour and are used widely in cooking. They add flavour to such dishes as stews, roasts, soups, and salads and are also served as a cooked vegetable.",
null,
"15,30,2",
0,
"sandy to loamy, loose soil, free of stones",
new ArrayList<>(),
growthPhases);
}
@AfterEach
void tearDown() {
}
@Test
void inZone() {
assertEquals(7,testPlant.lifecycleForGroup(0).size());
testPlant.inZone(HardinessZone.ZONE_8A);
assertEquals(3,testPlant.lifecycleForGroup(0).size());
assertEquals(Arrays.asList(3,5,7),testPlant.lifecycleForGroup(1).stream().map(gp ->gp.startDate().getMonthValue()).toList() );
testPlant.inZone(HardinessZone.ZONE_1A);
assertEquals(0,testPlant.lifecycleForGroup(0).size());
}
@Test
void lifecycleForGroup() {
assertEquals(Arrays.asList(2,4,6,4,6,7,8),testPlant.lifecycleForGroup(0).stream().map(gp ->gp.startDate().getMonthValue()).toList() );
assertEquals(Arrays.asList(3,5,7),testPlant.lifecycleForGroup(1).stream().map(gp ->gp.startDate().getMonthValue()).toList() );
}
@Test
void sowDateFromHarvestDate() {
assertEquals(LocalDate.of(2022,8,1),
testPlant.sowDateFromHarvestDate(LocalDate.of(2022,12,1))
);
}
@Test
void timeToHarvest() {
testPlant.inZone(HardinessZone.ZONE_8A);
assertEquals(122,testPlant.timeToHarvest(0));
}
@Test
void lifecycleGroupFromHarvestDate() {
testPlant.inZone(HardinessZone.ZONE_8A);
assertEquals(0,testPlant.lifecycleGroupFromHarvestDate(LocalDate.of(2022,6,30)));
assertEquals(1,testPlant.lifecycleGroupFromHarvestDate(LocalDate.of(2022,8,30)));
}
@Test
void isDateInPhase() {
assertTrue(testPlant.isDateInPhase(LocalDate.of(2022,6,30),GrowthPhaseType.HARVEST));
assertTrue(testPlant.isDateInPhase(LocalDate.of(2022,8,30),GrowthPhaseType.HARVEST));
assertTrue(testPlant.isDateInPhase(LocalDate.of(2022,2,1),GrowthPhaseType.SOW));
assertTrue(testPlant.isDateInPhase(LocalDate.of(2022,4,2),GrowthPhaseType.PLANT));
assertFalse(testPlant.isDateInPhase(LocalDate.of(2022,6,30),GrowthPhaseType.SOW));
}
}