-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor surveys service to be acessed at usecases level (#46)
- Loading branch information
1 parent
27488c7
commit 8e095b0
Showing
7 changed files
with
394 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
pkg/engagement/infrastructure/services/surveys/service_integration_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
67
pkg/engagement/infrastructure/services/surveys/service_test.go
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
} |
120 changes: 120 additions & 0 deletions
120
pkg/engagement/usecases/surveys/surveys_integration_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package surveys_test | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
"github.com/savannahghi/engagementcore/pkg/engagement/application/common/dto" | ||
"github.com/savannahghi/engagementcore/pkg/engagement/infrastructure" | ||
"github.com/savannahghi/engagementcore/pkg/engagement/usecases/surveys" | ||
"github.com/savannahghi/firebasetools" | ||
"github.com/savannahghi/interserviceclient" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func InitializeTestNewSurveys(ctx context.Context) (*surveys.ImplSurveys, infrastructure.Interactor, error) { | ||
infra := infrastructure.NewInteractor() | ||
surveys := surveys.NewSurveys(infra) | ||
return surveys, infra, nil | ||
} | ||
|
||
func TestMain(m *testing.M) { | ||
os.Exit(m.Run()) | ||
} | ||
|
||
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 := InitializeTestNewSurveys(ctx) | ||
if err != nil { | ||
t.Errorf("failed to initialize test mail interractor: %v", err) | ||
} | ||
assert.NotNil(t, s) | ||
|
||
tests := []struct { | ||
name string | ||
want *surveys.ImplSurveys | ||
}{ | ||
{ | ||
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 := InitializeTestNewSurveys(ctx) | ||
if err != nil { | ||
t.Errorf("failed to initialize test mail interractor: %v", err) | ||
} | ||
|
||
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) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.