From 6e3b2a1c03542f41cf4d29f27a6d1a242bef3232 Mon Sep 17 00:00:00 2001 From: Willy Kristianto Date: Fri, 16 Aug 2024 15:08:28 +0700 Subject: [PATCH] fixing generator delivery rest test and usecase test --- candihelper/helper.go | 1 + cmd/candi/template_delivery_rest.go | 17 +++++++---------- cmd/candi/template_usecase.go | 21 +++++++-------------- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/candihelper/helper.go b/candihelper/helper.go index 6bc5494..20eb245 100644 --- a/candihelper/helper.go +++ b/candihelper/helper.go @@ -580,6 +580,7 @@ func ReflectTypeUnwrapPtr(f reflect.Type) reflect.Type { } return f } + func ReflectValueUnwrapPtr(f reflect.Value) reflect.Value { if f.Kind() == reflect.Ptr { if f.IsNil() { diff --git a/cmd/candi/template_delivery_rest.go b/cmd/candi/template_delivery_rest.go index 2357551..82ec888 100644 --- a/cmd/candi/template_delivery_rest.go +++ b/cmd/candi/template_delivery_rest.go @@ -94,7 +94,9 @@ func (h *RestHandler) getAll{{upper (camel .ModuleName)}}(rw http.ResponseWriter } message := "Success, with your user id (" + tokenClaim.Subject + ") and role (" + tokenClaim.Role + ")" - wrapper.NewHTTPResponse(http.StatusOK, message, result.Meta, result.Data).JSON(rw) + response := wrapper.NewHTTPResponse(http.StatusOK, message, result.Data) + response.Meta = result.Meta + response.JSON(rw) } // GetDetail{{upper (camel .ModuleName)}} documentation @@ -289,10 +291,9 @@ func TestRestHandler_getAll{{upper (camel .ModuleName)}}(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - {{camel .ModuleName}}Usecase := &mockusecase.{{upper (camel .ModuleName)}}Usecase{} {{camel .ModuleName}}Usecase.On("GetAll{{upper (camel .ModuleName)}}", mock.Anything, mock.Anything).Return( - []domain.Response{{upper (camel .ModuleName)}}{}, candishared.Meta{}, tt.wantUsecaseError) + domain.Response{{upper (camel .ModuleName)}}List{}, tt.wantUsecaseError) mockValidator := &mockinterfaces.Validator{} mockValidator.On("ValidateDocument", mock.Anything, mock.Anything).Return(tt.wantValidateError) @@ -322,7 +323,6 @@ func TestRestHandler_getDetail{{upper (camel .ModuleName)}}ByID(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - {{camel .ModuleName}}Usecase := &mockusecase.{{upper (camel .ModuleName)}}Usecase{} {{camel .ModuleName}}Usecase.On("GetDetail{{upper (camel .ModuleName)}}", mock.Anything, mock.Anything).Return(domain.Response{{upper (camel .ModuleName)}}{}, tt.wantUsecaseError) mockValidator := &mockinterfaces.Validator{} @@ -333,7 +333,7 @@ func TestRestHandler_getDetail{{upper (camel .ModuleName)}}ByID(t *testing.T) { handler := RestHandler{uc: uc, validator: mockValidator} - req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(tt.reqBody)) + req := httptest.NewRequest(http.MethodGet, "/", strings.NewReader(tt.reqBody)) req = req.WithContext(candishared.SetToContext(req.Context(), candishared.ContextKeyTokenClaim, &candishared.TokenClaim{})) req.Header.Add(candihelper.HeaderContentType, candihelper.HeaderMIMEApplicationJSON) res := httptest.NewRecorder() @@ -357,7 +357,6 @@ func TestRestHandler_create{{upper (camel .ModuleName)}}(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - {{camel .ModuleName}}Usecase := &mockusecase.{{upper (camel .ModuleName)}}Usecase{} {{camel .ModuleName}}Usecase.On("Create{{upper (camel .ModuleName)}}", mock.Anything, mock.Anything).Return(domain.Response{{upper (camel .ModuleName)}}{}, tt.wantUsecaseError) mockValidator := &mockinterfaces.Validator{} @@ -394,7 +393,6 @@ func TestRestHandler_update{{upper (camel .ModuleName)}}(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - {{camel .ModuleName}}Usecase := &mockusecase.{{upper (camel .ModuleName)}}Usecase{} {{camel .ModuleName}}Usecase.On("Update{{upper (camel .ModuleName)}}", mock.Anything, mock.Anything, mock.Anything).Return(tt.wantUsecaseError) mockValidator := &mockinterfaces.Validator{} @@ -405,7 +403,7 @@ func TestRestHandler_update{{upper (camel .ModuleName)}}(t *testing.T) { handler := RestHandler{uc: uc, validator: mockValidator} - req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(tt.reqBody)) + req := httptest.NewRequest(http.MethodPut, "/", strings.NewReader(tt.reqBody)) req = req.WithContext(candishared.SetToContext(req.Context(), candishared.ContextKeyTokenClaim, &candishared.TokenClaim{})) req.Header.Add(candihelper.HeaderContentType, candihelper.HeaderMIMEApplicationJSON) res := httptest.NewRecorder() @@ -426,7 +424,6 @@ func TestRestHandler_delete{{upper (camel .ModuleName)}}(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - {{camel .ModuleName}}Usecase := &mockusecase.{{upper (camel .ModuleName)}}Usecase{} {{camel .ModuleName}}Usecase.On("Delete{{upper (camel .ModuleName)}}", mock.Anything, mock.Anything).Return(tt.wantUsecaseError) mockValidator := &mockinterfaces.Validator{} @@ -437,7 +434,7 @@ func TestRestHandler_delete{{upper (camel .ModuleName)}}(t *testing.T) { handler := RestHandler{uc: uc, validator: mockValidator} - req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(tt.reqBody)) + req := httptest.NewRequest(http.MethodDelete, "/", strings.NewReader(tt.reqBody)) req.Header.Add(candihelper.HeaderContentType, candihelper.HeaderMIMEApplicationJSON) res := httptest.NewRecorder() handler.delete{{upper (camel .ModuleName)}}(res, req) diff --git a/cmd/candi/template_usecase.go b/cmd/candi/template_usecase.go index 20672bf..8026e68 100644 --- a/cmd/candi/template_usecase.go +++ b/cmd/candi/template_usecase.go @@ -156,12 +156,12 @@ func (uc *{{camel .ModuleName}}UsecaseImpl) GetAll{{upper (camel .ModuleName)}}( import ( "context" "errors" + "testing" "{{$.PackagePrefix}}/internal/modules/{{cleanPathModule .ModuleName}}/domain" mockrepo "{{$.PackagePrefix}}/pkg/mocks/modules/{{cleanPathModule .ModuleName}}/repository" mocksharedrepo "{{$.PackagePrefix}}/pkg/mocks/shared/repository" shareddomain "{{$.PackagePrefix}}/pkg/shared/domain" - "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" @@ -169,7 +169,6 @@ import ( func Test_{{camel .ModuleName}}UsecaseImpl_GetAll{{upper (camel .ModuleName)}}(t *testing.T) { t.Run("Testcase #1: Positive", func(t *testing.T) { - {{camel .ModuleName}}Repo := &mockrepo.{{upper (camel .ModuleName)}}Repository{} {{camel .ModuleName}}Repo.On("FetchAll", mock.Anything, mock.Anything, mock.Anything).Return([]shareddomain.{{upper (camel .ModuleName)}}{}, nil) {{camel .ModuleName}}Repo.On("Count", mock.Anything, mock.Anything).Return(10) @@ -181,12 +180,11 @@ func Test_{{camel .ModuleName}}UsecaseImpl_GetAll{{upper (camel .ModuleName)}}(t repo{{if .SQLDeps}}SQL{{else if .MongoDeps}}Mongo{{else if .ArangoDeps}}Arango{{end}}: repo{{if .SQLDeps}}SQL{{else if .MongoDeps}}Mongo{{else if .ArangoDeps}}Arango{{end}}, } - _, _, err := uc.GetAll{{upper (camel .ModuleName)}}(context.Background(), &domain.Filter{{upper (camel .ModuleName)}}{}) + _, err := uc.GetAll{{upper (camel .ModuleName)}}(context.Background(), &domain.Filter{{upper (camel .ModuleName)}}{}) assert.NoError(t, err) }) t.Run("Testcase #2: Negative", func(t *testing.T) { - {{camel .ModuleName}}Repo := &mockrepo.{{upper (camel .ModuleName)}}Repository{} {{camel .ModuleName}}Repo.On("FetchAll", mock.Anything, mock.Anything, mock.Anything).Return([]shareddomain.{{upper (camel .ModuleName)}}{}, errors.New("Error")) {{camel .ModuleName}}Repo.On("Count", mock.Anything, mock.Anything).Return(10) @@ -198,7 +196,7 @@ func Test_{{camel .ModuleName}}UsecaseImpl_GetAll{{upper (camel .ModuleName)}}(t repo{{if .SQLDeps}}SQL{{else if .MongoDeps}}Mongo{{else if .ArangoDeps}}Arango{{end}}: repo{{if .SQLDeps}}SQL{{else if .MongoDeps}}Mongo{{else if .ArangoDeps}}Arango{{end}}, } - _, _, err := uc.GetAll{{upper (camel .ModuleName)}}(context.Background(), &domain.Filter{{upper (camel .ModuleName)}}{}) + _, err := uc.GetAll{{upper (camel .ModuleName)}}(context.Background(), &domain.Filter{{upper (camel .ModuleName)}}{}) assert.Error(t, err) }) } @@ -233,11 +231,11 @@ func (uc *{{camel .ModuleName}}UsecaseImpl) GetDetail{{upper (camel .ModuleName) import ( "context" + "testing" mockrepo "{{$.PackagePrefix}}/pkg/mocks/modules/{{cleanPathModule .ModuleName}}/repository" mocksharedrepo "{{$.PackagePrefix}}/pkg/mocks/shared/repository" shareddomain "{{$.PackagePrefix}}/pkg/shared/domain" - "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" @@ -245,7 +243,6 @@ import ( func Test_{{camel .ModuleName}}UsecaseImpl_GetDetail{{upper (camel .ModuleName)}}(t *testing.T) { t.Run("Testcase #1: Positive", func(t *testing.T) { - {{camel .ModuleName}}Repo := &mockrepo.{{upper (camel .ModuleName)}}Repository{} {{camel .ModuleName}}Repo.On("Find", mock.Anything, mock.Anything).Return(shareddomain.{{upper (camel .ModuleName)}}{}, nil) @@ -296,11 +293,11 @@ func (uc *{{camel .ModuleName}}UsecaseImpl) Create{{upper (camel .ModuleName)}}( import ( "context" + "testing" "{{$.PackagePrefix}}/internal/modules/{{cleanPathModule .ModuleName}}/domain" mockrepo "{{$.PackagePrefix}}/pkg/mocks/modules/{{cleanPathModule .ModuleName}}/repository" mocksharedrepo "{{$.PackagePrefix}}/pkg/mocks/shared/repository" - "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" @@ -308,7 +305,6 @@ import ( func Test_{{camel .ModuleName}}UsecaseImpl_Create{{upper (camel .ModuleName)}}(t *testing.T) { t.Run("Testcase #1: Positive", func(t *testing.T) { - {{camel .ModuleName}}Repo := &mockrepo.{{upper (camel .ModuleName)}}Repository{} {{camel .ModuleName}}Repo.On("Save", mock.Anything, mock.Anything).Return(nil) @@ -359,12 +355,12 @@ func (uc *{{camel .ModuleName}}UsecaseImpl) Update{{upper (camel .ModuleName)}}( import ( "context" "errors" + "testing" "{{$.PackagePrefix}}/internal/modules/{{cleanPathModule .ModuleName}}/domain" mockrepo "{{$.PackagePrefix}}/pkg/mocks/modules/{{cleanPathModule .ModuleName}}/repository" mocksharedrepo "{{$.PackagePrefix}}/pkg/mocks/shared/repository" shareddomain "{{$.PackagePrefix}}/pkg/shared/domain" - "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" @@ -373,7 +369,6 @@ import ( func Test_{{camel .ModuleName}}UsecaseImpl_Update{{upper (camel .ModuleName)}}(t *testing.T) { ctx := context.Background() t.Run("Testcase #1: Positive", func(t *testing.T) { - {{camel .ModuleName}}Repo := &mockrepo.{{upper (camel .ModuleName)}}Repository{} {{camel .ModuleName}}Repo.On("Find", mock.Anything, mock.Anything).Return(shareddomain.{{upper (camel .ModuleName)}}{}, nil) {{camel .ModuleName}}Repo.On("Save", mock.Anything, mock.Anything, mock.AnythingOfType("candishared.DBUpdateOptionFunc")).Return(nil) @@ -396,7 +391,6 @@ func Test_{{camel .ModuleName}}UsecaseImpl_Update{{upper (camel .ModuleName)}}(t }) t.Run("Testcase #2: Negative", func(t *testing.T) { - {{camel .ModuleName}}Repo := &mockrepo.{{upper (camel .ModuleName)}}Repository{} {{camel .ModuleName}}Repo.On("Find", mock.Anything, mock.Anything).Return(shareddomain.{{upper (camel .ModuleName)}}{}, errors.New("Error")) {{camel .ModuleName}}Repo.On("Save", mock.Anything, mock.Anything, mock.AnythingOfType("candishared.DBUpdateOptionFunc")).Return(nil) @@ -442,10 +436,10 @@ func (uc *{{camel .ModuleName}}UsecaseImpl) Delete{{upper (camel .ModuleName)}}( import ( "context" + "testing" mockrepo "{{$.PackagePrefix}}/pkg/mocks/modules/{{cleanPathModule .ModuleName}}/repository" mocksharedrepo "{{$.PackagePrefix}}/pkg/mocks/shared/repository" - "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" @@ -453,7 +447,6 @@ import ( func Test_{{camel .ModuleName}}UsecaseImpl_Delete{{upper (camel .ModuleName)}}(t *testing.T) { t.Run("Testcase #1: Positive", func(t *testing.T) { - {{camel .ModuleName}}Repo := &mockrepo.{{upper (camel .ModuleName)}}Repository{} {{camel .ModuleName}}Repo.On("Delete", mock.Anything, mock.Anything).Return(nil)