Skip to content

Commit

Permalink
chore: refactor library to use usecases
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwellgithinji committed Sep 8, 2021
1 parent 218ee0d commit a792fcf
Show file tree
Hide file tree
Showing 10 changed files with 1,370 additions and 194 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ jobs:
misspell -error .
gosec -exclude=G304,G101 ./...
go-acc -o coverage.txt --ignore generated,cmd ./... -- -timeout 60m
grep -v "generated.go" coverage.txt | grep -v "_gen.go" | grep -v "mocks.go" | grep -v "mock.go" | grep -v "*resolver*go" | grep -v "server.go" > coverage.out
grep -v "generated.go" coverage.txt | grep -v "_gen.go" | grep -v "mocks.go" | grep -v "*mocks.go" | grep -v "mock.go" | grep -v "*mock.go" | grep -v "*resolver*go" | grep -v "server.go" > coverage.out
go tool cover -html=coverage.out -o coverage.html
gocov convert coverage.out > coverage.json
gocov report coverage.json > coverage_report.txt
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package mock

import (
"context"
"fmt"

"github.com/savannahghi/engagementcore/pkg/engagement/domain"
"github.com/savannahghi/feedlib"
)

// FakeLibrary defines a mock Libray service
type FakeLibrary struct {
GetFeedContentFn func(
ctx context.Context,
flavour feedlib.Flavour,
) ([]*domain.GhostCMSPost, error)
GetFaqsContentFn func(
ctx context.Context,
flavour feedlib.Flavour,
) ([]*domain.GhostCMSPost, error)
GetLibraryContentFn func(
ctx context.Context,
) ([]*domain.GhostCMSPost, error)
}

// GetFeedContent mocks getting feed content from ghost cms
func (l *FakeLibrary) GetFeedContent(
ctx context.Context,
flavour feedlib.Flavour,
) ([]*domain.GhostCMSPost, error) {
fmt.Println(">>>>@here")
return l.GetFeedContentFn(ctx, flavour)
}

// GetFaqsContent mocks getting FAQ content from ghost cms
func (l *FakeLibrary) GetFaqsContent(
ctx context.Context,
flavour feedlib.Flavour,
) ([]*domain.GhostCMSPost, error) {
return l.GetFaqsContentFn(ctx, flavour)

}

// GetLibraryContent mocks getting Library content from ghost cms
func (l *FakeLibrary) GetLibraryContent(
ctx context.Context,
) ([]*domain.GhostCMSPost, error) {
return l.GetLibraryContentFn(ctx)
}
279 changes: 279 additions & 0 deletions pkg/engagement/infrastructure/services/library/service_unit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
package library_test

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

"github.com/savannahghi/engagementcore/pkg/engagement/domain"
"github.com/savannahghi/engagementcore/pkg/engagement/infrastructure/services/library"
libraryMock "github.com/savannahghi/engagementcore/pkg/engagement/infrastructure/services/library/mock"
"github.com/savannahghi/feedlib"
"github.com/segmentio/ksuid"
)

var (
fakeLibraryService libraryMock.FakeLibrary
)

func getGhostCMSPost() []*domain.GhostCMSPost {
var (
standardTime = time.Date(2021, 2, 3, 4, 4, 4, 5, time.Local)
testText = "test text"
)
return []*domain.GhostCMSPost{
{
ID: ksuid.New().String(),
UUID: ksuid.New().String(),
Slug: "slug",
Title: "title",
HTML: "<html></html>",
Excerpt: "test",
URL: "https://example.com",
FeatureImage: "https://example.com/test.jepeg",
Featured: true,
Visibility: "published",
ReadingTime: 30,
CreatedAt: standardTime,
UpdatedAt: standardTime,
PublishedAt: standardTime,
CommentID: ksuid.New().String(),
Tags: []domain.GhostCMSTag{
{
ID: ksuid.New().String(),
Name: "test",
Slug: "slug",
Description: &testText,
Visibility: "published",
URL: "https://example.com",
},
},
Authors: []domain.GhostCMSAuthor{
{
ID: ksuid.New().String(),
Name: "test",
Slug: "slug",
ProfileImage: "https://example.com/test.jepeg",
Website: "https://example.com",
Location: "test",
Facebook: "https://facebook.com",
Twitter: "https://twitter.com",
URL: "https://example.com",
},
},
PrimaryAuthor: domain.GhostCMSAuthor{
ID: ksuid.New().String(),
Name: "test",
Slug: "slug",
ProfileImage: "https://example.com/test.jepeg",
Website: "https://example.com",
Location: "test",
Facebook: "https://facebook.com",
Twitter: "https://twitter.com",
URL: "https://example.com",
},
PrimaryTag: domain.GhostCMSTag{
ID: ksuid.New().String(),
Name: "test",
Slug: "slug",
URL: "https://example.com",
},
},
}

}

func TestUnit_GetFeedContent(t *testing.T) {
var s library.ServiceLibrary = &fakeLibraryService

ctx := context.Background()
flavor := feedlib.FlavourPro

post := getGhostCMSPost()

type args struct {
ctx context.Context
flavour feedlib.Flavour
}
tests := []struct {
name string
args args
want []*domain.GhostCMSPost
wantErr bool
}{
{
name: "valid: correct params passed",
args: args{
ctx: ctx,
flavour: flavor,
},
want: post,
wantErr: false,
},
{
name: "invalid: no flavor passed",
args: args{
ctx: ctx,
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.name == "valid: correct params passed" {
fakeLibraryService.GetFeedContentFn = func(
ctx context.Context,
flavour feedlib.Flavour,
) ([]*domain.GhostCMSPost, error) {
return post, nil
}
}
if tt.name == "invalid: no flavor passed" {
fakeLibraryService.GetFeedContentFn = func(
ctx context.Context,
flavour feedlib.Flavour,
) ([]*domain.GhostCMSPost, error) {
return nil, fmt.Errorf("test error")
}
}
got, err := s.GetFeedContent(tt.args.ctx, tt.args.flavour)
if (err != nil) != tt.wantErr {
t.Errorf("ServiceLibraryImpl.GetFeedContent() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ServiceLibraryImpl.GetFeedContent() = %v, want %v", got, tt.want)
}
})
}
}

func TestUnit_GetFaqsContent(t *testing.T) {
var s library.ServiceLibrary = &fakeLibraryService

ctx := context.Background()
flavour := feedlib.FlavourPro

post := getGhostCMSPost()

type args struct {
ctx context.Context
flavour feedlib.Flavour
}
tests := []struct {
name string
args args
want []*domain.GhostCMSPost
wantErr bool
}{
{
name: "valid: correct params passed",
args: args{
ctx: ctx,
flavour: flavour,
},
want: post,
wantErr: false,
},
{
name: "invalid: no flavor passed",
args: args{
ctx: ctx,
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.name == "valid: correct params passed" {
fakeLibraryService.GetFaqsContentFn = func(
ctx context.Context,
flavour feedlib.Flavour,
) ([]*domain.GhostCMSPost, error) {
return post, nil
}
}
if tt.name == "invalid: no flavor passed" {
fakeLibraryService.GetFaqsContentFn = func(
ctx context.Context,
flavour feedlib.Flavour,
) ([]*domain.GhostCMSPost, error) {
return nil, fmt.Errorf("test error")
}
}
got, err := s.GetFaqsContent(tt.args.ctx, tt.args.flavour)
if (err != nil) != tt.wantErr {
t.Errorf("ServiceLibraryImpl.GetFaqsContent() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ServiceLibraryImpl.GetFaqsContent() = %v, want %v", got, tt.want)
}
})
}
}

func TestUnit_GetLibraryContent(t *testing.T) {
var s library.ServiceLibrary = &fakeLibraryService

ctx := context.Background()

post := getGhostCMSPost()

type args struct {
ctx context.Context
}
tests := []struct {
name string
args args
want []*domain.GhostCMSPost
wantErr bool
}{
{
name: "valid: correct params passed",
args: args{
ctx: ctx,
},
want: post,
wantErr: false,
},
{
name: "invalid: empty args",
args: args{
ctx: ctx,
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.name == "valid: correct params passed" {
fakeLibraryService.GetLibraryContentFn = func(
ctx context.Context,
) ([]*domain.GhostCMSPost, error) {
return post, nil
}
}
if tt.name == "invalid: empty args" {
fakeLibraryService.GetLibraryContentFn = func(
ctx context.Context,
) ([]*domain.GhostCMSPost, error) {
return nil, fmt.Errorf("test error")
}
}
got, err := s.GetLibraryContent(tt.args.ctx)
if (err != nil) != tt.wantErr {
t.Errorf("ServiceLibraryImpl.GetLibraryContent() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ServiceLibraryImpl.GetLibraryContent() = %v, want %v", got, tt.want)
}
})
}
}
Loading

0 comments on commit a792fcf

Please sign in to comment.