-
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
Showing
5 changed files
with
491 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,94 @@ | ||
package tests | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/brianvoe/gofakeit/v7" | ||
"github.com/gojuno/minimock/v3" | ||
"github.com/stretchr/testify/require" | ||
"github.com/xeeetu/gRPC/internal/repository" | ||
repositoryMocks "github.com/xeeetu/gRPC/internal/repository/mocks" | ||
noteService "github.com/xeeetu/gRPC/internal/service/note" | ||
"github.com/xeeetu/gRPC/model" | ||
) | ||
|
||
func TestCreate(t *testing.T) { | ||
//t.Parallel() | ||
type noteRepositoryMockFunc func(mc *minimock.Controller) repository.NoteRepository | ||
|
||
type args struct { | ||
ctx context.Context | ||
modelInfo *model.NoteInfo | ||
} | ||
var ( | ||
ctx = context.Background() | ||
mc = minimock.NewController(t) | ||
|
||
id = gofakeit.Int64() | ||
title = gofakeit.Name() | ||
content = gofakeit.Name() | ||
|
||
repoErr = fmt.Errorf("repository error") | ||
repoErrInServ = fmt.Errorf("s.noteRepository.Create: %w", repoErr) | ||
|
||
modelInfo = &model.NoteInfo{ | ||
Title: title, | ||
Content: content, | ||
} | ||
) | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
want int64 | ||
err error | ||
noteRepositoryMockFunc noteRepositoryMockFunc | ||
}{ | ||
{ | ||
name: "success case", | ||
args: args{ | ||
ctx: ctx, | ||
modelInfo: modelInfo, | ||
}, | ||
want: id, | ||
err: nil, | ||
noteRepositoryMockFunc: func(mc *minimock.Controller) repository.NoteRepository { | ||
mock := repositoryMocks.NewNoteRepositoryMock(mc) | ||
|
||
mock.CreateMock.Expect(ctx, modelInfo).Return(id, nil) | ||
return mock | ||
}, | ||
}, | ||
{ | ||
name: "service error case", | ||
args: args{ | ||
ctx: ctx, | ||
modelInfo: modelInfo, | ||
}, | ||
want: 0, | ||
err: repoErrInServ, | ||
noteRepositoryMockFunc: func(mc *minimock.Controller) repository.NoteRepository { | ||
mock := repositoryMocks.NewNoteRepositoryMock(mc) | ||
|
||
mock.CreateMock.Expect(ctx, modelInfo).Return(0, repoErr) | ||
return mock | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
//t.Parallel() | ||
|
||
mock := tt.noteRepositoryMockFunc(mc) | ||
serv := noteService.NewService(mock) | ||
|
||
resHandler, err := serv.Create(tt.args.ctx, tt.args.modelInfo) | ||
require.Equal(t, tt.want, resHandler) | ||
require.Equal(t, tt.err, 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,82 @@ | ||
package tests | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/brianvoe/gofakeit/v7" | ||
"github.com/gojuno/minimock/v3" | ||
"github.com/stretchr/testify/require" | ||
"github.com/xeeetu/gRPC/internal/repository" | ||
repositoryMocks "github.com/xeeetu/gRPC/internal/repository/mocks" | ||
noteService "github.com/xeeetu/gRPC/internal/service/note" | ||
) | ||
|
||
func TestDelete(t *testing.T) { | ||
//t.Parallel() | ||
type noteRepositoryMockFunc func(mc *minimock.Controller) repository.NoteRepository | ||
|
||
type args struct { | ||
ctx context.Context | ||
id int64 | ||
} | ||
var ( | ||
ctx = context.Background() | ||
mc = minimock.NewController(t) | ||
|
||
id = gofakeit.Int64() | ||
|
||
repoErr = fmt.Errorf("repository error") | ||
repoErrInServ = fmt.Errorf("s.noteRepository.Delete: %w", repoErr) | ||
) | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
err error | ||
noteRepositoryMockFunc noteRepositoryMockFunc | ||
}{ | ||
{ | ||
name: "success case", | ||
args: args{ | ||
ctx: ctx, | ||
id: id, | ||
}, | ||
err: nil, | ||
noteRepositoryMockFunc: func(mc *minimock.Controller) repository.NoteRepository { | ||
mock := repositoryMocks.NewNoteRepositoryMock(mc) | ||
|
||
mock.DeleteMock.Expect(ctx, id).Return(nil) | ||
return mock | ||
}, | ||
}, | ||
{ | ||
name: "service error case", | ||
args: args{ | ||
ctx: ctx, | ||
id: id, | ||
}, | ||
err: repoErrInServ, | ||
noteRepositoryMockFunc: func(mc *minimock.Controller) repository.NoteRepository { | ||
mock := repositoryMocks.NewNoteRepositoryMock(mc) | ||
|
||
mock.DeleteMock.Expect(ctx, id).Return(repoErr) | ||
return mock | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
//t.Parallel() | ||
|
||
mock := tt.noteRepositoryMockFunc(mc) | ||
serv := noteService.NewService(mock) | ||
|
||
err := serv.Delete(tt.args.ctx, tt.args.id) | ||
require.Equal(t, tt.err, 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,107 @@ | ||
package tests | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/brianvoe/gofakeit/v7" | ||
"github.com/gojuno/minimock/v3" | ||
"github.com/stretchr/testify/require" | ||
"github.com/xeeetu/gRPC/internal/repository" | ||
repositoryMocks "github.com/xeeetu/gRPC/internal/repository/mocks" | ||
noteService "github.com/xeeetu/gRPC/internal/service/note" | ||
"github.com/xeeetu/gRPC/model" | ||
) | ||
|
||
func TestGet(t *testing.T) { | ||
//t.Parallel() | ||
type noteRepositoryMockFunc func(mc *minimock.Controller) repository.NoteRepository | ||
|
||
type args struct { | ||
ctx context.Context | ||
id int64 | ||
} | ||
var ( | ||
ctx = context.Background() | ||
mc = minimock.NewController(t) | ||
|
||
id = gofakeit.Int64() | ||
title = gofakeit.Name() | ||
content = gofakeit.Name() | ||
createdAt = gofakeit.Date() | ||
updatedAt = gofakeit.Date() | ||
|
||
repoErr = fmt.Errorf("repository error") | ||
repoErrInServ = fmt.Errorf("s.noteRepository.Get: %w", repoErr) | ||
|
||
modelInfo = model.NoteInfo{ | ||
Title: title, | ||
Content: content, | ||
} | ||
|
||
modelNote = &model.Note{ | ||
ID: id, | ||
Info: modelInfo, | ||
CreatedAt: createdAt, | ||
UpdatedAt: sql.NullTime{ | ||
Time: updatedAt, | ||
Valid: true, | ||
}, | ||
} | ||
) | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
want *model.Note | ||
err error | ||
noteRepositoryMockFunc noteRepositoryMockFunc | ||
}{ | ||
{ | ||
name: "success case", | ||
args: args{ | ||
ctx: ctx, | ||
id: id, | ||
}, | ||
want: modelNote, | ||
err: nil, | ||
noteRepositoryMockFunc: func(mc *minimock.Controller) repository.NoteRepository { | ||
mock := repositoryMocks.NewNoteRepositoryMock(mc) | ||
|
||
mock.GetMock.Expect(ctx, id).Return(modelNote, nil) | ||
return mock | ||
}, | ||
}, | ||
{ | ||
name: "service error case", | ||
args: args{ | ||
ctx: ctx, | ||
id: id, | ||
}, | ||
want: nil, | ||
err: repoErrInServ, | ||
noteRepositoryMockFunc: func(mc *minimock.Controller) repository.NoteRepository { | ||
mock := repositoryMocks.NewNoteRepositoryMock(mc) | ||
|
||
mock.GetMock.Expect(ctx, id).Return(nil, repoErr) | ||
return mock | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
//t.Parallel() | ||
|
||
mock := tt.noteRepositoryMockFunc(mc) | ||
serv := noteService.NewService(mock) | ||
|
||
resHandler, err := serv.Get(tt.args.ctx, tt.args.id) | ||
require.Equal(t, tt.want, resHandler) | ||
require.Equal(t, tt.err, 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,113 @@ | ||
package tests | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/brianvoe/gofakeit/v7" | ||
"github.com/gojuno/minimock/v3" | ||
"github.com/stretchr/testify/require" | ||
"github.com/xeeetu/gRPC/internal/repository" | ||
repositoryMocks "github.com/xeeetu/gRPC/internal/repository/mocks" | ||
noteService "github.com/xeeetu/gRPC/internal/service/note" | ||
"github.com/xeeetu/gRPC/model" | ||
) | ||
|
||
func TestList(t *testing.T) { | ||
//t.Parallel() | ||
type noteRepositoryMockFunc func(mc *minimock.Controller) repository.NoteRepository | ||
|
||
type args struct { | ||
ctx context.Context | ||
offset, limit int64 | ||
} | ||
var ( | ||
ctx = context.Background() | ||
mc = minimock.NewController(t) | ||
|
||
id = gofakeit.Int64() | ||
offset = int64(gofakeit.Number(0, 10)) | ||
limit = int64(gofakeit.Number(1, 10)) | ||
title = gofakeit.Name() | ||
content = gofakeit.Name() | ||
createdAt = gofakeit.Date() | ||
updatedAt = gofakeit.Date() | ||
|
||
repoErr = fmt.Errorf("repository error") | ||
repoErrInServ = fmt.Errorf("s.noteRepository.List: %w", repoErr) | ||
|
||
modelInfo = model.NoteInfo{ | ||
Title: title, | ||
Content: content, | ||
} | ||
|
||
modelNote = &model.Note{ | ||
ID: id, | ||
Info: modelInfo, | ||
CreatedAt: createdAt, | ||
UpdatedAt: sql.NullTime{ | ||
Time: updatedAt, | ||
Valid: true, | ||
}, | ||
} | ||
|
||
modelNotes = []*model.Note{modelNote} | ||
) | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
want []*model.Note | ||
err error | ||
noteRepositoryMockFunc noteRepositoryMockFunc | ||
}{ | ||
{ | ||
name: "success case", | ||
args: args{ | ||
ctx: ctx, | ||
offset: offset, | ||
limit: limit, | ||
}, | ||
want: modelNotes, | ||
err: nil, | ||
noteRepositoryMockFunc: func(mc *minimock.Controller) repository.NoteRepository { | ||
mock := repositoryMocks.NewNoteRepositoryMock(mc) | ||
|
||
mock.ListMock.Expect(ctx, offset, limit).Return(modelNotes, nil) | ||
return mock | ||
}, | ||
}, | ||
{ | ||
name: "service error case", | ||
args: args{ | ||
ctx: ctx, | ||
offset: offset, | ||
limit: limit, | ||
}, | ||
want: nil, | ||
err: repoErrInServ, | ||
noteRepositoryMockFunc: func(mc *minimock.Controller) repository.NoteRepository { | ||
mock := repositoryMocks.NewNoteRepositoryMock(mc) | ||
|
||
mock.ListMock.Expect(ctx, offset, limit).Return(nil, repoErr) | ||
return mock | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
//t.Parallel() | ||
|
||
mock := tt.noteRepositoryMockFunc(mc) | ||
serv := noteService.NewService(mock) | ||
|
||
resHandler, err := serv.List(tt.args.ctx, tt.args.offset, tt.args.limit) | ||
require.Equal(t, tt.want, resHandler) | ||
require.Equal(t, tt.err, err) | ||
}) | ||
} | ||
} |
Oops, something went wrong.