discount/internal/utils/expression/bson_condition_test.go
2024-12-11 15:14:39 +03:00

83 lines
2.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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,
},
),
)
})
}