main_page_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package handler
  2. import (
  3. "database/sql"
  4. "errors"
  5. "net/http/httptest"
  6. "testing"
  7. "git.dmitriygnatenko.ru/dima/go-common/logger"
  8. "github.com/brianvoe/gofakeit/v6"
  9. "github.com/gofiber/fiber/v2"
  10. "github.com/gojuno/minimock/v3"
  11. "github.com/stretchr/testify/assert"
  12. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/helpers"
  13. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/models"
  14. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/handler/mocks"
  15. )
  16. func TestMainPageHandler(t *testing.T) {
  17. logger.Init(logger.Config{})
  18. t.Parallel()
  19. type req struct {
  20. method string
  21. route string
  22. }
  23. var (
  24. publishTime = gofakeit.Date()
  25. internalErr = errors.New(gofakeit.Phrase())
  26. previewArticles = []models.ArticlePreview{
  27. {
  28. ID: gofakeit.Uint64(),
  29. URL: gofakeit.URL(),
  30. Title: gofakeit.Phrase(),
  31. PublishTime: publishTime,
  32. PreviewText: sql.NullString{Valid: true, String: gofakeit.Phrase()},
  33. Image: sql.NullString{Valid: true, String: gofakeit.URL()},
  34. },
  35. {
  36. ID: gofakeit.Uint64(),
  37. URL: gofakeit.URL(),
  38. Title: gofakeit.Phrase(),
  39. PublishTime: publishTime,
  40. PreviewText: sql.NullString{Valid: true, String: gofakeit.Phrase()},
  41. Image: sql.NullString{Valid: true, String: gofakeit.URL()},
  42. },
  43. {
  44. ID: gofakeit.Uint64(),
  45. URL: gofakeit.URL(),
  46. Title: gofakeit.Phrase(),
  47. PublishTime: publishTime,
  48. PreviewText: sql.NullString{Valid: true, String: gofakeit.Phrase()},
  49. Image: sql.NullString{Valid: true, String: gofakeit.URL()},
  50. },
  51. }
  52. )
  53. tests := []struct {
  54. name string
  55. req req
  56. res int
  57. err error
  58. cacheMock func(mc *minimock.Controller) CacheService
  59. articleMock func(mc *minimock.Controller) ArticleRepository
  60. }{
  61. {
  62. name: "positive case",
  63. req: req{
  64. method: fiber.MethodGet,
  65. route: "/",
  66. },
  67. res: fiber.StatusOK,
  68. err: nil,
  69. cacheMock: func(mc *minimock.Controller) CacheService {
  70. mock := mocks.NewCacheServiceMock(mc)
  71. mock.GetMock.Return(nil, false)
  72. mock.SetMock.Return()
  73. return mock
  74. },
  75. articleMock: func(mc *minimock.Controller) ArticleRepository {
  76. mock := mocks.NewArticleRepositoryMock(mc)
  77. mock.GetAllPreviewMock.Return(previewArticles, nil)
  78. return mock
  79. },
  80. },
  81. {
  82. name: "negative case - article repository error",
  83. req: req{
  84. method: fiber.MethodGet,
  85. route: "/",
  86. },
  87. res: fiber.StatusInternalServerError,
  88. err: nil,
  89. cacheMock: func(mc *minimock.Controller) CacheService {
  90. mock := mocks.NewCacheServiceMock(mc)
  91. mock.GetMock.Return(nil, false)
  92. return mock
  93. },
  94. articleMock: func(mc *minimock.Controller) ArticleRepository {
  95. mock := mocks.NewArticleRepositoryMock(mc)
  96. mock.GetAllPreviewMock.Return(nil, internalErr)
  97. return mock
  98. },
  99. },
  100. }
  101. for _, tt := range tests {
  102. t.Run(tt.name, func(t *testing.T) {
  103. t.Parallel()
  104. mc := minimock.NewController(t)
  105. fiberApp := fiber.New(helpers.GetFiberTestConfig())
  106. fiberReq := httptest.NewRequest(tt.req.method, tt.req.route, nil)
  107. fiberApp.Get("/", MainPageHandler(
  108. tt.cacheMock(mc),
  109. tt.articleMock(mc),
  110. ))
  111. fiberRes, fiberErr := fiberApp.Test(fiberReq)
  112. assert.Equal(t, tt.res, fiberRes.StatusCode)
  113. assert.Equal(t, tt.err, fiberErr)
  114. })
  115. }
  116. }