discount/internal/utils/expression/bson_condition_test.go

83 lines
2.3 KiB
Go
Raw Normal View History

2023-07-04 04:04:31 +00:00
package expression_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
2024-12-11 12:14:39 +00:00
"gitea.pena/PenaSide/discount/internal/fields"
"gitea.pena/PenaSide/discount/internal/models"
"gitea.pena/PenaSide/discount/internal/utils/expression"
2023-07-04 04:04:31 +00:00
)
func TestGetAscRangeConditionBSON(t *testing.T) {
t.Run("Получение bson условия диапазона от меньшего с заполненными данными", func(t *testing.T) {
assert.Equal(t,
[]bson.D{
{{Key: "test", Value: bson.M{"$lte": "2020"}}},
2023-09-18 13:45:26 +00:00
{{Key: "test", Value: 0}},
2023-07-04 04:04:31 +00:00
},
expression.GetAscRangeConditionBSON("test", "2020"),
)
assert.Equal(t,
[]bson.D{
{{Key: "test2", Value: bson.M{"$lte": 200.00}}},
2023-09-18 13:45:26 +00:00
{{Key: "test2", Value: 0}},
2023-07-04 04:04:31 +00:00
},
expression.GetAscRangeConditionBSON("test2", 200.00),
)
})
}
func TestGetValueConditionBSON(t *testing.T) {
t.Run("Получение bson условия значения с заполненными данными", func(t *testing.T) {
assert.Equal(t,
[]bson.D{
{{Key: "test", Value: "2020"}},
2023-09-18 08:32:11 +00:00
{{Key: "test", Value: ""}},
2023-07-04 04:04:31 +00:00
},
expression.GetValueConditionBSON("test", "2020"),
)
assert.Equal(t,
[]bson.D{
{{Key: "test2", Value: 200.00}},
2023-09-18 08:32:11 +00:00
{{Key: "test2", Value: ""}},
2023-07-04 04:04:31 +00:00
},
expression.GetValueConditionBSON("test2", 200.00),
)
})
}
func TestGetPerionConditionBSON(t *testing.T) {
t.Run("Получение bson условия периода с заполненными данными", func(t *testing.T) {
assert.Equal(t,
[]bson.M{
{
"$and": []bson.D{
{{Key: fields.PeriodCondition.From, Value: bson.M{"$lte": primitive.NewDateTimeFromTime(time.Unix(1674546287, 0).UTC())}}},
{{Key: fields.PeriodCondition.To, Value: bson.M{"$gte": primitive.NewDateTimeFromTime(time.Unix(1674546287, 0).UTC())}}},
},
},
{
"$and": []bson.D{
2023-09-18 13:45:26 +00:00
{{Key: fields.PeriodCondition.From, Value: 0}},
{{Key: fields.PeriodCondition.To, Value: 0}},
2023-07-04 04:04:31 +00:00
},
},
},
expression.GetPerionConditionBSON(
models.PeriodCondition{
From: time.Unix(1674546287, 0).UTC(),
To: time.Unix(1674546287, 0).UTC(),
},
expression.PeriodFieldPaths{
From: fields.PeriodCondition.From,
To: fields.PeriodCondition.To,
},
),
)
})
}