@@ -30,6 +30,25 @@ func Test_accumulatorsBuilder_Sum(t *testing.T) {
3030 })
3131}
3232
33+ func Test_accumulatorsBuilder_SumWithoutKey (t * testing.T ) {
34+ testCases := []struct {
35+ name string
36+ expression any
37+ expected bson.D
38+ }{
39+ {
40+ name : "normal" ,
41+ expression : "$price" ,
42+ expected : bson.D {{Key : "$sum" , Value : "$price" }},
43+ },
44+ }
45+ for _ , tc := range testCases {
46+ t .Run (tc .name , func (t * testing.T ) {
47+ assert .Equal (t , tc .expected , BsonBuilder ().SumWithoutKey (tc .expression ).Build ())
48+ })
49+ }
50+ }
51+
3352func Test_accumulatorsBuilder_Push (t * testing.T ) {
3453 t .Run ("single operation" , func (t * testing.T ) {
3554 assert .Equal (t , bson.D {bson.E {Key : "items" , Value : bson.D {{Key : "$push" , Value : "$item" }}}}, BsonBuilder ().Push ("items" , "$item" ).Build ())
@@ -39,6 +58,26 @@ func Test_accumulatorsBuilder_Push(t *testing.T) {
3958 })
4059}
4160
61+ func Test_accumulatorsBuilder_PushWithoutKey (t * testing.T ) {
62+ testCases := []struct {
63+ name string
64+ expression any
65+ expected bson.D
66+ }{
67+ {
68+ name : "normal" ,
69+ // { item: "$item", quantity: "$quantity" }
70+ expression : BsonBuilder ().AddKeyValues ("item" , "$item" ).AddKeyValues ("quantity" , "$quantity" ).Build (),
71+ expected : bson.D {{Key : "$push" , Value : bson.D {{Key : "item" , Value : "$item" }, {Key : "quantity" , Value : "$quantity" }}}},
72+ },
73+ }
74+ for _ , tc := range testCases {
75+ t .Run (tc .name , func (t * testing.T ) {
76+ assert .Equal (t , tc .expected , BsonBuilder ().PushWithoutKey (tc .expression ).Build ())
77+ })
78+ }
79+ }
80+
4281func Test_accumulatorsBuilder_Avg (t * testing.T ) {
4382 t .Run ("single operation" , func (t * testing.T ) {
4483 assert .Equal (t , bson.D {bson.E {Key : "avgAmount" , Value : bson.D {{Key : "$avg" , Value : "$price" }}}}, BsonBuilder ().Avg ("avgAmount" , "$price" ).Build ())
@@ -48,6 +87,26 @@ func Test_accumulatorsBuilder_Avg(t *testing.T) {
4887 })
4988}
5089
90+ func Test_accumulatorsBuilder_AvgWithoutKey (t * testing.T ) {
91+ testCases := []struct {
92+ name string
93+ expression any
94+ expected bson.D
95+ }{
96+ {
97+ name : "normal" ,
98+ // { $multiply: [ "$price", "$quantity" ] }
99+ expression : BsonBuilder ().MultiplyWithoutKey ("$price" , "$quantity" ).Build (),
100+ expected : bson.D {{Key : "$avg" , Value : bson.D {{Key : "$multiply" , Value : []any {"$price" , "$quantity" }}}}},
101+ },
102+ }
103+ for _ , tc := range testCases {
104+ t .Run (tc .name , func (t * testing.T ) {
105+ assert .Equal (t , tc .expected , BsonBuilder ().AvgWithoutKey (tc .expression ).Build ())
106+ })
107+ }
108+ }
109+
51110func Test_accumulatorsBuilder_First (t * testing.T ) {
52111 t .Run ("single operation" , func (t * testing.T ) {
53112 assert .Equal (t , bson.D {bson.E {Key : "firstType" , Value : bson.D {{Key : "$first" , Value : "$type" }}}}, BsonBuilder ().First ("firstType" , "$type" ).Build ())
@@ -57,6 +116,25 @@ func Test_accumulatorsBuilder_First(t *testing.T) {
57116 })
58117}
59118
119+ func Test_accumulatorsBuilder_FirstWithoutKey (t * testing.T ) {
120+ testCases := []struct {
121+ name string
122+ expression any
123+ expected bson.D
124+ }{
125+ {
126+ name : "normal" ,
127+ expression : "$type" ,
128+ expected : bson.D {{Key : "$first" , Value : "$type" }},
129+ },
130+ }
131+ for _ , tc := range testCases {
132+ t .Run (tc .name , func (t * testing.T ) {
133+ assert .Equal (t , tc .expected , BsonBuilder ().FirstWithoutKey (tc .expression ).Build ())
134+ })
135+ }
136+ }
137+
60138func Test_accumulatorsBuilder_Last (t * testing.T ) {
61139 t .Run ("single operation" , func (t * testing.T ) {
62140 assert .Equal (t , bson.D {bson.E {Key : "lastType" , Value : bson.D {{Key : "$last" , Value : "$type" }}}}, BsonBuilder ().Last ("lastType" , "$type" ).Build ())
@@ -66,6 +144,25 @@ func Test_accumulatorsBuilder_Last(t *testing.T) {
66144 })
67145}
68146
147+ func Test_accumulatorsBuilder_LastWithoutKey (t * testing.T ) {
148+ testCases := []struct {
149+ name string
150+ expression any
151+ expected bson.D
152+ }{
153+ {
154+ name : "normal" ,
155+ expression : "$type" ,
156+ expected : bson.D {{Key : "$last" , Value : "$type" }},
157+ },
158+ }
159+ for _ , tc := range testCases {
160+ t .Run (tc .name , func (t * testing.T ) {
161+ assert .Equal (t , tc .expected , BsonBuilder ().LastWithoutKey (tc .expression ).Build ())
162+ })
163+ }
164+ }
165+
69166func Test_accumulatorsBuilder_Min (t * testing.T ) {
70167 t .Run ("single operation" , func (t * testing.T ) {
71168 assert .Equal (t , bson.D {bson.E {Key : "minPrice" , Value : bson.D {{Key : "$min" , Value : "$price" }}}}, BsonBuilder ().Min ("minPrice" , "$price" ).Build ())
@@ -74,6 +171,24 @@ func Test_accumulatorsBuilder_Min(t *testing.T) {
74171 assert .Equal (t , bson.D {bson.E {Key : "minPrice" , Value : bson.D {{Key : "$min" , Value : "$price" }}}, bson.E {Key : "minFee" , Value : bson.D {{Key : "$min" , Value : "$fee" }}}}, BsonBuilder ().Min ("minPrice" , "$price" ).Min ("minFee" , "$fee" ).Build ())
75172 })
76173}
174+ func Test_accumulatorsBuilder_MinWithoutKey (t * testing.T ) {
175+ testCases := []struct {
176+ name string
177+ expression any
178+ expected bson.D
179+ }{
180+ {
181+ name : "normal" ,
182+ expression : "$price" ,
183+ expected : bson.D {{Key : "$min" , Value : "$price" }},
184+ },
185+ }
186+ for _ , tc := range testCases {
187+ t .Run (tc .name , func (t * testing.T ) {
188+ assert .Equal (t , tc .expected , BsonBuilder ().MinWithoutKey (tc .expression ).Build ())
189+ })
190+ }
191+ }
77192
78193func Test_accumulatorsBuilder_Max (t * testing.T ) {
79194 t .Run ("single operation" , func (t * testing.T ) {
@@ -83,3 +198,22 @@ func Test_accumulatorsBuilder_Max(t *testing.T) {
83198 assert .Equal (t , bson.D {bson.E {Key : "maxPrice" , Value : bson.D {{Key : "$max" , Value : "$price" }}}, bson.E {Key : "maxFee" , Value : bson.D {{Key : "$max" , Value : "$fee" }}}}, BsonBuilder ().Max ("maxPrice" , "$price" ).Max ("maxFee" , "$fee" ).Build ())
84199 })
85200}
201+
202+ func Test_accumulatorsBuilder_MaxWithoutKey (t * testing.T ) {
203+ testCases := []struct {
204+ name string
205+ expression any
206+ expected bson.D
207+ }{
208+ {
209+ name : "normal" ,
210+ expression : "$price" ,
211+ expected : bson.D {{Key : "$max" , Value : "$price" }},
212+ },
213+ }
214+ for _ , tc := range testCases {
215+ t .Run (tc .name , func (t * testing.T ) {
216+ assert .Equal (t , tc .expected , BsonBuilder ().MaxWithoutKey (tc .expression ).Build ())
217+ })
218+ }
219+ }
0 commit comments