From b3932007b2c67a0bf5132771e034edc0c1e917b6 Mon Sep 17 00:00:00 2001 From: pikachu0310 Date: Wed, 4 Jan 2023 22:54:27 +0900 Subject: [PATCH 1/5] =?UTF-8?q?seat.go=E3=81=AB=E3=80=81go=20generate?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/service/seat.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/service/seat.go b/src/service/seat.go index b124d71b..e3b87d98 100644 --- a/src/service/seat.go +++ b/src/service/seat.go @@ -1,5 +1,7 @@ package service +//go:generate go run github.com/golang/mock/mockgen -source=$GOFILE -destination=mock/${GOFILE} -package=mock + import ( "context" From 2ade918319aabf4011e893a666a11bd66c69ac99 Mon Sep 17 00:00:00 2001 From: pikachu0310 Date: Wed, 4 Jan 2023 22:55:12 +0900 Subject: [PATCH 2/5] =?UTF-8?q?TestSeat=5FGetSeats=E3=82=92=E9=80=94?= =?UTF-8?q?=E4=B8=AD=E3=81=BE=E3=81=A7=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/handler/v2/seat_test.go | 163 ++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 src/handler/v2/seat_test.go diff --git a/src/handler/v2/seat_test.go b/src/handler/v2/seat_test.go new file mode 100644 index 00000000..281abf01 --- /dev/null +++ b/src/handler/v2/seat_test.go @@ -0,0 +1,163 @@ +package v2 + +import ( + "encoding/json" + "github.com/golang/mock/gomock" + "github.com/labstack/echo/v4" + "github.com/stretchr/testify/assert" + mockConfig "github.com/traPtitech/trap-collection-server/src/config/mock" + "github.com/traPtitech/trap-collection-server/src/handler/common" + "github.com/traPtitech/trap-collection-server/src/handler/v2/openapi" + "github.com/traPtitech/trap-collection-server/src/service" + "github.com/traPtitech/trap-collection-server/src/service/mock" + "net/http" + "net/http/httptest" + "testing" +) + +func TestNewSeat(t *testing.T) { +} + +func TestSeat_GetSeats(t *testing.T) { + t.Parallel() + + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + mockConf := mockConfig.NewMockHandler(ctrl) + mockConf. + EXPECT(). + SessionKey(). + Return("key", nil) + mockConf. + EXPECT(). + SessionSecret(). + Return("secret", nil) + sess, err := common.NewSession(mockConf) + if err != nil { + t.Fatalf("failed to create session: %v", err) + return + } + session, err := NewSession(sess) + if err != nil { + t.Fatalf("failed to create session: %v", err) + return + } + + mockSeatService := mock.NewMockSeat(ctrl) + seatHandler := NewSeat(mockSeatService) + + type test struct { + description string + isErr bool + statusCode int + seats []*openapi.Seat + } + + testCases := []test{ + { + description: "正常に席の情報を取得できる", + isErr: false, + statusCode: http.StatusOK, + seats: []*openapi.Seat{{Id: 1, Status: "in-use"}}, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.description, func(t *testing.T) { + e := echo.New() + req := httptest.NewRequest(http.MethodGet, "/seats", nil) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + + if testCase.executeGetAllActiveUser { + mockUserService. + EXPECT(). + GetAllActiveUser(gomock.Any(), gomock.Any()). + Return(testCase.userInfos, testCase.GetAllActiveUserErr) + } + + err := seatHandler.GetSeats(c) + if err != nil { + t.Errorf("failed to get seats: %v", err) + } + + //if testCase.isErr { + // var httpError *echo.HTTPError + // if errors.As(err, &httpError) { + // assert.Equal(t, testCase.statusCode, httpError.Code) + // } else { + // t.Errorf("error is not *echo.HTTPError") + // } + // return + //} + + var resSeats []*openapi.User + err = json.NewDecoder(rec.Body).Decode(&resSeats) + if err != nil { + t.Fatalf("failed to decode response body: %v", err) + } + + assert.Equal(t, testCase.seats, resSeats) + for i, seat := range resSeats { + assert.Equal(t, seat.Id, resSeats[i].Id) + assert.Equal(t, seat.Name, resSeats[i].Name) + } + }) + } +} + +func TestSeat_PostSeat(t *testing.T) { + type fields struct { + seatService service.Seat + } + type args struct { + c echo.Context + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + seat := &Seat{ + seatService: tt.fields.seatService, + } + if err := seat.PostSeat(tt.args.c); (err != nil) != tt.wantErr { + t.Errorf("Seat.PostSeat() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestSeat_PatchSeatStatus(t *testing.T) { + type fields struct { + seatService service.Seat + } + type args struct { + c echo.Context + seatID openapi.SeatIDInPath + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + seat := &Seat{ + seatService: tt.fields.seatService, + } + if err := seat.PatchSeatStatus(tt.args.c, tt.args.seatID); (err != nil) != tt.wantErr { + t.Errorf("Seat.PatchSeatStatus() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} From 801239cf98fb2324a64a529c9f4708b0d439d95c Mon Sep 17 00:00:00 2001 From: pikachu0310 Date: Tue, 21 Mar 2023 18:26:09 +0900 Subject: [PATCH 3/5] =?UTF-8?q?TestSeat=5FGetSeats=E3=81=AE=E5=AE=9F?= =?UTF-8?q?=E8=A3=85=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/handler/v2/seat_test.go | 87 +++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 46 deletions(-) diff --git a/src/handler/v2/seat_test.go b/src/handler/v2/seat_test.go index 281abf01..fe655975 100644 --- a/src/handler/v2/seat_test.go +++ b/src/handler/v2/seat_test.go @@ -2,11 +2,11 @@ package v2 import ( "encoding/json" + "errors" "github.com/golang/mock/gomock" "github.com/labstack/echo/v4" "github.com/stretchr/testify/assert" - mockConfig "github.com/traPtitech/trap-collection-server/src/config/mock" - "github.com/traPtitech/trap-collection-server/src/handler/common" + "github.com/traPtitech/trap-collection-server/src/domain" "github.com/traPtitech/trap-collection-server/src/handler/v2/openapi" "github.com/traPtitech/trap-collection-server/src/service" "github.com/traPtitech/trap-collection-server/src/service/mock" @@ -24,42 +24,27 @@ func TestSeat_GetSeats(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - mockConf := mockConfig.NewMockHandler(ctrl) - mockConf. - EXPECT(). - SessionKey(). - Return("key", nil) - mockConf. - EXPECT(). - SessionSecret(). - Return("secret", nil) - sess, err := common.NewSession(mockConf) - if err != nil { - t.Fatalf("failed to create session: %v", err) - return - } - session, err := NewSession(sess) - if err != nil { - t.Fatalf("failed to create session: %v", err) - return - } - mockSeatService := mock.NewMockSeat(ctrl) seatHandler := NewSeat(mockSeatService) type test struct { - description string - isErr bool - statusCode int - seats []*openapi.Seat + description string + seats []*domain.Seat + res []*openapi.Seat + executeGetSeats bool + getSeatsErr error + isErr bool + err error + statusCode int } testCases := []test{ { - description: "正常に席の情報を取得できる", - isErr: false, - statusCode: http.StatusOK, - seats: []*openapi.Seat{{Id: 1, Status: "in-use"}}, + description: "正常に席の情報を取得できる", + executeGetSeats: true, + statusCode: http.StatusOK, + seats: []*domain.Seat{domain.NewSeat(1, 2)}, + res: []*openapi.Seat{{Id: 1, Status: "in-use"}}, }, } @@ -70,11 +55,11 @@ func TestSeat_GetSeats(t *testing.T) { rec := httptest.NewRecorder() c := e.NewContext(req, rec) - if testCase.executeGetAllActiveUser { - mockUserService. + if testCase.executeGetSeats { + mockSeatService. EXPECT(). - GetAllActiveUser(gomock.Any(), gomock.Any()). - Return(testCase.userInfos, testCase.GetAllActiveUserErr) + GetSeats(gomock.Any()). + Return(testCase.seats, testCase.getSeatsErr) } err := seatHandler.GetSeats(c) @@ -82,26 +67,36 @@ func TestSeat_GetSeats(t *testing.T) { t.Errorf("failed to get seats: %v", err) } - //if testCase.isErr { - // var httpError *echo.HTTPError - // if errors.As(err, &httpError) { - // assert.Equal(t, testCase.statusCode, httpError.Code) - // } else { - // t.Errorf("error is not *echo.HTTPError") - // } - // return - //} + if testCase.isErr { + if testCase.statusCode != 0 { + var httpError *echo.HTTPError + if errors.As(err, &httpError) { + assert.Equal(t, testCase.statusCode, httpError.Code) + } else { + t.Errorf("error is not *echo.HTTPError") + } + } else if testCase.err == nil { + assert.Error(t, err) + } else if !errors.Is(err, testCase.err) { + t.Errorf("error must be %v, but actual is %v", testCase.err, err) + } + } else { + assert.NoError(t, err) + } + if err != nil { + return + } - var resSeats []*openapi.User + var resSeats []*openapi.Seat err = json.NewDecoder(rec.Body).Decode(&resSeats) if err != nil { t.Fatalf("failed to decode response body: %v", err) } - assert.Equal(t, testCase.seats, resSeats) + assert.Equal(t, testCase.res, resSeats) for i, seat := range resSeats { assert.Equal(t, seat.Id, resSeats[i].Id) - assert.Equal(t, seat.Name, resSeats[i].Name) + assert.Equal(t, seat.Status, resSeats[i].Status) } }) } From 17916e6a923bfbef3390ac4c2bcbc91630c3c9dc Mon Sep 17 00:00:00 2001 From: pikachu0310 Date: Tue, 21 Mar 2023 18:50:54 +0900 Subject: [PATCH 4/5] =?UTF-8?q?getSeats=E3=81=AEtestCase=E3=81=AE=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/handler/v2/seat_test.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/handler/v2/seat_test.go b/src/handler/v2/seat_test.go index fe655975..faf4e21b 100644 --- a/src/handler/v2/seat_test.go +++ b/src/handler/v2/seat_test.go @@ -15,9 +15,6 @@ import ( "testing" ) -func TestNewSeat(t *testing.T) { -} - func TestSeat_GetSeats(t *testing.T) { t.Parallel() @@ -46,6 +43,13 @@ func TestSeat_GetSeats(t *testing.T) { seats: []*domain.Seat{domain.NewSeat(1, 2)}, res: []*openapi.Seat{{Id: 1, Status: "in-use"}}, }, + { + description: "getSeatsがエラーなので500", + executeGetSeats: true, + getSeatsErr: errors.New("error"), + isErr: true, + statusCode: http.StatusInternalServerError, + }, } for _, testCase := range testCases { @@ -63,9 +67,6 @@ func TestSeat_GetSeats(t *testing.T) { } err := seatHandler.GetSeats(c) - if err != nil { - t.Errorf("failed to get seats: %v", err) - } if testCase.isErr { if testCase.statusCode != 0 { From 609a74f9ceae831a94d60ccbb58c63ee54e1b826 Mon Sep 17 00:00:00 2001 From: pikachu0310 Date: Tue, 21 Mar 2023 19:53:09 +0900 Subject: [PATCH 5/5] =?UTF-8?q?TestSeat=5FPostSeat=E3=81=AE=E5=AE=9F?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/handler/v2/seat_test.go | 122 ++++++++++++++++++++++++++++++------ 1 file changed, 104 insertions(+), 18 deletions(-) diff --git a/src/handler/v2/seat_test.go b/src/handler/v2/seat_test.go index faf4e21b..51b97b3c 100644 --- a/src/handler/v2/seat_test.go +++ b/src/handler/v2/seat_test.go @@ -1,6 +1,7 @@ package v2 import ( + "bytes" "encoding/json" "errors" "github.com/golang/mock/gomock" @@ -10,6 +11,7 @@ import ( "github.com/traPtitech/trap-collection-server/src/handler/v2/openapi" "github.com/traPtitech/trap-collection-server/src/service" "github.com/traPtitech/trap-collection-server/src/service/mock" + "log" "net/http" "net/http/httptest" "testing" @@ -104,27 +106,111 @@ func TestSeat_GetSeats(t *testing.T) { } func TestSeat_PostSeat(t *testing.T) { - type fields struct { - seatService service.Seat - } - type args struct { - c echo.Context + t.Parallel() + + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + mockSeatService := mock.NewMockSeat(ctrl) + seatHandler := NewSeat(mockSeatService) + + type test struct { + description string + seats []*domain.Seat + res []*openapi.Seat + isBadRequestBody bool + newSeat *openapi.PostSeatJSONRequestBody + executePostSeats bool + postSeatsErr error + isErr bool + err error + statusCode int } - tests := []struct { - name string - fields fields - args args - wantErr bool - }{ - // TODO: Add test cases. + + testCases := []test{ + { + description: "正常に席数の変更に成功200", + newSeat: &openapi.PostSeatRequest{Num: 1}, + executePostSeats: true, + statusCode: http.StatusOK, + seats: []*domain.Seat{domain.NewSeat(1, 2)}, + res: []*openapi.Seat{{Id: 1, Status: "in-use"}}, + }, + { + description: "postSeatsがエラーなので500", + executePostSeats: true, + postSeatsErr: errors.New("error"), + isErr: true, + statusCode: http.StatusInternalServerError, + }, + { + description: "リクエストが不正なので400", + isBadRequestBody: true, + isErr: true, + statusCode: http.StatusBadRequest, + }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - seat := &Seat{ - seatService: tt.fields.seatService, + + for _, testCase := range testCases { + t.Run(testCase.description, func(t *testing.T) { + e := echo.New() + + reqBody := new(bytes.Buffer) + if !testCase.isBadRequestBody { + err := json.NewEncoder(reqBody).Encode(testCase.newSeat) + if err != nil { + log.Printf("failed to create request body") + t.Fatal(err) + } + } else { + reqBody = bytes.NewBufferString("bad requset body") } - if err := seat.PostSeat(tt.args.c); (err != nil) != tt.wantErr { - t.Errorf("Seat.PostSeat() error = %v, wantErr %v", err, tt.wantErr) + + req := httptest.NewRequest(http.MethodPost, "/seats", reqBody) + req.Header.Set(echo.HeaderContentType, "application/json") + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + + if testCase.executePostSeats { + mockSeatService. + EXPECT(). + UpdateSeatNum(gomock.Any(), gomock.Any()). + Return(testCase.seats, testCase.postSeatsErr) + } + + err := seatHandler.PostSeat(c) + + if testCase.isErr { + if testCase.statusCode != 0 { + var httpError *echo.HTTPError + if errors.As(err, &httpError) { + assert.Equal(t, testCase.statusCode, httpError.Code) + } else { + t.Errorf("error is not *echo.HTTPError") + } + } else if testCase.err == nil { + assert.Error(t, err) + } else if !errors.Is(err, testCase.err) { + t.Errorf("error must be %v, but actual is %v", testCase.err, err) + } + } else { + assert.NoError(t, err) + } + if err != nil { + return + } + + var resSeats []*openapi.Seat + err = json.NewDecoder(rec.Body).Decode(&resSeats) + if err != nil { + t.Fatalf("failed to decode response body: %v", err) + } + + assert.Equal(t, testCase.res, resSeats) + assert.Equal(t, len(testCase.res), len(resSeats)) + for i, seat := range resSeats { + assert.Equal(t, seat.Id, resSeats[i].Id) + assert.Equal(t, seat.Status, resSeats[i].Status) } }) }