Skip to content

Commit

Permalink
init service tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thanhhaudev committed Oct 30, 2024
1 parent 760d20f commit ab034ca
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/service/author_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package service

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/thanhhaudev/go-graphql/src/graph/model"
mock_repository "github.com/thanhhaudev/go-graphql/src/mock"
"go.uber.org/mock/gomock"
)

func TestAuthorService(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockAuthorRepo := mock_repository.NewMockAuthorRepository(ctrl)
service := NewAuthorService(mockAuthorRepo)

tests := []struct {
name string
setup func()
run func() (interface{}, error)
verify func(t *testing.T, result interface{}, err error)
}{
{
name: "CreateAuthor",
setup: func() {
mockAuthorRepo.EXPECT().Create(context.Background(), gomock.Any()).Return(&model.Author{
ID: 1,
Name: "John Doe",
}, nil)

},
run: func() (interface{}, error) {
return service.Create(context.Background(), &model.CreateAuthorInput{
Name: "John Doe",
})
},
verify: func(t *testing.T, result interface{}, err error) {
author := result.(*model.Author)
assert.NoError(t, err)
assert.NotNil(t, author)
assert.Equal(t, "John Doe", author.Name)
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.setup()
result, err := tt.run()
tt.verify(t, result, err)
})
}
}
63 changes: 63 additions & 0 deletions src/service/book_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package service

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/thanhhaudev/go-graphql/src/graph/model"
mock_repository "github.com/thanhhaudev/go-graphql/src/mock"
"go.uber.org/mock/gomock"
)

func TestBookService(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockBookRepo := mock_repository.NewMockBookRepository(ctrl)
mockAuthorRepo := mock_repository.NewMockAuthorRepository(ctrl)
service := NewBookService(mockBookRepo, mockAuthorRepo)

tests := []struct {
name string
setup func()
run func() (interface{}, error)
verify func(t *testing.T, result interface{}, err error)
}{
{
name: "CreateBook",
setup: func() {
mockBookRepo.EXPECT().Create(context.Background(), gomock.Any()).Return(nil)
mockAuthorRepo.EXPECT().FindByIDs(context.Background(), []int{1}).Return([]*model.Author{
{
ID: 1,
},
}, nil)
},
run: func() (interface{}, error) {
return service.Create(context.Background(), &model.CreateBookInput{
Title: "Animal Farm",
Quantity: 1,
Rating: 5,
AuthorIds: []int{1},
PublishAt: time.Date(1945, time.August, 17, 0, 0, 0, 0, time.UTC),
})
},
verify: func(t *testing.T, result interface{}, err error) {
book := result.(*model.Book)
assert.NoError(t, err)
assert.NotNil(t, book)
assert.Equal(t, "Animal Farm", book.Title)
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.setup()
result, err := tt.run()
tt.verify(t, result, err)
})
}
}
64 changes: 64 additions & 0 deletions src/service/borrower_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package service

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/thanhhaudev/go-graphql/src/graph/model"
mock_repository "github.com/thanhhaudev/go-graphql/src/mock"
"go.uber.org/mock/gomock"
)

func TestBorrowerService(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockBorrowerRepo := mock_repository.NewMockBorrowerRepository(ctrl)
mockBookRepo := mock_repository.NewMockBookRepository(ctrl)
service := NewBorrowerService(mockBorrowerRepo, mockBookRepo)

tests := []struct {
name string
setup func()
run func() (interface{}, error)
verify func(t *testing.T, result interface{}, err error)
}{
{
name: "CreateBorrower",
setup: func() {
mockBorrowerRepo.EXPECT().FindByTelNumber(gomock.Any(), "123456789").Return(nil, nil)
mockBorrowerRepo.EXPECT().Create(gomock.Any(), gomock.Any()).Return(&model.Borrower{
ID: 1,
Name: "John Doe",
TelNumber: "123456789",
}, nil)
},
run: func() (interface{}, error) {
input := &model.CreateBorrowerInput{
Name: "John Doe",
TelNumber: "123456789",
Address: "123 Main St",
BirthDate: time.Date(1990, 1, 1, 0, 0, 0, 0, time.UTC),
}
return service.Create(context.Background(), input)
},
verify: func(t *testing.T, result interface{}, err error) {
borrower := result.(*model.Borrower)
assert.NoError(t, err)
assert.NotNil(t, borrower)
assert.Equal(t, "John Doe", borrower.Name)
assert.Equal(t, "123456789", borrower.TelNumber)
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.setup()
result, err := tt.run()
tt.verify(t, result, err)
})
}
}

0 comments on commit ab034ca

Please sign in to comment.