Skip to content
Snippets Groups Projects
Commit 8dbe03c5 authored by Danis Kirasirov's avatar Danis Kirasirov
Browse files

Добавлены тесты для структур в expr

parent 32dc837e
No related branches found
No related tags found
No related merge requests found
package expr
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestIsExpression(t *testing.T) {
......@@ -49,3 +51,65 @@ func TestIsExpression(t *testing.T) {
})
}
}
type testEnvStruct struct {
ID string `expr:"id"`
Size int `expr:"size"`
Data interface{} `expr:"data"`
}
func TestEval_StructInEnv(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
exp string
env map[string]interface{}
wantErr bool
wantResult interface{}
}{
{
name: "get field by expr tag",
exp: "s.id",
env: map[string]interface{}{"s": &testEnvStruct{ID: "id1"}},
wantResult: "id1",
},
{
name: "get field by field name",
exp: "s.ID",
env: map[string]interface{}{"s": &testEnvStruct{ID: "id1"}},
wantResult: "id1",
},
{
name: "get nested field",
exp: "m.s.size",
env: map[string]interface{}{"m": map[string]interface{}{"s": &testEnvStruct{Size: 1}}},
wantResult: 1,
},
{
name: "check field",
exp: "s.data.size < 100",
env: map[string]interface{}{"s": &testEnvStruct{Data: &testEnvStruct{Size: 0}}},
wantResult: true,
},
{
name: "field not exists",
exp: "s.not_exists",
env: map[string]interface{}{"s": &testEnvStruct{}},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := Eval(ctx, tt.exp, tt.env)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, tt.wantResult, result)
})
}
}
......@@ -69,6 +69,38 @@ func TestConvertToMongo(t *testing.T) {
}
}
func TestConvertToMongo_StructInEnv(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
exp string
env map[string]interface{}
wantErr bool
wantResult interface{}
}{
{
name: "get field",
exp: "db_item.id == env_item.id",
env: map[string]interface{}{"env_item": &testEnvStruct{ID: "id1"}},
wantResult: bson.M{"db_item.id": "id1"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := ConvertToMongo(ctx, tt.exp, tt.env, nil)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, tt.wantResult, result)
})
}
}
func BenchmarkConvertToMongo(b *testing.B) {
const idsNum = 1_000_000
ctx := context.Background()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment