Skip to content

Commit

Permalink
added repository test
Browse files Browse the repository at this point in the history
  • Loading branch information
VikashChauhan51 committed Jul 26, 2024
1 parent 7de37d5 commit d8db8e6
Show file tree
Hide file tree
Showing 48 changed files with 13,456 additions and 4 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/go-playground/validator v9.31.0+incompatible
github.com/joho/godotenv v1.5.1
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
github.com/swaggo/files v1.0.1
github.com/swaggo/gin-swagger v1.6.0
github.com/swaggo/swag v1.16.3
Expand All @@ -18,6 +19,7 @@ require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
Expand Down Expand Up @@ -46,12 +48,14 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down
7 changes: 7 additions & 0 deletions internal/core/interfaces/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package interfaces

import "gorm.io/gorm"

type Database interface {
Find(dest interface{}, conds ...interface{}) *gorm.DB
}
8 changes: 4 additions & 4 deletions internal/infra/repositories/book_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package repositories

import (
"github.com/VikashChauhan51/go-sample-api/internal/core/entities"
interfaces "github.com/VikashChauhan51/go-sample-api/internal/core/interfaces/repositories"
interfaces "github.com/VikashChauhan51/go-sample-api/internal/core/interfaces"
repo "github.com/VikashChauhan51/go-sample-api/internal/core/interfaces/repositories"
_ "gorm.io/driver/sqlserver"
"gorm.io/gorm"
)

type BookRepository struct {
db *gorm.DB
db interfaces.Database
}

func NewBookRepository(db *gorm.DB) interfaces.BookRepository {
func NewBookRepository(db interfaces.Database) repo.BookRepository {
return &BookRepository{db}
}

Expand Down
Empty file removed test/.keep
Empty file.
15 changes: 15 additions & 0 deletions test/mocks/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package mocks

import (
"github.com/stretchr/testify/mock"
"gorm.io/gorm"
)

type MockDB struct {
mock.Mock
}

func (m *MockDB) Find(out interface{}, where ...interface{}) *gorm.DB {
args := m.Called(out, where)
return args.Get(0).(*gorm.DB)
}
36 changes: 36 additions & 0 deletions test/repositories/book_repository_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package repositories

import (
"testing"

"github.com/VikashChauhan51/go-sample-api/internal/core/entities"
repo "github.com/VikashChauhan51/go-sample-api/internal/infra/repositories"
"github.com/VikashChauhan51/go-sample-api/test/mocks"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"gorm.io/gorm"
)

func TestFetchBooksAsync(t *testing.T) {
mockDB := new(mocks.MockDB)
repo := repo.NewBookRepository(mockDB)

expectedBooks := []entities.Book{
{ID: 1, Title: "Title1", Author: "Author1"},
{ID: 2, Title: "Title2", Author: "Author2"},
}

mockDB.On("Find", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
arg := args.Get(0).(*[]entities.Book)
*arg = expectedBooks
}).Return(&gorm.DB{Error: nil}).Once()

result, err := repo.FetchBooksAsync()

assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, len(expectedBooks), len(*result))

mockDB.AssertExpectations(t)
}
15 changes: 15 additions & 0 deletions vendor/github.com/davecgh/go-spew/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

145 changes: 145 additions & 0 deletions vendor/github.com/davecgh/go-spew/spew/bypass.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions vendor/github.com/davecgh/go-spew/spew/bypasssafe.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d8db8e6

Please sign in to comment.