123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- package handler
- import (
- "context"
- "database/sql"
- "errors"
- "net/http/httptest"
- "strconv"
- "testing"
- "github.com/brianvoe/gofakeit/v6"
- "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 req struct {
- method string
- route string
- }
- var (
- mc = minimock.NewController(t)
- articleID = gofakeit.Number(1, 100)
- date = gofakeit.Date()
- publishTime = date.Format("2006-01-02 15:04:05")
- internalErr = errors.New(gofakeit.Phrase())
- article = models.Article{
- ID: articleID,
- URL: gofakeit.URL(),
- Title: gofakeit.Phrase(),
- Text: gofakeit.Phrase(),
- PublishTime: publishTime,
- PreviewText: sql.NullString{Valid: true, String: gofakeit.Phrase()},
- Image: sql.NullString{Valid: true, String: gofakeit.URL()},
- IsActive: true,
- }
- notActiveArticle = models.Article{
- ID: articleID,
- URL: gofakeit.URL(),
- Title: gofakeit.Phrase(),
- Text: gofakeit.Phrase(),
- PublishTime: publishTime,
- PreviewText: sql.NullString{Valid: true, String: gofakeit.Phrase()},
- Image: sql.NullString{Valid: true, String: gofakeit.URL()},
- }
- tags = []models.Tag{
- {
- ID: gofakeit.Number(1, 100),
- Tag: gofakeit.Word(),
- URL: gofakeit.Word(),
- },
- {
- ID: gofakeit.Number(1, 100),
- Tag: gofakeit.Word(),
- URL: gofakeit.Word(),
- },
- }
- previewArticles = []models.ArticlePreview{
- {
- ID: gofakeit.Number(1, 100),
- URL: gofakeit.URL(),
- Title: gofakeit.Phrase(),
- PublishTime: publishTime,
- PreviewText: sql.NullString{Valid: true, String: gofakeit.Phrase()},
- Image: sql.NullString{Valid: true, String: gofakeit.URL()},
- },
- {
- ID: gofakeit.Number(1, 100),
- URL: gofakeit.URL(),
- Title: gofakeit.Phrase(),
- PublishTime: publishTime,
- PreviewText: sql.NullString{Valid: true, String: gofakeit.Phrase()},
- Image: sql.NullString{Valid: true, String: gofakeit.URL()},
- },
- {
- ID: gofakeit.Number(1, 100),
- URL: gofakeit.URL(),
- Title: gofakeit.Phrase(),
- PublishTime: publishTime,
- PreviewText: sql.NullString{Valid: true, String: gofakeit.Phrase()},
- Image: sql.NullString{Valid: true, String: gofakeit.URL()},
- },
- {
- ID: gofakeit.Number(1, 100),
- URL: gofakeit.URL(),
- Title: gofakeit.Phrase(),
- PublishTime: publishTime,
- PreviewText: sql.NullString{Valid: true, String: gofakeit.Phrase()},
- Image: sql.NullString{Valid: true, String: gofakeit.URL()},
- },
- }
- )
- tests := []struct {
- name string
- req req
- res int
- err error
- cacheMock cacheMockFunc
- tagMock tagMockFunc
- articleMock articleMockFunc
- }{
- {
- name: "positive case",
- req: req{
- method: fiber.MethodGet,
- route: "/article/" + strconv.Itoa(articleID),
- },
- res: fiber.StatusOK,
- err: nil,
- cacheMock: func(mc *minimock.Controller) interfaces.ICache {
- mock := cacheMocks.NewICacheMock(mc)
- mock.GetMock.Return(nil, false)
- mock.SetMock.Return()
- 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(previewArticles, nil)
- return mock
- },
- },
- {
- name: "negative case - article not found",
- req: req{
- method: fiber.MethodGet,
- route: "/article/" + strconv.Itoa(articleID),
- },
- res: fiber.StatusNotFound,
- 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(nil, sql.ErrNoRows)
- return mock
- },
- },
- {
- name: "negative case - article repository 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(nil, internalErr)
- return mock
- },
- },
- {
- name: "negative case - article not active",
- req: req{
- method: fiber.MethodGet,
- route: "/article/" + strconv.Itoa(articleID),
- },
- res: fiber.StatusNotFound,
- 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(¬ActiveArticle, nil)
- 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{
- 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(nil, internalErr)
- 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)
- return mock
- },
- },
- {
- name: "negative case - articles repository 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(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
- },
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- fiberApp := fiber.New(test.GetFiberConfig())
- fiberReq := httptest.NewRequest(tt.req.method, tt.req.route, nil)
- serviceProvider := sp.InitMock(tt.cacheMock(mc), tt.tagMock(mc), tt.articleMock(mc))
- fiberApp.Get("/article/:article", ArticleHandler(serviceProvider))
- fiberRes, fiberErr := fiberApp.Test(fiberReq)
- assert.Equal(t, tt.res, fiberRes.StatusCode)
- assert.Equal(t, tt.err, fiberErr)
- })
- }
- }
|