From fa63efcb248740496b996872f304599759f1a624 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Wed, 31 May 2023 16:10:54 +0200 Subject: [PATCH] Add tests using $slice on dbref field. Closes: #2191 --- .../mongodb/core/MongoTemplateDbRefTests.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateDbRefTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateDbRefTests.java index d0ed01dca..b845221c0 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateDbRefTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateDbRefTests.java @@ -35,6 +35,7 @@ import org.springframework.data.mongodb.core.convert.LazyLoadingTestUtils; import org.springframework.data.mongodb.core.mapping.DBRef; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.MongoId; +import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.test.util.MongoTemplateExtension; import org.springframework.data.mongodb.test.util.MongoTestTemplate; import org.springframework.data.mongodb.test.util.Template; @@ -232,6 +233,53 @@ public class MongoTemplateDbRefTests { assertThat(target.getValue()).containsExactlyInAnyOrder(one, two); } + @Test // GH-2191 + void shouldAllowToSliceCollectionOfDbRefs() { + + JustSomeType one = new JustSomeType(); + one.value = "one"; + + JustSomeType two = new JustSomeType(); + two.value = "two"; + + template.insertAll(Arrays.asList(one, two)); + + WithCollectionDbRef source = new WithCollectionDbRef(); + source.refs = Arrays.asList(one, two); + + template.save(source); + + Query theQuery = query(where("id").is(source.id)); + theQuery.fields().slice("refs", 1, 1); + + WithCollectionDbRef target = template.findOne(theQuery, WithCollectionDbRef.class); + assertThat(target.getRefs()).containsExactly(two); + } + + @Test // GH-2191 + void shouldAllowToSliceCollectionOfLazyDbRefs() { + + JustSomeType one = new JustSomeType(); + one.value = "one"; + + JustSomeType two = new JustSomeType(); + two.value = "two"; + + template.insertAll(Arrays.asList(one, two)); + + WithCollectionDbRef source = new WithCollectionDbRef(); + source.lazyrefs = Arrays.asList(one, two); + + template.save(source); + + Query theQuery = query(where("id").is(source.id)); + theQuery.fields().slice("lazyrefs", 1, 1); + + WithCollectionDbRef target = template.findOne(theQuery, WithCollectionDbRef.class); + LazyLoadingTestUtils.assertProxyIsResolved(target.lazyrefs, false); + assertThat(target.getLazyrefs()).containsExactly(two); + } + @Data @Document("cycle-with-different-type-root") static class RefCycleLoadingIntoDifferentTypeRoot { @@ -264,6 +312,16 @@ public class MongoTemplateDbRefTests { String value; } + @Data + static class WithCollectionDbRef { + + @Id String id; + + @DBRef List refs; + + @DBRef(lazy = true) List lazyrefs; + } + @Data static class WithDBRefOnRawStringId {