Skip to content

Commit d5f27cd

Browse files
make AstSortOrder implement AstNode
1 parent 190c22e commit d5f27cd

File tree

6 files changed

+28
-69
lines changed

6 files changed

+28
-69
lines changed

src/integrationTest/java/com/mongodb/hibernate/query/select/Book.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ class Book {
2727
@Id
2828
int id;
2929

30-
String title;
31-
Boolean outOfStock;
32-
Integer publishYear;
33-
Long isbn13;
34-
Double discount;
35-
BigDecimal price;
30+
// dummy values are set for currently null value is not supported
31+
String title = "";
32+
Boolean outOfStock = false;
33+
Integer publishYear = 0;
34+
Long isbn13 = 0L;
35+
Double discount = 0.0;
36+
BigDecimal price = new BigDecimal("0.0");
3637

3738
Book() {}
3839

@@ -41,16 +42,5 @@ class Book {
4142
this.title = title;
4243
this.publishYear = publishYear;
4344
this.outOfStock = outOfStock;
44-
45-
// the following fields are set dummy values
46-
// for currently null value is not supported
47-
this.isbn13 = 0L;
48-
this.discount = 0.0;
49-
this.price = BigDecimal.valueOf(0);
50-
}
51-
52-
@Override
53-
public String toString() {
54-
return "Book{" + "id=" + id + '}';
5545
}
5646
}

src/integrationTest/java/com/mongodb/hibernate/query/select/SortingIntegrationTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void testOrderBySingleFieldWithoutTies(AstSortOrder sortOrder) {
6262
"from Book as b ORDER BY b.publishYear " + sortOrder,
6363
Book.class,
6464
null,
65-
"{ 'aggregate': 'books', 'pipeline': [ { '$sort': { 'publishYear': " + sortOrder.getRenderedValue()
65+
"{ 'aggregate': 'books', 'pipeline': [ { '$sort': { 'publishYear': " + (sortOrder == ASC ? "1" : "-1")
6666
+ " } }, {'$project': {'_id': true, 'discount': true, 'isbn13': true, 'outOfStock': true, 'price': true, 'publishYear': true, 'title': true} } ] }",
6767
sortOrder == ASC ? getBooksByIds(2, 1, 3, 4, 5) : getBooksByIds(5, 4, 3, 1, 2));
6868
}
@@ -74,7 +74,7 @@ void testOrderBySingleFieldWithTies(AstSortOrder sortOrder) {
7474
"from Book as b ORDER BY b.title " + sortOrder,
7575
Book.class,
7676
null,
77-
"{ 'aggregate': 'books', 'pipeline': [ { '$sort': { 'title': " + sortOrder.getRenderedValue()
77+
"{ 'aggregate': 'books', 'pipeline': [ { '$sort': { 'title': " + (sortOrder == ASC ? "1" : "-1")
7878
+ " } }, {'$project': {'_id': true, 'discount': true, 'isbn13': true, 'outOfStock': true, 'price': true, 'publishYear': true, 'title': true} } ] }",
7979
sortOrder == ASC
8080
? resultList -> assertThat(resultList)
@@ -128,16 +128,16 @@ void testSortFieldByAlias() {
128128
@Test
129129
void testSortFieldByOrdinalReference() {
130130
assertSelectionQuery(
131-
"select b.title as title, b.publishYear as year from Book as b ORDER BY 2 DESC, 1 ASC",
131+
"select b.title as title, b.publishYear as year from Book as b ORDER BY 1 ASC, 2 DESC",
132132
Object[].class,
133133
null,
134134
"{'aggregate': 'books', 'pipeline': [{'$sort': {'publishYear': -1, 'title': 1}}, {'$project': {'title': true, 'publishYear': true}}]}",
135135
List.of(
136-
new Object[] {"War and Peace", 2025},
137-
new Object[] {"The Brothers Karamazov", 1880},
138136
new Object[] {"Anna Karenina", 1877},
139-
new Object[] {"War and Peace", 1869},
140-
new Object[] {"Crime and Punishment", 1866}));
137+
new Object[] {"Crime and Punishment", 1866},
138+
new Object[] {"The Brothers Karamazov", 1880},
139+
new Object[] {"War and Peace", 2025},
140+
new Object[] {"War and Peace", 1869}));
141141
}
142142

143143
@Test

src/main/java/com/mongodb/hibernate/internal/translate/mongoast/command/aggregate/AstSortField.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ public record AstSortField(String path, AstSortOrder order) implements AstNode {
2323
@Override
2424
public void render(BsonWriter writer) {
2525
writer.writeName(path);
26-
writer.writeInt32(order.getRenderedValue());
26+
order.render(writer);
2727
}
2828
}

src/main/java/com/mongodb/hibernate/internal/translate/mongoast/command/aggregate/AstSortOrder.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
package com.mongodb.hibernate.internal.translate.mongoast.command.aggregate;
1818

19-
public enum AstSortOrder {
19+
import com.mongodb.hibernate.internal.translate.mongoast.AstNode;
20+
import org.bson.BsonWriter;
21+
22+
public enum AstSortOrder implements AstNode {
2023
ASC(1),
2124
DESC(-1);
2225

@@ -26,7 +29,8 @@ public enum AstSortOrder {
2629
this.renderedValue = renderedValue;
2730
}
2831

29-
public int getRenderedValue() {
30-
return renderedValue;
32+
@Override
33+
public void render(BsonWriter writer) {
34+
writer.writeInt32(renderedValue);
3135
}
3236
}

src/test/java/com/mongodb/hibernate/internal/translate/mongoast/command/aggregate/AstSortFieldTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.mongodb.hibernate.internal.translate.mongoast.command.aggregate;
1818

1919
import static com.mongodb.hibernate.internal.translate.mongoast.AstNodeAssertions.assertElementRender;
20+
import static com.mongodb.hibernate.internal.translate.mongoast.command.aggregate.AstSortOrder.ASC;
2021

2122
import org.junit.jupiter.params.ParameterizedTest;
2223
import org.junit.jupiter.params.provider.EnumSource;
@@ -26,11 +27,11 @@ class AstSortFieldTests {
2627
@ParameterizedTest
2728
@EnumSource(AstSortOrder.class)
2829
void testRendering(AstSortOrder sortOrder) {
29-
var sortField = new AstSortField("path", sortOrder);
30-
var expectedJson =
31-
"""
32-
{"path": %d}\
33-
""".formatted(sortOrder.getRenderedValue());
30+
var sortField = new AstSortField("field", sortOrder);
31+
var expectedJson = """
32+
{"field": %d}\
33+
"""
34+
.formatted(sortOrder == ASC ? 1 : -1);
3435
assertElementRender(expectedJson, sortField);
3536
}
3637
}

src/test/java/com/mongodb/hibernate/internal/translate/mongoast/command/aggregate/AstSortOrderTests.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)