Created
August 20, 2019 14:54
-
-
Save samaita/3fb4186282daf9ddb7612315193c3c9d to your computer and use it in GitHub Desktop.
Quick Make Unit Test for your Spaghetty Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package unittest | |
import ( | |
"context" | |
"errors" | |
"log" | |
"time" | |
"github.com/tokopedia/kunyit/src/conf" | |
"github.com/tokopedia/sqlt" | |
) | |
type User struct { | |
ID int64 | |
Username string | |
Status int | |
} | |
var GetUser = func(id int64) (User, error) { | |
var user User | |
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(conf.QueryTimeout)*time.Second) | |
defer cancel() | |
oauthClient := conf.DB.OauthClient | |
resp, err := oauthClient.GetBasicUserInfoClientCredentialsWithContext(ctx, id) | |
if err != nil { | |
log.Println(err) | |
return user, err | |
} | |
if resp.Error != "" { | |
err = errors.New(resp.Error) | |
return user, err | |
} | |
return user, nil | |
} | |
var InsertTalk = func(uid int64, message string) error { | |
var err error | |
db, err := sqlt.Open("postgres", "http://127.0.0.1") | |
if err != nil { | |
return err | |
} | |
if err := db.Ping(); err != nil { | |
return err | |
} | |
query := `INSERT INTO ws_talk (id, message) VALUE ($1, $2)` | |
_, err = db.ExecContext(context.TODO(), query, uid, message) | |
if err != nil { | |
return err | |
} | |
return err | |
} | |
func CreateTalk(uid int64, message string) error { | |
var err error | |
var u User | |
u, err = GetUser(uid) | |
if err != nil { | |
return err | |
} | |
err = InsertTalk(u.ID, message) | |
if err != nil { | |
return err | |
} | |
return err | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package unittest | |
import ( | |
"database/sql" | |
"fmt" | |
"testing" | |
) | |
func TestCreateTalk(t *testing.T) { | |
type args struct { | |
uid int64 | |
message string | |
errGetUser error | |
errInsertTalk error | |
} | |
tests := []struct { | |
name string | |
args args | |
wantErr bool | |
}{ | |
{ | |
name: "test create talk success", | |
args: args{ | |
uid: 100, | |
message: "hello world", | |
}, | |
wantErr: false, | |
}, | |
{ | |
name: "test create talk - failed to insert", | |
args: args{ | |
uid: 100, | |
message: "hellow world", | |
errInsertTalk: sql.ErrConnDone, | |
}, | |
wantErr: true, | |
}, | |
{ | |
name: "test create talk - failed to get user", | |
args: args{ | |
uid: 100, | |
message: "hellow world", | |
errInsertTalk: sql.ErrConnDone, | |
errGetUser: fmt.Errorf("failed to retrieve token, empty access token/expires in"), | |
}, | |
wantErr: true, | |
}, | |
} | |
for _, tt := range tests { | |
t.Run(tt.name, func(t *testing.T) { | |
GetUser = func(uid int64) (User, error) { | |
return User{ID: uid}, tt.args.errGetUser | |
} | |
InsertTalk = func(uid int64, message string) error { | |
return tt.args.errInsertTalk | |
} | |
if err := CreateTalk(tt.args.uid, tt.args.message); (err != nil) != tt.wantErr { | |
t.Errorf("CreateTalk() error = %v, wantErr %v", err, tt.wantErr) | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instead of
We use
And mock it in test