Add data-mongo test for lazy loading collection like DBRef.

See: spring-projects/spring-data-mongodb#4351
See gh-176
This commit is contained in:
Christoph Strobl
2023-03-31 11:43:04 +02:00
committed by Andy Wilkinson
parent c3f8660e45
commit 99f416cffc
3 changed files with 22 additions and 0 deletions

View File

@@ -47,6 +47,14 @@ class DataMongoDbApplicationAotTests {
});
}
@Test
void lazyDbRef(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
assertThat(output).hasLineMatching(
".*List\\(\\d\\$LazyLoadingProxy\\)\\{\\W?com\\.example\\.data\\.mongodb\\.Coupon@.*\\W?\\}.*");
});
}
@Test
void queryByExample(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {

View File

@@ -259,12 +259,15 @@ class CLR implements CommandLineRunner {
order.setCoupon(coupon);
order.setReduction(coupon);
order.setSimpleRef(coupon);
order.setAllCoupons(List.of(coupon));
orderRepository.save(order);
Optional<Order> loaded = orderRepository.findById(order.getId());
log("simple ref (no proxy): %s", loaded.get().getSimpleRef().getCode());
log("lazyLoading (aot): %s", loaded.get().getCoupon().getCode());
log("lazyLoading (jdk): %s", loaded.get().getReduction().getId());
log("lazyLoading - lists (jdk): List(%s){ %s }", loaded.get().getAllCoupons(),
loaded.get().getAllCoupons().get(0));
log("-----------------\n\n\n");
}

View File

@@ -59,6 +59,9 @@ public class Order {
@DBRef(lazy = true) // JdkProxy
private PriceReduction reduction;
@DBRef(lazy = true) // JdkProxy
private List<Coupon> allCoupons;
@DocumentReference
private Discount documentRef;
@@ -185,6 +188,14 @@ public class Order {
this.lazyDocumentRef = lazyDocumentRef;
}
public void setAllCoupons(List<Coupon> allCoupons) {
this.allCoupons = allCoupons;
}
public List<Coupon> getAllCoupons() {
return allCoupons;
}
@Override
public String toString() {
return "Order{" + "id='" + id + '\'' + ", customerId='" + customerId + '\'' + ", orderDate=" + orderDate