main_page_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/dmitriygnatenko/internal/interfaces"
  9. "github.com/dmitriygnatenko/internal/models"
  10. repositoryMocks "github.com/dmitriygnatenko/internal/repositories/mocks"
  11. sp "github.com/dmitriygnatenko/internal/service_provider"
  12. cacheMocks "github.com/dmitriygnatenko/internal/services/cache/mocks"
  13. "github.com/dmitriygnatenko/internal/services/handler/test"
  14. "github.com/gofiber/fiber/v2"
  15. "github.com/gojuno/minimock/v3"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. func Test_MainPageHandler(t *testing.T) {
  19. type cacheMockFunc func(mc *minimock.Controller) interfaces.ICache
  20. type articleMockFunc func(mc *minimock.Controller) interfaces.IArticleRepository
  21. type req struct {
  22. method string
  23. route string
  24. }
  25. var (
  26. mc = minimock.NewController(t)
  27. date = gofakeit.Date()
  28. publishTime = date.Format("2006-01-02 15:04:05")
  29. internalErr = errors.New(gofakeit.Phrase())
  30. previewArticles = []models.ArticlePreview{
  31. {
  32. ID: gofakeit.Number(1, 100),
  33. URL: gofakeit.URL(),
  34. Title: gofakeit.Phrase(),
  35. PublishTime: publishTime,
  36. PreviewText: sql.NullString{Valid: true, String: gofakeit.Phrase()},
  37. Image: sql.NullString{Valid: true, String: gofakeit.URL()},
  38. },
  39. {
  40. ID: gofakeit.Number(1, 100),
  41. URL: gofakeit.URL(),
  42. Title: gofakeit.Phrase(),
  43. PublishTime: publishTime,
  44. PreviewText: sql.NullString{Valid: true, String: gofakeit.Phrase()},
  45. Image: sql.NullString{Valid: true, String: gofakeit.URL()},
  46. },
  47. {
  48. ID: gofakeit.Number(1, 100),
  49. URL: gofakeit.URL(),
  50. Title: gofakeit.Phrase(),
  51. PublishTime: publishTime,
  52. PreviewText: sql.NullString{Valid: true, String: gofakeit.Phrase()},
  53. Image: sql.NullString{Valid: true, String: gofakeit.URL()},
  54. },
  55. }
  56. )
  57. tests := []struct {
  58. name string
  59. req req
  60. res int
  61. err error
  62. cacheMock cacheMockFunc
  63. articleMock articleMockFunc
  64. }{
  65. {
  66. name: "positive case",
  67. req: req{
  68. method: fiber.MethodGet,
  69. route: "/",
  70. },
  71. res: fiber.StatusOK,
  72. err: nil,
  73. cacheMock: func(mc *minimock.Controller) interfaces.ICache {
  74. mock := cacheMocks.NewICacheMock(mc)
  75. mock.GetMock.Return(nil, false)
  76. mock.SetMock.Return()
  77. return mock
  78. },
  79. articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
  80. mock := repositoryMocks.NewIArticleRepositoryMock(mc)
  81. mock.GetAllPreviewMock.Return(previewArticles, nil)
  82. return mock
  83. },
  84. },
  85. {
  86. name: "negative case - article repository error",
  87. req: req{
  88. method: fiber.MethodGet,
  89. route: "/",
  90. },
  91. res: fiber.StatusInternalServerError,
  92. err: nil,
  93. cacheMock: func(mc *minimock.Controller) interfaces.ICache {
  94. mock := cacheMocks.NewICacheMock(mc)
  95. mock.GetMock.Return(nil, false)
  96. return mock
  97. },
  98. articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
  99. mock := repositoryMocks.NewIArticleRepositoryMock(mc)
  100. mock.GetAllPreviewMock.Return(nil, internalErr)
  101. return mock
  102. },
  103. },
  104. {
  105. name: "negative case - articles mapper error",
  106. req: req{
  107. method: fiber.MethodGet,
  108. route: "/",
  109. },
  110. res: fiber.StatusInternalServerError,
  111. err: nil,
  112. cacheMock: func(mc *minimock.Controller) interfaces.ICache {
  113. mock := cacheMocks.NewICacheMock(mc)
  114. mock.GetMock.Return(nil, false)
  115. return mock
  116. },
  117. articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
  118. mock := repositoryMocks.NewIArticleRepositoryMock(mc)
  119. mock.GetAllPreviewMock.Return([]models.ArticlePreview{{}}, nil)
  120. return mock
  121. },
  122. },
  123. }
  124. for _, tt := range tests {
  125. t.Run(tt.name, func(t *testing.T) {
  126. fiberApp := fiber.New(test.GetFiberConfig())
  127. fiberReq := httptest.NewRequest(tt.req.method, tt.req.route, nil)
  128. serviceProvider := sp.InitMock(tt.cacheMock(mc), tt.articleMock(mc))
  129. fiberApp.Get("/", MainPageHandler(serviceProvider))
  130. fiberRes, fiberErr := fiberApp.Test(fiberReq)
  131. assert.Equal(t, tt.res, fiberRes.StatusCode)
  132. assert.Equal(t, tt.err, fiberErr)
  133. })
  134. }
  135. }