Compare commits
8 Commits
v4.0.0
...
0cf246d2d6
Author | SHA1 | Date | |
---|---|---|---|
0cf246d2d6 | |||
af278bd7d3 | |||
814b90efe5 | |||
e403ae3d8e | |||
c9816a3957 | |||
5691238a6a | |||
963a0fa7b7 | |||
485257035c |
@@ -145,12 +145,11 @@ type Stream interface {
|
|||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// type Greeter struct {}
|
// type Greeter struct {}
|
||||||
//
|
|
||||||
// func (g *Greeter) Hello(context, request, response) error {
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
//
|
//
|
||||||
|
// func (g *Greeter) Hello(context, request, response) error {
|
||||||
|
// return nil
|
||||||
|
// }
|
||||||
type Handler interface {
|
type Handler interface {
|
||||||
Name() string
|
Name() string
|
||||||
Handler() interface{}
|
Handler() interface{}
|
||||||
|
@@ -88,6 +88,7 @@ func (s *service) Name() string {
|
|||||||
// Init initialises options. Additionally it calls cmd.Init
|
// Init initialises options. Additionally it calls cmd.Init
|
||||||
// which parses command line flags. cmd.Init is only called
|
// which parses command line flags. cmd.Init is only called
|
||||||
// on first Init.
|
// on first Init.
|
||||||
|
//
|
||||||
//nolint:gocyclo
|
//nolint:gocyclo
|
||||||
func (s *service) Init(opts ...Option) error {
|
func (s *service) Init(opts ...Option) error {
|
||||||
var err error
|
var err error
|
||||||
|
@@ -58,6 +58,7 @@ func IsLocal(addr string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Extract returns a real ip
|
// Extract returns a real ip
|
||||||
|
//
|
||||||
//nolint:gocyclo
|
//nolint:gocyclo
|
||||||
func Extract(addr string) (string, error) {
|
func Extract(addr string) (string, error) {
|
||||||
// if addr specified then its returned
|
// if addr specified then its returned
|
||||||
|
17
util/io/redirect.go
Normal file
17
util/io/redirect.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package io
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
var osStderrMu sync.Mutex
|
||||||
|
|
||||||
|
var OrigStderr = func() *os.File {
|
||||||
|
fd, err := dupFD(os.Stderr.Fd())
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return os.NewFile(fd, os.Stderr.Name())
|
||||||
|
}()
|
40
util/io/redirect_test.go
Normal file
40
util/io/redirect_test.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package io
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"regexp"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ErrorPattern = regexp.MustCompile(`"error"`)
|
||||||
|
|
||||||
|
func TestRedirect(t *testing.T) {
|
||||||
|
r, w, err := os.Pipe()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ch := make(chan string)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
buf := bytes.NewBuffer(nil)
|
||||||
|
_, _ = io.Copy(buf, r)
|
||||||
|
ch <- buf.String()
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err = RedirectStderr(w); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Stderr.Write([]byte(`test redirect`))
|
||||||
|
time.Sleep(1 * time.Millisecond)
|
||||||
|
r.Close()
|
||||||
|
str := <-ch
|
||||||
|
if ErrorPattern.MatchString(str) {
|
||||||
|
t.Fatal(fmt.Errorf(str))
|
||||||
|
}
|
||||||
|
}
|
48
util/io/redirect_unix.go
Normal file
48
util/io/redirect_unix.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
//go:build !windows
|
||||||
|
// +build !windows
|
||||||
|
|
||||||
|
package io
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// dupFD is used to initialize OrigStderr (see stderr_redirect.go).
|
||||||
|
func dupFD(fd uintptr) (uintptr, error) {
|
||||||
|
// Warning: failing to set FD_CLOEXEC causes the duplicated file descriptor
|
||||||
|
// to leak into subprocesses created by exec.Command. If the file descriptor
|
||||||
|
// is a pipe, these subprocesses will hold the pipe open (i.e., prevent
|
||||||
|
// EOF), potentially beyond the lifetime of this process.
|
||||||
|
//
|
||||||
|
// This can break go test's timeouts. go test usually spawns a test process
|
||||||
|
// with its stdin and stderr streams hooked up to pipes; if the test process
|
||||||
|
// times out, it sends a SIGKILL and attempts to read stdin and stderr to
|
||||||
|
// completion. If the test process has itself spawned long-lived
|
||||||
|
// subprocesses that hold references to the stdin or stderr pipes, go test
|
||||||
|
// will hang until the subprocesses exit, rather defeating the purpose of
|
||||||
|
// a timeout.
|
||||||
|
nfd, err := unix.FcntlInt(fd, unix.F_DUPFD_CLOEXEC, 0)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return uintptr(nfd), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RedirectStderr is used to redirect internal writes to fd 2 to the
|
||||||
|
// specified file. This is needed to ensure that harcoded writes to fd
|
||||||
|
// 2 by e.g. the Go runtime are redirected to a log file of our
|
||||||
|
// choosing.
|
||||||
|
//
|
||||||
|
// We also override os.Stderr for those other parts of Go which use
|
||||||
|
// that and not fd 2 directly.
|
||||||
|
func RedirectStderr(f *os.File) error {
|
||||||
|
osStderrMu.Lock()
|
||||||
|
defer osStderrMu.Unlock()
|
||||||
|
if err := unix.Dup2(int(f.Fd()), unix.Stderr); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
os.Stderr = f
|
||||||
|
return nil
|
||||||
|
}
|
32
util/io/redirect_windows.go
Normal file
32
util/io/redirect_windows.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package io
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"golang.org/x/sys/windows"
|
||||||
|
)
|
||||||
|
|
||||||
|
// dupFD is used to initialize OrigStderr (see stderr_redirect.go).
|
||||||
|
func dupFD(fd uintptr) (uintptr, error) {
|
||||||
|
// Adapted from https://github.com/golang/go/blob/go1.8/src/syscall/exec_windows.go#L303.
|
||||||
|
p := windows.CurrentProcess()
|
||||||
|
var h windows.Handle
|
||||||
|
return uintptr(h), windows.DuplicateHandle(p, windows.Handle(fd), p, &h, 0, true, windows.DUPLICATE_SAME_ACCESS)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RedirectStderr is used to redirect internal writes to the error
|
||||||
|
// handle to the specified file. This is needed to ensure that
|
||||||
|
// harcoded writes to the error handle by e.g. the Go runtime are
|
||||||
|
// redirected to a log file of our choosing.
|
||||||
|
//
|
||||||
|
// We also override os.Stderr for those other parts of Go which use
|
||||||
|
// that and not fd 2 directly.
|
||||||
|
func RedirectStderr(f *os.File) error {
|
||||||
|
osStderrMu.Lock()
|
||||||
|
defer osStderrMu.Unlock()
|
||||||
|
if err := windows.SetStdHandle(windows.STD_ERROR_HANDLE, windows.Handle(f.Fd())); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
os.Stderr = f
|
||||||
|
return nil
|
||||||
|
}
|
@@ -493,13 +493,14 @@ func btSplitter(str string) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// queryToMap turns something like a[b][c]=4 into
|
// queryToMap turns something like a[b][c]=4 into
|
||||||
// map[string]interface{}{
|
//
|
||||||
// "a": map[string]interface{}{
|
// map[string]interface{}{
|
||||||
// "b": map[string]interface{}{
|
// "a": map[string]interface{}{
|
||||||
// "c": 4,
|
// "b": map[string]interface{}{
|
||||||
// },
|
// "c": 4,
|
||||||
// },
|
// },
|
||||||
// }
|
// },
|
||||||
|
// }
|
||||||
func queryToMap(param string) (map[string]interface{}, error) {
|
func queryToMap(param string) (map[string]interface{}, error) {
|
||||||
rawKey, rawValue, err := splitKeyAndValue(param)
|
rawKey, rawValue, err := splitKeyAndValue(param)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@@ -1,25 +0,0 @@
|
|||||||
package test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/DATA-DOG/go-sqlmock"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Test_NewSQLRowsFromFile(t *testing.T) {
|
|
||||||
db, c, err := sqlmock.New()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
rows, err := NewSQLRowsFromFile(c, "testdata/Call.csv")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if !strings.Contains(fmt.Sprintf("%#+v", rows), `cols:[]string{"DepAgrId", "DepAgrNum", "DepAgrDate", "DepAgrCloseDate", "AccCur", "MainFinaccNum", "MainFinaccName", "MainFinaccId", "MainFinaccOpenDt", "DepAgrStatus", "MainFinaccBal", "DepartCode", "CardAccId"}`) {
|
|
||||||
t.Fatal("invalid cols after import csv")
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,28 +1,63 @@
|
|||||||
package test
|
package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/DATA-DOG/go-sqlmock"
|
"github.com/DATA-DOG/go-sqlmock"
|
||||||
"go.unistack.org/micro/v4/client"
|
"go.unistack.org/micro/v4/client"
|
||||||
"go.unistack.org/micro/v4/codec"
|
"go.unistack.org/micro/v4/codec"
|
||||||
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func getExt(name string) string {
|
||||||
|
ext := filepath.Ext(name)
|
||||||
|
if len(ext) > 0 && ext[0] == '.' {
|
||||||
|
ext = ext[1:]
|
||||||
|
}
|
||||||
|
return ext
|
||||||
|
}
|
||||||
|
|
||||||
|
func getNameWithoutExt(name string) string {
|
||||||
|
return strings.TrimSuffix(name, filepath.Ext(name))
|
||||||
|
}
|
||||||
|
|
||||||
var ErrUnknownContentType = errors.New("unknown content type")
|
var ErrUnknownContentType = errors.New("unknown content type")
|
||||||
|
|
||||||
type Extension struct {
|
type Extension struct {
|
||||||
Ext []string
|
Ext []string
|
||||||
}
|
}
|
||||||
|
|
||||||
var ExtToTypes = map[string][]string{
|
var (
|
||||||
"json": {"application/json", "application/grpc+json"},
|
ExtToTypes = map[string][]string{
|
||||||
"yaml": {"application/yaml", "application/yml", "text/yaml", "text/yml"},
|
"json": {"application/json", "application/grpc+json"},
|
||||||
"yml": {"application/yaml", "application/yml", "text/yaml", "text/yml"},
|
"yaml": {"application/yaml", "application/yml", "text/yaml", "text/yml"},
|
||||||
"proto": {"application/grpc", "application/grpc+proto", "application/proto"},
|
"yml": {"application/yaml", "application/yml", "text/yaml", "text/yml"},
|
||||||
|
"proto": {"application/grpc", "application/grpc+proto", "application/proto"},
|
||||||
|
}
|
||||||
|
|
||||||
|
DefaultExts = []string{"csv", "json", "yaml", "yml", "proto"}
|
||||||
|
)
|
||||||
|
|
||||||
|
func clientCall(ctx context.Context, c client.Client, req client.Request, rsp interface{}) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewResponseFromFile(rspfile string) (*codec.Frame, error) {
|
||||||
|
rspbuf, err := os.ReadFile(rspfile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &codec.Frame{Data: rspbuf}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRequestFromFile(c client.Client, reqfile string) (client.Request, error) {
|
func NewRequestFromFile(c client.Client, reqfile string) (client.Request, error) {
|
||||||
@@ -32,10 +67,7 @@ func NewRequestFromFile(c client.Client, reqfile string) (client.Request, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
endpoint := path.Base(path.Dir(reqfile))
|
endpoint := path.Base(path.Dir(reqfile))
|
||||||
ext := path.Ext(reqfile)
|
ext := getExt(reqfile)
|
||||||
if len(ext) > 0 && ext[0] == '.' {
|
|
||||||
ext = ext[1:]
|
|
||||||
}
|
|
||||||
|
|
||||||
var ct string
|
var ct string
|
||||||
if cts, ok := ExtToTypes[ext]; ok {
|
if cts, ok := ExtToTypes[ext]; ok {
|
||||||
@@ -56,26 +88,189 @@ func NewRequestFromFile(c client.Client, reqfile string) (client.Request, error)
|
|||||||
return req, nil
|
return req, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSQLRowsFromFile(c sqlmock.Sqlmock, file string) (*sqlmock.Rows, error) {
|
func SQLFromFile(m sqlmock.Sqlmock, name string) error {
|
||||||
fp, err := os.Open(file)
|
fp, err := os.Open(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
defer fp.Close()
|
defer fp.Close()
|
||||||
|
return SQLFromReader(m, fp)
|
||||||
r := csv.NewReader(fp)
|
}
|
||||||
r.Comma = '\t'
|
|
||||||
r.Comment = '#'
|
func SQLFromBytes(m sqlmock.Sqlmock, buf []byte) error {
|
||||||
|
return SQLFromReader(m, bytes.NewReader(buf))
|
||||||
records, err := r.ReadAll()
|
}
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
func SQLFromString(m sqlmock.Sqlmock, buf string) error {
|
||||||
}
|
return SQLFromReader(m, strings.NewReader(buf))
|
||||||
|
}
|
||||||
rows := c.NewRows(records[0])
|
|
||||||
for idx := 1; idx < len(records); idx++ {
|
func SQLFromReader(m sqlmock.Sqlmock, r io.Reader) error {
|
||||||
rows.FromCSVString(strings.Join(records[idx], ";"))
|
var rows *sqlmock.Rows
|
||||||
}
|
var exp *sqlmock.ExpectedQuery
|
||||||
|
br := bufio.NewReader(r)
|
||||||
return rows, nil
|
|
||||||
|
for {
|
||||||
|
s, err := br.ReadString('\n')
|
||||||
|
if err != nil && err != io.EOF {
|
||||||
|
return err
|
||||||
|
} else if err == io.EOF && len(s) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if s[0] != '#' {
|
||||||
|
r := csv.NewReader(strings.NewReader(s))
|
||||||
|
r.Comma = ','
|
||||||
|
var records [][]string
|
||||||
|
records, err = r.ReadAll()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if rows == nil {
|
||||||
|
rows = m.NewRows(records[0])
|
||||||
|
} else {
|
||||||
|
for idx := 0; idx < len(records); idx++ {
|
||||||
|
rows.FromCSVString(strings.Join(records[idx], ","))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if rows != nil {
|
||||||
|
exp.WillReturnRows(rows)
|
||||||
|
rows = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(strings.ToLower(s[2:]), "begin"):
|
||||||
|
m.ExpectBegin()
|
||||||
|
case strings.HasPrefix(strings.ToLower(s[2:]), "commit"):
|
||||||
|
m.ExpectCommit()
|
||||||
|
case strings.HasPrefix(strings.ToLower(s[2:]), "rollback"):
|
||||||
|
m.ExpectRollback()
|
||||||
|
case strings.HasPrefix(strings.ToLower(s[2:]), "exec "):
|
||||||
|
m.ExpectExec(s[2+len("exec "):])
|
||||||
|
case strings.HasPrefix(strings.ToLower(s[2:]), "query "):
|
||||||
|
exp = m.ExpectQuery(s[2+len("query "):])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func RunWithClientExpectResults(ctx context.Context, c client.Client, m sqlmock.Sqlmock, dir string, exts []string) error {
|
||||||
|
tcases, err := GetCases(dir, exts)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
g, gctx := errgroup.WithContext(ctx)
|
||||||
|
if !strings.Contains(dir, "parallel") {
|
||||||
|
g.SetLimit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tcase := range tcases {
|
||||||
|
for _, dbfile := range tcase.dbfiles {
|
||||||
|
if err = SQLFromFile(m, dbfile); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for idx := 0; idx < len(tcase.reqfiles); idx++ {
|
||||||
|
g.TryGo(func() error {
|
||||||
|
req, err := NewRequestFromFile(c, tcase.reqfiles[idx])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rsp, err := NewResponseFromFile(tcase.rspfiles[idx])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
data := &codec.Frame{}
|
||||||
|
err = c.Call(gctx, req, data, client.WithContentType(req.ContentType()))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !bytes.Equal(rsp.Data, data.Data) {
|
||||||
|
return fmt.Errorf("rsp not equal test %s != %s", rsp.Data, data.Data)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return g.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
func RunWithClientExpectErrors(ctx context.Context, c client.Client, dir string) error {
|
||||||
|
g, gctx := errgroup.WithContext(ctx)
|
||||||
|
if !strings.Contains(dir, "parallel") {
|
||||||
|
g.SetLimit(1)
|
||||||
|
}
|
||||||
|
_ = gctx
|
||||||
|
g.TryGo(func() error {
|
||||||
|
// rsp := &codec.Frame{}
|
||||||
|
// return c.Call(ctx, req, rsp, client.WithContentType(req.ContentType()))
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return g.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
type Case struct {
|
||||||
|
dbfiles []string
|
||||||
|
reqfiles []string
|
||||||
|
rspfiles []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetCases(dir string, exts []string) ([]Case, error) {
|
||||||
|
var tcases []Case
|
||||||
|
entries, err := os.ReadDir(dir)
|
||||||
|
if len(entries) == 0 && err != nil {
|
||||||
|
return tcases, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if exts == nil {
|
||||||
|
exts = DefaultExts
|
||||||
|
}
|
||||||
|
|
||||||
|
var dirs []string
|
||||||
|
var dbfiles, reqfiles, rspfiles []string
|
||||||
|
|
||||||
|
for _, entry := range entries {
|
||||||
|
if entry.IsDir() {
|
||||||
|
dirs = append(dirs, filepath.Join(dir, entry.Name()))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if info, err := entry.Info(); err != nil {
|
||||||
|
return tcases, err
|
||||||
|
} else if !info.Mode().IsRegular() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ext := range exts {
|
||||||
|
if getExt(entry.Name()) == ext {
|
||||||
|
name := getNameWithoutExt(entry.Name())
|
||||||
|
switch {
|
||||||
|
case strings.HasSuffix(name, "_db"):
|
||||||
|
dbfiles = append(dbfiles, filepath.Join(dir, entry.Name()))
|
||||||
|
case strings.HasSuffix(name, "_req"):
|
||||||
|
reqfiles = append(reqfiles, filepath.Join(dir, entry.Name()))
|
||||||
|
case strings.HasSuffix(name, "_rsp"):
|
||||||
|
rspfiles = append(rspfiles, filepath.Join(dir, entry.Name()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(reqfiles) > 0 && len(rspfiles) > 0 {
|
||||||
|
tcases = append(tcases, Case{dbfiles: dbfiles, reqfiles: reqfiles, rspfiles: rspfiles})
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, dir = range dirs {
|
||||||
|
ntcases, err := GetCases(dir, exts)
|
||||||
|
if len(ntcases) == 0 && err != nil {
|
||||||
|
return tcases, err
|
||||||
|
} else if len(ntcases) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
tcases = append(tcases, ntcases...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return tcases, nil
|
||||||
}
|
}
|
||||||
|
72
util/test/test_test.go
Normal file
72
util/test/test_test.go
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/DATA-DOG/go-sqlmock"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_SQLFromFile(t *testing.T) {
|
||||||
|
ctx := context.TODO()
|
||||||
|
db, c, err := sqlmock.New()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
if err = SQLFromFile(c, "testdata/result/01_firstcase/Call_db.csv"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tx, err := db.BeginTx(ctx, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := tx.QueryContext(ctx, "select * from test;")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for rows.Next() {
|
||||||
|
var id int64
|
||||||
|
var name string
|
||||||
|
err = rows.Scan(&id, &name)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if id != 1 || name != "test" {
|
||||||
|
t.Fatalf("invalid rows %v %v", id, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = rows.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = rows.Err(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = tx.Commit(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err = c.ExpectationsWereMet(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_GetCases(t *testing.T) {
|
||||||
|
files, err := GetCases("testdata/", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(files) == 0 {
|
||||||
|
t.Fatalf("no files matching")
|
||||||
|
}
|
||||||
|
|
||||||
|
if n := len(files); n != 1 {
|
||||||
|
t.Fatalf("invalid number of test cases %d", n)
|
||||||
|
}
|
||||||
|
}
|
5
util/test/testdata/result/01_firstcase/Call_db.csv
vendored
Normal file
5
util/test/testdata/result/01_firstcase/Call_db.csv
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# begin
|
||||||
|
# query select \* from test;
|
||||||
|
id,name
|
||||||
|
1,test
|
||||||
|
# commit
|
|
1
util/test/testdata/result/01_firstcase/Call_req.json
vendored
Normal file
1
util/test/testdata/result/01_firstcase/Call_req.json
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{}
|
1
util/test/testdata/result/01_firstcase/Call_rsp.json
vendored
Normal file
1
util/test/testdata/result/01_firstcase/Call_rsp.json
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{}
|
@@ -1,6 +1,5 @@
|
|||||||
package text
|
package text
|
||||||
|
|
||||||
|
|
||||||
func DetectEncoding(text string) map[string]int {
|
func DetectEncoding(text string) map[string]int {
|
||||||
charsets := map[string]int{
|
charsets := map[string]int{
|
||||||
"UTF-8": 0,
|
"UTF-8": 0,
|
||||||
@@ -77,7 +76,7 @@ func DetectEncoding(text string) map[string]int {
|
|||||||
charsets["MAC"] += uppercase
|
charsets["MAC"] += uppercase
|
||||||
}
|
}
|
||||||
|
|
||||||
last_simb = char
|
last_simb = char
|
||||||
}
|
}
|
||||||
|
|
||||||
return charsets
|
return charsets
|
||||||
|
Reference in New Issue
Block a user