main_page_test.go 3.2 KB

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