Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added mocks for testing http service #1013

Merged
merged 6 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 45 additions & 15 deletions pkg/gofr/container/mock_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,35 @@ import (
"gofr.dev/pkg/gofr/datasource/pubsub"
"gofr.dev/pkg/gofr/datasource/sql"
"gofr.dev/pkg/gofr/logging"
"gofr.dev/pkg/gofr/service"
)

type Mocks struct {
Redis *MockRedis
SQL *mockSQL
Clickhouse *MockClickhouse
Cassandra *MockCassandra
Mongo *MockMongo
KVStore *MockKVStore
File *file.MockFileSystemProvider
Redis *MockRedis
SQL *mockSQL
Clickhouse *MockClickhouse
Cassandra *MockCassandra
Mongo *MockMongo
KVStore *MockKVStore
File *file.MockFileSystemProvider
HTTPService *service.MockHTTP
}

func NewMockContainer(t *testing.T) (*Container, Mocks) {
type options func(c *Container, ctrl *gomock.Controller) any

//nolint:revive //Because user should not access the options, and we might change it to an interface in the future.
func WithMockHTTPService(httpServiceNames ...string) options {
return func(c *Container, ctrl *gomock.Controller) any {
mockservice := service.NewMockHTTP(ctrl)
for i := range httpServiceNames {
c.Services[httpServiceNames[i]] = mockservice
}
coolwednesday marked this conversation as resolved.
Show resolved Hide resolved

return mockservice
}
}

func NewMockContainer(t *testing.T, options ...options) (*Container, Mocks) {
t.Helper()

container := &Container{}
Expand Down Expand Up @@ -62,14 +78,28 @@ func NewMockContainer(t *testing.T) (*Container, Mocks) {
fileStoreMock := file.NewMockFileSystemProvider(ctrl)
container.File = fileStoreMock

var httpMock *service.MockHTTP

container.Services = make(map[string]service.HTTP)

for _, option := range options {
optionsAdded := option(container, ctrl)

val, ok := optionsAdded.(*service.MockHTTP)
if ok {
httpMock = val
}
}

mocks := Mocks{
Redis: redisMock,
SQL: sqlMockWrapper,
Clickhouse: clickhouseMock,
Cassandra: cassandraMock,
Mongo: mongoMock,
KVStore: kvStoreMock,
File: fileStoreMock,
Redis: redisMock,
SQL: sqlMockWrapper,
Clickhouse: clickhouseMock,
Cassandra: cassandraMock,
Mongo: mongoMock,
KVStore: kvStoreMock,
File: fileStoreMock,
HTTPService: httpMock,
}

redisMock.EXPECT().Close().AnyTimes()
Expand Down
45 changes: 45 additions & 0 deletions pkg/gofr/container/mockcontainer_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,59 @@
package container

import (
"bytes"
"context"
"net/http/httptest"
"testing"

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

"gofr.dev/pkg/gofr/datasource"
)

func Test_HttpServiceMock(t *testing.T) {
test := struct {
desc string
path string
statusCode int
expectedRes string
}{

desc: "simple service handler",
path: "/fact",
expectedRes: `{"data":{"fact":"Cats have 3 eyelids.","length":20}}` + "\n",
statusCode: 200,
}

httpservices := []string{"cat-facts", "cat-facts1", "cat-facts2"}

_, mock := NewMockContainer(t, WithMockHTTPService(httpservices...))

res := httptest.NewRecorder()
res.Body = bytes.NewBufferString(`{"fact":"Cats have 3 eyelids.","length":20}` + "\n")
res.Code = test.statusCode
result := res.Result()

// Setting mock expectations
mock.HTTPService.EXPECT().Get(context.Background(), "fact", map[string]interface{}{
"max_length": 20,
}).Return(result, nil)

resp, err := mock.HTTPService.Get(context.Background(), "fact", map[string]interface{}{
coolwednesday marked this conversation as resolved.
Show resolved Hide resolved
"max_length": 20,
})

require.NoError(t, err)
assert.Equal(t, resp, result)

err = result.Body.Close()
require.NoError(t, err)

err = resp.Body.Close()
require.NoError(t, err)
}

// TestMockSQL_Select tests the successful operation of SQL mocking for SELECT statements.
// It checks that the mock expectations are correctly set and that the SQL database function
// is called as expected.
Expand Down
Loading
Loading