-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
319 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package handler | ||
|
||
import ( | ||
"go_todo_app/entity" | ||
"go_todo_app/store" | ||
"go_todo_app/testutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestListTask(t *testing.T) { | ||
type want struct { | ||
status int | ||
rspFile string | ||
} | ||
tests := map[string]struct { | ||
tasks map[entity.TaskID]*entity.Task | ||
want want | ||
}{ | ||
"ok": { | ||
tasks: map[entity.TaskID]*entity.Task{ | ||
1: { | ||
ID: 1, | ||
Title: "test1", | ||
Status: "todo", | ||
}, | ||
2: { | ||
ID: 2, | ||
Title: "test2", | ||
Status: "done", | ||
}, | ||
}, | ||
want: want{ | ||
status: http.StatusOK, | ||
rspFile: "testdata/list_task/ok_rsp.json.golden", | ||
}, | ||
}, | ||
"empty": { | ||
tasks: map[entity.TaskID]*entity.Task{}, | ||
want: want{ | ||
status: http.StatusOK, | ||
rspFile: "testdata/list_task/empty_rsp.json.golden", | ||
}, | ||
}, | ||
} | ||
for n, tt := range tests { | ||
tt := tt | ||
t.Run(n, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
w := httptest.NewRecorder() | ||
r := httptest.NewRequest(http.MethodGet, "/tasks", nil) | ||
|
||
sut := ListTask{Store: &store.TaskStore{Tasks: tt.tasks}} | ||
sut.ServeHTTP(w, r) | ||
|
||
resp := w.Result() | ||
testutil.AssertResponse(t, | ||
resp, tt.want.status, testutil.LoadFile(t, tt.want.rspFile), | ||
) | ||
}) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
_chapter17/section70/handler/testdata/list_task/empty_rsp.json.golden
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
12 changes: 12 additions & 0 deletions
12
_chapter17/section70/handler/testdata/list_task/ok_rsp.json.golden
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[ | ||
{ | ||
"id" : 1, | ||
"title" : "test1", | ||
"status" : "todo" | ||
}, | ||
{ | ||
"id" : 2, | ||
"title" : "test2", | ||
"status" : "done" | ||
} | ||
] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package handler | ||
|
||
import ( | ||
"go_todo_app/entity" | ||
"go_todo_app/store" | ||
"go_todo_app/testutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestListTask(t *testing.T) { | ||
type want struct { | ||
status int | ||
rspFile string | ||
} | ||
tests := map[string]struct { | ||
tasks map[entity.TaskID]*entity.Task | ||
want want | ||
}{ | ||
"ok": { | ||
tasks: map[entity.TaskID]*entity.Task{ | ||
1: { | ||
ID: 1, | ||
Title: "test1", | ||
Status: "todo", | ||
}, | ||
2: { | ||
ID: 2, | ||
Title: "test2", | ||
Status: "done", | ||
}, | ||
}, | ||
want: want{ | ||
status: http.StatusOK, | ||
rspFile: "testdata/list_task/ok_rsp.json.golden", | ||
}, | ||
}, | ||
"empty": { | ||
tasks: map[entity.TaskID]*entity.Task{}, | ||
want: want{ | ||
status: http.StatusOK, | ||
rspFile: "testdata/list_task/empty_rsp.json.golden", | ||
}, | ||
}, | ||
} | ||
for n, tt := range tests { | ||
tt := tt | ||
t.Run(n, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
w := httptest.NewRecorder() | ||
r := httptest.NewRequest(http.MethodGet, "/tasks", nil) | ||
|
||
sut := ListTask{Store: &store.TaskStore{Tasks: tt.tasks}} | ||
sut.ServeHTTP(w, r) | ||
|
||
resp := w.Result() | ||
testutil.AssertResponse(t, | ||
resp, tt.want.status, testutil.LoadFile(t, tt.want.rspFile), | ||
) | ||
}) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[ | ||
{ | ||
"id" : 1, | ||
"title" : "test1", | ||
"status" : "todo" | ||
}, | ||
{ | ||
"id" : 2, | ||
"title" : "test2", | ||
"status" : "done" | ||
} | ||
] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package handler | ||
|
||
import ( | ||
"go_todo_app/entity" | ||
"go_todo_app/store" | ||
"go_todo_app/testutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestListTask(t *testing.T) { | ||
type want struct { | ||
status int | ||
rspFile string | ||
} | ||
tests := map[string]struct { | ||
tasks map[entity.TaskID]*entity.Task | ||
want want | ||
}{ | ||
"ok": { | ||
tasks: map[entity.TaskID]*entity.Task{ | ||
1: { | ||
ID: 1, | ||
Title: "test1", | ||
Status: "todo", | ||
}, | ||
2: { | ||
ID: 2, | ||
Title: "test2", | ||
Status: "done", | ||
}, | ||
}, | ||
want: want{ | ||
status: http.StatusOK, | ||
rspFile: "testdata/list_task/ok_rsp.json.golden", | ||
}, | ||
}, | ||
"empty": { | ||
tasks: map[entity.TaskID]*entity.Task{}, | ||
want: want{ | ||
status: http.StatusOK, | ||
rspFile: "testdata/list_task/empty_rsp.json.golden", | ||
}, | ||
}, | ||
} | ||
for n, tt := range tests { | ||
tt := tt | ||
t.Run(n, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
w := httptest.NewRecorder() | ||
r := httptest.NewRequest(http.MethodGet, "/tasks", nil) | ||
|
||
sut := ListTask{Store: &store.TaskStore{Tasks: tt.tasks}} | ||
sut.ServeHTTP(w, r) | ||
|
||
resp := w.Result() | ||
testutil.AssertResponse(t, | ||
resp, tt.want.status, testutil.LoadFile(t, tt.want.rspFile), | ||
) | ||
}) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
_chapter19/section75/handler/testdata/list_task/empty_rsp.json.golden
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
12 changes: 12 additions & 0 deletions
12
_chapter19/section75/handler/testdata/list_task/ok_rsp.json.golden
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[ | ||
{ | ||
"id" : 1, | ||
"title" : "test1", | ||
"status" : "todo" | ||
}, | ||
{ | ||
"id" : 2, | ||
"title" : "test2", | ||
"status" : "done" | ||
} | ||
] |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package handler | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"go_todo_app/entity" | ||
"go_todo_app/testutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestListTask(t *testing.T) { | ||
type want struct { | ||
status int | ||
rspFile string | ||
} | ||
tests := map[string]struct { | ||
tasks []*entity.Task | ||
want want | ||
}{ | ||
"ok": { | ||
tasks: []*entity.Task{ | ||
{ | ||
ID: 1, | ||
Title: "test1", | ||
Status: entity.TaskStatusTodo, | ||
}, | ||
{ | ||
ID: 2, | ||
Title: "test2", | ||
Status: entity.TaskStatusDone, | ||
}, | ||
}, | ||
want: want{ | ||
status: http.StatusOK, | ||
rspFile: "testdata/list_task/ok_rsp.json.golden", | ||
}, | ||
}, | ||
"empty": { | ||
tasks: []*entity.Task{}, | ||
want: want{ | ||
status: http.StatusOK, | ||
rspFile: "testdata/list_task/empty_rsp.json.golden", | ||
}, | ||
}, | ||
} | ||
|
||
for n, tt := range tests { | ||
tt := tt | ||
t.Run(n, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
w := httptest.NewRecorder() | ||
r := httptest.NewRequest(http.MethodGet, "/tasks", nil) | ||
|
||
moq := &ListTasksServiceMock{} | ||
moq.ListTasksFunc = func(ctx context.Context) (entity.Tasks, error) { | ||
if tt.tasks != nil { | ||
return tt.tasks, nil | ||
} | ||
return nil, errors.New("error from mock") | ||
} | ||
sut := ListTask{Service: moq} | ||
sut.ServeHTTP(w, r) | ||
|
||
resp := w.Result() | ||
testutil.AssertResponse(t, | ||
resp, tt.want.status, testutil.LoadFile(t, tt.want.rspFile), | ||
) | ||
}) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[ | ||
{ | ||
"id" : 1, | ||
"title" : "test1", | ||
"status" : "todo" | ||
}, | ||
{ | ||
"id" : 2, | ||
"title" : "test2", | ||
"status" : "done" | ||
} | ||
] |