From 1fa1dab4577d005a4be3c7eefb2a65d0768cf269 Mon Sep 17 00:00:00 2001 From: Leon Zhang Date: Tue, 4 Dec 2018 15:52:30 +0800 Subject: [PATCH] update test case golden file go test ./... -update --- Makefile | 6 ++++ ast/pretty.go | 2 +- cmd/soar/soar_test.go | 4 +++ common/config.go | 9 ++---- common/config_test.go | 21 ++++++++++--- common/testdata/TestParseDSN.golden | 2 +- common/testdata/TestPrintConfiguration.golden | 31 ++++++++++--------- database/trace_test.go | 7 ++--- env/env_test.go | 6 ++-- env/testdata/TestNewVirtualEnv.golden | 1 - 10 files changed, 53 insertions(+), 36 deletions(-) diff --git a/Makefile b/Makefile index 03cb66c..72c2d34 100644 --- a/Makefile +++ b/Makefile @@ -64,6 +64,12 @@ test: go test ./... @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 # colorful coverage numerical >=90% GREEN, <80% RED, Other YELLOW .PHONY: cover diff --git a/ast/pretty.go b/ast/pretty.go index 6e4862f..69895a8 100644 --- a/ast/pretty.go +++ b/ast/pretty.go @@ -32,7 +32,7 @@ func Pretty(sql string, method string) (output string) { // 超出 Config.MaxPrettySQLLength 长度的 SQL 会对其指纹进行 pretty if len(sql) > common.Config.MaxPrettySQLLength { fingerprint := query.Fingerprint(sql) - // 超出 Config.MaxFpPrettySqlLength 长度的指纹不会进行pretty + // 超出 Config.MaxPrettySQLLength 长度的指纹不会进行pretty if len(fingerprint) > common.Config.MaxPrettySQLLength { return sql } diff --git a/cmd/soar/soar_test.go b/cmd/soar/soar_test.go index 82be382..dc75b60 100644 --- a/cmd/soar/soar_test.go +++ b/cmd/soar/soar_test.go @@ -17,13 +17,17 @@ package main import ( + "flag" "testing" "github.com/XiaoMi/soar/common" ) +var update = flag.Bool("update", false, "update .golden files") + func init() { common.Config.OnlineDSN.Schema = "sakila" + _ = update } func Test_Main(_ *testing.T) { diff --git a/common/config.go b/common/config.go index 4f84116..2c36bd2 100644 --- a/common/config.go +++ b/common/config.go @@ -25,6 +25,7 @@ import ( "io" "io/ioutil" "os" + "path/filepath" "regexp" "runtime" "strings" @@ -587,17 +588,13 @@ func readCmdFlags() error { Config.QueryTimeOut = *queryTimeOut Config.LogLevel = *logLevel - if strings.HasPrefix(*logOutput, "/") { + if filepath.IsAbs(*logOutput) { Config.LogOutput = *logOutput } else { if BaseDir == "" { Config.LogOutput = *logOutput } else { - if runtime.GOOS == "windows" { - Config.LogOutput = *logOutput - } else { - Config.LogOutput = BaseDir + "/" + *logOutput - } + Config.LogOutput = filepath.Join(BaseDir, *logOutput) } } Config.ReportType = strings.ToLower(*reportType) diff --git a/common/config_test.go b/common/config_test.go index 48f5a7a..6b61aef 100644 --- a/common/config_test.go +++ b/common/config_test.go @@ -19,6 +19,7 @@ package common import ( "flag" "os" + "path/filepath" "testing" "github.com/kr/pretty" @@ -37,7 +38,7 @@ func TestReadConfigFile(t *testing.T) { if Config == nil { Config = new(Configuration) } - Config.readConfigFile("../soar.yaml") + Config.readConfigFile(filepath.Join(DevPath, "etc/soar.yaml")) } func TestParseDSN(t *testing.T) { @@ -59,11 +60,14 @@ func TestParseDSN(t *testing.T) { "/database", } - GoldenDiff(func() { + err := GoldenDiff(func() { for _, dsn := range dsns { pretty.Println(parseDSN(dsn, nil)) } }, t.Name(), update) + if nil != err { + t.Fatal(err) + } } func TestListReportTypes(t *testing.T) { @@ -100,7 +104,14 @@ func TestArgConfig(t *testing.T) { } func TestPrintConfiguration(t *testing.T) { - Config.Verbose = true - PrintConfiguration() - + Config.readConfigFile(filepath.Join(DevPath, "etc/soar.yaml")) + oldLogOutput := Config.LogOutput + Config.LogOutput = "soar.log" + err := GoldenDiff(func() { + PrintConfiguration() + }, t.Name(), update) + if err != nil { + t.Error(err) + } + Config.LogOutput = oldLogOutput } diff --git a/common/testdata/TestParseDSN.golden b/common/testdata/TestParseDSN.golden index af08f5e..749a499 100644 --- a/common/testdata/TestParseDSN.golden +++ b/common/testdata/TestParseDSN.golden @@ -1,4 +1,4 @@ -&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:"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} diff --git a/common/testdata/TestPrintConfiguration.golden b/common/testdata/TestPrintConfiguration.golden index 09bd65a..314b355 100644 --- a/common/testdata/TestPrintConfiguration.golden +++ b/common/testdata/TestPrintConfiguration.golden @@ -1,31 +1,31 @@ online-dsn: - addr: "" - schema: information_schema - user: "" - password: "" + addr: 127.0.0.1:3306 + schema: sakila + user: root + password: '********' charset: utf8mb4 - disable: true + disable: false test-dsn: - addr: "" - schema: information_schema - user: "" - password: "" + addr: 127.0.0.1:3306 + schema: sakila + user: root + password: '********' charset: utf8mb4 - disable: true -allow-online-as-test: false + disable: false +allow-online-as-test: true drop-test-temporary: true cleanup-test-database: false only-syntax-check: false sampling-statistic-target: 100 -sampling: false +sampling: true profiling: false trace: false explain: true conn-time-out: 3 query-time-out: 30 delimiter: ; -log-level: 3 -log-output: /dev/stderr +log-level: 7 +log-output: soar.log report-type: markdown report-css: "" report-javascript: "" @@ -62,6 +62,7 @@ table-allow-engines: - innodb max-index-count: 10 max-column-count: 40 +max-value-count: 100 index-prefix: idx_ unique-key-prefix: uk_ max-subquery-depth: 5 @@ -89,6 +90,6 @@ list-heuristic-rules: false list-rewrite-rules: false list-test-sqls: false list-report-types: false -verbose: true +verbose: false dry-run: true max-pretty-sql-length: 1024 diff --git a/database/trace_test.go b/database/trace_test.go index 8dea2d7..faf1f55 100644 --- a/database/trace_test.go +++ b/database/trace_test.go @@ -30,13 +30,10 @@ var update = flag.Bool("update", false, "update .golden files") func TestTrace(t *testing.T) { common.Config.QueryTimeOut = 1 res, err := connTest.Trace("select 1") - if err == nil { - common.GoldenDiff(func() { - pretty.Println(res) - }, t.Name(), update) - } else { + if err != nil { t.Error(err) } + pretty.Println(res) } func TestFormatTrace(t *testing.T) { diff --git a/env/env_test.go b/env/env_test.go index 887c347..3cf674b 100644 --- a/env/env_test.go +++ b/env/env_test.go @@ -46,7 +46,6 @@ func TestNewVirtualEnv(t *testing.T) { testSQL := []string{ "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);", "select * from city where country_id = 44;", "select * from address where address2 is not null;", "select * from address where address2 is null;", @@ -92,7 +91,7 @@ func TestNewVirtualEnv(t *testing.T) { env := NewVirtualEnv(connTest) defer env.CleanUp() - common.GoldenDiff(func() { + err := common.GoldenDiff(func() { for _, sql := range testSQL { env.BuildVirtualEnv(rEnv, sql) switch err := env.Error.(type) { @@ -111,6 +110,9 @@ func TestNewVirtualEnv(t *testing.T) { } } }, t.Name(), update) + if err != nil { + t.Error(err) + } } func TestCleanupTestDatabase(t *testing.T) { diff --git a/env/testdata/TestNewVirtualEnv.golden b/env/testdata/TestNewVirtualEnv.golden index 65edf3f..63b6946 100644 --- a/env/testdata/TestNewVirtualEnv.golden +++ b/env/testdata/TestNewVirtualEnv.golden @@ -1,6 +1,5 @@ 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 select * from city where country_id = 44; OK select * from address where address2 is not null; OK select * from address where address2 is null; OK -- GitLab