package handler import ( "database/sql" "errors" "net/http/httptest" "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_MainPageHandler(t *testing.T) { type cacheMockFunc func(mc *minimock.Controller) interfaces.ICache type articleMockFunc func(mc *minimock.Controller) interfaces.IArticleRepository type req struct { method string route string } var ( mc = minimock.NewController(t) date = gofakeit.Date() publishTime = date.Format("2006-01-02 15:04:05") internalErr = errors.New(gofakeit.Phrase()) 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()}, }, } ) tests := []struct { name string req req res int err error cacheMock cacheMockFunc articleMock articleMockFunc }{ { name: "positive case", req: req{ method: fiber.MethodGet, route: "/", }, 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 }, articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository { mock := repositoryMocks.NewIArticleRepositoryMock(mc) mock.GetAllPreviewMock.Return(previewArticles, nil) return mock }, }, { name: "negative case - article repository error", req: req{ method: fiber.MethodGet, route: "/", }, res: fiber.StatusInternalServerError, err: nil, cacheMock: func(mc *minimock.Controller) interfaces.ICache { mock := cacheMocks.NewICacheMock(mc) mock.GetMock.Return(nil, false) return mock }, articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository { mock := repositoryMocks.NewIArticleRepositoryMock(mc) mock.GetAllPreviewMock.Return(nil, internalErr) return mock }, }, { name: "negative case - articles mapper error", req: req{ method: fiber.MethodGet, route: "/", }, res: fiber.StatusInternalServerError, err: nil, cacheMock: func(mc *minimock.Controller) interfaces.ICache { mock := cacheMocks.NewICacheMock(mc) mock.GetMock.Return(nil, false) return mock }, articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository { mock := repositoryMocks.NewIArticleRepositoryMock(mc) 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.articleMock(mc)) fiberApp.Get("/", MainPageHandler(serviceProvider)) fiberRes, fiberErr := fiberApp.Test(fiberReq) assert.Equal(t, tt.res, fiberRes.StatusCode) assert.Equal(t, tt.err, fiberErr) }) } }