-
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.
- Loading branch information
1 parent
d8db8e6
commit 907c6f5
Showing
6 changed files
with
73 additions
and
19 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
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
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 |
---|---|---|
@@ -1,26 +1,35 @@ | ||
package services | ||
|
||
import ( | ||
"time" | ||
|
||
repo "github.com/VikashChauhan51/go-sample-api/internal/core/interfaces/repositories" | ||
svc "github.com/VikashChauhan51/go-sample-api/internal/core/interfaces/services" | ||
"github.com/VikashChauhan51/go-sample-api/internal/dto" | ||
) | ||
|
||
// Simulate fetching books from an external API | ||
func FetchBooksAsync() ([]dto.Book, error) { | ||
type BookService struct { | ||
bookRepository repo.BookRepository | ||
} | ||
|
||
r := make(chan []dto.Book) | ||
go func() { | ||
time.Sleep(2 * time.Second) // Simulate external API call | ||
books := []dto.Book{ | ||
{ID: 1, Title: "The Lord of the Rings", Author: "J. R. R. Tolkien"}, | ||
{ID: 2, Title: "Pride and Prejudice", Author: "Jane Austen"}, | ||
{ID: 3, Title: "To Kill a Mockingbird", Author: "Harper Lee"}, | ||
} | ||
r <- books // Send book list to channel | ||
func NewBookService(bookRepository repo.BookRepository) svc.BookService { | ||
return &BookService{bookRepository} | ||
} | ||
|
||
}() | ||
result := <-r | ||
func (b *BookService) FetchBooksAsync() ([]dto.Book, error) { | ||
|
||
books, err := b.bookRepository.FetchBooksAsync() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
result := make([]dto.Book, len(*books)) | ||
// map result | ||
for index, val := range *books { | ||
result[index] = dto.Book{ | ||
ID: val.ID, | ||
Title: val.Title, | ||
Author: val.Author, | ||
} | ||
} | ||
return result, nil | ||
|
||
} |
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,15 @@ | ||
package mocks | ||
|
||
import ( | ||
"github.com/VikashChauhan51/go-sample-api/internal/core/entities" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
type BookRepository struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *BookRepository) FetchBooksAsync() (*[]entities.Book, error) { | ||
args := m.Called() | ||
return args.Get(0).(*[]entities.Book), args.Error(1) | ||
} |
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
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,30 @@ | ||
package services | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/VikashChauhan51/go-sample-api/internal/core/entities" | ||
svc "github.com/VikashChauhan51/go-sample-api/internal/infra/services" | ||
"github.com/VikashChauhan51/go-sample-api/test/mocks" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFetchBooksAsync(t *testing.T) { | ||
bookRepository := new(mocks.BookRepository) | ||
bookService := svc.NewBookService(bookRepository) | ||
|
||
expectedBooks := []entities.Book{ | ||
{ID: 1, Title: "Title1", Author: "Author1"}, | ||
{ID: 2, Title: "Title2", Author: "Author2"}, | ||
} | ||
|
||
bookRepository.On("FetchBooksAsync").Return(&expectedBooks, nil) | ||
result, err := bookService.FetchBooksAsync() | ||
|
||
assert.NoError(t, err) | ||
assert.NotNil(t, result) | ||
assert.Equal(t, len(expectedBooks), len(result)) | ||
|
||
bookRepository.AssertExpectations(t) | ||
} |