Add tests using $slice on dbref field.

Closes: #2191
This commit is contained in:
Christoph Strobl
2023-05-31 16:10:54 +02:00
parent 5ffaa79f4e
commit fa63efcb24

View File

@@ -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<JustSomeType> refs;
@DBRef(lazy = true) List<JustSomeType> lazyrefs;
}
@Data
static class WithDBRefOnRawStringId {