Skip to content

Commit

Permalink
[1125] section75
Browse files Browse the repository at this point in the history
  • Loading branch information
myeunee committed Nov 24, 2024
1 parent fa4f211 commit 4f3148b
Show file tree
Hide file tree
Showing 12 changed files with 342 additions and 41 deletions.
5 changes: 4 additions & 1 deletion chapter19/section75/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ dry-migrate: ## Try migration
mysqldef -u todo -p todo -h 127.0.0.1 -P 33306 todo --dry-run < ./_tools/mysql/schema.sql

migrate: ## Execute migration
mysqldef -u todo -p todo -h 127.0.0.1 -P 33306 todo < ./_tools/mysql/schema.sql
mysqldef -u todo -p todo -h 127.0.0.1 -P 33306 todo < ./_tools/mysql/schema.sql

generate:
go generate ./...
13 changes: 8 additions & 5 deletions chapter19/section75/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ require (
github.com/go-chi/chi/v5 v5.1.0
github.com/go-playground/validator/v10 v10.22.1
github.com/google/go-cmp v0.6.0
golang.org/x/sync v0.8.0
github.com/matryer/moq v0.5.0
golang.org/x/sync v0.9.0
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/tools v0.27.0 // indirect
)

require (
Expand All @@ -23,8 +26,8 @@ require (
github.com/go-sql-driver/mysql v1.8.1
github.com/jmoiron/sqlx v1.4.0
github.com/leodido/go-urn v1.4.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.23.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
)
26 changes: 16 additions & 10 deletions chapter19/section75/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,27 @@ github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/matryer/moq v0.5.0 h1:h2PJUYjZSiyEahzVogDRmrgL9Bsx9xYAl8l+LPfmwL8=
github.com/matryer/moq v0.5.0/go.mod h1:39GTnrD0mVWHPvWdYj5ki/lxfhLQEtHcLh+tWoYF/iE=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo=
golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
golang.org/x/tools v0.27.0 h1:qEKojBykQkQ4EynWy4S8Weg69NumxKdn40Fce3uc/8o=
golang.org/x/tools v0.27.0/go.mod h1:sUi0ZgbwW9ZPAq26Ekut+weQPR5eIM6GQLQ1Yjm1H0Q=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
11 changes: 2 additions & 9 deletions chapter19/section75/handler/add_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ import (
"net/http"

"github.com/go-playground/validator/v10"
"github.com/jmoiron/sqlx"
"github.com/myeunee/GolangStudy/chapter19/section75/entity"
"github.com/myeunee/GolangStudy/chapter19/section75/store"
)

type AddTask struct {
DB *sqlx.DB
Repo *store.Repository
Service AddTaskService
Validator *validator.Validate
}

Expand All @@ -35,11 +32,7 @@ func (at *AddTask) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

t := &entity.Task{
Title: b.Title,
Status: entity.TaskStatusTodo,
}
err := at.Repo.AddTask(ctx, at.DB, t)
t, err := at.Service.AddTask(ctx, b.Title)
if err != nil {
RespondJSON(ctx, w, &ErrResponse{
Message: err.Error(),
Expand Down
20 changes: 16 additions & 4 deletions chapter19/section75/handler/add_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package handler

import (
"bytes"
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"

"github.com/go-playground/validator/v10"
"github.com/myeunee/GolangStudy/chapter19/section75/entity"
"github.com/myeunee/GolangStudy/chapter19/section75/store"
"github.com/myeunee/GolangStudy/chapter19/section75/testutil"
)

Expand Down Expand Up @@ -47,10 +48,21 @@ func TestAddTask(t *testing.T) {
"/tasks",
bytes.NewReader(testutil.LoadFile(t, tt.reqFile)),
)
moq := &AddTaskServiceMock{}
moq.AddTaskFunc = func(
ctx context.Context, title string,
) (*entity.Task, error) {
if tt.want.status == http.StatusOK {
return &entity.Task{ID: 1}, nil
}
return nil, errors.New("error from mock")
}

sut := AddTask{
Service: moq,
Validator: validator.New(),
}

sut := AddTask{Store: &store.TaskStore{
Tasks: map[entity.TaskID]*entity.Task{},
}, Validator: validator.New()}
sut.ServeHTTP(w, r)

resp := w.Result()
Expand Down
7 changes: 2 additions & 5 deletions chapter19/section75/handler/list_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ package handler
import (
"net/http"

"github.com/jmoiron/sqlx"
"github.com/myeunee/GolangStudy/chapter19/section75/entity"
"github.com/myeunee/GolangStudy/chapter19/section75/store"
)

type ListTask struct {
DB *sqlx.DB
Repo *store.Repository
Service ListTasksService
}

type task struct {
Expand All @@ -21,7 +18,7 @@ type task struct {

func (lt *ListTask) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
tasks, err := lt.Repo.ListTasks(ctx, lt.DB)
tasks, err := lt.Service.ListTasks(ctx)
if err != nil {
RespondJSON(ctx, w, &ErrResponse{
Message: err.Error(),
Expand Down
73 changes: 73 additions & 0 deletions chapter19/section75/handler/list_task_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package handler

import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"

"github.com/myeunee/GolangStudy/chapter19/section75/entity"
"github.com/myeunee/GolangStudy/chapter19/section75/testutil"
)

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),
)
})
}
}
148 changes: 148 additions & 0 deletions chapter19/section75/handler/moq_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4f3148b

Please sign in to comment.