Skip to content

Commit

Permalink
fixing generator delivery rest test and usecase test
Browse files Browse the repository at this point in the history
  • Loading branch information
willy182 authored and agungdwiprasetyo committed Aug 17, 2024
1 parent e8d5848 commit 6e3b2a1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
1 change: 1 addition & 0 deletions candihelper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
17 changes: 7 additions & 10 deletions cmd/candi/template_delivery_rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{}
Expand All @@ -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()
Expand All @@ -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{}
Expand Down Expand Up @@ -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{}
Expand All @@ -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()
Expand All @@ -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{}
Expand All @@ -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)
Expand Down
21 changes: 7 additions & 14 deletions cmd/candi/template_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,19 @@ 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"
)
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)
Expand All @@ -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)
Expand All @@ -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)
})
}
Expand Down Expand Up @@ -233,19 +231,18 @@ 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"
)
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)
Expand Down Expand Up @@ -296,19 +293,18 @@ 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"
)
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)
Expand Down Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -442,18 +436,17 @@ 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"
)
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)
Expand Down

0 comments on commit 6e3b2a1

Please sign in to comment.