-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a new sort method for FindOptions that accepts a Meta and a…
… Sort args clean up some javadoc fixes #2342
- Loading branch information
1 parent
5759aed
commit e4a5062
Showing
10 changed files
with
251 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
core/src/test/java/dev/morphia/test/aggregation/expressions/TemplatedTestBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package dev.morphia.test.aggregation.expressions; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import dev.morphia.test.TestBase; | ||
|
||
import org.bson.Document; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import static java.lang.Character.toLowerCase; | ||
import static java.lang.String.format; | ||
import static org.testng.Assert.fail; | ||
|
||
public class TemplatedTestBase extends TestBase { | ||
public final String prefix() { | ||
String root = getClass().getSimpleName().substring(4); | ||
return toLowerCase(root.charAt(0)) + root.substring(1); | ||
} | ||
|
||
protected void loadData(String collection, String resourceName) { | ||
insert(collection, loadJson(format("%s/%s/data.json", prefix(), resourceName))); | ||
} | ||
|
||
protected @NotNull List<Document> loadExpected(String resourceName) { | ||
return loadJson(format("%s/%s/expected.json", prefix(), resourceName)); | ||
} | ||
|
||
protected @NotNull <T> List<T> loadExpected(Class<T> type, String resourceName) { | ||
return loadJson(type, format("%s/%s/expected.json", prefix(), resourceName)); | ||
} | ||
|
||
@NotNull | ||
protected List<Document> loadJson(String name) { | ||
List<Document> data = new ArrayList<>(); | ||
InputStream stream = getClass().getResourceAsStream(name); | ||
if (stream == null) { | ||
fail("missing data file: " + name); | ||
} | ||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) { | ||
while (reader.ready()) { | ||
data.add(Document.parse(reader.readLine())); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e.getMessage(), e); | ||
} | ||
return data; | ||
} | ||
|
||
@NotNull | ||
protected <T> List<T> loadJson(Class<T> type, String name) { | ||
List<T> data = new ArrayList<>(); | ||
InputStream stream = getClass().getResourceAsStream(name); | ||
if (stream == null) { | ||
fail("missing data file: " + name); | ||
} | ||
ObjectMapper mapper = new ObjectMapper(); | ||
|
||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) { | ||
while (reader.ready()) { | ||
data.add(mapper.readValue(reader.readLine(), type)); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e.getMessage(), e); | ||
} | ||
return data; | ||
} | ||
} |
131 changes: 131 additions & 0 deletions
131
core/src/test/java/dev/morphia/test/query/TestSorts.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package dev.morphia.test.query; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import com.mongodb.lang.NonNull; | ||
|
||
import dev.morphia.annotations.Entity; | ||
import dev.morphia.annotations.Id; | ||
import dev.morphia.annotations.Text; | ||
import dev.morphia.mapping.codec.reader.DocumentReader; | ||
import dev.morphia.query.FindOptions; | ||
import dev.morphia.query.Query; | ||
import dev.morphia.test.aggregation.expressions.TemplatedTestBase; | ||
|
||
import org.bson.Document; | ||
import org.bson.codecs.DecoderContext; | ||
import org.testng.annotations.Test; | ||
|
||
import static dev.morphia.query.Meta.textScore; | ||
import static dev.morphia.query.Sort.ascending; | ||
import static dev.morphia.query.filters.Filters.text; | ||
import static java.lang.String.format; | ||
import static java.util.stream.Collectors.*; | ||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertNotNull; | ||
|
||
public class TestSorts extends TemplatedTestBase { | ||
public <D> void testQuery(double serverVersion, String resourceName, boolean orderMatters, Query<D> query, FindOptions options) { | ||
checkMinServerVersion(serverVersion); | ||
|
||
loadData(getDs().getCollection(query.getEntityClass()).getNamespace().getCollectionName(), resourceName); | ||
|
||
List<D> actual = runQuery(resourceName, query, options); | ||
|
||
List<D> expected = map(query.getEntityClass(), loadExpected(resourceName)); | ||
|
||
if (orderMatters) { | ||
assertEquals(actual, expected); | ||
} else { | ||
assertListEquals(actual, expected); | ||
} | ||
} | ||
|
||
private <D> List<D> map(Class<D> entityClass, List<Document> documents) { | ||
var codec = getDs().getCodecRegistry().get(entityClass); | ||
|
||
DecoderContext context = DecoderContext.builder().build(); | ||
return documents.stream() | ||
.map(document -> { | ||
return codec.decode(new DocumentReader(document), context); | ||
}) | ||
.collect(toList()); | ||
} | ||
|
||
@Test | ||
public void metaAndSorts() { | ||
getMapper().map(Article.class); | ||
getDs().ensureIndexes(); | ||
|
||
Query<Article> query = getDs().find(Article.class) | ||
.filter(text("coffee")); | ||
FindOptions options = new FindOptions() | ||
.logQuery() | ||
.sort(textScore("textScore"), | ||
ascending("subject")); | ||
|
||
testQuery(0, "metaAndSorts", true, query, options); | ||
} | ||
|
||
@NonNull | ||
protected <D> List<D> runQuery(@NonNull String queryTemplate, @NonNull Query<D> query, | ||
@NonNull FindOptions options) { | ||
String queryName = format("%s/%s/query.json", prefix(), queryTemplate); | ||
try { | ||
|
||
InputStream stream = getClass().getResourceAsStream(queryName); | ||
assertNotNull(stream, "Could not find query template: " + queryName); | ||
Document expectedQuery; | ||
try (InputStreamReader reader = new InputStreamReader(stream)) { | ||
expectedQuery = Document.parse(new BufferedReader(reader).readLine()); | ||
} | ||
|
||
assertDocumentEquals(query.toDocument(), expectedQuery); | ||
|
||
try (var cursor = query.iterator(options)) { | ||
return cursor.toList(); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e.getMessage(), e); | ||
} | ||
} | ||
|
||
@Entity(useDiscriminator = false) | ||
private static class Article { | ||
@Id | ||
int id; | ||
@Text | ||
String subject; | ||
String author; | ||
int views; | ||
double textScore; | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (!(o instanceof Article)) { | ||
return false; | ||
} | ||
Article article = (Article) o; | ||
return id == article.id && views == article.views && Double.compare(article.textScore, textScore) == 0 && | ||
Objects.equals(subject, article.subject) && Objects.equals(author, article.author); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id, subject, author, views, textScore); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return format("Article{id=%d, subject='%s', author='%s', views=%d, textScore=%s}", id, subject, author, views, textScore); | ||
} | ||
} | ||
} |
Oops, something went wrong.