Skip to content

Commit

Permalink
mock task distributor for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
aradwann committed Jan 15, 2024
1 parent 11bb7e9 commit 735d8e8
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 5 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dropdb:

mock:
mockgen -package mockdb -destination db/mock/store.go github.com/aradwann/eenergy/db/store Store
mockgen -package mockwk -destination worker/mock/distributor.go github.com/aradwann/eenergy/worker TaskDistributor

migrateup:
go run db/scripts/migrate.go
Expand Down
22 changes: 22 additions & 0 deletions gapi/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package gapi

import (
"testing"
"time"

db "github.com/aradwann/eenergy/db/store"
"github.com/aradwann/eenergy/util"
"github.com/aradwann/eenergy/worker"
"github.com/stretchr/testify/require"
)

func newTestServer(t *testing.T, store db.Store, taskDistributor worker.TaskDistributor) *Server {
config := util.Config{
TokenSymmetricKey: util.RandomString(32),
AccessTokenDuration: time.Minute,
}

server, err := NewServer(config, store, taskDistributor)
require.NoError(t, err)
return server
}
133 changes: 133 additions & 0 deletions gapi/rpc_create_user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package gapi

import (
"context"
"fmt"
"reflect"
"testing"

mockdb "github.com/aradwann/eenergy/db/mock"
db "github.com/aradwann/eenergy/db/store"
"github.com/aradwann/eenergy/pb"
"github.com/aradwann/eenergy/util"
"github.com/aradwann/eenergy/worker"
mockwk "github.com/aradwann/eenergy/worker/mock"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

type eqCreateUserTxParamsMatcher struct {
arg db.CreateUserTxParams
password string
user db.User
}

func (expected eqCreateUserTxParamsMatcher) Matches(x interface{}) bool {
actualArg, ok := x.(db.CreateUserTxParams)
if !ok {
return false
}

err := util.CheckPassword(expected.password, actualArg.HashedPassword)
if err != nil {
return false
}

expected.arg.HashedPassword = actualArg.HashedPassword
if !reflect.DeepEqual(expected.arg.CreateUserParams, actualArg.CreateUserParams) {
return false
}

err = actualArg.AfterCreate(expected.user)
return err == nil
}

func (e eqCreateUserTxParamsMatcher) String() string {
return fmt.Sprintf("matches arg %v and password %v", e.arg, e.password)
}

func EqCreateUserTxParams(arg db.CreateUserTxParams, password string, user db.User) gomock.Matcher {
return eqCreateUserTxParamsMatcher{arg, password, user}
}

func randomUser(t *testing.T) (user db.User, password string) {
password = util.RandomString(6)
hashedPassword, err := util.HashPassword(password)
require.NoError(t, err)

user = db.User{
Username: util.RandomOwner(),
HashedPassword: hashedPassword,
FullName: util.RandomOwner(),
Email: util.RandomEmail(),
}
return
}

func TestCreateUserRPC(t *testing.T) {
user, password := randomUser(t)

testCases := []struct {
name string
req *pb.CreateUserRequest
buildStubs func(store *mockdb.MockStore, taskDistributor *mockwk.MockTaskDistributor)
checkResponse func(t *testing.T, res *pb.CreateUserResponse, err error)
}{
{
name: "OK",
req: &pb.CreateUserRequest{
Username: user.Username,
Password: password,
FullName: user.FullName,
Email: user.Email,
},
buildStubs: func(store *mockdb.MockStore, taskDistributor *mockwk.MockTaskDistributor) {
arg := db.CreateUserTxParams{
CreateUserParams: db.CreateUserParams{
Username: user.Username,
FullName: user.FullName,
Email: user.Email,
},
}
store.EXPECT().
CreateUserTx(gomock.Any(), EqCreateUserTxParams(arg, password, user)).
Times(1).
Return(db.CreateUserTxResult{User: user}, nil)

taskPayload := &worker.PayloadSendVerifyEmail{
Username: user.Username,
}
taskDistributor.EXPECT().
DistributeTaskSendVerifyEmail(gomock.Any(), taskPayload, gomock.Any()).
Times(1).
Return(nil)
},
checkResponse: func(t *testing.T, res *pb.CreateUserResponse, err error) {
require.NoError(t, err)
require.NotNil(t, res)
createdUser := res.GetUser()
require.Equal(t, user.Username, createdUser.Username)
require.Equal(t, user.FullName, createdUser.FullName)
require.Equal(t, user.Email, createdUser.Email)
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
storeCtrl := gomock.NewController(t)
defer storeCtrl.Finish()
store := mockdb.NewMockStore(storeCtrl)

taskCtrl := gomock.NewController(t)
defer taskCtrl.Finish()
taskDistributor := mockwk.NewMockTaskDistributor(taskCtrl)

tc.buildStubs(store, taskDistributor)
server := newTestServer(t, store, taskDistributor)

res, err := server.CreateUser(context.Background(), tc.req)
tc.checkResponse(t, res, err)

})
}
}
10 changes: 5 additions & 5 deletions pb/service_eenergy.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions worker/mock/distributor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 735d8e8

Please sign in to comment.