-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
760d20f
commit ab034ca
Showing
3 changed files
with
183 additions
and
0 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
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) | ||
}) | ||
} | ||
} |
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,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) | ||
}) | ||
} | ||
} |
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,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) | ||
}) | ||
} | ||
} |