Skip to content

Commit

Permalink
feat: mock tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
macgeargear committed Jul 11, 2024
1 parent 9c7891a commit 2b34e80
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 51 deletions.
85 changes: 46 additions & 39 deletions internal/checkin/test/checkin.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,54 @@ import (
"testing"

"github.com/golang/mock/gomock"
"github.com/isd-sgcu/rpkm67-checkin/config"
"github.com/isd-sgcu/rpkm67-checkin/constant"
"github.com/isd-sgcu/rpkm67-checkin/internal/checkin"
mock_checkin "github.com/isd-sgcu/rpkm67-checkin/mocks/checkin"
"github.com/isd-sgcu/rpkm67-checkin/tracer"
proto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/checkin/checkin/v1"
"github.com/isd-sgcu/rpkm67-model/model"
"github.com/stretchr/testify/suite"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

type CheckinServiceTest struct {
suite.Suite
controller *gomock.Controller
logger *zap.Logger
checkinsModel []*model.CheckIn
checkinModel *model.CheckIn
checkinsProto []*proto.CheckIn
checkinProto *proto.CheckIn
controller *gomock.Controller
logger *zap.Logger
tracer trace.Tracer
checkinsModel []*model.CheckIn
checkinModel *model.CheckIn
checkinsProto []*proto.CheckIn
checkinProto *proto.CheckIn
createCheckInProtoRequest *proto.CreateCheckInRequest
findByEmailCheckInRequest *proto.FindByEmailCheckInRequest
findByUserIdCheckInRequest *proto.FindByUserIdCheckInRequest
}

func TestPinService(t *testing.T) {
func TestCheckinService(t *testing.T) {
suite.Run(t, new(CheckinServiceTest))
}

func (t *CheckinServiceTest) SetupTest() {
tracer, err := tracer.New(&config.Config{})
if err != nil {
t.T().Fatal(err)
}
t.tracer = tracer.Tracer("test")
t.controller = gomock.NewController(t.T())
t.logger = zap.NewNop()
t.checkinsModel = MockCheckInsModel()
t.checkinModel = t.checkinsModel[0]
t.checkinsProto = checkin.ModelToProtoList(t.checkinsModel)
t.checkinProto = t.checkinsProto[0]
t.createCheckInProtoRequest = &proto.CreateCheckInRequest{
Email: t.checkinProto.Email,
Email: t.checkinProto.Email,
UserId: t.checkinProto.UserId,
Event: t.checkinProto.Event,
Event: t.checkinProto.Event,
}
t.findByEmailCheckInRequest = &proto.FindByEmailCheckInRequest{
Email: t.checkinProto.Email,
Expand All @@ -54,15 +63,15 @@ func (t *CheckinServiceTest) SetupTest() {
}

func (t *CheckinServiceTest) TestCreateSuccess() {
repo := mock_checkin.NewMockRepository(t.controller)
svc := checkin.NewService(repo, t.logger)
repo := mock_checkin.NewMockRepository(t.controller)
svc := checkin.NewService(repo, t.logger, t.tracer)

expectedResp := &proto.CreateCheckInResponse {
expectedResp := &proto.CreateCheckInResponse{
CheckIn: t.checkinProto,
}
repo.EXPECT().FindByUserId(gomock.Any(), gomock.Any()).Return(nil)
repo.EXPECT().Create(t.checkinModel).Return(nil)

repo.EXPECT().FindByUserId(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
repo.EXPECT().Create(gomock.Any(), t.checkinModel).Return(nil)

res, err := svc.Create(context.Background(), t.createCheckInProtoRequest)

Expand All @@ -73,11 +82,11 @@ func (t *CheckinServiceTest) TestCreateSuccess() {

func (t *CheckinServiceTest) TestCreateInternalError() {
repo := mock_checkin.NewMockRepository(t.controller)
svc := checkin.NewService(repo, t.logger)
svc := checkin.NewService(repo, t.logger, t.tracer)

expectedErr := status.Error(codes.Internal, constant.InternalServerErrorMessage)

repo.EXPECT().FindByUserId(gomock.Any(), gomock.Any()).Return(expectedErr)
repo.EXPECT().FindByUserId(gomock.Any(), gomock.Any(), gomock.Any()).Return(expectedErr)

res, err := svc.Create(context.Background(), t.createCheckInProtoRequest)

Expand All @@ -88,12 +97,12 @@ func (t *CheckinServiceTest) TestCreateInternalError() {

func (t *CheckinServiceTest) TestCreateAlreadyCheckinError() {
repo := mock_checkin.NewMockRepository(t.controller)
svc := checkin.NewService(repo, t.logger)
svc := checkin.NewService(repo, t.logger, t.tracer)

expectedErr := status.Error(codes.AlreadyExists, constant.AlreadyCheckinErrorMessage)

repo.EXPECT().FindByUserId(gomock.Any(), gomock.Any()).Return(nil)
repo.EXPECT().Create(gomock.Any()).Return(expectedErr)
repo.EXPECT().FindByUserId(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
repo.EXPECT().Create(gomock.Any(), t.checkinModel).Return(expectedErr)

res, err := svc.Create(context.Background(), t.createCheckInProtoRequest)

Expand All @@ -103,12 +112,12 @@ func (t *CheckinServiceTest) TestCreateAlreadyCheckinError() {

func (t *CheckinServiceTest) TestCreateInvalidArgumentError() {
repo := mock_checkin.NewMockRepository(t.controller)
svc := checkin.NewService(repo, t.logger)
svc := checkin.NewService(repo, t.logger, t.tracer)

expectedErr := status.Error(codes.InvalidArgument, constant.InvalidDataErrorMessage)

repo.EXPECT().FindByUserId(gomock.Any(), gomock.Any()).Return(nil)
repo.EXPECT().Create(gomock.Any()).Return(expectedErr)
repo.EXPECT().FindByUserId(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
repo.EXPECT().Create(gomock.Any(), gomock.Any()).Return(expectedErr)

res, err := svc.Create(context.Background(), t.createCheckInProtoRequest)

Expand All @@ -118,15 +127,15 @@ func (t *CheckinServiceTest) TestCreateInvalidArgumentError() {

func (t *CheckinServiceTest) TestFindByEmailSuccess() {
repo := mock_checkin.NewMockRepository(t.controller)
svc := checkin.NewService(repo, t.logger)
svc := checkin.NewService(repo, t.logger, t.tracer)

expectedResp := &proto.FindByEmailCheckInResponse {
expectedResp := &proto.FindByEmailCheckInResponse{
CheckIns: t.checkinsProto,
}

email := t.checkinModel.Email

repo.EXPECT().FindByEmail(email, gomock.Any()).SetArg(1, t.checkinsModel).Return(nil)
repo.EXPECT().FindByEmail(gomock.Any(), email, gomock.Any()).SetArg(2, t.checkinsModel).Return(nil)

res, err := svc.FindByEmail(context.Background(), t.findByEmailCheckInRequest)

Expand All @@ -136,12 +145,12 @@ func (t *CheckinServiceTest) TestFindByEmailSuccess() {

func (t *CheckinServiceTest) TestFindByEmailInternalError() {
repo := mock_checkin.NewMockRepository(t.controller)
svc := checkin.NewService(repo, t.logger)
svc := checkin.NewService(repo, t.logger, t.tracer)

email := t.checkinModel.Email

expectedErr := status.Error(codes.Internal, constant.InternalServerErrorMessage)
repo.EXPECT().FindByEmail(email, gomock.Any()).SetArg(1, t.checkinsModel).Return(expectedErr)
repo.EXPECT().FindByEmail(gomock.Any(), email, gomock.Any()).SetArg(2, t.checkinsModel).Return(expectedErr)

res, err := svc.FindByEmail(context.Background(), t.findByEmailCheckInRequest)

Expand All @@ -151,12 +160,10 @@ func (t *CheckinServiceTest) TestFindByEmailInternalError() {

func (t *CheckinServiceTest) TestFindByEmailRequestCanceledError() {
repo := mock_checkin.NewMockRepository(t.controller)
svc := checkin.NewService(repo, t.logger)

email := t.checkinModel.Email
svc := checkin.NewService(repo, t.logger, t.tracer)

expectedErr := status.Error(codes.Canceled, constant.RequestCancelledErrorMessage)
repo.EXPECT().FindByEmail(email, gomock.Any()).SetArg(1, t.checkinsModel).Return(expectedErr)
repo.EXPECT().FindByEmail(gomock.Any(), t.checkinModel.Email, gomock.Any()).SetArg(2, t.checkinsModel).Return(expectedErr)

res, err := svc.FindByEmail(context.Background(), t.findByEmailCheckInRequest)

Expand All @@ -166,13 +173,13 @@ func (t *CheckinServiceTest) TestFindByEmailRequestCanceledError() {

func (t *CheckinServiceTest) TestFindByUserIdSuccess() {
repo := mock_checkin.NewMockRepository(t.controller)
svc := checkin.NewService(repo, t.logger)
svc := checkin.NewService(repo, t.logger, t.tracer)

expectedResp := &proto.FindByUserIdCheckInResponse {
expectedResp := &proto.FindByUserIdCheckInResponse{
CheckIns: t.checkinsProto,
}

repo.EXPECT().FindByUserId(gomock.Any(), gomock.Any()).SetArg(1, t.checkinsModel).Return(nil)
repo.EXPECT().FindByUserId(gomock.Any(), t.checkinModel.UserID, gomock.Any()).SetArg(2, t.checkinsModel).Return(nil)

res, err := svc.FindByUserId(context.Background(), t.findByUserIdCheckInRequest)

Expand All @@ -182,10 +189,10 @@ func (t *CheckinServiceTest) TestFindByUserIdSuccess() {

func (t *CheckinServiceTest) TestFindByUserIdInternalError() {
repo := mock_checkin.NewMockRepository(t.controller)
svc := checkin.NewService(repo, t.logger)
svc := checkin.NewService(repo, t.logger, t.tracer)

expectedErr := status.Error(codes.Internal, constant.InternalServerErrorMessage)
repo.EXPECT().FindByUserId(gomock.Any(), gomock.Any()).Return(expectedErr)
repo.EXPECT().FindByUserId(gomock.Any(), t.checkinModel.UserID, gomock.Any()).Return(expectedErr)

res, err := svc.FindByUserId(context.Background(), t.findByUserIdCheckInRequest)

Expand All @@ -195,10 +202,10 @@ func (t *CheckinServiceTest) TestFindByUserIdInternalError() {

func (t *CheckinServiceTest) TestFindByUserIdRequestCanceledError() {
repo := mock_checkin.NewMockRepository(t.controller)
svc := checkin.NewService(repo, t.logger)
svc := checkin.NewService(repo, t.logger, t.tracer)

expectedErr := status.Error(codes.Canceled, constant.RequestCancelledErrorMessage)
repo.EXPECT().FindByUserId(gomock.Any(), gomock.Any()).Return(expectedErr)
repo.EXPECT().FindByUserId(gomock.Any(), t.checkinModel.UserID, gomock.Any()).Return(expectedErr)

res, err := svc.FindByUserId(context.Background(), t.findByUserIdCheckInRequest)

Expand Down
27 changes: 27 additions & 0 deletions microservices/opentelemetry/opentelemetry.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
http:
endpoint: 0.0.0.0:4318

processors:
batch:
timeout: 1s

exporters:
otlp:
endpoint: jaeger:4317
tls:
insecure: true
otlphttp:
endpoint: jaeger:4318
debug:

service:
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [otlp, otlphttp, debug]
25 changes: 13 additions & 12 deletions mocks/checkin/checkin.repository.go

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

0 comments on commit 2b34e80

Please sign in to comment.