Skip to content

Commit

Permalink
Add test case for multi sql type + refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyzhen committed Oct 24, 2018
1 parent 434d613 commit 6e33592
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 126 deletions.
253 changes: 128 additions & 125 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
{
Expand All @@ -78,6 +148,9 @@ func Test_MySQL(t *testing.T) {
"db3\t1",
"db3\t2",
"db3\t3",
"db4\t1",
"db4\t2",
"db4\t3",
},
},
{
Expand All @@ -95,120 +168,50 @@ 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{}
_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)
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)
}
})
}
}
2 changes: 1 addition & 1 deletion sql_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
)

func (t sqlType) String() string {
return [...]string{"MySQL", "PostgreSQL"}[t]
return [...]string{"", "MySQL", "PostgreSQL"}[t]
}

type exists struct{}
Expand Down

0 comments on commit 6e33592

Please # to comment.