From ab034cabc7ca198d5ff75d177cdcdda24a42cd3a Mon Sep 17 00:00:00 2001 From: thanhhaudev Date: Wed, 30 Oct 2024 15:36:06 +0700 Subject: [PATCH] init service tests --- src/service/author_test.go | 56 +++++++++++++++++++++++++++++++ src/service/book_test.go | 63 +++++++++++++++++++++++++++++++++++ src/service/borrower_test.go | 64 ++++++++++++++++++++++++++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 src/service/author_test.go create mode 100644 src/service/book_test.go create mode 100644 src/service/borrower_test.go diff --git a/src/service/author_test.go b/src/service/author_test.go new file mode 100644 index 0000000..8297e15 --- /dev/null +++ b/src/service/author_test.go @@ -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) + }) + } +} diff --git a/src/service/book_test.go b/src/service/book_test.go new file mode 100644 index 0000000..ab6de13 --- /dev/null +++ b/src/service/book_test.go @@ -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) + }) + } +} diff --git a/src/service/borrower_test.go b/src/service/borrower_test.go new file mode 100644 index 0000000..1b7984f --- /dev/null +++ b/src/service/borrower_test.go @@ -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) + }) + } +}