Skip to content

Commit

Permalink
補上遺漏的測試
Browse files Browse the repository at this point in the history
Test coverage 的目的不是 100% 覆蓋,而是該測的地方通通要測到
  • Loading branch information
Ronmi committed Jul 11, 2016
1 parent a166211 commit 2ec1d9e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
21 changes: 20 additions & 1 deletion cmd/xchg/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestAddDuplicated(t *testing.T) {
}
}

func TestAddBadParam(t *testing.T) {
func TestAddNoData(t *testing.T) {
mgr, err := initDB([]Order{})
if err != nil {
t.Fatalf("Cannot initial database: %s", err)
Expand All @@ -97,3 +97,22 @@ func TestAddBadParam(t *testing.T) {
t.Fatalf("unexpected status code %d for bas request: %s", resp.Code, resp.Body.String())
}
}

func TestAddNotJSON(t *testing.T) {
mgr, err := initDB([]Order{})
if err != nil {
t.Fatalf("Cannot initial database: %s", err)
}
defer mgr.Connection().Close()
h := &add{mgr}

resp, err := jsonapi.HandlerTest(h.Handle).Post("/api/add", "", `1234`)

if err != nil {
t.Fatalf("unexpected error occured when testing add: %s", err)
}

if resp.Code != http.StatusBadRequest {
t.Fatalf("unexpected status code %d for bas request: %s", resp.Code, resp.Body.String())
}
}
38 changes: 38 additions & 0 deletions cmd/xchg/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,41 @@ func TestListEmpty(t *testing.T) {
t.Errorf("list: not returning empty array: '%s'", str)
}
}

func TestListNoData(t *testing.T) {
mgr, err := initDB([]Order{})
if err != nil {
t.Fatalf("Cannot initial database: %s", err)
}
defer mgr.Connection().Close()
h := &list{mgr}

resp, err := jsonapi.HandlerTest(h.Handle).Post("/api/add", "", `{}`)

if err != nil {
t.Fatalf("unexpected error occured when testing add: %s", err)
}

if resp.Code != http.StatusBadRequest {
t.Fatalf("unexpected status code %d for bas request: %s", resp.Code, resp.Body.String())
}
}

func TestListNotJSON(t *testing.T) {
mgr, err := initDB([]Order{})
if err != nil {
t.Fatalf("Cannot initial database: %s", err)
}
defer mgr.Connection().Close()
h := &list{mgr}

resp, err := jsonapi.HandlerTest(h.Handle).Post("/api/add", "", `1234`)

if err != nil {
t.Fatalf("unexpected error occured when testing add: %s", err)
}

if resp.Code != http.StatusBadRequest {
t.Fatalf("unexpected status code %d for bas request: %s", resp.Code, resp.Body.String())
}
}

0 comments on commit 2ec1d9e

Please sign in to comment.