fix(#20): use inclusive query for rent payment month range

This commit is contained in:
nessar committed 2026-08-17 14:18:15 +02:00
1 parent df17f6e378
commit 479012d661
2 files changed
+22 -11

No files matched your search

@@ -2,12 +2,14 @@ package fr.nessar.lmnporganizer.backend.repository;
import fr.nessar.lmnporganizer.backend.model.RentPayment;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface RentPaymentRepository extends MongoRepository<RentPayment, String> {
@Query("{ 'leaseId': ?0, 'month': { $gte: ?1, $lte: ?2 } }")
List<RentPayment> findAllByLeaseIdAndMonthBetween(String leaseId, String firstMonth, String lastMonth);
List<RentPayment> findAllByOrderByDueDateAsc();
@@ -23,14 +23,10 @@ import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@SpringBootTest(
classes = RentPaymentPersistenceIT.MongoTestApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.NONE,
properties = {
"lmnp.data-initializer.enabled=false",
"spring.data.mongodb.auto-index-creation=true"
}
)
@SpringBootTest(classes = RentPaymentPersistenceIT.MongoTestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.NONE, properties = {
"lmnp.data-initializer.enabled=false",
"spring.data.mongodb.auto-index-creation=true"
})
class RentPaymentPersistenceIT {
private static final MongoDBContainer mongoDb = startMongoDb();
@@ -55,7 +51,8 @@ class RentPaymentPersistenceIT {
void rejectsTwoRentPaymentsForTheSameLeaseAndMonth() {
rentPaymentRepository.save(rentPayment("payment-1", "2026-06", LocalDate.of(2026, 6, 5)));
assertThatThrownBy(() -> rentPaymentRepository.save(rentPayment("payment-2", "2026-06", LocalDate.of(2026, 6, 5))))
assertThatThrownBy(
() -> rentPaymentRepository.save(rentPayment("payment-2", "2026-06", LocalDate.of(2026, 6, 5))))
.isInstanceOf(DuplicateKeyException.class);
}
@@ -64,14 +61,26 @@ class RentPaymentPersistenceIT {
rentPaymentRepository.saveAll(List.of(
rentPayment("payment-3", "2026-03", LocalDate.of(2026, 3, 5)),
rentPayment("payment-1", "2026-01", LocalDate.of(2026, 1, 5)),
rentPayment("payment-2", "2026-02", LocalDate.of(2026, 2, 5))
));
rentPayment("payment-2", "2026-02", LocalDate.of(2026, 2, 5))));
assertThat(rentPaymentRepository.findAllByOrderByDueDateAsc())
.extracting(RentPayment::getMonth)
.containsExactly("2026-01", "2026-02", "2026-03");
}
@Test
void testBetweenBehavior() {
rentPaymentRepository.saveAll(List.of(
rentPayment("payment-1", "2026-01", LocalDate.of(2026, 1, 5)),
rentPayment("payment-2", "2026-02", LocalDate.of(2026, 2, 5)),
rentPayment("payment-3", "2026-03", LocalDate.of(2026, 3, 5))
));
assertThat(rentPaymentRepository.findAllByLeaseIdAndMonthBetween("lease-1", "2026-01", "2026-03"))
.extracting(RentPayment::getMonth)
.containsExactly("2026-01", "2026-02", "2026-03");
}
private static RentPayment rentPayment(String id, String month, LocalDate dueDate) {
return new RentPayment(id, "default-landlord", "lease-1", month, dueDate,
new BigDecimal("850.00"), new BigDecimal("50.00"), null);