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

Добавлены тесты для полей File, Reference

parent 3f111e90
No related branches found
No related tags found
No related merge requests found
......@@ -62,7 +62,7 @@ func (s *testEnvStruct) Equal(other *testEnvStruct) bool {
return s.ID == other.ID
}
func TestEval_StructInEnv(t *testing.T) {
func TestExpr_Example(t *testing.T) {
ctx := context.Background()
tests := []struct {
......
......@@ -55,6 +55,7 @@ func TestConvertToMongo(t *testing.T) {
{"in", "In(s, [1,2,3])", nil, bson.M{"s": bson.M{"$in": []interface{}{1, 2, 3}}}, false},
{"in", "In(s, 1)", nil, bson.M{"s": bson.M{"$in": []interface{}{1}}}, false},
{"text search or id", "id", nil, nil, true},
{"struct env", "db_item.id == env_item.id", map[string]interface{}{"env_item": &testEnvStruct{ID: "id1"}}, bson.M{"db_item.id": "id1"}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
......@@ -69,38 +70,6 @@ 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()
......
package files
import (
"context"
"testing"
"text/template"
"git.perx.ru/perxis/perxis-go/pkg/expr"
"github.com/stretchr/testify/require"
)
......@@ -55,3 +57,35 @@ func TestFile_SetURLWithTemplate(t *testing.T) {
})
}
}
func TestFile_InExpr(t *testing.T) {
ctx := context.Background()
tests := []struct {
exp string
env map[string]interface{}
wantResult interface{}
wantErr bool
}{
{"f.id", map[string]interface{}{"f": &File{ID: "some_id"}}, "some_id", false},
{"f.name", map[string]interface{}{"f": &File{Name: "some_name"}}, "some_name", false},
{"f.size", map[string]interface{}{"f": &File{Size: 1}}, 1, false},
{"f.mime_type", map[string]interface{}{"f": &File{MimeType: "some_mime_type"}}, "some_mime_type", false},
{"f.url", map[string]interface{}{"f": &File{URL: "some_url"}}, "some_url", false},
{"f.key", map[string]interface{}{"f": &File{Key: "some_key"}}, "some_key", false},
{"f.not_exists", map[string]interface{}{"f": &File{}}, "", true},
}
for _, tt := range tests {
t.Run(tt.exp, func(t *testing.T) {
result, err := expr.Eval(ctx, tt.exp, tt.env)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, tt.wantResult, result)
})
}
}
package references
import (
"context"
"testing"
"git.perx.ru/perxis/perxis-go/pkg/expr"
"github.com/stretchr/testify/require"
)
func TestReference_InExpr(t *testing.T) {
ctx := context.Background()
tests := []struct {
exp string
env map[string]interface{}
wantResult interface{}
wantErr bool
}{
{"r.id", map[string]interface{}{"r": &Reference{ID: "some_id"}}, "some_id", false},
{"r.collection_id", map[string]interface{}{"r": &Reference{CollectionID: "some_coll_id"}}, "some_coll_id", false},
{"r.disabled", map[string]interface{}{"r": &Reference{Disabled: true}}, true, false},
{"r.String()", map[string]interface{}{"r": &Reference{ID: "id", CollectionID: "collID"}}, "collID.id", false},
{"r1.Equal(r2)", map[string]interface{}{"r1": &Reference{"id", "collID", false}, "r2": &Reference{"id", "collID", false}}, true, false},
{"r.IsValid()", map[string]interface{}{"r": &Reference{}}, false, false},
{"r.not_exists", map[string]interface{}{"r": &Reference{}}, false, true},
}
for _, tt := range tests {
t.Run(tt.exp, func(t *testing.T) {
result, err := expr.Eval(ctx, tt.exp, tt.env)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, tt.wantResult, result)
})
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment