2024-08-08 12:00:49: merge [master:958e40c1b5f280b681f8c9bf09c476b347ea7b7e];
This commit is contained in:
1 parent
bdbb3216ce
commit
d8a0db34ef
25 files changed
+1752
-1
No files matched your search
@@ -0,0 +1,192 @@
|
||||
package tests_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"github.com/txix-open/isp-kit/grpc/client"
|
||||
"github.com/txix-open/isp-kit/test"
|
||||
"github.com/txix-open/isp-kit/test/grpct"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"isp-script-service/assembly"
|
||||
"isp-script-service/conf"
|
||||
"isp-script-service/domain"
|
||||
)
|
||||
|
||||
func TestApiScriptTestSuite(t *testing.T) {
|
||||
t.Parallel()
|
||||
suite.Run(t, &ApiScript{})
|
||||
}
|
||||
|
||||
type ApiScript struct {
|
||||
suite.Suite
|
||||
test *test.Test
|
||||
require *require.Assertions
|
||||
apiCli *client.Client
|
||||
}
|
||||
|
||||
func (s *ApiScript) SetupSuite() {
|
||||
s.test, s.require = test.New(s.T())
|
||||
|
||||
cli := s.initMockServer()
|
||||
|
||||
cfg := conf.Remote{
|
||||
ScriptExecutionTimeoutMs: 10000,
|
||||
Scripts: []conf.ScriptDefinition{
|
||||
{
|
||||
Id: "1",
|
||||
Script: "return true",
|
||||
},
|
||||
{
|
||||
Id: "2",
|
||||
Script: "return false",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
locator := assembly.NewLocator(s.test.Logger(), cli)
|
||||
handler := locator.Handler(cfg)
|
||||
|
||||
_, s.apiCli = grpct.TestServer(s.test, handler)
|
||||
}
|
||||
|
||||
func (s *ApiScript) TestExternalInvoke() {
|
||||
req := domain.ExecuteRequest{
|
||||
Script: "return external.invoke('mock', true, {})",
|
||||
Arg: struct{}{},
|
||||
}
|
||||
res := domain.ScriptResp{}
|
||||
err := s.apiCli.Invoke("script/script/execute").
|
||||
JsonRequestBody(req).
|
||||
JsonResponseBody(&res).
|
||||
Do(context.Background())
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(true, res.Result)
|
||||
}
|
||||
|
||||
func (s *ApiScript) TestExecuteHappyPath() {
|
||||
req := domain.ExecuteRequest{
|
||||
Script: "return arg.a",
|
||||
Arg: map[string]bool{
|
||||
"a": true,
|
||||
},
|
||||
}
|
||||
res := domain.ScriptResp{}
|
||||
err := s.apiCli.Invoke("script/script/execute").
|
||||
JsonRequestBody(req).
|
||||
JsonResponseBody(&res).
|
||||
Do(context.Background())
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(true, res.Result)
|
||||
}
|
||||
|
||||
func (s *ApiScript) TestExecuteByIdHappyPath() {
|
||||
req := domain.ExecuteByIdRequest{
|
||||
Id: "1",
|
||||
Arg: struct{}{},
|
||||
}
|
||||
res := domain.ScriptResp{}
|
||||
err := s.apiCli.Invoke("script/script/execute_by_id").
|
||||
JsonRequestBody(req).
|
||||
JsonResponseBody(&res).
|
||||
Do(context.Background())
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(true, res.Result)
|
||||
}
|
||||
|
||||
func (s *ApiScript) TestExecuteByIdNotFound() {
|
||||
req := domain.ExecuteByIdRequest{
|
||||
Id: "unknown",
|
||||
Arg: struct{}{},
|
||||
}
|
||||
res := domain.ScriptResp{}
|
||||
err := s.apiCli.Invoke("script/script/execute_by_id").
|
||||
JsonRequestBody(req).
|
||||
JsonResponseBody(&res).
|
||||
Do(context.Background())
|
||||
s.Require().Error(err)
|
||||
st, ok := status.FromError(err)
|
||||
s.Require().True(ok)
|
||||
s.Require().Equal(codes.NotFound, st.Code())
|
||||
}
|
||||
|
||||
func (s *ApiScript) TestBatchExecuteHappyPath() {
|
||||
req := []domain.ExecuteByIdRequest{
|
||||
{
|
||||
Id: "1",
|
||||
Arg: struct{}{},
|
||||
},
|
||||
{
|
||||
Id: "2",
|
||||
Arg: struct{}{},
|
||||
},
|
||||
}
|
||||
res := make([]domain.ScriptResp, 0)
|
||||
err := s.apiCli.Invoke("script/script/batch_execute").
|
||||
JsonRequestBody(req).
|
||||
JsonResponseBody(&res).
|
||||
Do(context.Background())
|
||||
s.Require().NoError(err)
|
||||
expected := []domain.ScriptResp{
|
||||
{Result: true, Error: nil},
|
||||
{Result: false, Error: nil},
|
||||
}
|
||||
s.Require().ElementsMatch(expected, res)
|
||||
}
|
||||
|
||||
func (s *ApiScript) TestBatchExecuteMixedNotFound() {
|
||||
req := []domain.ExecuteByIdRequest{
|
||||
{
|
||||
Id: "1",
|
||||
Arg: struct{}{},
|
||||
},
|
||||
{
|
||||
Id: "unknown",
|
||||
Arg: struct{}{},
|
||||
},
|
||||
}
|
||||
res := make([]domain.ScriptResp, 0)
|
||||
err := s.apiCli.Invoke("script/script/batch_execute").
|
||||
JsonRequestBody(req).
|
||||
JsonResponseBody(&res).
|
||||
Do(context.Background())
|
||||
s.Require().NoError(err)
|
||||
expected := []domain.ScriptResp{
|
||||
{Result: true, Error: nil},
|
||||
{Result: nil, Error: &domain.Error{
|
||||
Type: domain.ErrorCompile,
|
||||
Description: "not defined script for id unknown",
|
||||
}},
|
||||
}
|
||||
s.Require().ElementsMatch(expected, res)
|
||||
}
|
||||
|
||||
func (s *ApiScript) TestBatchExecuteByIdHappyPath() {
|
||||
req := domain.BatchExecuteByIdsRequest{
|
||||
Ids: []string{"1", "2"},
|
||||
Arg: struct{}{},
|
||||
}
|
||||
res := make([]domain.ScriptResp, 0)
|
||||
err := s.apiCli.Invoke("script/script/batch_execute_by_ids").
|
||||
JsonRequestBody(req).
|
||||
JsonResponseBody(&res).
|
||||
Do(context.Background())
|
||||
s.Require().NoError(err)
|
||||
expected := []domain.ScriptResp{
|
||||
{Result: true, Error: nil},
|
||||
{Result: false, Error: nil},
|
||||
}
|
||||
s.Require().ElementsMatch(expected, res)
|
||||
}
|
||||
|
||||
func (s *ApiScript) initMockServer() *client.Client {
|
||||
server, cli := grpct.NewMock(s.test)
|
||||
server.Mock("mock", func(req bool) bool {
|
||||
return req
|
||||
})
|
||||
|
||||
return cli
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
// nolint:gochecknoglobals
|
||||
package tests_test
|
||||
|
||||
import (
|
||||
json2 "github.com/txix-open/isp-kit/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/dop251/goja"
|
||||
"github.com/robertkrimen/otto"
|
||||
"github.com/robertkrimen/otto/parser"
|
||||
"github.com/stretchr/testify/require"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
"github.com/yuin/gopher-lua/parse"
|
||||
json "layeh.com/gopher-json"
|
||||
)
|
||||
|
||||
const (
|
||||
jsScript = `
|
||||
var a = {};
|
||||
for (i=0; i<b.length; i++) {
|
||||
a[b[i]["id_tp_cd"]] = b[i]
|
||||
}
|
||||
`
|
||||
luaScript = `
|
||||
dst = {}
|
||||
for k,v in pairs(src) do
|
||||
dst[v.id_tp_cd] = v
|
||||
end
|
||||
`
|
||||
)
|
||||
|
||||
var (
|
||||
recordsExample = []map[string]interface{}{
|
||||
{
|
||||
"ref_num": "IIИК778068",
|
||||
"id_tp_cd": "1000017",
|
||||
"start_dt": "2006-10-10 00:00:00.000",
|
||||
"etalon_id": "7d53bf34-0aa6-11e9-9799-87ba01ce01e5",
|
||||
"ext_upd_date": "2018-01-11 17:52:01.000",
|
||||
"last_update_dt": "2018-09-13 23:41:35.324",
|
||||
"identification_issuer": "Красногорским Управлением ЗАГС Главного Управления ЗАГС Московской области",
|
||||
},
|
||||
{
|
||||
"ref_num": "18605520974",
|
||||
"id_tp_cd": "1000015",
|
||||
"start_dt": "2018-09-13 00:00:00.000",
|
||||
"etalon_id": "7d57dde9-0aa6-11e9-9799-87ba01ce01e5",
|
||||
"ext_upd_date": "2018-01-11 17:52:01.000",
|
||||
"last_update_dt": "2018-09-13 23:41:35.330",
|
||||
},
|
||||
{
|
||||
"ref_num": "5090399778001137",
|
||||
"id_tp_cd": "1000014",
|
||||
"start_dt": "2018-09-13 23:41:35.327",
|
||||
"etalon_id": "7d58a13e-0aa6-11e9-9799-87ba01ce01e5",
|
||||
"ext_upd_date": "2018-01-11 17:52:01.000",
|
||||
"last_update_dt": "2018-09-13 23:41:35.327",
|
||||
},
|
||||
}
|
||||
srcString []byte
|
||||
expecting = make(map[string]interface{})
|
||||
)
|
||||
|
||||
// nolint:gochecknoinits
|
||||
func init() {
|
||||
for _, v := range recordsExample {
|
||||
expecting[v["id_tp_cd"].(string)] = v // nolint:forcetypeassert
|
||||
}
|
||||
if bytes, err := json2.Marshal(recordsExample); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
srcString = bytes
|
||||
}
|
||||
}
|
||||
|
||||
func TestOtto(t *testing.T) {
|
||||
t.Parallel()
|
||||
require := require.New(t)
|
||||
|
||||
prog, err := parser.ParseFile(nil, "example", jsScript, 0)
|
||||
require.NoError(err)
|
||||
|
||||
runTime := otto.New()
|
||||
err = runTime.Set("b", recordsExample)
|
||||
require.NoError(err)
|
||||
|
||||
_, err = runTime.Run(prog)
|
||||
require.NoError(err)
|
||||
|
||||
res, err := runTime.Get("a")
|
||||
require.NoError(err)
|
||||
|
||||
_, err = res.Export()
|
||||
require.NoError(err)
|
||||
}
|
||||
|
||||
func TestGoja(t *testing.T) {
|
||||
t.Parallel()
|
||||
require := require.New(t)
|
||||
|
||||
prog := goja.MustCompile("test.js", jsScript, false)
|
||||
|
||||
runTime := goja.New()
|
||||
err := runTime.Set("b", recordsExample)
|
||||
require.NoError(err)
|
||||
|
||||
_, err = runTime.RunProgram(prog)
|
||||
require.NoError(err)
|
||||
|
||||
a := runTime.Get("a")
|
||||
_ = a.Export()
|
||||
}
|
||||
|
||||
func TestGopherLua(t *testing.T) {
|
||||
t.Parallel()
|
||||
require := require.New(t)
|
||||
|
||||
l := lua.NewState()
|
||||
defer l.Close()
|
||||
|
||||
b, err := json2.Marshal(recordsExample)
|
||||
require.NoError(err)
|
||||
|
||||
val, err := json.Decode(l, b)
|
||||
require.NoError(err)
|
||||
|
||||
l.SetGlobal("src", val)
|
||||
|
||||
err = l.DoString(luaScript)
|
||||
require.NoError(err)
|
||||
|
||||
dst := l.GetGlobal("dst")
|
||||
b, err = json.Encode(dst)
|
||||
require.NoError(err)
|
||||
|
||||
m := make(map[string]interface{})
|
||||
err = json2.Unmarshal(b, &m)
|
||||
require.NoError(err)
|
||||
|
||||
require.EqualValues(expecting, m)
|
||||
}
|
||||
|
||||
func BenchmarkOtto(b *testing.B) {
|
||||
prog, err := parser.ParseFile(nil, "example", jsScript, 0)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
runTime := otto.New()
|
||||
err = runTime.Set("b", recordsExample)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = runTime.Run(prog)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
res, err := runTime.Get("a")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = res.Export()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGoja(b *testing.B) {
|
||||
prog := goja.MustCompile("test.js", jsScript, false)
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
runTime := goja.New()
|
||||
err := runTime.Set("b", recordsExample)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = runTime.RunProgram(prog)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
a := runTime.Get("a")
|
||||
_ = a.Export()
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGopherLua(b *testing.B) {
|
||||
chunk, err := parse.Parse(strings.NewReader(luaScript), "test")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
proto, err := lua.Compile(chunk, "test")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
l := lua.NewState()
|
||||
|
||||
val, err := json.Decode(l, srcString)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
l.SetGlobal("src", val)
|
||||
|
||||
prog := l.NewFunctionFromProto(proto)
|
||||
l.Push(prog)
|
||||
err = l.PCall(0, lua.MultRet, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
dst := l.GetGlobal("dst")
|
||||
c, err := json.Encode(dst)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
m := make(map[string]interface{})
|
||||
err = json2.Unmarshal(c, &m)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
l.Close()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user