提交 1fa1dab4 编写于 作者: martianzhang's avatar martianzhang

update test case golden file

  go test ./... -update
上级 dda41ec5
...@@ -64,6 +64,12 @@ test: ...@@ -64,6 +64,12 @@ test:
go test ./... go test ./...
@echo "test Success!" @echo "test Success!"
# Rule golang test cases with `-update` flag
test-update:
@echo "\033[92mRun all test cases with -update flag ...\033[0m"
go test ./... -update
@echo "test-update Success!"
# Code Coverage # Code Coverage
# colorful coverage numerical >=90% GREEN, <80% RED, Other YELLOW # colorful coverage numerical >=90% GREEN, <80% RED, Other YELLOW
.PHONY: cover .PHONY: cover
......
...@@ -32,7 +32,7 @@ func Pretty(sql string, method string) (output string) { ...@@ -32,7 +32,7 @@ func Pretty(sql string, method string) (output string) {
// 超出 Config.MaxPrettySQLLength 长度的 SQL 会对其指纹进行 pretty // 超出 Config.MaxPrettySQLLength 长度的 SQL 会对其指纹进行 pretty
if len(sql) > common.Config.MaxPrettySQLLength { if len(sql) > common.Config.MaxPrettySQLLength {
fingerprint := query.Fingerprint(sql) fingerprint := query.Fingerprint(sql)
// 超出 Config.MaxFpPrettySqlLength 长度的指纹不会进行pretty // 超出 Config.MaxPrettySQLLength 长度的指纹不会进行pretty
if len(fingerprint) > common.Config.MaxPrettySQLLength { if len(fingerprint) > common.Config.MaxPrettySQLLength {
return sql return sql
} }
......
...@@ -17,13 +17,17 @@ ...@@ -17,13 +17,17 @@
package main package main
import ( import (
"flag"
"testing" "testing"
"github.com/XiaoMi/soar/common" "github.com/XiaoMi/soar/common"
) )
var update = flag.Bool("update", false, "update .golden files")
func init() { func init() {
common.Config.OnlineDSN.Schema = "sakila" common.Config.OnlineDSN.Schema = "sakila"
_ = update
} }
func Test_Main(_ *testing.T) { func Test_Main(_ *testing.T) {
......
...@@ -25,6 +25,7 @@ import ( ...@@ -25,6 +25,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
"regexp" "regexp"
"runtime" "runtime"
"strings" "strings"
...@@ -587,17 +588,13 @@ func readCmdFlags() error { ...@@ -587,17 +588,13 @@ func readCmdFlags() error {
Config.QueryTimeOut = *queryTimeOut Config.QueryTimeOut = *queryTimeOut
Config.LogLevel = *logLevel Config.LogLevel = *logLevel
if strings.HasPrefix(*logOutput, "/") { if filepath.IsAbs(*logOutput) {
Config.LogOutput = *logOutput Config.LogOutput = *logOutput
} else { } else {
if BaseDir == "" { if BaseDir == "" {
Config.LogOutput = *logOutput Config.LogOutput = *logOutput
} else { } else {
if runtime.GOOS == "windows" { Config.LogOutput = filepath.Join(BaseDir, *logOutput)
Config.LogOutput = *logOutput
} else {
Config.LogOutput = BaseDir + "/" + *logOutput
}
} }
} }
Config.ReportType = strings.ToLower(*reportType) Config.ReportType = strings.ToLower(*reportType)
......
...@@ -19,6 +19,7 @@ package common ...@@ -19,6 +19,7 @@ package common
import ( import (
"flag" "flag"
"os" "os"
"path/filepath"
"testing" "testing"
"github.com/kr/pretty" "github.com/kr/pretty"
...@@ -37,7 +38,7 @@ func TestReadConfigFile(t *testing.T) { ...@@ -37,7 +38,7 @@ func TestReadConfigFile(t *testing.T) {
if Config == nil { if Config == nil {
Config = new(Configuration) Config = new(Configuration)
} }
Config.readConfigFile("../soar.yaml") Config.readConfigFile(filepath.Join(DevPath, "etc/soar.yaml"))
} }
func TestParseDSN(t *testing.T) { func TestParseDSN(t *testing.T) {
...@@ -59,11 +60,14 @@ func TestParseDSN(t *testing.T) { ...@@ -59,11 +60,14 @@ func TestParseDSN(t *testing.T) {
"/database", "/database",
} }
GoldenDiff(func() { err := GoldenDiff(func() {
for _, dsn := range dsns { for _, dsn := range dsns {
pretty.Println(parseDSN(dsn, nil)) pretty.Println(parseDSN(dsn, nil))
} }
}, t.Name(), update) }, t.Name(), update)
if nil != err {
t.Fatal(err)
}
} }
func TestListReportTypes(t *testing.T) { func TestListReportTypes(t *testing.T) {
...@@ -100,7 +104,14 @@ func TestArgConfig(t *testing.T) { ...@@ -100,7 +104,14 @@ func TestArgConfig(t *testing.T) {
} }
func TestPrintConfiguration(t *testing.T) { func TestPrintConfiguration(t *testing.T) {
Config.Verbose = true Config.readConfigFile(filepath.Join(DevPath, "etc/soar.yaml"))
PrintConfiguration() oldLogOutput := Config.LogOutput
Config.LogOutput = "soar.log"
err := GoldenDiff(func() {
PrintConfiguration()
}, t.Name(), update)
if err != nil {
t.Error(err)
}
Config.LogOutput = oldLogOutput
} }
&common.dsn{Addr:"", Schema:"", User:"", Password:"", Charset:"", Disable:true, Version:0} (*common.dsn)(nil)
&common.dsn{Addr:"hostname:3307", Schema:"database", User:"user", Password:"password", Charset:"utf8mb4", Disable:false, Version:999} &common.dsn{Addr:"hostname:3307", Schema:"database", User:"user", Password:"password", Charset:"utf8mb4", Disable:false, Version:999}
&common.dsn{Addr:"hostname:3307", Schema:"information_schema", User:"user", Password:"password", Charset:"utf8mb4", Disable:false, Version:999} &common.dsn{Addr:"hostname:3307", Schema:"information_schema", User:"user", Password:"password", Charset:"utf8mb4", Disable:false, Version:999}
&common.dsn{Addr:"hostname:3306", Schema:"database", User:"user", Password:"password", Charset:"utf8mb4", Disable:false, Version:999} &common.dsn{Addr:"hostname:3306", Schema:"database", User:"user", Password:"password", Charset:"utf8mb4", Disable:false, Version:999}
......
online-dsn: online-dsn:
addr: "" addr: 127.0.0.1:3306
schema: information_schema schema: sakila
user: "" user: root
password: "" password: '********'
charset: utf8mb4 charset: utf8mb4
disable: true disable: false
test-dsn: test-dsn:
addr: "" addr: 127.0.0.1:3306
schema: information_schema schema: sakila
user: "" user: root
password: "" password: '********'
charset: utf8mb4 charset: utf8mb4
disable: true disable: false
allow-online-as-test: false allow-online-as-test: true
drop-test-temporary: true drop-test-temporary: true
cleanup-test-database: false cleanup-test-database: false
only-syntax-check: false only-syntax-check: false
sampling-statistic-target: 100 sampling-statistic-target: 100
sampling: false sampling: true
profiling: false profiling: false
trace: false trace: false
explain: true explain: true
conn-time-out: 3 conn-time-out: 3
query-time-out: 30 query-time-out: 30
delimiter: ; delimiter: ;
log-level: 3 log-level: 7
log-output: /dev/stderr log-output: soar.log
report-type: markdown report-type: markdown
report-css: "" report-css: ""
report-javascript: "" report-javascript: ""
...@@ -62,6 +62,7 @@ table-allow-engines: ...@@ -62,6 +62,7 @@ table-allow-engines:
- innodb - innodb
max-index-count: 10 max-index-count: 10
max-column-count: 40 max-column-count: 40
max-value-count: 100
index-prefix: idx_ index-prefix: idx_
unique-key-prefix: uk_ unique-key-prefix: uk_
max-subquery-depth: 5 max-subquery-depth: 5
...@@ -89,6 +90,6 @@ list-heuristic-rules: false ...@@ -89,6 +90,6 @@ list-heuristic-rules: false
list-rewrite-rules: false list-rewrite-rules: false
list-test-sqls: false list-test-sqls: false
list-report-types: false list-report-types: false
verbose: true verbose: false
dry-run: true dry-run: true
max-pretty-sql-length: 1024 max-pretty-sql-length: 1024
...@@ -30,13 +30,10 @@ var update = flag.Bool("update", false, "update .golden files") ...@@ -30,13 +30,10 @@ var update = flag.Bool("update", false, "update .golden files")
func TestTrace(t *testing.T) { func TestTrace(t *testing.T) {
common.Config.QueryTimeOut = 1 common.Config.QueryTimeOut = 1
res, err := connTest.Trace("select 1") res, err := connTest.Trace("select 1")
if err == nil { if err != nil {
common.GoldenDiff(func() {
pretty.Println(res)
}, t.Name(), update)
} else {
t.Error(err) t.Error(err)
} }
pretty.Println(res)
} }
func TestFormatTrace(t *testing.T) { func TestFormatTrace(t *testing.T) {
......
...@@ -46,7 +46,6 @@ func TestNewVirtualEnv(t *testing.T) { ...@@ -46,7 +46,6 @@ func TestNewVirtualEnv(t *testing.T) {
testSQL := []string{ testSQL := []string{
"create table t(id int,c1 varchar(20),PRIMARY KEY (id));", "create table t(id int,c1 varchar(20),PRIMARY KEY (id));",
"alter table t add index `idx_c1`(c1);", "alter table t add index `idx_c1`(c1);",
"alter table t add index `idx_c1`(c1);",
"select * from city where country_id = 44;", "select * from city where country_id = 44;",
"select * from address where address2 is not null;", "select * from address where address2 is not null;",
"select * from address where address2 is null;", "select * from address where address2 is null;",
...@@ -92,7 +91,7 @@ func TestNewVirtualEnv(t *testing.T) { ...@@ -92,7 +91,7 @@ func TestNewVirtualEnv(t *testing.T) {
env := NewVirtualEnv(connTest) env := NewVirtualEnv(connTest)
defer env.CleanUp() defer env.CleanUp()
common.GoldenDiff(func() { err := common.GoldenDiff(func() {
for _, sql := range testSQL { for _, sql := range testSQL {
env.BuildVirtualEnv(rEnv, sql) env.BuildVirtualEnv(rEnv, sql)
switch err := env.Error.(type) { switch err := env.Error.(type) {
...@@ -111,6 +110,9 @@ func TestNewVirtualEnv(t *testing.T) { ...@@ -111,6 +110,9 @@ func TestNewVirtualEnv(t *testing.T) {
} }
} }
}, t.Name(), update) }, t.Name(), update)
if err != nil {
t.Error(err)
}
} }
func TestCleanupTestDatabase(t *testing.T) { func TestCleanupTestDatabase(t *testing.T) {
......
create table t(id int,c1 varchar(20),PRIMARY KEY (id)); OK create table t(id int,c1 varchar(20),PRIMARY KEY (id)); OK
alter table t add index `idx_c1`(c1); OK alter table t add index `idx_c1`(c1); OK
alter table t add index `idx_c1`(c1); OK
select * from city where country_id = 44; OK select * from city where country_id = 44; OK
select * from address where address2 is not null; OK select * from address where address2 is not null; OK
select * from address where address2 is null; OK select * from address where address2 is null; OK
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册