Skip to content

Commit 52776b6

Browse files
committed
Test expressions in context
1 parent 56e8756 commit 52776b6

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed

driver-core/src/main/com/mongodb/client/model/expressions/Expressions.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ public static ArrayExpression<StringExpression> ofStringArray(final String... ar
172172
return new MqlExpression<>((cr) -> new AstPlaceholder(new BsonArray(result)));
173173
}
174174

175+
public static <R extends DocumentExpression> R current() {
176+
return new MqlExpression<>((cr) -> new AstPlaceholder(new BsonString("$$CURRENT")))
177+
.assertImplementsAllExpressions();
178+
}
179+
175180
@SafeVarargs // nothing is stored in the array
176181
public static <T extends Expression> ArrayExpression<T> ofArray(final T... array) {
177182
Assertions.notNull("array", array);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.client.model.expressions;
18+
19+
import com.mongodb.MongoClientSettings;
20+
import com.mongodb.client.AggregateIterable;
21+
import com.mongodb.client.MongoClient;
22+
import com.mongodb.client.MongoClients;
23+
import com.mongodb.client.MongoCollection;
24+
import com.mongodb.client.model.Aggregates;
25+
import com.mongodb.client.model.Filters;
26+
import org.bson.Document;
27+
import org.bson.conversions.Bson;
28+
import org.junit.jupiter.api.AfterEach;
29+
import org.junit.jupiter.api.BeforeEach;
30+
import org.junit.jupiter.api.Test;
31+
32+
import java.util.ArrayList;
33+
import java.util.Arrays;
34+
import java.util.List;
35+
36+
import static com.mongodb.client.model.Accumulators.sum;
37+
import static com.mongodb.client.model.Aggregates.match;
38+
import static com.mongodb.client.model.Aggregates.project;
39+
import static com.mongodb.client.model.Projections.computed;
40+
import static com.mongodb.client.model.Projections.excludeId;
41+
import static com.mongodb.client.model.Projections.fields;
42+
import static com.mongodb.client.model.Projections.include;
43+
import static com.mongodb.client.model.Sorts.ascending;
44+
import static com.mongodb.client.model.expressions.Expressions.current;
45+
import static com.mongodb.client.model.expressions.Expressions.of;
46+
import static com.mongodb.client.model.expressions.Expressions.ofArray;
47+
import static org.junit.jupiter.api.Assertions.assertEquals;
48+
49+
class InContextExpressionsFunctionalTest extends AbstractExpressionsFunctionalTest {
50+
51+
private MongoClient client;
52+
private MongoCollection<Document> col;
53+
54+
@BeforeEach
55+
public void setUp() {
56+
client = MongoClients.create();
57+
col = client.getDatabase("testdb").getCollection("testcol");
58+
col.drop();
59+
}
60+
61+
@AfterEach
62+
public void tearDown() {
63+
client.close();
64+
}
65+
66+
private String bsonToString(final Bson project) {
67+
return project.toBsonDocument(Document.class, MongoClientSettings.getDefaultCodecRegistry()).toString().replaceAll("\"", "'");
68+
}
69+
70+
private List<Document> aggregate(final Bson... stages) {
71+
AggregateIterable<Document> result = col.aggregate(Arrays.asList(stages));
72+
List<Document> results = new ArrayList<>();
73+
result.forEach(r -> results.add(r));
74+
return results;
75+
}
76+
77+
@Test
78+
public void matchTest() {
79+
col.insertMany(Arrays.asList(
80+
Document.parse("{_id: 1, x: 0, y: 2}"),
81+
Document.parse("{_id: 2, x: 0, y: 3}"),
82+
Document.parse("{_id: 3, x: 1, y: 3}")));
83+
84+
List<Document> results = aggregate(
85+
match(Filters.expr(current().getInteger("x").eq(of(1)))));
86+
87+
assertEquals(
88+
Arrays.asList(Document.parse("{_id: 3, x: 1, y: 3}")),
89+
results);
90+
}
91+
92+
@Test
93+
public void projectTest() {
94+
col.insertMany(Arrays.asList(
95+
Document.parse("{_id: 1, x: 0, y: 2}")));
96+
97+
List<Document> expected = Arrays.asList(Document.parse("{_id: 1, x: 0, c: 2}"));
98+
99+
// old, using "$y"
100+
Bson projectOld = project(fields(include("x"), computed("c",
101+
"$y")));
102+
assertEquals("{'$project': {'x': 1, 'c': '$y'}}", bsonToString(projectOld));
103+
assertEquals(expected,
104+
aggregate(projectOld));
105+
106+
// new, using current() with add/subtract
107+
Bson projectNew = project(fields(include("x"), computed("c",
108+
current().getInteger("y").add(10).subtract(10))));
109+
assertEquals(
110+
"{'$project': {'x': 1, 'c': "
111+
+ "{'$subtract': [{'$add': [{'$getField': "
112+
+ "{'input': '$$CURRENT', 'field': 'y'}}, 10]}, 10]}}}",
113+
bsonToString(projectNew));
114+
assertEquals(expected,
115+
aggregate(projectNew));
116+
}
117+
118+
@Test
119+
public void projectTest2() {
120+
col.insertMany(Arrays.asList(Document.parse("{_id: 0, x: 1}")));
121+
122+
// new, nestedArray
123+
Bson projectNestedArray = project(fields(excludeId(), computed("nestedArray", ofArray(
124+
current().getInteger("x").max(of(4)),
125+
current().getInteger("x"),
126+
of(0), of(1), of(true), of(false)
127+
))));
128+
assertEquals(
129+
Arrays.asList(Document.parse("{ nestedArray: [ 4, 1, 0, 1, true, false ] }")),
130+
aggregate(projectNestedArray));
131+
132+
// new, document
133+
Bson projectDocument = project(fields(computed("nested",
134+
//BsonDocument.parse("{ x: {$max : ['$x', 4] }}")
135+
of(new Document()).setField("x", current().getInteger("x").max(of(4)))
136+
)));
137+
assertEquals(
138+
Arrays.asList(Document.parse("{_id: 0, nested: { x: 4 } }")),
139+
aggregate(projectDocument));
140+
}
141+
142+
@Test
143+
public void groupTest() {
144+
col.insertMany(Arrays.asList(
145+
Document.parse("{t: 0, a: 1}"),
146+
Document.parse("{t: 0, a: 2}"),
147+
Document.parse("{t: 1, a: 9}")));
148+
149+
List<Document> results = aggregate(
150+
Aggregates.group(
151+
current().getInteger("t").add(of(100)),
152+
sum("sum", current().getInteger("a").add(1))),
153+
Aggregates.sort(ascending("_id")));
154+
assertEquals(
155+
Arrays.asList(
156+
Document.parse("{_id: 100, sum: 5}"),
157+
Document.parse("{_id: 101, sum: 10}")),
158+
results);
159+
}
160+
}

0 commit comments

Comments
 (0)