83 lines
2.3 KiB
Go
83 lines
2.3 KiB
Go
package expression_test
|
||
|
||
import (
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/stretchr/testify/assert"
|
||
"go.mongodb.org/mongo-driver/bson"
|
||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||
"gitea.pena/PenaSide/discount/internal/fields"
|
||
"gitea.pena/PenaSide/discount/internal/models"
|
||
"gitea.pena/PenaSide/discount/internal/utils/expression"
|
||
)
|
||
|
||
func TestGetAscRangeConditionBSON(t *testing.T) {
|
||
t.Run("Получение bson условия диапазона от меньшего с заполненными данными", func(t *testing.T) {
|
||
assert.Equal(t,
|
||
[]bson.D{
|
||
{{Key: "test", Value: bson.M{"$lte": "2020"}}},
|
||
{{Key: "test", Value: 0}},
|
||
},
|
||
expression.GetAscRangeConditionBSON("test", "2020"),
|
||
)
|
||
assert.Equal(t,
|
||
[]bson.D{
|
||
{{Key: "test2", Value: bson.M{"$lte": 200.00}}},
|
||
{{Key: "test2", Value: 0}},
|
||
},
|
||
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"}},
|
||
{{Key: "test", Value: ""}},
|
||
},
|
||
expression.GetValueConditionBSON("test", "2020"),
|
||
)
|
||
assert.Equal(t,
|
||
[]bson.D{
|
||
{{Key: "test2", Value: 200.00}},
|
||
{{Key: "test2", Value: ""}},
|
||
},
|
||
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{
|
||
{{Key: fields.PeriodCondition.From, Value: 0}},
|
||
{{Key: fields.PeriodCondition.To, Value: 0}},
|
||
},
|
||
},
|
||
},
|
||
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,
|
||
},
|
||
),
|
||
)
|
||
})
|
||
}
|