Skip to content

Commit

Permalink
update test case golden file
Browse files Browse the repository at this point in the history
  go test ./... -update
  • Loading branch information
martianzhang committed Dec 4, 2018
1 parent dda41ec commit 1fa1dab
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 36 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ast/pretty.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/soar/soar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 3 additions & 6 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 16 additions & 5 deletions common/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package common
import (
"flag"
"os"
"path/filepath"
"testing"

"github.com/kr/pretty"
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion common/testdata/TestParseDSN.golden
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
31 changes: 16 additions & 15 deletions common/testdata/TestPrintConfiguration.golden
Original file line number Diff line number Diff line change
@@ -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: ""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
7 changes: 2 additions & 5 deletions database/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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;",
Expand Down Expand Up @@ -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) {
Expand All @@ -111,6 +110,9 @@ func TestNewVirtualEnv(t *testing.T) {
}
}
}, t.Name(), update)
if err != nil {
t.Error(err)
}
}

func TestCleanupTestDatabase(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion env/testdata/TestNewVirtualEnv.golden
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 1fa1dab

Please sign in to comment.