@@ -2733,6 +2733,171 @@ void testProjectWithCondition() throws Exception {
27332733 );
27342734 }
27352735
2736+ @ Test
2737+ void testAggregateWithSwitch () throws Exception {
2738+ collection .insertOne (json ("_id: 1, name: 'Dave', qty: 1" ));
2739+ collection .insertOne (json ("_id: 2, name: 'Carol', qty: 5" ));
2740+ collection .insertOne (json ("_id: 3, name: 'Bob', qty: 10" ));
2741+ collection .insertOne (json ("_id: 4, name: 'Alice', qty: 20" ));
2742+
2743+ List <Document > pipeline = jsonList ("""
2744+ $project: {
2745+ name: 1,
2746+ qtyDiscount: {
2747+ $switch: {
2748+ branches: [
2749+ { case: { $gte: ['$qty', 10] }, then: 0.15 },
2750+ { case: { $gte: ['$qty', 5] }, then: 0.10 },
2751+ { case: { $gte: ['$qty', 1] }, then: 0.05 }
2752+ ],
2753+ default: 0
2754+ }
2755+ }
2756+ }
2757+ """ );
2758+
2759+ assertThat (collection .aggregate (pipeline ))
2760+ .containsExactlyInAnyOrder (
2761+ json ("_id: 1, name: 'Dave', qtyDiscount: 0.05" ),
2762+ json ("_id: 2, name: 'Carol', qtyDiscount: 0.10" ),
2763+ json ("_id: 3, name: 'Bob', qtyDiscount: 0.15" ),
2764+ json ("_id: 4, name: 'Alice', qtyDiscount: 0.15" )
2765+ );
2766+ }
2767+
2768+ @ Test
2769+ void testAggregateWithSwitchDefault () throws Exception {
2770+ collection .insertOne (json ("_id: 1, status: 'active'" ));
2771+ collection .insertOne (json ("_id: 2, status: 'inactive'" ));
2772+ collection .insertOne (json ("_id: 3, status: 'unknown'" ));
2773+
2774+ List <Document > pipeline = jsonList ("""
2775+ $project: {
2776+ statusCode: {
2777+ $switch: {
2778+ branches: [
2779+ { case: { $eq: ['$status', 'active'] }, then: 1 },
2780+ { case: { $eq: ['$status', 'inactive'] }, then: 0 }
2781+ ],
2782+ default: -1
2783+ }
2784+ }
2785+ }
2786+ """ );
2787+
2788+ assertThat (collection .aggregate (pipeline ))
2789+ .containsExactlyInAnyOrder (
2790+ json ("_id: 1, statusCode: 1" ),
2791+ json ("_id: 2, statusCode: 0" ),
2792+ json ("_id: 3, statusCode: -1" )
2793+ );
2794+ }
2795+
2796+ @ Test
2797+ void testAggregateWithSwitchMissingDefault () throws Exception {
2798+ collection .insertOne (json ("_id: 1, value: 100" ));
2799+
2800+ List <Document > pipeline = jsonList ("""
2801+ $project: {
2802+ result: {
2803+ $switch: {
2804+ branches: [
2805+ { case: { $eq: ['$value', 50] }, then: 'fifty' }
2806+ ]
2807+ }
2808+ }
2809+ }
2810+ """ );
2811+
2812+ assertThatExceptionOfType (MongoCommandException .class )
2813+ .isThrownBy (() -> collection .aggregate (pipeline ).first ())
2814+ .withMessageContaining ("$switch could not find a matching branch for an input, and no default was specified" );
2815+ }
2816+
2817+ @ Test
2818+ void testAggregateWithSwitchMissingBranches () throws Exception {
2819+ collection .insertOne (json ("_id: 1, value: 100" ));
2820+
2821+ List <Document > pipeline = jsonList ("""
2822+ $project: {
2823+ result: {
2824+ $switch: {
2825+ default: 'none'
2826+ }
2827+ }
2828+ }
2829+ """ );
2830+
2831+ assertThatExceptionOfType (MongoCommandException .class )
2832+ .isThrownBy (() -> collection .aggregate (pipeline ).first ())
2833+ .withMessageContaining ("$switch requires at least one branch" );
2834+ }
2835+
2836+ @ Test
2837+ void testAggregateWithSwitchEmptyBranches () throws Exception {
2838+ collection .insertOne (json ("_id: 1, value: 100" ));
2839+
2840+ List <Document > pipeline = jsonList ("""
2841+ $project: {
2842+ result: {
2843+ $switch: {
2844+ branches: [
2845+ ],
2846+ default: 'none'
2847+ }
2848+ }
2849+ }
2850+ """ );
2851+
2852+ assertThatExceptionOfType (MongoCommandException .class )
2853+ .isThrownBy (() -> collection .aggregate (pipeline ).first ())
2854+ .withMessageContaining ("$switch requires at least one branch" );
2855+ }
2856+
2857+ @ Test
2858+ void testAggregateWithSwitchInvalidBranch () throws Exception {
2859+ collection .insertOne (json ("_id: 1, value: 100" ));
2860+
2861+ List <Document > pipeline = jsonList ("""
2862+ $project: {
2863+ result: {
2864+ $switch: {
2865+ branches: [
2866+ { case: { $eq: ['$value', 100] } }
2867+ ],
2868+ default: 'none'
2869+ }
2870+ }
2871+ }
2872+ """ );
2873+
2874+ assertThatExceptionOfType (MongoCommandException .class )
2875+ .isThrownBy (() -> collection .aggregate (pipeline ).first ())
2876+ .withMessageContaining ("$switch requires each branch have a 'then' expression" );
2877+ }
2878+
2879+ @ Test
2880+ void testAggregateWithSwitchInvalidArgument () throws Exception {
2881+ collection .insertOne (json ("_id: 1, value: 100" ));
2882+
2883+ List <Document > pipeline = jsonList ("""
2884+ $project: {
2885+ result: {
2886+ $switch: {
2887+ branches: [
2888+ { case: { $eq: ['$value', 100] }, then: 'one hundred' }
2889+ ],
2890+ default_value: 'none'
2891+ }
2892+ }
2893+ }
2894+ """ );
2895+
2896+ assertThatExceptionOfType (MongoCommandException .class )
2897+ .isThrownBy (() -> collection .aggregate (pipeline ).first ())
2898+ .withMessageContaining ("$switch found an unknown argument: default_value" );
2899+ }
2900+
27362901 // https://github.com/bwaldvogel/mongo-java-server/issues/138
27372902 @ Test
27382903 public void testAggregateWithGeoNear () throws Exception {
0 commit comments