Skip to content

Commit

Permalink
✅ Add test for internal/config/mysql.go (#563)
Browse files Browse the repository at this point in the history
* feat: add test case

Signed-off-by: hlts2 <[email protected]>

* feat: add comment

Signed-off-by: hlts2 <[email protected]>
  • Loading branch information
hlts2 authored Jul 10, 2020
1 parent 45a2381 commit f707aae
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 53 deletions.
2 changes: 2 additions & 0 deletions internal/config/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// Package config providers configuration type and load configuration logic
package config

// MySQL represent the mysql configuration.
type MySQL struct {
DB string `json:"db" yaml:"db"`
Host string `json:"host" yaml:"host"`
Expand All @@ -35,6 +36,7 @@ type MySQL struct {
TCP *TCP `json:"tcp" yaml:"tcp"`
}

// Bind returns MySQL object whose some string value is filed value or environment value.
func (m *MySQL) Bind() *MySQL {
if m.TLS != nil {
m.TLS.Bind()
Expand Down
202 changes: 149 additions & 53 deletions internal/config/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package config

import (
"os"
"reflect"
"testing"

Expand Down Expand Up @@ -60,59 +61,155 @@ func TestMySQL_Bind(t *testing.T) {
return nil
}
tests := []test{
// TODO test cases
/*
{
name: "test_case_1",
fields: fields {
DB: "",
Host: "",
Port: 0,
User: "",
Pass: "",
Name: "",
Charset: "",
Timezone: "",
InitialPingTimeLimit: "",
InitialPingDuration: "",
ConnMaxLifeTime: "",
MaxOpenConns: 0,
MaxIdleConns: 0,
TLS: TLS{},
TCP: TCP{},
},
want: want{},
checkFunc: defaultCheckFunc,
},
*/
{
name: "return MySQL when all fields contain no prefix/suffix symbol and tls and tcp configuration is not set",
fields: fields{
DB: "db",
Host: "host",
Port: 80,
User: "user",
Pass: "pass",
Name: "name",
Charset: "charset",
Timezone: "timezone",
InitialPingTimeLimit: "initialPingTimeLimit",
InitialPingDuration: "initialPingDuration",
ConnMaxLifeTime: "connMaxLifeTime",
MaxOpenConns: 10,
MaxIdleConns: 100,
},
want: want{
want: &MySQL{
DB: "db",
Host: "host",
Port: 80,
User: "user",
Pass: "pass",
Name: "name",
Charset: "charset",
Timezone: "timezone",
InitialPingTimeLimit: "initialPingTimeLimit",
InitialPingDuration: "initialPingDuration",
ConnMaxLifeTime: "connMaxLifeTime",
MaxOpenConns: 10,
MaxIdleConns: 100,
TLS: new(TLS),
TCP: new(TCP),
},
},
},

// TODO test cases
/*
func() test {
return test {
name: "test_case_2",
fields: fields {
DB: "",
Host: "",
Port: 0,
User: "",
Pass: "",
Name: "",
Charset: "",
Timezone: "",
InitialPingTimeLimit: "",
InitialPingDuration: "",
ConnMaxLifeTime: "",
MaxOpenConns: 0,
MaxIdleConns: 0,
TLS: TLS{},
TCP: TCP{},
},
want: want{},
checkFunc: defaultCheckFunc,
}
}(),
*/
{
name: "return MySQL when all fields contain no prefix/suffix symbol and tls and tcp configuration is set",
fields: fields{
DB: "db",
Host: "host",
Port: 80,
User: "user",
Pass: "pass",
Name: "name",
Charset: "charset",
Timezone: "timezone",
InitialPingTimeLimit: "initialPingTimeLimit",
InitialPingDuration: "initialPingDuration",
ConnMaxLifeTime: "connMaxLifeTime",
MaxOpenConns: 10,
MaxIdleConns: 100,
TLS: &TLS{
Enabled: true,
},
TCP: &TCP{
DNS: new(DNS),
},
},
want: want{
want: &MySQL{
DB: "db",
Host: "host",
Port: 80,
User: "user",
Pass: "pass",
Name: "name",
Charset: "charset",
Timezone: "timezone",
InitialPingTimeLimit: "initialPingTimeLimit",
InitialPingDuration: "initialPingDuration",
ConnMaxLifeTime: "connMaxLifeTime",
MaxOpenConns: 10,
MaxIdleConns: 100,
TLS: &TLS{
Enabled: true,
},
TCP: &TCP{
DNS: new(DNS),
},
},
},
},

{
name: "return MySQL with environment variable when it contains `_` as prefix and suffix",
fields: fields{
DB: "_db_",
Host: "_host_",
Port: 80,
User: "_user_",
Pass: "_pass_",
Name: "_name_",
Charset: "_charset_",
Timezone: "_timezone_",
InitialPingTimeLimit: "_initialPingTimeLimit_",
InitialPingDuration: "_initialPingDuration_",
ConnMaxLifeTime: "_connMaxLifeTime_",
MaxOpenConns: 10,
MaxIdleConns: 100,
TLS: new(TLS),
TCP: new(TCP),
},
want: want{
want: &MySQL{
DB: "db",
Host: "host",
Port: 80,
User: "user",
Pass: "pass",
Name: "name",
Charset: "charset",
Timezone: "timezone",
InitialPingTimeLimit: "initialPingTimeLimit",
InitialPingDuration: "initialPingDuration",
ConnMaxLifeTime: "connMaxLifeTime",
MaxOpenConns: 10,
MaxIdleConns: 100,
TLS: new(TLS),
TCP: new(TCP),
},
},
beforeFunc: func() {
_ = os.Setenv("db", "db")
_ = os.Setenv("host", "host")
_ = os.Setenv("user", "user")
_ = os.Setenv("pass", "pass")
_ = os.Setenv("name", "name")
_ = os.Setenv("charset", "charset")
_ = os.Setenv("timezone", "timezone")
_ = os.Setenv("initialPingTimeLimit", "initialPingTimeLimit")
_ = os.Setenv("initialPingDuration", "initialPingDuration")
_ = os.Setenv("connMaxLifeTime", "connMaxLifeTime")
},
afterFunc: func() {
_ = os.Unsetenv("db")
_ = os.Unsetenv("host")
_ = os.Unsetenv("user")
_ = os.Unsetenv("pass")
_ = os.Unsetenv("name")
_ = os.Unsetenv("charset")
_ = os.Unsetenv("timezone")
_ = os.Unsetenv("initialPingTimeLimit")
_ = os.Unsetenv("initialPingDuration")
_ = os.Unsetenv("connMaxLifeTime")
},
},
}

for _, test := range tests {
Expand Down Expand Up @@ -148,7 +245,6 @@ func TestMySQL_Bind(t *testing.T) {
if err := test.checkFunc(test.want, got); err != nil {
tt.Errorf("error = %v", err)
}

})
}
}

0 comments on commit f707aae

Please sign in to comment.