Skip to content

Commit fa63efc

Browse files
Add tests using $slice on dbref field.
Closes: #2191
1 parent 5ffaa79 commit fa63efc

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

Diff for: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateDbRefTests.java

+58
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.data.mongodb.core.mapping.DBRef;
3636
import org.springframework.data.mongodb.core.mapping.Document;
3737
import org.springframework.data.mongodb.core.mapping.MongoId;
38+
import org.springframework.data.mongodb.core.query.Query;
3839
import org.springframework.data.mongodb.test.util.MongoTemplateExtension;
3940
import org.springframework.data.mongodb.test.util.MongoTestTemplate;
4041
import org.springframework.data.mongodb.test.util.Template;
@@ -232,6 +233,53 @@ public void shouldResolveLazyListDBRefToAnotherDb() {
232233
assertThat(target.getValue()).containsExactlyInAnyOrder(one, two);
233234
}
234235

236+
@Test // GH-2191
237+
void shouldAllowToSliceCollectionOfDbRefs() {
238+
239+
JustSomeType one = new JustSomeType();
240+
one.value = "one";
241+
242+
JustSomeType two = new JustSomeType();
243+
two.value = "two";
244+
245+
template.insertAll(Arrays.asList(one, two));
246+
247+
WithCollectionDbRef source = new WithCollectionDbRef();
248+
source.refs = Arrays.asList(one, two);
249+
250+
template.save(source);
251+
252+
Query theQuery = query(where("id").is(source.id));
253+
theQuery.fields().slice("refs", 1, 1);
254+
255+
WithCollectionDbRef target = template.findOne(theQuery, WithCollectionDbRef.class);
256+
assertThat(target.getRefs()).containsExactly(two);
257+
}
258+
259+
@Test // GH-2191
260+
void shouldAllowToSliceCollectionOfLazyDbRefs() {
261+
262+
JustSomeType one = new JustSomeType();
263+
one.value = "one";
264+
265+
JustSomeType two = new JustSomeType();
266+
two.value = "two";
267+
268+
template.insertAll(Arrays.asList(one, two));
269+
270+
WithCollectionDbRef source = new WithCollectionDbRef();
271+
source.lazyrefs = Arrays.asList(one, two);
272+
273+
template.save(source);
274+
275+
Query theQuery = query(where("id").is(source.id));
276+
theQuery.fields().slice("lazyrefs", 1, 1);
277+
278+
WithCollectionDbRef target = template.findOne(theQuery, WithCollectionDbRef.class);
279+
LazyLoadingTestUtils.assertProxyIsResolved(target.lazyrefs, false);
280+
assertThat(target.getLazyrefs()).containsExactly(two);
281+
}
282+
235283
@Data
236284
@Document("cycle-with-different-type-root")
237285
static class RefCycleLoadingIntoDifferentTypeRoot {
@@ -264,6 +312,16 @@ static class RawStringId {
264312
String value;
265313
}
266314

315+
@Data
316+
static class WithCollectionDbRef {
317+
318+
@Id String id;
319+
320+
@DBRef List<JustSomeType> refs;
321+
322+
@DBRef(lazy = true) List<JustSomeType> lazyrefs;
323+
}
324+
267325
@Data
268326
static class WithDBRefOnRawStringId {
269327

0 commit comments

Comments
 (0)