Skip to content

Commit

Permalink
GH-450 - AccountancyRepository now finds custom entries by Interval.
Browse files Browse the repository at this point in the history
  • Loading branch information
odrotbohm committed Nov 12, 2023
1 parent 2f4139a commit 9f510ea
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,8 @@ <T extends AccountancyEntry> Streamable<T> findByDateBetween(LocalDateTime from,
* @param to must not be {@literal null}.
* @return will never be {@literal null}.
*/
default Streamable<AccountancyEntry> findByDateBetween(LocalDateTime from, LocalDateTime to) {

Assert.notNull(from, "Start date must not be null!");
Assert.notNull(to, "End date must not be null!");

return findByDateBetween(from, to, AccountancyEntry.class);
}
@Query("select ae from AccountancyEntry ae where ae.date > :from and ae.date < :to")
Streamable<AccountancyEntry> findByDateBetween(LocalDateTime from, LocalDateTime to);

/**
* Returns all {@link AccountancyEntry}s within the given {@link Interval}.
Expand All @@ -85,7 +80,7 @@ default Streamable<AccountancyEntry> findByDateIn(Interval interval) {

Assert.notNull(interval, "Interval must not be null!");

return findByDateBetween(interval.getStart(), interval.getEnd());
return findByDateIn(interval, AccountancyEntry.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import lombok.RequiredArgsConstructor;

import java.time.LocalDateTime;
import java.util.stream.Stream;

import javax.money.MonetaryAmount;

Expand Down Expand Up @@ -104,12 +105,42 @@ void findsEntriesByConcreteType() {
assertThat(repository.findAll(CustomAccountancyEntry.class)).containsExactly(customEntry);
}

@Test // GH-450
void findsCustomEntryWithinInterval() {

var now = LocalDateTime.now();

var entries = Stream.of(
new AccountancyEntry(Money.of(10, Currencies.EURO)),
new CustomAccountancyEntry(Money.of(10, Currencies.EURO)))
.map(it -> it.setDate(now))
.map(repository::save)
.toList();

var start = now.minusMinutes(1);
var end = now.plusMinutes(1);

assertThat(repository.findByDateBetween(start, end))
.containsExactlyInAnyOrderElementsOf(entries);
assertThat(repository.findByDateIn(Interval.from(start).to(end)))
.containsExactlyInAnyOrderElementsOf(entries);
}

@Entity
@NoArgsConstructor(force = true)
static class CustomAccountancyEntry extends AccountancyEntry {

CustomAccountancyEntry(MonetaryAmount amount) {
super(amount);
}

/*
* (non-Javadoc)
* @see org.salespointframework.accountancy.AccountancyEntry#toString()
*/
@Override
public String toString() {
return "Custom" + super.toString();
}
}
}

0 comments on commit 9f510ea

Please sign in to comment.