From 3f2e4a81f181615e3aae5f8fed73b1583139e834 Mon Sep 17 00:00:00 2001 From: ryoha000 Date: Mon, 18 Dec 2023 18:57:54 +0900 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=F0=9F=90=9B=20item=20=E3=81=AE=20co?= =?UTF-8?q?de=20=E3=81=8C=E7=A9=BA=E6=96=87=E5=AD=97=E3=81=AA=E3=81=93?= =?UTF-8?q?=E3=81=A8=E3=82=92=E8=A8=B1=E5=AE=B9=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/items.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model/items.go b/model/items.go index 433ea2f..72f1866 100644 --- a/model/items.go +++ b/model/items.go @@ -86,7 +86,7 @@ func (item Item) Validate() error { return validation.ValidateStruct(&item, validation.Field(&item.Name, validation.Required), validation.Field(&item.Type, validation.By(checkItemType)), - validation.Field(&item.Code, validation.Required), + validation.Field(&item.Code, validation.NotNil), validation.Field(&item.Description, validation.Required), validation.Field(&item.ImgURL, is.URL), ) From 93ba1540760944bbcc383d85325691fbb3776b97 Mon Sep 17 00:00:00 2001 From: ryoha000 Date: Mon, 18 Dec 2023 19:04:48 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=F0=9F=90=9B=20test=20=E3=81=AE?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- router/items_test.go | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/router/items_test.go b/router/items_test.go index bcfa34c..c040246 100644 --- a/router/items_test.go +++ b/router/items_test.go @@ -34,6 +34,16 @@ func TestPostItems(t *testing.T) { ImgURL: "http://example.com/testKojin.jpg", } + testValidBodies := []model.Item{ + { + Name: "testPostInvalidItem3", + Type: 0, + Code: "", + Description: "これはバリデーションのテスト3です", + ImgURL: "http://example.com/testInvalid3.jpg", + }, + } + testInvalidBodies := []model.Item{ { Name: "", @@ -49,13 +59,6 @@ func TestPostItems(t *testing.T) { Description: "これはバリデーションのテスト2です", ImgURL: "http://example.com/testInvalid2.jpg", }, - { - Name: "testPostInvalidItem3", - Type: 0, - Code: "", - Description: "これはバリデーションのテスト3です", - ImgURL: "http://example.com/testInvalid3.jpg", - }, { Name: "testPostInvalidItem4", Type: 0, @@ -67,7 +70,7 @@ func TestPostItems(t *testing.T) { Name: "testPostInvalidItem5", Type: 0, Code: "3575736936335", - Description: "これはバリデーションのテスト5です", + Description: "これはバリデーションのテスト4です", ImgURL: "not a url", }, } @@ -138,6 +141,21 @@ func TestPostItems(t *testing.T) { assert.Equal(http.StatusBadRequest, rec.Code) }) } + + for _, body := range testValidBodies { + t.Run("admin user/validation pass", func(t *testing.T) { + assert := assert.New(t) + e := echoSetupWithAdminUser() + + reqBody, _ := json.Marshal(body) + req := httptest.NewRequest(echo.POST, "/api/items", bytes.NewReader(reqBody)) + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + e.ServeHTTP(rec, req) + + assert.Equal(http.StatusCreated, rec.Code) + }) + } } func TestPutItem(t *testing.T) {