diff --git a/common_test.go b/common_test.go new file mode 100644 index 0000000..08ce555 --- /dev/null +++ b/common_test.go @@ -0,0 +1,1304 @@ +/* + * Copyright (c) 2020 Alex + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @author Alex + * @copyright Copyright (c) 2020 Alex + * @since 29.12.2020 + * + */ + +package dtoLib_test + +import ( + "fmt" + "net/http" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/echo-go/dto-lib" +) + +func TestAllApiResponses(t *testing.T) { + nameUnknown := "not existing name" + expectedHealthcheck := map[string]dtoLib.ApiResponse{ + dtoLib.StatusFound: { + Code: 9999200, + Type: dtoLib.TypeHealthcheck, + Message: "Healthcheck(s) found", + }, + } + + actualResponses := dtoLib.AllApiResponses() + assert.Equal(t, 2, len(actualResponses)) + + tests := map[string]struct { + input string + expect struct { + obj *map[string]dtoLib.ApiResponse + ok bool + } + }{ + "healthcheck ok": { + input: dtoLib.TypeHealthcheck, + expect: struct { + obj *map[string]dtoLib.ApiResponse + ok bool + }{ + obj: &expectedHealthcheck, + ok: true, + }, + }, + "name wrong": { + input: nameUnknown, + expect: struct { + obj *map[string]dtoLib.ApiResponse + ok bool + }{ + obj: nil, + ok: false, + }, + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + actual, ok := actualResponses[tc.input] + assert.Equal(t, tc.expect.ok, ok) + if ok { + assert.Equal(t, *tc.expect.obj, actual) + } + }) + } +} + +func TestAddNewApiResponse(t *testing.T) { + name := "new name" + newNameApiResponses := map[string]dtoLib.ApiResponse{ + dtoLib.StatusFound: { + Code: 1111200, + Type: name, + Message: "New name(s) found", + }, + } + + actualResponses := dtoLib.AllApiResponses() + assert.Equal(t, 2, len(actualResponses)) + + err := dtoLib.AddNewApiResponse(name, newNameApiResponses) + + assert.Nil(t, err) + actualResponses = dtoLib.AllApiResponses() + assert.Equal(t, 3, len(actualResponses)) + + err = dtoLib.AddNewApiResponse(name, newNameApiResponses) + + assert.EqualError(t, err, "this name already exists") + actualResponses = dtoLib.AllApiResponses() + assert.Equal(t, 3, len(actualResponses)) +} + +func TestAllApiErrorResponses(t *testing.T) { + nameUnknown := "not existing name" + expectedHealthcheck := map[string]dtoLib.ApiErrorResponse{ + dtoLib.StatusFailed: { + Code: 9999502, + Type: dtoLib.TypeHealthcheck, + Message: "Healthcheck(s) failed", + }, + } + + actualResponses := dtoLib.AllApiErrorResponses() + assert.Equal(t, 2, len(actualResponses)) + + tests := map[string]struct { + input string + expect struct { + obj *map[string]dtoLib.ApiErrorResponse + ok bool + } + }{ + "healthcheck ok": { + input: dtoLib.TypeHealthcheck, + expect: struct { + obj *map[string]dtoLib.ApiErrorResponse + ok bool + }{ + obj: &expectedHealthcheck, + ok: true, + }, + }, + "name wrong": { + input: nameUnknown, + expect: struct { + obj *map[string]dtoLib.ApiErrorResponse + ok bool + }{ + obj: nil, + ok: false, + }, + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + actual, ok := actualResponses[tc.input] + assert.Equal(t, tc.expect.ok, ok) + if ok { + assert.Equal(t, *tc.expect.obj, actual) + } + }) + } +} + +func TestAddNewApiErrorResponse(t *testing.T) { + name := "new name 1" + newNameApiErrorResponses := map[string]dtoLib.ApiErrorResponse{ + dtoLib.StatusNotFound: { + Code: 1111404, + Type: name, + Message: "New name(s) not found", + }, + } + + actualResponses := dtoLib.AllApiErrorResponses() + assert.Equal(t, 2, len(actualResponses)) + + err := dtoLib.AddNewApiErrorResponse(name, newNameApiErrorResponses) + + assert.Nil(t, err) + actualResponses = dtoLib.AllApiErrorResponses() + assert.Equal(t, 3, len(actualResponses)) + + err = dtoLib.AddNewApiErrorResponse(name, newNameApiErrorResponses) + + assert.EqualError(t, err, "this name already exists") + actualResponses = dtoLib.AllApiErrorResponses() + assert.Equal(t, 3, len(actualResponses)) +} + +func TestNotCreated(t *testing.T) { + errorMsg := "not created error" + errorMsg1 := "not created error 1" + errorMsg2 := "not created error 2" + var errorsEmpty []error + errors := []error{ + fmt.Errorf(errorMsg), + } + errors2 := []error{ + fmt.Errorf(errorMsg1), + fmt.Errorf(errorMsg2), + } + + nameUnknown := "not existing name" + fallbackStatus, fallbackErrorResponse := dtoLib.FallbackToFailedError(nameUnknown, nil) + + name := "new name 2" + apiErrorResponse := dtoLib.ApiErrorResponse{ + Code: 1111406, + Type: name, + Message: "New name not created", + } + apiErrorResponse1 := dtoLib.ApiErrorResponse{ + Code: 1111406, + Type: name, + Message: "New name not created", + Errors: errors, + Data: errorMsg, + } + apiErrorResponse2 := dtoLib.ApiErrorResponse{ + Code: 1111406, + Type: name, + Message: "New name not created", + Errors: errors2, + } + + err := dtoLib.AddNewApiErrorResponse( + name, + map[string]dtoLib.ApiErrorResponse{ + dtoLib.StatusNotCreated: apiErrorResponse, + }, + ) + assert.Nil(t, err) + + tests := map[string]struct { + input struct { + name string + errors []error + } + expect struct { + obj *dtoLib.ApiErrorResponse + status int + } + }{ + "NotCreated ok": { + // input + input: struct { + name string + errors []error + }{ + name: name, + errors: errorsEmpty, + }, + // expect + expect: struct { + obj *dtoLib.ApiErrorResponse + status int + }{ + obj: &apiErrorResponse, + status: http.StatusNotAcceptable, + }, + }, + "NotCreated 1 error ok": { + // input + input: struct { + name string + errors []error + }{ + name: name, + errors: errors, + }, + // expect + expect: struct { + obj *dtoLib.ApiErrorResponse + status int + }{ + obj: &apiErrorResponse1, + status: http.StatusNotAcceptable, + }, + }, + "NotCreated 2 errors ok": { + // input + input: struct { + name string + errors []error + }{ + name: name, + errors: errors2, + }, + // expect + expect: struct { + obj *dtoLib.ApiErrorResponse + status int + }{ + obj: &apiErrorResponse2, + status: http.StatusNotAcceptable, + }, + }, + "name wrong": { + // input + input: struct { + name string + errors []error + }{ + name: nameUnknown, + errors: errorsEmpty, + }, + // expect + expect: struct { + obj *dtoLib.ApiErrorResponse + status int + }{ + obj: &fallbackErrorResponse, + status: fallbackStatus, + }, + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + actualStatus, actualResponse := dtoLib.NotCreated(tc.input.name, tc.input.errors) + assert.Equal(t, tc.expect.status, actualStatus) + if nil != tc.expect.obj { + assert.Equal(t, *tc.expect.obj, actualResponse) + } + }) + } +} + +func TestCreated(t *testing.T) { + nameUnknown := "not existing name" + fallbackStatus, fallbackResponse := dtoLib.FallbackToFailed(nameUnknown) + + name := "new name 3" + apiResponse := dtoLib.ApiResponse{ + Code: 1111201, + Type: name, + Message: "New name created", + } + + var data interface{} + err := dtoLib.AddNewApiResponse( + name, + map[string]dtoLib.ApiResponse{ + dtoLib.StatusCreated: apiResponse, + }, + ) + assert.Nil(t, err) + + tests := map[string]struct { + input struct { + name string + data interface{} + } + expect struct { + obj *dtoLib.ApiResponse + status int + } + }{ + "Created ok": { + // input + input: struct { + name string + data interface{} + }{ + name: name, + data: data, + }, + // expect + expect: struct { + obj *dtoLib.ApiResponse + status int + }{ + obj: &apiResponse, + status: http.StatusCreated, + }, + }, + "name wrong": { + // input + input: struct { + name string + data interface{} + }{ + name: nameUnknown, + data: data, + }, + // expect + expect: struct { + obj *dtoLib.ApiResponse + status int + }{ + obj: &fallbackResponse, + status: fallbackStatus, + }, + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + actualStatus, actualResponse := dtoLib.Created(tc.input.name, tc.input.data) + assert.Equal(t, tc.expect.status, actualStatus) + if nil != tc.expect.obj { + assert.Equal(t, *tc.expect.obj, actualResponse) + } + }) + } +} + +func TestNotUpdated(t *testing.T) { + errorMsg := "not updated error" + errorMsg1 := "not updated error 1" + errorMsg2 := "not updated error 2" + var errorsEmpty []error + errors := []error{ + fmt.Errorf(errorMsg), + } + errors2 := []error{ + fmt.Errorf(errorMsg1), + fmt.Errorf(errorMsg2), + } + + nameUnknown := "not existing name" + fallbackStatus, fallbackErrorResponse := dtoLib.FallbackToFailedError(nameUnknown, nil) + + name := "new name 4" + apiErrorResponse := dtoLib.ApiErrorResponse{ + Code: 1111406, + Type: name, + Message: "New name not updated", + } + apiErrorResponse1 := dtoLib.ApiErrorResponse{ + Code: 1111406, + Type: name, + Message: "New name not updated", + Errors: errors, + Data: errorMsg, + } + apiErrorResponse2 := dtoLib.ApiErrorResponse{ + Code: 1111406, + Type: name, + Message: "New name not updated", + Errors: errors2, + } + + err := dtoLib.AddNewApiErrorResponse( + name, + map[string]dtoLib.ApiErrorResponse{ + dtoLib.StatusNotUpdated: apiErrorResponse, + }, + ) + assert.Nil(t, err) + + tests := map[string]struct { + input struct { + name string + errors []error + } + expect struct { + obj *dtoLib.ApiErrorResponse + status int + } + }{ + "NotUpdated ok": { + // input + input: struct { + name string + errors []error + }{ + name: name, + errors: errorsEmpty, + }, + // expect + expect: struct { + obj *dtoLib.ApiErrorResponse + status int + }{ + obj: &apiErrorResponse, + status: http.StatusNotAcceptable, + }, + }, + "NotUpdated 1 error ok": { + // input + input: struct { + name string + errors []error + }{ + name: name, + errors: errors, + }, + // expect + expect: struct { + obj *dtoLib.ApiErrorResponse + status int + }{ + obj: &apiErrorResponse1, + status: http.StatusNotAcceptable, + }, + }, + "NotUpdated 2 errors ok": { + // input + input: struct { + name string + errors []error + }{ + name: name, + errors: errors2, + }, + // expect + expect: struct { + obj *dtoLib.ApiErrorResponse + status int + }{ + obj: &apiErrorResponse2, + status: http.StatusNotAcceptable, + }, + }, + "name wrong": { + // input + input: struct { + name string + errors []error + }{ + name: nameUnknown, + errors: errorsEmpty, + }, + // expect + expect: struct { + obj *dtoLib.ApiErrorResponse + status int + }{ + obj: &fallbackErrorResponse, + status: fallbackStatus, + }, + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + actualStatus, actualResponse := dtoLib.NotUpdated(tc.input.name, tc.input.errors) + assert.Equal(t, tc.expect.status, actualStatus) + if nil != tc.expect.obj { + assert.Equal(t, *tc.expect.obj, actualResponse) + } + }) + } +} + +func TestUpdated(t *testing.T) { + nameUnknown := "not existing name" + fallbackStatus, fallbackResponse := dtoLib.FallbackToFailed(nameUnknown) + + name := "new name 5" + apiResponse := dtoLib.ApiResponse{ + Code: 1111202, + Type: name, + Message: "New name updated", + } + + var data interface{} + err := dtoLib.AddNewApiResponse( + name, + map[string]dtoLib.ApiResponse{ + dtoLib.StatusUpdated: apiResponse, + }, + ) + assert.Nil(t, err) + + tests := map[string]struct { + input struct { + name string + data interface{} + } + expect struct { + obj *dtoLib.ApiResponse + status int + } + }{ + "Updated ok": { + // input + input: struct { + name string + data interface{} + }{ + name: name, + data: data, + }, + // expect + expect: struct { + obj *dtoLib.ApiResponse + status int + }{ + obj: &apiResponse, + status: http.StatusAccepted, + }, + }, + "name wrong": { + // input + input: struct { + name string + data interface{} + }{ + name: nameUnknown, + data: data, + }, + // expect + expect: struct { + obj *dtoLib.ApiResponse + status int + }{ + obj: &fallbackResponse, + status: fallbackStatus, + }, + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + actualStatus, actualResponse := dtoLib.Updated(tc.input.name, tc.input.data) + assert.Equal(t, tc.expect.status, actualStatus) + if nil != tc.expect.obj { + assert.Equal(t, *tc.expect.obj, actualResponse) + } + }) + } +} + +func TestNotDeleted(t *testing.T) { + errorMsg := "not deleted error" + errorMsg1 := "not deleted error 1" + errorMsg2 := "not deleted error 2" + var errorsEmpty []error + errors := []error{ + fmt.Errorf(errorMsg), + } + errors2 := []error{ + fmt.Errorf(errorMsg1), + fmt.Errorf(errorMsg2), + } + + nameUnknown := "not existing name" + fallbackStatus, fallbackErrorResponse := dtoLib.FallbackToFailedError(nameUnknown, nil) + + name := "new name 6" + apiErrorResponse := dtoLib.ApiErrorResponse{ + Code: 1111406, + Type: name, + Message: "New name not deleted", + } + apiErrorResponse1 := dtoLib.ApiErrorResponse{ + Code: 1111406, + Type: name, + Message: "New name not deleted", + Errors: errors, + Data: errorMsg, + } + apiErrorResponse2 := dtoLib.ApiErrorResponse{ + Code: 1111406, + Type: name, + Message: "New name not deleted", + Errors: errors2, + } + + err := dtoLib.AddNewApiErrorResponse( + name, + map[string]dtoLib.ApiErrorResponse{ + dtoLib.StatusNotDeleted: apiErrorResponse, + }, + ) + assert.Nil(t, err) + + tests := map[string]struct { + input struct { + name string + errors []error + } + expect struct { + obj *dtoLib.ApiErrorResponse + status int + } + }{ + "NotDeleted ok": { + // input + input: struct { + name string + errors []error + }{ + name: name, + errors: errorsEmpty, + }, + // expect + expect: struct { + obj *dtoLib.ApiErrorResponse + status int + }{ + obj: &apiErrorResponse, + status: http.StatusNotFound, + }, + }, + "NotDeleted 1 error ok": { + // input + input: struct { + name string + errors []error + }{ + name: name, + errors: errors, + }, + // expect + expect: struct { + obj *dtoLib.ApiErrorResponse + status int + }{ + obj: &apiErrorResponse1, + status: http.StatusNotFound, + }, + }, + "NotDeleted 2 errors ok": { + // input + input: struct { + name string + errors []error + }{ + name: name, + errors: errors2, + }, + // expect + expect: struct { + obj *dtoLib.ApiErrorResponse + status int + }{ + obj: &apiErrorResponse2, + status: http.StatusNotFound, + }, + }, + "name wrong": { + // input + input: struct { + name string + errors []error + }{ + name: nameUnknown, + errors: errorsEmpty, + }, + // expect + expect: struct { + obj *dtoLib.ApiErrorResponse + status int + }{ + obj: &fallbackErrorResponse, + status: fallbackStatus, + }, + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + actualStatus, actualResponse := dtoLib.NotDeleted(tc.input.name, tc.input.errors) + assert.Equal(t, tc.expect.status, actualStatus) + if nil != tc.expect.obj { + assert.Equal(t, *tc.expect.obj, actualResponse) + } + }) + } +} + +func TestDeleted(t *testing.T) { + nameUnknown := "not existing name" + fallbackStatus, fallbackResponse := dtoLib.FallbackToFailed(nameUnknown) + + name := "new name 7" + apiResponse := dtoLib.ApiResponse{ + Code: 1111200, + Type: name, + Message: "New name deleted", + } + + var data interface{} + err := dtoLib.AddNewApiResponse( + name, + map[string]dtoLib.ApiResponse{ + dtoLib.StatusDeleted: apiResponse, + }, + ) + assert.Nil(t, err) + + tests := map[string]struct { + input struct { + name string + data interface{} + } + expect struct { + obj *dtoLib.ApiResponse + status int + } + }{ + "Deleted ok": { + // input + input: struct { + name string + data interface{} + }{ + name: name, + data: data, + }, + // expect + expect: struct { + obj *dtoLib.ApiResponse + status int + }{ + obj: &apiResponse, + status: http.StatusOK, + }, + }, + "name wrong": { + // input + input: struct { + name string + data interface{} + }{ + name: nameUnknown, + data: data, + }, + // expect + expect: struct { + obj *dtoLib.ApiResponse + status int + }{ + obj: &fallbackResponse, + status: fallbackStatus, + }, + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + actualStatus, actualResponse := dtoLib.Deleted(tc.input.name, tc.input.data) + assert.Equal(t, tc.expect.status, actualStatus) + if nil != tc.expect.obj { + assert.Equal(t, *tc.expect.obj, actualResponse) + } + }) + } +} + +func TestNotFound(t *testing.T) { + errorMsg := "not found error" + errorMsg1 := "not found error 1" + errorMsg2 := "not found error 2" + var errorsEmpty []error + errors := []error{ + fmt.Errorf(errorMsg), + } + errors2 := []error{ + fmt.Errorf(errorMsg1), + fmt.Errorf(errorMsg2), + } + + nameUnknown := "not existing name" + fallbackStatus, fallbackErrorResponse := dtoLib.FallbackToFailedError(nameUnknown, nil) + + name := "new name 8" + apiErrorResponse := dtoLib.ApiErrorResponse{ + Code: 1111404, + Type: name, + Message: "New name not found", + } + apiErrorResponse1 := dtoLib.ApiErrorResponse{ + Code: 1111404, + Type: name, + Message: "New name not found", + Errors: errors, + Data: errorMsg, + } + apiErrorResponse2 := dtoLib.ApiErrorResponse{ + Code: 1111404, + Type: name, + Message: "New name not found", + Errors: errors2, + } + + err := dtoLib.AddNewApiErrorResponse( + name, + map[string]dtoLib.ApiErrorResponse{ + dtoLib.StatusNotFound: apiErrorResponse, + }, + ) + assert.Nil(t, err) + + tests := map[string]struct { + input struct { + name string + errors []error + } + expect struct { + obj *dtoLib.ApiErrorResponse + status int + } + }{ + "NotFound ok": { + // input + input: struct { + name string + errors []error + }{ + name: name, + errors: errorsEmpty, + }, + // expect + expect: struct { + obj *dtoLib.ApiErrorResponse + status int + }{ + obj: &apiErrorResponse, + status: http.StatusNotFound, + }, + }, + "NotFound 1 error ok": { + // input + input: struct { + name string + errors []error + }{ + name: name, + errors: errors, + }, + // expect + expect: struct { + obj *dtoLib.ApiErrorResponse + status int + }{ + obj: &apiErrorResponse1, + status: http.StatusNotFound, + }, + }, + "NotFound 2 errors ok": { + // input + input: struct { + name string + errors []error + }{ + name: name, + errors: errors2, + }, + // expect + expect: struct { + obj *dtoLib.ApiErrorResponse + status int + }{ + obj: &apiErrorResponse2, + status: http.StatusNotFound, + }, + }, + "name wrong": { + // input + input: struct { + name string + errors []error + }{ + name: nameUnknown, + errors: errorsEmpty, + }, + // expect + expect: struct { + obj *dtoLib.ApiErrorResponse + status int + }{ + obj: &fallbackErrorResponse, + status: fallbackStatus, + }, + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + actualStatus, actualResponse := dtoLib.NotFound(tc.input.name, tc.input.errors) + assert.Equal(t, tc.expect.status, actualStatus) + if nil != tc.expect.obj { + assert.Equal(t, *tc.expect.obj, actualResponse) + } + }) + } +} + +func TestFound(t *testing.T) { + nameUnknown := "not existing name" + fallbackStatus, fallbackResponse := dtoLib.FallbackToFailed(nameUnknown) + + name := "new name 9" + apiResponse := dtoLib.ApiResponse{ + Code: 1111200, + Type: name, + Message: "New name found", + } + apiResponsePaginated := dtoLib.ApiResponse{ + Code: 1111200, + Type: name, + Message: "New name found", + PaginationContainer: &dtoLib.PaginationContainer{ + Pagination: dtoLib.Pagination{ + Page: 0, + PerPage: 0, + Total: 0, + }, + }, + } + + var data interface{} + var pagination interface{} + err := dtoLib.AddNewApiResponse( + name, + map[string]dtoLib.ApiResponse{ + dtoLib.StatusFound: apiResponse, + }, + ) + assert.Nil(t, err) + + tests := map[string]struct { + input struct { + name string + data interface{} + pagination interface{} + } + expect struct { + obj *dtoLib.ApiResponse + status int + } + }{ + "Found ok": { + // input + input: struct { + name string + data interface{} + pagination interface{} + }{ + name: name, + data: data, + }, + // expect + expect: struct { + obj *dtoLib.ApiResponse + status int + }{ + obj: &apiResponse, + status: http.StatusOK, + }, + }, + "Found with pagination ok": { + // input + input: struct { + name string + data interface{} + pagination interface{} + }{ + name: name, + data: data, + pagination: &pagination, + }, + // expect + expect: struct { + obj *dtoLib.ApiResponse + status int + }{ + obj: &apiResponsePaginated, + status: http.StatusOK, + }, + }, + "name wrong": { + // input + input: struct { + name string + data interface{} + pagination interface{} + }{ + name: nameUnknown, + data: data, + }, + // expect + expect: struct { + obj *dtoLib.ApiResponse + status int + }{ + obj: &fallbackResponse, + status: fallbackStatus, + }, + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + actualStatus, actualResponse := dtoLib.Found(tc.input.name, tc.input.data, tc.input.pagination) + assert.Equal(t, tc.expect.status, actualStatus) + if nil != tc.expect.obj { + assert.Equal(t, *tc.expect.obj, actualResponse) + } + }) + } +} + +func TestOk(t *testing.T) { + nameUnknown := "not existing name" + fallbackStatus, fallbackResponse := dtoLib.FallbackToFailed(nameUnknown) + + name := "new name 10" + apiResponse := dtoLib.ApiResponse{ + Code: 1111200, + Type: name, + Message: "New name ok", + } + + var data interface{} + err := dtoLib.AddNewApiResponse( + name, + map[string]dtoLib.ApiResponse{ + dtoLib.StatusOk: apiResponse, + }, + ) + assert.Nil(t, err) + + tests := map[string]struct { + input struct { + name string + data interface{} + } + expect struct { + obj *dtoLib.ApiResponse + status int + } + }{ + "Ok ok": { + // input + input: struct { + name string + data interface{} + }{ + name: name, + data: data, + }, + // expect + expect: struct { + obj *dtoLib.ApiResponse + status int + }{ + obj: &apiResponse, + status: http.StatusOK, + }, + }, + "name wrong": { + // input + input: struct { + name string + data interface{} + }{ + name: nameUnknown, + data: data, + }, + // expect + expect: struct { + obj *dtoLib.ApiResponse + status int + }{ + obj: &fallbackResponse, + status: fallbackStatus, + }, + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + actualStatus, actualResponse := dtoLib.Ok(tc.input.name, tc.input.data) + assert.Equal(t, tc.expect.status, actualStatus) + if nil != tc.expect.obj { + assert.Equal(t, *tc.expect.obj, actualResponse) + } + }) + } +} + +func TestFailed(t *testing.T) { + errorMsg := "not found error" + errorMsg1 := "not found error 1" + errorMsg2 := "not found error 2" + var errorsEmpty []error + errors := []error{ + fmt.Errorf(errorMsg), + } + errors2 := []error{ + fmt.Errorf(errorMsg1), + fmt.Errorf(errorMsg2), + } + + nameUnknown := "not existing name" + fallbackStatus, fallbackErrorResponse := dtoLib.FallbackToFailedError(nameUnknown, nil) + + name := "new name 11" + apiErrorResponse := dtoLib.ApiErrorResponse{ + Code: 1111502, + Type: name, + Message: "New name failed", + } + apiErrorResponse1 := dtoLib.ApiErrorResponse{ + Code: 1111502, + Type: name, + Message: "New name failed", + Errors: errors, + Data: errorMsg, + } + apiErrorResponse2 := dtoLib.ApiErrorResponse{ + Code: 1111502, + Type: name, + Message: "New name failed", + Errors: errors2, + } + + err := dtoLib.AddNewApiErrorResponse( + name, + map[string]dtoLib.ApiErrorResponse{ + dtoLib.StatusFailed: apiErrorResponse, + }, + ) + assert.Nil(t, err) + + tests := map[string]struct { + input struct { + name string + errors []error + } + expect struct { + obj *dtoLib.ApiErrorResponse + status int + } + }{ + "Failed ok": { + // input + input: struct { + name string + errors []error + }{ + name: name, + errors: errorsEmpty, + }, + // expect + expect: struct { + obj *dtoLib.ApiErrorResponse + status int + }{ + obj: &apiErrorResponse, + status: http.StatusInternalServerError, + }, + }, + "Failed 1 error ok": { + // input + input: struct { + name string + errors []error + }{ + name: name, + errors: errors, + }, + // expect + expect: struct { + obj *dtoLib.ApiErrorResponse + status int + }{ + obj: &apiErrorResponse1, + status: http.StatusInternalServerError, + }, + }, + "Failed 2 errors ok": { + // input + input: struct { + name string + errors []error + }{ + name: name, + errors: errors2, + }, + // expect + expect: struct { + obj *dtoLib.ApiErrorResponse + status int + }{ + obj: &apiErrorResponse2, + status: http.StatusInternalServerError, + }, + }, + "name wrong": { + // input + input: struct { + name string + errors []error + }{ + name: nameUnknown, + errors: errorsEmpty, + }, + // expect + expect: struct { + obj *dtoLib.ApiErrorResponse + status int + }{ + obj: &fallbackErrorResponse, + status: fallbackStatus, + }, + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + actualStatus, actualResponse := dtoLib.Failed(tc.input.name, tc.input.errors) + assert.Equal(t, tc.expect.status, actualStatus) + if nil != tc.expect.obj { + assert.Equal(t, *tc.expect.obj, actualResponse) + } + }) + } +} diff --git a/go.mod b/go.mod index 4a74ff5..081cd3b 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/echo-go/dto-lib go 1.15 -require github.com/go-convert/fields v0.0.0-20200219202448-dea7cb49b0d6 +require ( + github.com/go-convert/fields v0.0.0-20200219202448-dea7cb49b0d6 + github.com/stretchr/testify v1.4.0 +)