Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Krithika Sundararajan committed Nov 3, 2022
1 parent 0aa265c commit e156b48
Show file tree
Hide file tree
Showing 3 changed files with 304 additions and 10 deletions.
117 changes: 107 additions & 10 deletions grpc/response_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package grpc
package grpc_test

import (
"log"
"testing"

"github.com/gojek/fiber"
"github.com/gojek/fiber/grpc"
testproto "github.com/gojek/fiber/internal/testdata/gen/testdata/proto"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/codes"
Expand All @@ -15,16 +17,16 @@ import (
func TestResponse_Backend(t *testing.T) {
tests := []struct {
name string
res Response
want Response
res grpc.Response
want grpc.Response
backendName string
}{
{
name: "ok",
res: Response{
res: grpc.Response{
Metadata: map[string][]string{},
},
want: Response{
want: grpc.Response{
Metadata: metadata.New(map[string]string{"backend": "testing"}),
},
backendName: "testing",
Expand All @@ -42,21 +44,21 @@ func TestResponse_Backend(t *testing.T) {
func TestResponse_Status(t *testing.T) {
tests := []struct {
name string
res Response
res grpc.Response
expectedCode int
expectedSuccess bool
}{
{
name: "ok",
res: Response{
res: grpc.Response{
Status: *status.New(codes.OK, ""),
},
expectedCode: 0,
expectedSuccess: true,
},
{
name: "ok",
res: Response{
res: grpc.Response{
Status: *status.New(codes.InvalidArgument, ""),
},
expectedCode: 3,
Expand All @@ -83,12 +85,12 @@ func TestResponse_Payload(t *testing.T) {
responseByte, _ := proto.Marshal(response)
tests := []struct {
name string
req Response
req grpc.Response
expected []byte
}{
{
name: "",
req: Response{
req: grpc.Response{
Message: responseByte,
},
expected: responseByte,
Expand All @@ -100,3 +102,98 @@ func TestResponse_Payload(t *testing.T) {
})
}
}

func TestResponse_Label(t *testing.T) {
tests := map[string]struct {
response fiber.Response
key string
expected []string
}{
"empty labels": {
response: &grpc.Response{
Metadata: map[string][]string{},
},
key: "dummy-key",
},
"non-empty labels": {
response: &grpc.Response{
Metadata: map[string][]string{"key": []string{"v1", "v2"}},
},
key: "key",
expected: []string{"v1", "v2"},
},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
values := tt.response.Label(tt.key)
assert.Equal(t, tt.expected, values)
})
}
}

func TestResponse_WithLabel(t *testing.T) {
tests := map[string]struct {
response fiber.Response
key string
values []string
expected []string
}{
"new labels": {
response: &grpc.Response{
Metadata: map[string][]string{"key": []string{"v1", "v2"}},
},
key: "k1",
values: []string{"v1", "v2"},
expected: []string{"v1", "v2"},
},
"append labels": {
response: &grpc.Response{
Metadata: map[string][]string{"k1": []string{"v1", "v2"}},
},
key: "k1",
values: []string{"v3"},
expected: []string{"v1", "v2", "v3"},
},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
newLabels := tt.response.WithLabel(tt.key, tt.values...)
assert.Equal(t, tt.expected, newLabels.Label(tt.key))
})
}
}

func TestHTTPResponse_WithLabels(t *testing.T) {
tests := map[string]struct {
response fiber.Response
labels fiber.Labels
key string
expected []string
}{
"new labels": {
response: &grpc.Response{
Metadata: map[string][]string{"key": []string{"v1", "v2"}},
},
labels: fiber.LabelsMap{"k1": []string{"v1", "v2"}},
key: "k1",
expected: []string{"v1", "v2"},
},
"append labels": {
response: &grpc.Response{
Metadata: map[string][]string{"k1": []string{"v1", "v2"}},
},
labels: fiber.LabelsMap{"k1": []string{"v3"}},
key: "k1",
expected: []string{"v1", "v2", "v3"},
},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
newLabels := tt.response.WithLabels(tt.labels)
assert.Equal(t, tt.expected, newLabels.Label(tt.key))
})
}
}
106 changes: 106 additions & 0 deletions http/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/gojek/fiber"
fiberHTTP "github.com/gojek/fiber/http"
"github.com/gojek/fiber/internal/testutils"
)
Expand Down Expand Up @@ -91,3 +93,107 @@ func TestNewHTTPResponse(t *testing.T) {
})
}
}

func TestHTTPResponseLabel(t *testing.T) {
tests := map[string]struct {
response fiber.Response
key string
expected []string
}{
"empty labels": {
response: fiberHTTP.NewHTTPResponse(&http.Response{
Body: makeBody([]byte("{}")),
StatusCode: http.StatusOK,
}),
key: "dummy-key",
},
"case insensitive key": {
response: fiberHTTP.NewHTTPResponse(&http.Response{
Header: http.Header{"Key": []string{"v1", "v2"}},
Body: makeBody([]byte("{}")),
StatusCode: http.StatusOK,
}),
key: "Key",
expected: []string{"v1", "v2"},
},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
values := tt.response.Label(tt.key)
assert.Equal(t, tt.expected, values)
})
}
}

func TestHTTPResponseWithLabel(t *testing.T) {
tests := map[string]struct {
response fiber.Response
key string
values []string
expected []string
}{
"new labels": {
response: fiberHTTP.NewHTTPResponse(&http.Response{
Body: makeBody([]byte("{}")),
StatusCode: http.StatusOK,
}),
key: "k1",
values: []string{"v1", "v2"},
expected: []string{"v1", "v2"},
},
"append labels": {
response: fiberHTTP.NewHTTPResponse(&http.Response{
Header: http.Header{"K1": []string{"v1", "v2"}},
Body: makeBody([]byte("{}")),
StatusCode: http.StatusOK,
}),
key: "k1",
values: []string{"v3"},
expected: []string{"v1", "v2", "v3"},
},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
newLabels := tt.response.WithLabel(tt.key, tt.values...)
assert.Equal(t, tt.expected, newLabels.Label(tt.key))
})
}
}

func TestHTTPResponseWithLabels(t *testing.T) {
tests := map[string]struct {
response fiber.Response
labels fiber.Labels
key string
expected []string
}{
"new labels": {
response: fiberHTTP.NewHTTPResponse(&http.Response{
Body: makeBody([]byte("{}")),
StatusCode: http.StatusOK,
}),
labels: fiber.LabelsMap{"k1": []string{"v1", "v2"}},
key: "K1",
expected: []string{"v1", "v2"},
},
"append labels": {
response: fiberHTTP.NewHTTPResponse(&http.Response{
Header: http.Header{"K1": []string{"v1", "v2"}},
Body: makeBody([]byte("{}")),
StatusCode: http.StatusOK,
}),
labels: fiber.LabelsMap{"k1": []string{"v3"}},
key: "k1",
expected: []string{"v1", "v2", "v3"},
},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
newLabels := tt.response.WithLabels(tt.labels)
assert.Equal(t, tt.expected, newLabels.Label(tt.key))
})
}
}
91 changes: 91 additions & 0 deletions labels_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package fiber_test

import (
"sort"
"testing"

"github.com/gojek/fiber"
"github.com/stretchr/testify/assert"
)

func TestLabelsMapKeys(t *testing.T) {
tests := map[string]struct {
data fiber.LabelsMap
expected []string
}{
"empty map": {
data: fiber.LabelsMap{},
expected: []string{},
},
"non-empty map": {
data: fiber.LabelsMap{"k1": []string{"v1", "v2"}, "k2": []string{"v3"}},
expected: []string{"k1", "k2"},
},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
keys := tt.data.Keys()
// Sort and compare
sort.Slice(keys, func(p, q int) bool {
return keys[p] < keys[q]
})
assert.Equal(t, tt.expected, keys)
})
}
}

func TestLabelsMapWithLabel(t *testing.T) {
tests := map[string]struct {
data fiber.LabelsMap
key string
values []string
expected fiber.LabelsMap
}{
"set key": {
data: fiber.LabelsMap{},
key: "k1",
values: []string{"v1", "v2"},
expected: fiber.LabelsMap{"k1": []string{"v1", "v2"}},
},
"overwrite key": {
data: fiber.LabelsMap{"k1": []string{"v1", "v2"}, "k2": []string{"v3"}},
key: "k2",
values: []string{"new-val"},
expected: fiber.LabelsMap{"k1": []string{"v1", "v2"}, "k2": []string{"new-val"}},
},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
newMap := tt.data.WithLabel(tt.key, tt.values...)
assert.Equal(t, tt.expected, newMap)
})
}
}

func TestLabelsMapLabel(t *testing.T) {
tests := map[string]struct {
data fiber.LabelsMap
key string
expected []string
}{
"empty map": {
data: fiber.LabelsMap{},
key: "k",
expected: []string{},
},
"non-empty map": {
data: fiber.LabelsMap{"k1": []string{"v1", "v2"}, "k2": []string{"v3"}},
key: "k1",
expected: []string{"v1", "v2"},
},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
values := tt.data.Label(tt.key)
assert.Equal(t, tt.expected, values)
})
}
}

0 comments on commit e156b48

Please sign in to comment.