From 2685baa3e7a1065acd2e47becdf8f7d1b4cec854 Mon Sep 17 00:00:00 2001 From: yyasha Date: Sun, 28 Jan 2024 11:37:18 +0300 Subject: [PATCH] add mocks for handlers --- Makefile | 4 ++ go.mod | 4 ++ go.sum | 1 + .../handlers/recipe/mocks/RecipeProvider.go | 57 ++++++++++++++++++ .../http-server/handlers/recipe/recipe.go | 1 + .../recipeImage/mocks/ImageProvider.go | 59 +++++++++++++++++++ .../handlers/recipeImage/recipeImage.go | 1 + .../handlers/recipes/mocks/RecipesProvider.go | 59 +++++++++++++++++++ .../http-server/handlers/recipes/recipes.go | 1 + .../mocks/RecipesProvider.go | 59 +++++++++++++++++++ .../recipesByCategory/recipesByCategory.go | 1 + 11 files changed, 247 insertions(+) create mode 100644 internal/http-server/handlers/recipe/mocks/RecipeProvider.go create mode 100644 internal/http-server/handlers/recipeImage/mocks/ImageProvider.go create mode 100644 internal/http-server/handlers/recipes/mocks/RecipesProvider.go create mode 100644 internal/http-server/handlers/recipesByCategory/mocks/RecipesProvider.go diff --git a/Makefile b/Makefile index 5055355..ea4b81a 100644 --- a/Makefile +++ b/Makefile @@ -10,3 +10,7 @@ run_parser: go run ./cmd/parser/main.go -config ./config/local.yaml lint: golangci-lint run +generate: + go generate ./... +test: + go test ./internal/... diff --git a/go.mod b/go.mod index d0d7da4..edb16b1 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/minio/minio-go/v7 v7.0.66 github.com/redis/go-redis/v9 v9.4.0 github.com/s32x/httpclient v0.0.0-20220217184346-6df4d4d51c14 + github.com/stretchr/testify v1.8.4 ) require ( @@ -25,6 +26,7 @@ require ( github.com/cenkalti/backoff/v4 v4.1.2 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/charmbracelet/lipgloss v0.9.1 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect @@ -52,10 +54,12 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/muesli/reflow v0.3.0 // indirect github.com/muesli/termenv v0.15.2 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rivo/uniseg v0.2.0 // indirect github.com/rogpeppe/go-internal v1.12.0 // indirect github.com/rs/xid v1.5.0 // indirect github.com/sirupsen/logrus v1.9.3 // indirect + github.com/stretchr/objx v0.5.0 // indirect go.uber.org/atomic v1.7.0 // indirect golang.org/x/crypto v0.17.0 // indirect golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect diff --git a/go.sum b/go.sum index a59db7d..64fa991 100644 --- a/go.sum +++ b/go.sum @@ -150,6 +150,7 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= 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 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= diff --git a/internal/http-server/handlers/recipe/mocks/RecipeProvider.go b/internal/http-server/handlers/recipe/mocks/RecipeProvider.go new file mode 100644 index 0000000..b7a01ce --- /dev/null +++ b/internal/http-server/handlers/recipe/mocks/RecipeProvider.go @@ -0,0 +1,57 @@ +// Code generated by mockery v2.40.1. DO NOT EDIT. + +package mocks + +import ( + context "context" + models "recipes/internal/domain/models" + + mock "github.com/stretchr/testify/mock" +) + +// RecipeProvider is an autogenerated mock type for the RecipeProvider type +type RecipeProvider struct { + mock.Mock +} + +// GetRecipe provides a mock function with given fields: ctx, r_id +func (_m *RecipeProvider) GetRecipe(ctx context.Context, r_id uint) (models.Recipe, error) { + ret := _m.Called(ctx, r_id) + + if len(ret) == 0 { + panic("no return value specified for GetRecipe") + } + + var r0 models.Recipe + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, uint) (models.Recipe, error)); ok { + return rf(ctx, r_id) + } + if rf, ok := ret.Get(0).(func(context.Context, uint) models.Recipe); ok { + r0 = rf(ctx, r_id) + } else { + r0 = ret.Get(0).(models.Recipe) + } + + if rf, ok := ret.Get(1).(func(context.Context, uint) error); ok { + r1 = rf(ctx, r_id) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// NewRecipeProvider creates a new instance of RecipeProvider. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewRecipeProvider(t interface { + mock.TestingT + Cleanup(func()) +}) *RecipeProvider { + mock := &RecipeProvider{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/internal/http-server/handlers/recipe/recipe.go b/internal/http-server/handlers/recipe/recipe.go index 4c3c16b..fdd4eb8 100644 --- a/internal/http-server/handlers/recipe/recipe.go +++ b/internal/http-server/handlers/recipe/recipe.go @@ -24,6 +24,7 @@ type Response struct { Recipe models.Recipe `json:"recipe"` } +//go:generate go run github.com/vektra/mockery/v2@v2.40.1 --name=RecipeProvider type RecipeProvider interface { GetRecipe(ctx context.Context, r_id uint) (models.Recipe, error) } diff --git a/internal/http-server/handlers/recipeImage/mocks/ImageProvider.go b/internal/http-server/handlers/recipeImage/mocks/ImageProvider.go new file mode 100644 index 0000000..1b57f75 --- /dev/null +++ b/internal/http-server/handlers/recipeImage/mocks/ImageProvider.go @@ -0,0 +1,59 @@ +// Code generated by mockery v2.40.1. DO NOT EDIT. + +package mocks + +import ( + context "context" + io "io" + + mock "github.com/stretchr/testify/mock" +) + +// ImageProvider is an autogenerated mock type for the ImageProvider type +type ImageProvider struct { + mock.Mock +} + +// RecipeImage provides a mock function with given fields: ctx, filename +func (_m *ImageProvider) RecipeImage(ctx context.Context, filename string) (io.ReadCloser, error) { + ret := _m.Called(ctx, filename) + + if len(ret) == 0 { + panic("no return value specified for RecipeImage") + } + + var r0 io.ReadCloser + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string) (io.ReadCloser, error)); ok { + return rf(ctx, filename) + } + if rf, ok := ret.Get(0).(func(context.Context, string) io.ReadCloser); ok { + r0 = rf(ctx, filename) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(io.ReadCloser) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, string) error); ok { + r1 = rf(ctx, filename) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// NewImageProvider creates a new instance of ImageProvider. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewImageProvider(t interface { + mock.TestingT + Cleanup(func()) +}) *ImageProvider { + mock := &ImageProvider{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/internal/http-server/handlers/recipeImage/recipeImage.go b/internal/http-server/handlers/recipeImage/recipeImage.go index 0bec4bf..90b7f02 100644 --- a/internal/http-server/handlers/recipeImage/recipeImage.go +++ b/internal/http-server/handlers/recipeImage/recipeImage.go @@ -13,6 +13,7 @@ import ( "github.com/go-chi/render" ) +//go:generate go run github.com/vektra/mockery/v2@v2.40.1 --name=ImageProvider type ImageProvider interface { RecipeImage(ctx context.Context, filename string) (io.ReadCloser, error) } diff --git a/internal/http-server/handlers/recipes/mocks/RecipesProvider.go b/internal/http-server/handlers/recipes/mocks/RecipesProvider.go new file mode 100644 index 0000000..da9e9c8 --- /dev/null +++ b/internal/http-server/handlers/recipes/mocks/RecipesProvider.go @@ -0,0 +1,59 @@ +// Code generated by mockery v2.40.1. DO NOT EDIT. + +package mocks + +import ( + context "context" + models "recipes/internal/domain/models" + + mock "github.com/stretchr/testify/mock" +) + +// RecipesProvider is an autogenerated mock type for the RecipesProvider type +type RecipesProvider struct { + mock.Mock +} + +// GetRecipes provides a mock function with given fields: ctx, offset, limit +func (_m *RecipesProvider) GetRecipes(ctx context.Context, offset int, limit int) ([]models.Recipe, error) { + ret := _m.Called(ctx, offset, limit) + + if len(ret) == 0 { + panic("no return value specified for GetRecipes") + } + + var r0 []models.Recipe + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, int, int) ([]models.Recipe, error)); ok { + return rf(ctx, offset, limit) + } + if rf, ok := ret.Get(0).(func(context.Context, int, int) []models.Recipe); ok { + r0 = rf(ctx, offset, limit) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]models.Recipe) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, int, int) error); ok { + r1 = rf(ctx, offset, limit) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// NewRecipesProvider creates a new instance of RecipesProvider. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewRecipesProvider(t interface { + mock.TestingT + Cleanup(func()) +}) *RecipesProvider { + mock := &RecipesProvider{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/internal/http-server/handlers/recipes/recipes.go b/internal/http-server/handlers/recipes/recipes.go index cf2bf35..7669067 100644 --- a/internal/http-server/handlers/recipes/recipes.go +++ b/internal/http-server/handlers/recipes/recipes.go @@ -22,6 +22,7 @@ type Response struct { Recipes []models.Recipe `json:"recipes"` } +//go:generate go run github.com/vektra/mockery/v2@v2.40.1 --name=RecipesProvider type RecipesProvider interface { GetRecipes(ctx context.Context, offset, limit int) ([]models.Recipe, error) } diff --git a/internal/http-server/handlers/recipesByCategory/mocks/RecipesProvider.go b/internal/http-server/handlers/recipesByCategory/mocks/RecipesProvider.go new file mode 100644 index 0000000..eeee26d --- /dev/null +++ b/internal/http-server/handlers/recipesByCategory/mocks/RecipesProvider.go @@ -0,0 +1,59 @@ +// Code generated by mockery v2.40.1. DO NOT EDIT. + +package mocks + +import ( + context "context" + models "recipes/internal/domain/models" + + mock "github.com/stretchr/testify/mock" +) + +// RecipesProvider is an autogenerated mock type for the RecipesProvider type +type RecipesProvider struct { + mock.Mock +} + +// GetRecipesByCategory provides a mock function with given fields: ctx, offset, limit, category +func (_m *RecipesProvider) GetRecipesByCategory(ctx context.Context, offset int, limit int, category string) ([]models.Recipe, error) { + ret := _m.Called(ctx, offset, limit, category) + + if len(ret) == 0 { + panic("no return value specified for GetRecipesByCategory") + } + + var r0 []models.Recipe + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, int, int, string) ([]models.Recipe, error)); ok { + return rf(ctx, offset, limit, category) + } + if rf, ok := ret.Get(0).(func(context.Context, int, int, string) []models.Recipe); ok { + r0 = rf(ctx, offset, limit, category) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]models.Recipe) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, int, int, string) error); ok { + r1 = rf(ctx, offset, limit, category) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// NewRecipesProvider creates a new instance of RecipesProvider. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewRecipesProvider(t interface { + mock.TestingT + Cleanup(func()) +}) *RecipesProvider { + mock := &RecipesProvider{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/internal/http-server/handlers/recipesByCategory/recipesByCategory.go b/internal/http-server/handlers/recipesByCategory/recipesByCategory.go index 8c91040..6401ebe 100644 --- a/internal/http-server/handlers/recipesByCategory/recipesByCategory.go +++ b/internal/http-server/handlers/recipesByCategory/recipesByCategory.go @@ -23,6 +23,7 @@ type Response struct { Recipes []models.Recipe `json:"recipes"` } +//go:generate go run github.com/vektra/mockery/v2@v2.40.1 --name=RecipesProvider type RecipesProvider interface { GetRecipesByCategory(ctx context.Context, offset, limit int, category string) ([]models.Recipe, error) }