Skip to content

Commit

Permalink
feat: controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SShlykov committed Mar 11, 2024
1 parent e102bf6 commit 8a30712
Show file tree
Hide file tree
Showing 47 changed files with 2,254 additions and 282 deletions.
2 changes: 1 addition & 1 deletion bookback/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/Masterminds/squirrel v1.5.4
github.com/fatih/color v1.16.0
github.com/georgysavva/scany/v2 v2.1.0
github.com/golang/mock v1.6.0
github.com/ilyakaznacheev/cleanenv v1.5.0
github.com/jackc/pgx/v5 v5.5.1
github.com/labstack/echo/v4 v4.11.4
Expand Down Expand Up @@ -54,6 +53,7 @@ require (
github.com/swaggo/swag v1.16.3 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
go.uber.org/mock v0.4.0 // indirect
golang.org/x/crypto v0.20.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sync v0.2.0 // indirect
Expand Down
9 changes: 7 additions & 2 deletions bookback/internal/application/usecase/minio_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@ import (
"io"
)

//go:generate mockgen -source=minio_usecase.go -destination=../../../tests/mocks/usecase/minio_usecase_mock.go
type MinioUseCase interface {
CreateMinioObject(ctx context.Context, request models.RequestMinioObject) (string, error)
GetMinioObject(ctx context.Context, request models.RequestMinioObject) (*models.MinioResp, error)
}

type MinioClient interface {
GetObject(ctx context.Context, bucketName, objectName string, opts minio.GetObjectOptions) (*minio.Object, error)
}

type minioUseCase struct {
minioClient *minio.Client
minioClient MinioClient
}

func NewMinioUseCase(minioClient *minio.Client) MinioUseCase {
func NewMinioUseCase(minioClient MinioClient) MinioUseCase {
return &minioUseCase{minioClient: minioClient}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// BookEventsRepo описывает репозиторий для работы с событиями книг.
//
//go:generate mockgen -destination=../../tests/mocks/domain/repository/pgrepo/book_events_repo_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/repository/pgrepo BookEventsRepo
//go:generate mockgen -destination=../../../../tests/mocks/domain/repository/pgrepo/book_events_repo_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/repository/pgrepo BookEventsRepo
type BookEventsRepo interface {
Repository[entity.BookEvent]
}
Expand Down
2 changes: 1 addition & 1 deletion bookback/internal/domain/repository/pgrepo/bookrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// BookRepo описывает репозиторий для работы с книгами.
//
//go:generate mockgen -destination=../../tests/mocks/domain/repository/pgrepo/book_repo_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/repository/pgrepo BookRepo
//go:generate mockgen -destination=../../../../tests/mocks/domain/repository/pgrepo/book_repo_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/repository/pgrepo BookRepo
type BookRepo interface {
Repository[entity.Book]
}
Expand Down
2 changes: 1 addition & 1 deletion bookback/internal/domain/repository/pgrepo/chapterrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// ChapterRepo описывает репозиторий для работы с главами.
//
//go:generate mockgen -destination=../../tests/mocks/domain/repository/pgrepo/chapter_repo_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/repository/pgrepo ChapterRepo
//go:generate mockgen -destination=../../../../tests/mocks/domain/repository/pgrepo/chapter_repo_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/repository/pgrepo ChapterRepo
type ChapterRepo interface {
Repository[entity.Chapter]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// MapVariablesRepo описывает репозиторий для работы с переменными карты.
//
//go:generate mockgen -destination=../../tests/mocks/domain/repository/pgrepo/map_variables_repo_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/repository/pgrepo MapVariablesRepo
//go:generate mockgen -destination=../../../../tests/mocks/domain/repository/pgrepo/map_variables_repo_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/repository/pgrepo MapVariablesRepo
type MapVariablesRepo interface {
Repository[entity.MapVariable]
}
Expand Down
2 changes: 1 addition & 1 deletion bookback/internal/domain/repository/pgrepo/pagerepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// PageRepo описывает репозиторий для работы с страницами.
//
//go:generate mockgen -destination=../../tests/mocks/domain/repository/pgrepo/page_repo_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/repository/pgrepo PageRepo
//go:generate mockgen -destination=../../../../tests/mocks/domain/repository/pgrepo/page_repo_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/repository/pgrepo PageRepo
type PageRepo interface {
Repository[entity.Page]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// ParagraphRepo описывает репозиторий для работы с параграфами.
//
//go:generate mockgen -destination=../../tests/mocks/domain/repository/pgrepo/paragraph_repo_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/repository/pgrepo ParagraphRepo
//go:generate mockgen -destination=../../../../tests/mocks/domain/repository/pgrepo/paragraph_repo_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/repository/pgrepo ParagraphRepo
type ParagraphRepo interface {
Repository[entity.Paragraph]
}
Expand Down
2 changes: 1 addition & 1 deletion bookback/internal/domain/services/book_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// BookService описывает сервис для работы с книгами.
//
//go:generate mockgen -destination=../../tests/mocks/domain/services/book_service_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/services BookService
//go:generate mockgen -destination=../../../tests/mocks/domain/services/book_service_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/services BookService
type BookService interface {
CreateBook(ctx context.Context, request models.CreateBookRequest) (*models.Book, error)
GetBookByID(ctx context.Context, id string) (*models.Book, error)
Expand Down
2 changes: 1 addition & 1 deletion bookback/internal/domain/services/bookevents_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/SShlykov/zeitment/bookback/internal/models/dbutils"
)

//go:generate mockgen -destination=../../tests/mocks/domain/services/bookevents_service_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/services BookEventsService
//go:generate mockgen -destination=../../../tests/mocks/domain/services/bookevents_service_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/services BookEventsService
type BookEventsService interface {
CreateBookEvent(ctx context.Context, request models.CreateBookEventRequest) (*models.BookEvent, error)
GetBookEventByID(ctx context.Context, id string) (*models.BookEvent, error)
Expand Down
2 changes: 1 addition & 1 deletion bookback/internal/domain/services/chapter_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/SShlykov/zeitment/bookback/internal/models/dbutils"
)

//go:generate mockgen -destination=../../tests/mocks/domain/services/chapter_service_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/services ChapterService
//go:generate mockgen -destination=../../../tests/mocks/domain/services/chapter_service_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/services ChapterService
type ChapterService interface {
CreateChapter(ctx context.Context, request models.CreateChapterRequest) (*models.Chapter, error)
GetChapterByID(ctx context.Context, id string) (*models.Chapter, error)
Expand Down
2 changes: 1 addition & 1 deletion bookback/internal/domain/services/mapvariables_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/SShlykov/zeitment/bookback/internal/models/dbutils"
)

//go:generate mockgen -destination=../../tests/mocks/domain/services/mapvariables_service_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/services MapVariablesService
//go:generate mockgen -destination=../../../tests/mocks/domain/services/mapvariables_service_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/services MapVariablesService
type MapVariablesService interface {
CreateMapVariable(ctx context.Context, request models.CreateMapVariableRequest) (*models.MapVariable, error)
GetMapVariableByID(ctx context.Context, id string) (*models.MapVariable, error)
Expand Down
2 changes: 1 addition & 1 deletion bookback/internal/domain/services/page_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/SShlykov/zeitment/bookback/internal/models/dbutils"
)

//go:generate mockgen -destination=../../tests/mocks/domain/services/mapvariables_service_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/services MapVariablesService
//go:generate mockgen -destination=../../../tests/mocks/domain/services/page_service_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/services PageService
type PageService interface {
CreatePage(ctx context.Context, request models.CreatePageRequest) (*models.Page, error)
GetPageByID(ctx context.Context, id string) (*models.Page, error)
Expand Down
2 changes: 1 addition & 1 deletion bookback/internal/domain/services/paragraph_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/SShlykov/zeitment/bookback/internal/models/dbutils"
)

//go:generate mockgen -destination=../../tests/mocks/domain/services/paragraph_service_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/services ParagraphService
//go:generate mockgen -destination=../../../tests/mocks/domain/services/paragraph_service_mock.go -package=mocks github.com/SShlykov/zeitment/bookback/internal/domain/services ParagraphService
type ParagraphService interface {
CreateParagraph(ctx context.Context, request models.CreateParagraphRequest) (*models.Paragraph, error)
GetParagraphByID(ctx context.Context, id string) (*models.Paragraph, error)
Expand Down
5 changes: 5 additions & 0 deletions bookback/internal/infrastructure/http/v1/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ const (
PagesPath = BasePath + "/pages"
ParagraphsPath = BasePath + "/paragraphs"
MinioPath = BasePath + "/storage"

IDVar = "/:id"
BookSubPath = "/book"
ChapterSubPath = "/chapter"
ListSubPath = "/list"
)
Original file line number Diff line number Diff line change
@@ -1,44 +1,35 @@
package controllers

import (
contextPkg "context"
v1 "github.com/SShlykov/zeitment/bookback/internal/infrastructure/http/v1"
"github.com/SShlykov/zeitment/bookback/internal/infrastructure/metrics/localmetrics"
"github.com/SShlykov/zeitment/bookback/internal/models"
loggerPkg "github.com/SShlykov/zeitment/bookback/pkg/logger"
mocks "github.com/SShlykov/zeitment/bookback/tests/mocks/domain/services"
gomock "github.com/golang/mock/gomock"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
"net/http"
"net/http/httptest"
"strings"
"testing"
)

func init() {
logger = loggerPkg.SetupLogger("debug")
metrics = localmetrics.NewLocalMetrics(logger)
context = contextPkg.Background()
requestPageOptions = `{"options": {"page": 1, "page_size": 10}}`
return
}

func TestBookController_ListBooks(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

fixture := NewTestFixture(v1.BooksPath)

service := mocks.NewMockBookService(ctrl)
listBooks := make([]*models.Book, 0)
service.EXPECT().ListBooks(gomock.Any(), gomock.Any()).Return(listBooks, nil)

e := echo.New()
req := httptest.NewRequest(http.MethodPost, v1.BooksPath+"/list", strings.NewReader(requestPageOptions))
req := httptest.NewRequest(http.MethodPost, v1.BooksPath+v1.ListSubPath, strings.NewReader(fixture.RequestPageOptions))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)

bc := NewBookController(service, metrics, logger, context)
bc := NewBookController(service, fixture.Metrics, fixture.Logger, fixture.Context)
err := bc.ListBooks(c)
if err != nil {
return
Expand All @@ -54,18 +45,20 @@ func TestBookController_GetBookByID(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

fixture := NewTestFixture(v1.BooksPath)

service := mocks.NewMockBookService(ctrl)
book := &models.Book{ID: id}
book := &models.Book{ID: fixture.ID}
service.EXPECT().GetBookByID(gomock.Any(), gomock.Any()).Return(book, nil)

e := echo.New()
req := httptest.NewRequest(http.MethodGet, idPath, nil)
req := httptest.NewRequest(http.MethodGet, fixture.IDPath, nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c.SetParamNames("id")
c.SetParamValues(id)
c.SetParamValues(fixture.ID)

bc := NewBookController(service, metrics, logger, context)
bc := NewBookController(service, fixture.Metrics, fixture.Logger, fixture.Context)
err := bc.GetBookByID(c)
if err != nil {
return
Expand All @@ -81,18 +74,19 @@ func TestBookController_GetBookByID(t *testing.T) {
func TestBookController_CreateBook(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
fixture := NewTestFixture(v1.BooksPath)

service := mocks.NewMockBookService(ctrl)
book := &models.Book{ID: "12b9b045-0845-462c-b372-0fca3180a6af"}
service.EXPECT().CreateBook(gomock.Any(), gomock.Any()).Return(book, nil)

e := echo.New()
req := httptest.NewRequest(http.MethodPost, v1.BooksPath, strings.NewReader(requestPageOptions))
req := httptest.NewRequest(http.MethodPost, v1.BooksPath, strings.NewReader(fixture.RequestPageOptions))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)

bc := NewBookController(service, metrics, logger, context)
bc := NewBookController(service, fixture.Metrics, fixture.Logger, fixture.Context)
err := bc.CreateBook(c)
if err != nil {
return
Expand All @@ -108,20 +102,21 @@ func TestBookController_CreateBook(t *testing.T) {
func TestBookController_UpdateBook(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
fixture := NewTestFixture(v1.BooksPath)

service := mocks.NewMockBookService(ctrl)
book := &models.Book{ID: id}
book := &models.Book{ID: fixture.ID}
service.EXPECT().UpdateBook(gomock.Any(), gomock.Any(), gomock.Any()).Return(book, nil)

e := echo.New()
req := httptest.NewRequest(http.MethodPut, idPath, strings.NewReader(requestPageOptions))
req := httptest.NewRequest(http.MethodPut, fixture.IDPath, strings.NewReader(fixture.RequestPageOptions))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c.SetParamNames("id")
c.SetParamValues(id)
c.SetParamValues(fixture.ID)

bc := NewBookController(service, metrics, logger, context)
bc := NewBookController(service, fixture.Metrics, fixture.Logger, fixture.Context)
err := bc.UpdateBook(c)
if err != nil {
return
Expand All @@ -137,19 +132,20 @@ func TestBookController_UpdateBook(t *testing.T) {
func TestBookController_DeleteBook(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
fixture := NewTestFixture(v1.BooksPath)

service := mocks.NewMockBookService(ctrl)
book := &models.Book{ID: id}
book := &models.Book{ID: fixture.ID}
service.EXPECT().DeleteBook(gomock.Any(), gomock.Any()).Return(book, nil)

e := echo.New()
req := httptest.NewRequest(http.MethodDelete, idPath, nil)
req := httptest.NewRequest(http.MethodDelete, fixture.IDPath, nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c.SetParamNames("id")
c.SetParamValues(id)
c.SetParamValues(fixture.ID)

bc := NewBookController(service, metrics, logger, context)
bc := NewBookController(service, fixture.Metrics, fixture.Logger, fixture.Context)
err := bc.DeleteBook(c)
if err != nil {
return
Expand Down
Loading

0 comments on commit 8a30712

Please sign in to comment.