Skip to content

Commit

Permalink
chore: refactor surveys service to be acessed at usecases level
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwellgithinji committed Sep 10, 2021
1 parent f08647e commit e886d35
Show file tree
Hide file tree
Showing 8 changed files with 513 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
package mock

import (
"context"

"github.com/savannahghi/engagementcore/pkg/engagement/application/common/dto"
)

// FakeSUrveys simulates the behavior of our Surveys response implementation
type FakeSUrveys struct {
RecordNPSResponseFn func(
ctx context.Context,
input dto.NPSInput,
) (bool, error)
}

// RecordNPSResponse is a mock of the RecordNPSResponse method
func (s *FakeSUrveys) RecordNPSResponse(
ctx context.Context,
input dto.NPSInput,
) (bool, error) {
return s.RecordNPSResponseFn(ctx, input)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package surveys_test

import (
"context"
"os"
"testing"

"github.com/savannahghi/engagementcore/pkg/engagement/application/common/dto"
db "github.com/savannahghi/engagementcore/pkg/engagement/infrastructure/database/firestore"
"github.com/savannahghi/engagementcore/pkg/engagement/infrastructure/services/surveys"
"github.com/savannahghi/firebasetools"
"github.com/savannahghi/interserviceclient"
"github.com/stretchr/testify/assert"
)

func TestMain(m *testing.M) {
os.Exit(m.Run())
}

func initializeTestService(ctx context.Context, t *testing.T) (*surveys.ServiceSurveyImpl, error) {
fr, err := db.NewFirebaseRepository(ctx)
if err != nil {
t.Errorf("can't instantiate firebase repository in resolver: %w", err)
return nil, err
}

s := surveys.NewService(fr)
if s == nil {
t.Errorf("nil FCM service")
return nil, err
}
return s, nil
}

func onboardingISCClient(t *testing.T) *interserviceclient.InterServiceClient {
deps, err := interserviceclient.LoadDepsFromYAML()
if err != nil {
t.Errorf("can't load inter-service config from YAML: %v", err)
return nil
}

profileClient, err := interserviceclient.SetupISCclient(*deps, "profile")
if err != nil {
t.Errorf("can't set up profile interservice client: %v", err)
return nil
}

return profileClient
}

func TestNewService(t *testing.T) {
ctx := context.Background()
s, err := initializeTestService(ctx, t)
if err != nil {
t.Errorf("an error occurred %v", err)
return
}
assert.NotNil(t, s)

tests := []struct {
name string
want *surveys.ServiceSurveyImpl
}{
{
name: "good case",
want: s,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := s
assert.NotNil(t, got)
})
}
}

func TestServiceSurveyImpl_RecordNPSResponse(t *testing.T) {
onboardingClient := onboardingISCClient(t)
ctx, token, err := interserviceclient.GetPhoneNumberAuthenticatedContextAndToken(t, onboardingClient)
if err != nil {
t.Errorf("cant get phone number authenticated context token: %v", err)
return
}
_, err = firebasetools.GetAuthenticatedContextFromUID(ctx, token.UID)
if err != nil {
t.Errorf("cant get authenticated context from UID: %v", err)
return
}

s, err := initializeTestService(ctx, t)
if err != nil {
t.Errorf("an error occurred %v", err)
return
}

type args struct {
ctx context.Context
input dto.NPSInput
}
tests := []struct {
name string
args args
want bool
wantErr bool
panics bool
}{
{
name: "invalid: missing input",
args: args{},
panics: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if !tt.panics {
got, err := s.RecordNPSResponse(tt.args.ctx, tt.args.input)
if (err != nil) != tt.wantErr {
t.Errorf("ServiceSurveyImpl.RecordNPSResponse() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ServiceSurveyImpl.RecordNPSResponse() = %v, want %v", got, tt.want)
}
}
if tt.panics {
fcRecordNPSResponse := func() { _, _ = s.RecordNPSResponse(tt.args.ctx, tt.args.input) }
assert.Panics(t, fcRecordNPSResponse)
}
})
}
}
67 changes: 0 additions & 67 deletions pkg/engagement/infrastructure/services/surveys/service_test.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package surveys_test

import (
"context"
"fmt"
"testing"

"github.com/savannahghi/engagementcore/pkg/engagement/application/common/dto"
"github.com/savannahghi/engagementcore/pkg/engagement/infrastructure/services/surveys"
surveysMock "github.com/savannahghi/engagementcore/pkg/engagement/infrastructure/services/surveys/mock"
"github.com/savannahghi/firebasetools"
"github.com/savannahghi/interserviceclient"
"github.com/segmentio/ksuid"
)

var (
fakeSurveysService surveysMock.FakeSUrveys
)

func TestUnit_RecordNPSResponse(t *testing.T) {

var s surveys.ServiceSurveys = &fakeSurveysService

email := firebasetools.TestUserEmail
phoneNumber := interserviceclient.TestUserPhoneNumber

feedback := []*dto.FeedbackInput{
{
Question: "test?",
Answer: "test",
},
}

input := dto.NPSInput{
Name: "test",
Score: 8,
SladeCode: ksuid.New().String(),
Email: &email,
PhoneNumber: &phoneNumber,
Feedback: feedback,
}

type args struct {
ctx context.Context
input dto.NPSInput
}
tests := []struct {
name string

args args
want bool
wantErr bool
}{
{
name: "happy case",
args: args{
input: input,
},
wantErr: false,
want: true,
},
{
name: "sad case",
args: args{},
wantErr: true,
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.name == "happy case" {
fakeSurveysService.RecordNPSResponseFn = func(
ctx context.Context,
input dto.NPSInput,
) (bool, error) {
return true, nil
}
}
if tt.name == "sad case" {
fakeSurveysService.RecordNPSResponseFn = func(
ctx context.Context,
input dto.NPSInput,
) (bool, error) {
return false, fmt.Errorf("test error")
}
}

got, err := s.RecordNPSResponse(tt.args.ctx, tt.args.input)
if (err != nil) != tt.wantErr {
t.Errorf("ServiceSurveyImpl.RecordNPSResponse() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ServiceSurveyImpl.RecordNPSResponse() = %v, want %v", got, tt.want)
}
})
}
}
40 changes: 40 additions & 0 deletions pkg/engagement/usecases/surveys/surveys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package surveys

import (
"context"

"github.com/savannahghi/engagementcore/pkg/engagement/application/common/dto"
"github.com/savannahghi/engagementcore/pkg/engagement/infrastructure"
)

// UsecaseSurveys defines surveys service usecases interface
type UsecaseSurveys interface {
RecordNPSResponse(
ctx context.Context,
input dto.NPSInput,
) (bool, error)
}

// ImplSurveys is the Surveys service implementation
type ImplSurveys struct {
infrastructure infrastructure.Interactor
}

// NewSurveys initializes a Surveys service instance
func NewSurveys(infrastructure infrastructure.Interactor) *ImplSurveys {
return &ImplSurveys{
infrastructure: infrastructure,
}
}

// RecordNPSResponse records the NPS response
func (f *ImplSurveys) RecordNPSResponse(
ctx context.Context,
input dto.NPSInput,
) (bool, error) {
i := f.infrastructure.ServiceSurveyImpl
return i.RecordNPSResponse(
ctx,
input,
)
}
Loading

0 comments on commit e886d35

Please sign in to comment.