26
26
import org .bson .Document ;
27
27
import org .bson .conversions .Bson ;
28
28
import org .junit .jupiter .api .Test ;
29
- import org .junit .jupiter .params .ParameterizedTest ;
30
- import org .junit .jupiter .params .provider .Arguments ;
31
- import org .junit .jupiter .params .provider .MethodSource ;
32
29
33
30
import java .util .AbstractMap ;
34
- import java .util .ArrayList ;
35
- import java .util .Arrays ;
36
31
import java .util .Collection ;
37
32
import java .util .Collections ;
38
33
import java .util .HashMap ;
39
- import java .util .List ;
40
34
import java .util .Map ;
41
35
import java .util .function .BiFunction ;
42
36
import java .util .function .Function ;
43
37
import java .util .function .Supplier ;
44
- import java .util .stream .Stream ;
45
38
46
39
import static com .mongodb .assertions .Assertions .assertNotNull ;
47
40
import static com .mongodb .client .model .Sorts .ascending ;
@@ -58,9 +51,6 @@ final class TestWindowOutputFields {
58
51
private static final String PATH = "newField" ;
59
52
private static final Bson SORT_BY = ascending ("sortByField" );
60
53
private static final Map .Entry <Integer , BsonValue > INT_EXPR = new AbstractMap .SimpleImmutableEntry <>(1 , new BsonInt32 (1 ));
61
- private static final Map .Entry <Object , BsonArray > ARRAY_EXPR =
62
- new AbstractMap .SimpleImmutableEntry <>(Arrays .asList (0.5 , 0.9 , "$$letValueX" ),
63
- new BsonArray (Arrays .asList (new BsonDouble (0.5 ), new BsonDouble (0.9 ), new BsonString ("$$letValueX" ))));
64
54
private static final Map .Entry <String , BsonValue > STR_EXPR =
65
55
new AbstractMap .SimpleImmutableEntry <>("$fieldToRead" , new BsonString ("$fieldToRead" ));
66
56
private static final Map .Entry <Document , BsonDocument > DOC_EXPR = new AbstractMap .SimpleImmutableEntry <>(
@@ -222,111 +212,6 @@ void pick() {
222
212
);
223
213
}
224
214
225
- @ ParameterizedTest
226
- @ MethodSource ("percentileWindowFunctionsSource" )
227
- void percentile (final Object inExpressionParameter ,
228
- final BsonValue expectedInExpression ,
229
- final Object pExpressionParameter ,
230
- final BsonValue expectedPExpression ,
231
- final Window window ) {
232
- String expectedFunctionName = "$percentile" ;
233
- String method = "approximate" ;
234
- BsonField expectedWindowOutputField = getExpectedBsonField (expectedFunctionName , expectedInExpression , expectedPExpression ,
235
- method , window );
236
-
237
- Supplier <String > msg = () -> "expectedFunctionName=" + expectedFunctionName
238
- + ", path=" + PATH
239
- + ", InExpression=" + inExpressionParameter
240
- + ", pExpression=" + pExpressionParameter
241
- + ", method=" + method
242
- + ", window=" + window ;
243
-
244
- assertWindowOutputField (expectedWindowOutputField , WindowOutputFields .percentile (PATH , inExpressionParameter , pExpressionParameter , method , window ),
245
- msg );
246
- assertThrows (IllegalArgumentException .class , () -> WindowOutputFields .percentile (null , inExpressionParameter , pExpressionParameter , method , window ), msg );
247
- assertThrows (IllegalArgumentException .class , () -> WindowOutputFields .percentile (PATH , null , pExpressionParameter , method , window ), msg );
248
- assertThrows (IllegalArgumentException .class , () -> WindowOutputFields .percentile (PATH , inExpressionParameter , null , method , window ), msg );
249
- assertThrows (IllegalArgumentException .class , () -> WindowOutputFields .percentile (PATH , inExpressionParameter , pExpressionParameter , null , window ), msg );
250
- }
251
-
252
- @ ParameterizedTest
253
- @ MethodSource ("medianWindowFunctionsSource" )
254
- void median (final Object inExpressionParameter ,
255
- final BsonValue expectedInExpression ,
256
- final Window window ) {
257
- String expectedFunctionName = "$median" ;
258
- String method = "approximate" ;
259
- BsonField expectedWindowOutputField = getExpectedBsonField (expectedFunctionName , expectedInExpression ,
260
- null , method , window );
261
-
262
- Supplier <String > msg = () -> "expectedFunctionName=" + expectedFunctionName
263
- + ", path=" + PATH
264
- + ", InExpression=" + inExpressionParameter
265
- + ", method=" + method
266
- + ", window=" + window ;
267
-
268
- assertWindowOutputField (expectedWindowOutputField , WindowOutputFields .median (PATH , inExpressionParameter , method , window ),
269
- msg );
270
- assertThrows (IllegalArgumentException .class , () -> WindowOutputFields .median (null , inExpressionParameter , method , window ),
271
- msg );
272
- assertThrows (IllegalArgumentException .class , () -> WindowOutputFields .median (PATH , null , method , window ), msg );
273
- assertThrows (IllegalArgumentException .class , () -> WindowOutputFields .median (PATH , inExpressionParameter , null , window ), msg );
274
- }
275
-
276
- private static Stream <Arguments > percentileWindowFunctionsSource () {
277
- Map <Object , BsonValue > inExpressions = new HashMap <>();
278
- inExpressions .put (INT_EXPR .getKey (), INT_EXPR .getValue ());
279
- inExpressions .put (STR_EXPR .getKey (), STR_EXPR .getValue ());
280
- inExpressions .put (DOC_EXPR .getKey (), DOC_EXPR .getValue ());
281
-
282
- Map <Object , BsonValue > pExpressions = new HashMap <>();
283
- pExpressions .put (ARRAY_EXPR .getKey (), ARRAY_EXPR .getValue ());
284
- pExpressions .put (STR_EXPR .getKey (), STR_EXPR .getValue ());
285
-
286
- Collection <Window > windows = asList (null , POSITION_BASED_WINDOW , RANGE_BASED_WINDOW );
287
-
288
- // Generate different combinations of test arguments using Cartesian product of inExpressions, pExpressions, and windows.
289
- List <Arguments > argumentsList = new ArrayList <>();
290
- inExpressions .forEach ((incomingInParameter , inBsonValue ) ->
291
- pExpressions .forEach ((incomingPParameter , pBsonValue ) ->
292
- windows .forEach (window ->
293
- argumentsList .add (
294
- Arguments .of (incomingInParameter , inBsonValue , incomingPParameter , pBsonValue , window )))));
295
- return Stream .of (argumentsList .toArray (new Arguments []{}));
296
- }
297
-
298
- private static Stream <Arguments > medianWindowFunctionsSource () {
299
- Map <Object , BsonValue > inExpressions = new HashMap <>();
300
- inExpressions .put (INT_EXPR .getKey (), INT_EXPR .getValue ());
301
- inExpressions .put (STR_EXPR .getKey (), STR_EXPR .getValue ());
302
- inExpressions .put (DOC_EXPR .getKey (), DOC_EXPR .getValue ());
303
-
304
- Collection <Window > windows = asList (null , POSITION_BASED_WINDOW , RANGE_BASED_WINDOW );
305
-
306
- // Generate different combinations of test arguments using Cartesian product of inExpressions and windows.
307
- List <Arguments > argumentsList = new ArrayList <>();
308
- inExpressions .forEach ((incomingInParameter , inBsonValue ) ->
309
- windows .forEach (window ->
310
- argumentsList .add (
311
- Arguments .of (incomingInParameter , inBsonValue , window ))));
312
- return Stream .of (argumentsList .toArray (new Arguments []{}));
313
- }
314
-
315
- private static BsonField getExpectedBsonField (final String expectedFunctionName , final BsonValue expectedInExpression ,
316
- final @ Nullable BsonValue expectedPExpression ,
317
- final String method , final @ Nullable Window window ) {
318
- BsonDocument expectedFunctionDoc = new BsonDocument ("input" , expectedInExpression );
319
- if (expectedPExpression != null ) {
320
- expectedFunctionDoc .append ("p" , expectedPExpression );
321
- }
322
- expectedFunctionDoc .append ("method" , new BsonString (method ));
323
- BsonDocument expectedFunctionAndWindow = new BsonDocument (expectedFunctionName , expectedFunctionDoc );
324
- if (window != null ) {
325
- expectedFunctionAndWindow .append ("window" , window .toBsonDocument ());
326
- }
327
- return new BsonField (PATH , expectedFunctionAndWindow );
328
- }
329
-
330
215
private static void assertPickNoSortWindowFunction (
331
216
final String expectedFunctionName ,
332
217
final QuadriFunction <String , Object , Object , Window , WindowOutputField > windowOutputFieldBuilder ,
0 commit comments