From 6e335920855915647c025eb24accaf2bdc40eee4 Mon Sep 17 00:00:00 2001 From: Jacky Zhen Date: Wed, 24 Oct 2018 22:20:16 +1300 Subject: [PATCH] Add test case for multi sql type + refactoring --- main_test.go | 253 +++++++++++++++++++++++++------------------------- sql_runner.go | 2 +- 2 files changed, 129 insertions(+), 126 deletions(-) diff --git a/main_test.go b/main_test.go index 48af77b..26bdfbd 100644 --- a/main_test.go +++ b/main_test.go @@ -11,56 +11,126 @@ import ( "time" ) +type tests []struct { + name string + targetDBs []string + query string + expected []string +} +type testConfig map[string]database + +var baseTests = tests{ + { + name: "reads from one database", + targetDBs: []string{"db1"}, + query: "SELECT id FROM table1", + expected: []string{ + "", + "1", + "2", + "3", + }, + }, + { + name: "reads from two databases", + targetDBs: []string{"db1", "db2"}, + query: "SELECT id FROM table1", + expected: []string{ + "", + "db1\t1", + "db1\t2", + "db1\t3", + "db2\t1", + "db2\t2", + "db2\t3", + }, + }, + { + name: "reads from all databases with the all keyword", + targetDBs: []string{"all"}, + query: "SELECT id FROM table1", + expected: []string{ + "", + "db1\t1", + "db1\t2", + "db1\t3", + "db2\t1", + "db2\t2", + "db2\t3", + "db3\t1", + "db3\t2", + "db3\t3", + }, + }, + { + name: "reads two fields from all databases", + targetDBs: []string{"all"}, + query: "SELECT id, name FROM table1", + expected: []string{ + "", + "db1\t1\tJohn", + "db1\t2\tGeorge", + "db1\t3\tRichard", + "db2\t1\tRob", + "db2\t2\tKen", + "db2\t3\tRobert", + "db3\t1\tAthos", + "db3\t2\tPorthos", + "db3\t3\tAramis", + }, + }, +} + func Test_MySQL(t *testing.T) { - var err error - for i := 1; i <= 30; i++ { // Try up to 30 times, because MySQL takes a while to become online - var c = exec.Command("mysql", "-h", "test-mysql", "-u", "root", "-e", "SELECT * FROM db1.table1") - if err = c.Run(); err == nil { - break - } - log.Printf("Retrying (%v/30) in 1 sec because MySQL is not yet ready", i) - time.Sleep(1 * time.Second) - } - for err != nil { - t.Errorf("bailing because couldn't connect to MySQL after 30 tries: %v", err) - t.FailNow() - } + awaitDB(mySQL, t) var ( - testConfig = map[string]database{ + testConfig = testConfig{ "db1": database{DbServer: "test-mysql", DbName: "db1", User: "root", Pass: "", SQLType: "mysql"}, "db2": database{DbServer: "test-mysql", DbName: "db2", User: "root", Pass: "", SQLType: ""}, "db3": database{DbServer: "test-mysql", DbName: "db3", User: "root", Pass: "", SQLType: ""}, } - ts = []struct { - name string - targetDBs []string - query string - expected []string - }{ - { - name: "reads from one database", - targetDBs: []string{"db1"}, - query: "SELECT id FROM table1", - expected: []string{ - "", - "1", - "2", - "3", - }, - }, + ) + runTests(baseTests, testConfig, t) +} + +func Test_PostgreSQL(t *testing.T) { + awaitDB(postgreSQL, t) + + var ( + testConfig = testConfig{ + "db1": database{DbServer: "test-postgres", DbName: "db1", User: "root", Pass: "", SQLType: "postgres"}, + "db2": database{DbServer: "test-postgres", DbName: "db2", User: "root", Pass: "", SQLType: "postgres"}, + "db3": database{DbServer: "test-postgres", DbName: "db3", User: "root", Pass: "", SQLType: "postgres"}, + } + ) + runTests(baseTests, testConfig, t) +} + +func Test_Mix_Mysql_PostgreSQL(t *testing.T) { + awaitDB(mySQL, t) + awaitDB(postgreSQL, t) + + var ( + testConfig = testConfig{ + "db1": database{DbServer: "test-postgres", DbName: "db1", User: "root", Pass: "", SQLType: "postgres"}, + "db2": database{DbServer: "test-postgres", DbName: "db2", User: "root", Pass: "", SQLType: "postgres"}, + "db3": database{DbServer: "test-mysql", DbName: "db3", User: "root", Pass: "", SQLType: ""}, + "db4": database{DbServer: "test-mysql", DbName: "db1", User: "root", Pass: "", SQLType: "mysql"}, + } + ts = tests{ { name: "reads from two databases", - targetDBs: []string{"db1", "db2"}, + targetDBs: []string{"db1", "db3"}, query: "SELECT id FROM table1", expected: []string{ "", "db1\t1", "db1\t2", "db1\t3", - "db2\t1", - "db2\t2", - "db2\t3", + "db3\t1", + "db3\t2", + "db3\t3", }, }, { @@ -78,6 +148,9 @@ func Test_MySQL(t *testing.T) { "db3\t1", "db3\t2", "db3\t3", + "db4\t1", + "db4\t2", + "db4\t3", }, }, { @@ -95,10 +168,17 @@ func Test_MySQL(t *testing.T) { "db3\t1\tAthos", "db3\t2\tPorthos", "db3\t3\tAramis", + "db4\t1\tJohn", + "db4\t2\tGeorge", + "db4\t3\tRichard", }, }, } ) + runTests(ts, testConfig, t) +} + +func runTests(ts tests, testConfig testConfig, t *testing.T) { for _, tc := range ts { t.Run(tc.name, func(t *testing.T) { var buf = bytes.Buffer{} @@ -106,109 +186,32 @@ func Test_MySQL(t *testing.T) { var actual = strings.Split(buf.String(), "\n") sort.Strings(actual) if !reflect.DeepEqual(tc.expected, actual) { - t.Errorf("Expected %v but got %v", tc.expected, actual) + t.Errorf("Expected\n%#v\n but got\n%#v\n", tc.expected, actual) } }) } } -func Test_PostgreSQL(t *testing.T) { +func awaitDB(typ sqlType, t *testing.T) { + var pgTestCmds = []string{"-h", "test-postgres", "-U", "root", "-d", "db1", "-c", "SELECT * FROM table1"} + var msTestCmds = []string{"-h", "test-mysql", "-u", "root", "-e", "SELECT * FROM db1.table1"} var err error + var c *exec.Cmd for i := 1; i <= 30; i++ { - var c = exec.Command("psql", "-h", "test-postgres", "-U", "root", "-d", "db1", "-c", "SELECT * FROM table1") + if typ == mySQL { + c = exec.Command("mysql", msTestCmds...) + } else { + c = exec.Command("psql", pgTestCmds...) + } if err = c.Run(); err == nil { + log.Printf("%s ready after %v/30 tries", typ, i) break } - log.Printf("Retrying (%v/30) in 1 sec because PostgreSQL is not yet ready", i) + log.Printf("Retrying (%v/30) in 1 sec because %s is not yet ready", i, typ) time.Sleep(1 * time.Second) } for err != nil { - t.Errorf("bailing because couldn't connect to PostgreSQL after 30 tries: %v", err) + t.Errorf("bailing because couldn't connect to %s after 30 tries: %v", typ, err) t.FailNow() } - - var ( - testConfig = map[string]database{ - "db1": database{DbServer: "test-postgres", DbName: "db1", User: "root", Pass: "", SQLType: "postgres"}, - "db2": database{DbServer: "test-postgres", DbName: "db2", User: "root", Pass: "", SQLType: "postgres"}, - "db3": database{DbServer: "test-postgres", DbName: "db3", User: "root", Pass: "", SQLType: "postgres"}, - } - ts = []struct { - name string - targetDBs []string - query string - expected []string - }{ - { - name: "reads from one database", - targetDBs: []string{"db1"}, - query: "SELECT id FROM table1", - expected: []string{ - "", - "1", - "2", - "3", - }, - }, - { - name: "reads from two databases", - targetDBs: []string{"db1", "db2"}, - query: "SELECT id FROM table1", - expected: []string{ - "", - "db1\t1", - "db1\t2", - "db1\t3", - "db2\t1", - "db2\t2", - "db2\t3", - }, - }, - { - name: "reads from all databases with the all keyword", - targetDBs: []string{"all"}, - query: "SELECT id FROM table1", - expected: []string{ - "", - "db1\t1", - "db1\t2", - "db1\t3", - "db2\t1", - "db2\t2", - "db2\t3", - "db3\t1", - "db3\t2", - "db3\t3", - }, - }, - { - name: "reads two fields from all databases", - targetDBs: []string{"all"}, - query: "SELECT id, name FROM table1", - expected: []string{ - "", - "db1\t1\tJohn", - "db1\t2\tGeorge", - "db1\t3\tRichard", - "db2\t1\tRob", - "db2\t2\tKen", - "db2\t3\tRobert", - "db3\t1\tAthos", - "db3\t2\tPorthos", - "db3\t3\tAramis", - }, - }, - } - ) - for _, tc := range ts { - t.Run(tc.name, func(t *testing.T) { - var buf = bytes.Buffer{} - _main(testConfig, tc.targetDBs, tc.query, newThreadSafePrintliner(&buf).println) - var actual = strings.Split(buf.String(), "\n") - sort.Strings(actual) - if !reflect.DeepEqual(tc.expected, actual) { - t.Errorf("Expected %v but got %v", tc.expected, actual) - } - }) - } } diff --git a/sql_runner.go b/sql_runner.go index c97cb38..fdec780 100644 --- a/sql_runner.go +++ b/sql_runner.go @@ -18,7 +18,7 @@ const ( ) func (t sqlType) String() string { - return [...]string{"MySQL", "PostgreSQL"}[t] + return [...]string{"", "MySQL", "PostgreSQL"}[t] } type exists struct{}