www.fgks.org   »   [go: up one dir, main page]

Skip to content

Commit

Permalink
refactor(aggregation/subtract): overhaul parameter handling and logic…
Browse files Browse the repository at this point in the history
… for clarity
  • Loading branch information
chenmingyong0423 committed Apr 5, 2024
1 parent d14d305 commit 2638887
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 30 deletions.
8 changes: 4 additions & 4 deletions builder/aggregation/arithmetic_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ func (b *arithmeticBuilder) MultiplyWithoutKey(expressions ...any) *Builder {
return b.parent
}

func (b *arithmeticBuilder) Subtract(key string, s string, start, length int64) *Builder {
e := bson.E{Key: types.AggregationSubtract, Value: []any{s, start, length}}
func (b *arithmeticBuilder) Subtract(key string, expressions ...any) *Builder {
e := bson.E{Key: types.AggregationSubtract, Value: expressions}
if !b.parent.tryMergeValue(key, e) {
b.parent.d = append(b.parent.d, bson.E{Key: key, Value: bson.D{e}})
}
return b.parent
}

func (b *arithmeticBuilder) SubtractWithoutKey(s string, start, length int64) *Builder {
b.parent.d = append(b.parent.d, bson.E{Key: types.AggregationSubtract, Value: []any{s, start, length}})
func (b *arithmeticBuilder) SubtractWithoutKey(expressions ...any) *Builder {
b.parent.d = append(b.parent.d, bson.E{Key: types.AggregationSubtract, Value: expressions})
return b.parent
}

Expand Down
22 changes: 9 additions & 13 deletions builder/aggregation/arithmetic_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,32 +107,28 @@ func Test_arithmeticBuilder_MultiplyWithoutKey(t *testing.T) {

func Test_arithmeticBuilder_Subtract(t *testing.T) {
t.Run("test subtract", func(t *testing.T) {
assert.Equal(t, bson.D{bson.E{Key: "total", Value: bson.D{bson.E{Key: "$subtract", Value: []any{"$quarter", int64(0), int64(2)}}}}},
BsonBuilder().Subtract("total", "$quarter", int64(0), int64(2)).Build(),
assert.Equal(t, bson.D{bson.E{Key: "dateDifference", Value: bson.D{bson.E{Key: "$subtract", Value: []any{"$date", 5 * 60 * 1000}}}}},
BsonBuilder().Subtract("dateDifference", []any{"$date", 5 * 60 * 1000}...).Build(),
)
})

}

func Test_arithmeticBuilder_SubtractWithoutKey(t *testing.T) {
testCases := []struct {
name string
s string
start int64
length int64
expected bson.D
name string
expressions []any
expected bson.D
}{
{
name: "normal",
s: "$quarter",
start: 0,
length: 2,
expected: bson.D{bson.E{Key: "$subtract", Value: []any{"$quarter", int64(0), int64(2)}}},
name: "normal",
expressions: []any{"$date", 5 * 60 * 1000},
expected: bson.D{bson.E{Key: "$subtract", Value: []any{"$date", 5 * 60 * 1000}}},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.expected, BsonBuilder().SubtractWithoutKey(tc.s, tc.start, tc.length).Build())
assert.Equal(t, tc.expected, BsonBuilder().SubtractWithoutKey(tc.expressions...).Build())
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions builder/aggregation/bson_construction_without_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func MultiplyWithoutKey(expressions ...any) bson.D {
return bson.D{{Key: types.AggregationMultiply, Value: expressions}}
}

func SubtractWithoutKey(s string, start, length int64) bson.D {
return bson.D{{Key: types.AggregationSubtract, Value: []any{s, start, length}}}
func SubtractWithoutKey(expressions ...any) bson.D {
return bson.D{{Key: types.AggregationSubtract, Value: expressions}}
}

func DivideWithoutKey(expressions ...any) bson.D {
Expand Down
18 changes: 7 additions & 11 deletions builder/aggregation/bson_construction_without_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,23 +244,19 @@ func TestMultiplyWithoutKey(t *testing.T) {

func TestSubtractWithoutKey(t *testing.T) {
testCases := []struct {
name string
s string
start int64
length int64
want bson.D
name string
expressions []any
expected bson.D
}{
{
name: "normal",
s: "$quarter",
start: 0,
length: 2,
want: bson.D{bson.E{Key: "$subtract", Value: []any{"$quarter", int64(0), int64(2)}}},
name: "normal",
expressions: []any{"$date", 5 * 60 * 1000},
expected: bson.D{bson.E{Key: "$subtract", Value: []any{"$date", 5 * 60 * 1000}}},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.want, SubtractWithoutKey(tc.s, tc.start, tc.length))
assert.Equal(t, tc.expected, SubtractWithoutKey(tc.expressions...))
})
}
}
Expand Down

0 comments on commit 2638887

Please sign in to comment.