|
@@ -9,21 +9,21 @@ import (
|
|
|
"testing"
|
|
|
|
|
|
"github.com/brianvoe/gofakeit/v6"
|
|
|
+ "github.com/dmitriygnatenko/internal/helpers"
|
|
|
"github.com/dmitriygnatenko/internal/interfaces"
|
|
|
"github.com/dmitriygnatenko/internal/models"
|
|
|
repositoryMocks "github.com/dmitriygnatenko/internal/repositories/mocks"
|
|
|
sp "github.com/dmitriygnatenko/internal/service_provider"
|
|
|
cacheMocks "github.com/dmitriygnatenko/internal/services/cache/mocks"
|
|
|
- "github.com/dmitriygnatenko/internal/services/handler/test"
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
"github.com/gojuno/minimock/v3"
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
)
|
|
|
|
|
|
func Test_ArticleHandler(t *testing.T) {
|
|
|
- type cacheMockFunc func(mc *minimock.Controller) interfaces.ICache
|
|
|
- type tagMockFunc func(mc *minimock.Controller) interfaces.ITagRepository
|
|
|
- type articleMockFunc func(mc *minimock.Controller) interfaces.IArticleRepository
|
|
|
+ type cacheMockFunc func(mc *minimock.Controller) interfaces.Cache
|
|
|
+ type tagMockFunc func(mc *minimock.Controller) interfaces.TagRepository
|
|
|
+ type articleMockFunc func(mc *minimock.Controller) interfaces.ArticleRepository
|
|
|
|
|
|
type req struct {
|
|
|
method string
|
|
@@ -33,8 +33,7 @@ func Test_ArticleHandler(t *testing.T) {
|
|
|
var (
|
|
|
mc = minimock.NewController(t)
|
|
|
articleID = gofakeit.Number(1, 100)
|
|
|
- date = gofakeit.Date()
|
|
|
- publishTime = date.Format("2006-01-02 15:04:05")
|
|
|
+ publishTime = gofakeit.Date()
|
|
|
internalErr = errors.New(gofakeit.Phrase())
|
|
|
|
|
|
article = models.Article{
|
|
@@ -124,21 +123,21 @@ func Test_ArticleHandler(t *testing.T) {
|
|
|
},
|
|
|
res: fiber.StatusOK,
|
|
|
err: nil,
|
|
|
- cacheMock: func(mc *minimock.Controller) interfaces.ICache {
|
|
|
- mock := cacheMocks.NewICacheMock(mc)
|
|
|
+ cacheMock: func(mc *minimock.Controller) interfaces.Cache {
|
|
|
+ mock := cacheMocks.NewCacheMock(mc)
|
|
|
mock.GetMock.Return(nil, false)
|
|
|
mock.SetMock.Return()
|
|
|
|
|
|
return mock
|
|
|
},
|
|
|
- tagMock: func(mc *minimock.Controller) interfaces.ITagRepository {
|
|
|
- mock := repositoryMocks.NewITagRepositoryMock(mc)
|
|
|
+ tagMock: func(mc *minimock.Controller) interfaces.TagRepository {
|
|
|
+ mock := repositoryMocks.NewTagRepositoryMock(mc)
|
|
|
mock.GetAllUsedMock.Return(tags, nil)
|
|
|
|
|
|
return mock
|
|
|
},
|
|
|
- articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
|
|
|
- mock := repositoryMocks.NewIArticleRepositoryMock(mc)
|
|
|
+ articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
|
|
|
+ mock := repositoryMocks.NewArticleRepositoryMock(mc)
|
|
|
|
|
|
mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
|
|
|
assert.Equal(mc, strconv.Itoa(articleID), url)
|
|
@@ -157,19 +156,19 @@ func Test_ArticleHandler(t *testing.T) {
|
|
|
},
|
|
|
res: fiber.StatusNotFound,
|
|
|
err: nil,
|
|
|
- cacheMock: func(mc *minimock.Controller) interfaces.ICache {
|
|
|
- mock := cacheMocks.NewICacheMock(mc)
|
|
|
+ cacheMock: func(mc *minimock.Controller) interfaces.Cache {
|
|
|
+ mock := cacheMocks.NewCacheMock(mc)
|
|
|
mock.GetMock.Return(nil, false)
|
|
|
|
|
|
return mock
|
|
|
},
|
|
|
- tagMock: func(mc *minimock.Controller) interfaces.ITagRepository {
|
|
|
- mock := repositoryMocks.NewITagRepositoryMock(mc)
|
|
|
+ tagMock: func(mc *minimock.Controller) interfaces.TagRepository {
|
|
|
+ mock := repositoryMocks.NewTagRepositoryMock(mc)
|
|
|
|
|
|
return mock
|
|
|
},
|
|
|
- articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
|
|
|
- mock := repositoryMocks.NewIArticleRepositoryMock(mc)
|
|
|
+ articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
|
|
|
+ mock := repositoryMocks.NewArticleRepositoryMock(mc)
|
|
|
mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
|
|
|
assert.Equal(mc, strconv.Itoa(articleID), url)
|
|
|
}).Return(nil, sql.ErrNoRows)
|
|
@@ -185,19 +184,19 @@ func Test_ArticleHandler(t *testing.T) {
|
|
|
},
|
|
|
res: fiber.StatusInternalServerError,
|
|
|
err: nil,
|
|
|
- cacheMock: func(mc *minimock.Controller) interfaces.ICache {
|
|
|
- mock := cacheMocks.NewICacheMock(mc)
|
|
|
+ cacheMock: func(mc *minimock.Controller) interfaces.Cache {
|
|
|
+ mock := cacheMocks.NewCacheMock(mc)
|
|
|
mock.GetMock.Return(nil, false)
|
|
|
|
|
|
return mock
|
|
|
},
|
|
|
- tagMock: func(mc *minimock.Controller) interfaces.ITagRepository {
|
|
|
- mock := repositoryMocks.NewITagRepositoryMock(mc)
|
|
|
+ tagMock: func(mc *minimock.Controller) interfaces.TagRepository {
|
|
|
+ mock := repositoryMocks.NewTagRepositoryMock(mc)
|
|
|
|
|
|
return mock
|
|
|
},
|
|
|
- articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
|
|
|
- mock := repositoryMocks.NewIArticleRepositoryMock(mc)
|
|
|
+ articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
|
|
|
+ mock := repositoryMocks.NewArticleRepositoryMock(mc)
|
|
|
mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
|
|
|
assert.Equal(mc, strconv.Itoa(articleID), url)
|
|
|
}).Return(nil, internalErr)
|
|
@@ -213,19 +212,19 @@ func Test_ArticleHandler(t *testing.T) {
|
|
|
},
|
|
|
res: fiber.StatusNotFound,
|
|
|
err: nil,
|
|
|
- cacheMock: func(mc *minimock.Controller) interfaces.ICache {
|
|
|
- mock := cacheMocks.NewICacheMock(mc)
|
|
|
+ cacheMock: func(mc *minimock.Controller) interfaces.Cache {
|
|
|
+ mock := cacheMocks.NewCacheMock(mc)
|
|
|
mock.GetMock.Return(nil, false)
|
|
|
|
|
|
return mock
|
|
|
},
|
|
|
- tagMock: func(mc *minimock.Controller) interfaces.ITagRepository {
|
|
|
- mock := repositoryMocks.NewITagRepositoryMock(mc)
|
|
|
+ tagMock: func(mc *minimock.Controller) interfaces.TagRepository {
|
|
|
+ mock := repositoryMocks.NewTagRepositoryMock(mc)
|
|
|
|
|
|
return mock
|
|
|
},
|
|
|
- articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
|
|
|
- mock := repositoryMocks.NewIArticleRepositoryMock(mc)
|
|
|
+ articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
|
|
|
+ mock := repositoryMocks.NewArticleRepositoryMock(mc)
|
|
|
mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
|
|
|
assert.Equal(mc, strconv.Itoa(articleID), url)
|
|
|
}).Return(¬ActiveArticle, nil)
|
|
@@ -233,34 +232,6 @@ func Test_ArticleHandler(t *testing.T) {
|
|
|
return mock
|
|
|
},
|
|
|
},
|
|
|
- {
|
|
|
- name: "negative case - article mapper error",
|
|
|
- req: req{
|
|
|
- method: fiber.MethodGet,
|
|
|
- route: "/article/" + strconv.Itoa(articleID),
|
|
|
- },
|
|
|
- res: fiber.StatusInternalServerError,
|
|
|
- err: nil,
|
|
|
- cacheMock: func(mc *minimock.Controller) interfaces.ICache {
|
|
|
- mock := cacheMocks.NewICacheMock(mc)
|
|
|
- mock.GetMock.Return(nil, false)
|
|
|
-
|
|
|
- return mock
|
|
|
- },
|
|
|
- tagMock: func(mc *minimock.Controller) interfaces.ITagRepository {
|
|
|
- mock := repositoryMocks.NewITagRepositoryMock(mc)
|
|
|
-
|
|
|
- return mock
|
|
|
- },
|
|
|
- articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
|
|
|
- mock := repositoryMocks.NewIArticleRepositoryMock(mc)
|
|
|
- mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
|
|
|
- assert.Equal(mc, strconv.Itoa(articleID), url)
|
|
|
- }).Return(&models.Article{IsActive: true}, nil)
|
|
|
-
|
|
|
- return mock
|
|
|
- },
|
|
|
- },
|
|
|
{
|
|
|
name: "negative case - tags repository error",
|
|
|
req: req{
|
|
@@ -269,20 +240,20 @@ func Test_ArticleHandler(t *testing.T) {
|
|
|
},
|
|
|
res: fiber.StatusInternalServerError,
|
|
|
err: nil,
|
|
|
- cacheMock: func(mc *minimock.Controller) interfaces.ICache {
|
|
|
- mock := cacheMocks.NewICacheMock(mc)
|
|
|
+ cacheMock: func(mc *minimock.Controller) interfaces.Cache {
|
|
|
+ mock := cacheMocks.NewCacheMock(mc)
|
|
|
mock.GetMock.Return(nil, false)
|
|
|
|
|
|
return mock
|
|
|
},
|
|
|
- tagMock: func(mc *minimock.Controller) interfaces.ITagRepository {
|
|
|
- mock := repositoryMocks.NewITagRepositoryMock(mc)
|
|
|
+ tagMock: func(mc *minimock.Controller) interfaces.TagRepository {
|
|
|
+ mock := repositoryMocks.NewTagRepositoryMock(mc)
|
|
|
mock.GetAllUsedMock.Return(nil, internalErr)
|
|
|
|
|
|
return mock
|
|
|
},
|
|
|
- articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
|
|
|
- mock := repositoryMocks.NewIArticleRepositoryMock(mc)
|
|
|
+ articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
|
|
|
+ mock := repositoryMocks.NewArticleRepositoryMock(mc)
|
|
|
|
|
|
mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
|
|
|
assert.Equal(mc, strconv.Itoa(articleID), url)
|
|
@@ -299,20 +270,20 @@ func Test_ArticleHandler(t *testing.T) {
|
|
|
},
|
|
|
res: fiber.StatusInternalServerError,
|
|
|
err: nil,
|
|
|
- cacheMock: func(mc *minimock.Controller) interfaces.ICache {
|
|
|
- mock := cacheMocks.NewICacheMock(mc)
|
|
|
+ cacheMock: func(mc *minimock.Controller) interfaces.Cache {
|
|
|
+ mock := cacheMocks.NewCacheMock(mc)
|
|
|
mock.GetMock.Return(nil, false)
|
|
|
|
|
|
return mock
|
|
|
},
|
|
|
- tagMock: func(mc *minimock.Controller) interfaces.ITagRepository {
|
|
|
- mock := repositoryMocks.NewITagRepositoryMock(mc)
|
|
|
+ tagMock: func(mc *minimock.Controller) interfaces.TagRepository {
|
|
|
+ mock := repositoryMocks.NewTagRepositoryMock(mc)
|
|
|
mock.GetAllUsedMock.Return(tags, nil)
|
|
|
|
|
|
return mock
|
|
|
},
|
|
|
- articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
|
|
|
- mock := repositoryMocks.NewIArticleRepositoryMock(mc)
|
|
|
+ articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
|
|
|
+ mock := repositoryMocks.NewArticleRepositoryMock(mc)
|
|
|
|
|
|
mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
|
|
|
assert.Equal(mc, strconv.Itoa(articleID), url)
|
|
@@ -320,38 +291,6 @@ func Test_ArticleHandler(t *testing.T) {
|
|
|
|
|
|
mock.GetAllPreviewMock.Return(nil, internalErr)
|
|
|
|
|
|
- return mock
|
|
|
- },
|
|
|
- },
|
|
|
- {
|
|
|
- name: "negative case - articles mapper error",
|
|
|
- req: req{
|
|
|
- method: fiber.MethodGet,
|
|
|
- route: "/article/" + strconv.Itoa(articleID),
|
|
|
- },
|
|
|
- res: fiber.StatusInternalServerError,
|
|
|
- err: nil,
|
|
|
- cacheMock: func(mc *minimock.Controller) interfaces.ICache {
|
|
|
- mock := cacheMocks.NewICacheMock(mc)
|
|
|
- mock.GetMock.Return(nil, false)
|
|
|
-
|
|
|
- return mock
|
|
|
- },
|
|
|
- tagMock: func(mc *minimock.Controller) interfaces.ITagRepository {
|
|
|
- mock := repositoryMocks.NewITagRepositoryMock(mc)
|
|
|
- mock.GetAllUsedMock.Return(tags, nil)
|
|
|
-
|
|
|
- return mock
|
|
|
- },
|
|
|
- articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
|
|
|
- mock := repositoryMocks.NewIArticleRepositoryMock(mc)
|
|
|
-
|
|
|
- mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
|
|
|
- assert.Equal(mc, strconv.Itoa(articleID), url)
|
|
|
- }).Return(&article, nil)
|
|
|
-
|
|
|
- mock.GetAllPreviewMock.Return([]models.ArticlePreview{{}}, nil)
|
|
|
-
|
|
|
return mock
|
|
|
},
|
|
|
},
|
|
@@ -359,7 +298,7 @@ func Test_ArticleHandler(t *testing.T) {
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
- fiberApp := fiber.New(test.GetFiberConfig())
|
|
|
+ fiberApp := fiber.New(helpers.GetFiberTestConfig())
|
|
|
fiberReq := httptest.NewRequest(tt.req.method, tt.req.route, nil)
|
|
|
serviceProvider := sp.InitMock(tt.cacheMock(mc), tt.tagMock(mc), tt.articleMock(mc))
|
|
|
|