Skip to content

Commit

Permalink
Тесты для сервис слоя
Browse files Browse the repository at this point in the history
  • Loading branch information
xeeetu committed Sep 14, 2024
1 parent 6b9d80c commit 4a8845b
Show file tree
Hide file tree
Showing 5 changed files with 491 additions and 0 deletions.
94 changes: 94 additions & 0 deletions internal/service/tests/create_test.go
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)
})
}
}
82 changes: 82 additions & 0 deletions internal/service/tests/delete_test.go
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)
})
}
}
107 changes: 107 additions & 0 deletions internal/service/tests/get_test.go
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)
})
}
}
113 changes: 113 additions & 0 deletions internal/service/tests/list_test.go
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)
})
}
}
Loading

0 comments on commit 4a8845b

Please sign in to comment.