main_page_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/helpers"
  9. "github.com/dmitriygnatenko/internal/interfaces"
  10. "github.com/dmitriygnatenko/internal/models"
  11. repositoryMocks "github.com/dmitriygnatenko/internal/repositories/mocks"
  12. sp "github.com/dmitriygnatenko/internal/service_provider"
  13. cacheMocks "github.com/dmitriygnatenko/internal/services/cache/mocks"
  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.Cache
  20. type articleMockFunc func(mc *minimock.Controller) interfaces.ArticleRepository
  21. type req struct {
  22. method string
  23. route string
  24. }
  25. var (
  26. mc = minimock.NewController(t)
  27. publishTime = gofakeit.Date()
  28. internalErr = errors.New(gofakeit.Phrase())
  29. previewArticles = []models.ArticlePreview{
  30. {
  31. ID: gofakeit.Number(1, 100),
  32. URL: gofakeit.URL(),
  33. Title: gofakeit.Phrase(),
  34. PublishTime: publishTime,
  35. PreviewText: sql.NullString{Valid: true, String: gofakeit.Phrase()},
  36. Image: sql.NullString{Valid: true, String: gofakeit.URL()},
  37. },
  38. {
  39. ID: gofakeit.Number(1, 100),
  40. URL: gofakeit.URL(),
  41. Title: gofakeit.Phrase(),
  42. PublishTime: publishTime,
  43. PreviewText: sql.NullString{Valid: true, String: gofakeit.Phrase()},
  44. Image: sql.NullString{Valid: true, String: gofakeit.URL()},
  45. },
  46. {
  47. ID: gofakeit.Number(1, 100),
  48. URL: gofakeit.URL(),
  49. Title: gofakeit.Phrase(),
  50. PublishTime: publishTime,
  51. PreviewText: sql.NullString{Valid: true, String: gofakeit.Phrase()},
  52. Image: sql.NullString{Valid: true, String: gofakeit.URL()},
  53. },
  54. }
  55. )
  56. tests := []struct {
  57. name string
  58. req req
  59. res int
  60. err error
  61. cacheMock cacheMockFunc
  62. articleMock articleMockFunc
  63. }{
  64. {
  65. name: "positive case",
  66. req: req{
  67. method: fiber.MethodGet,
  68. route: "/",
  69. },
  70. res: fiber.StatusOK,
  71. err: nil,
  72. cacheMock: func(mc *minimock.Controller) interfaces.Cache {
  73. mock := cacheMocks.NewCacheMock(mc)
  74. mock.GetMock.Return(nil, false)
  75. mock.SetMock.Return()
  76. return mock
  77. },
  78. articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
  79. mock := repositoryMocks.NewArticleRepositoryMock(mc)
  80. mock.GetAllPreviewMock.Return(previewArticles, nil)
  81. return mock
  82. },
  83. },
  84. {
  85. name: "negative case - article repository error",
  86. req: req{
  87. method: fiber.MethodGet,
  88. route: "/",
  89. },
  90. res: fiber.StatusInternalServerError,
  91. err: nil,
  92. cacheMock: func(mc *minimock.Controller) interfaces.Cache {
  93. mock := cacheMocks.NewCacheMock(mc)
  94. mock.GetMock.Return(nil, false)
  95. return mock
  96. },
  97. articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
  98. mock := repositoryMocks.NewArticleRepositoryMock(mc)
  99. mock.GetAllPreviewMock.Return(nil, internalErr)
  100. return mock
  101. },
  102. },
  103. }
  104. for _, tt := range tests {
  105. t.Run(tt.name, func(t *testing.T) {
  106. fiberApp := fiber.New(helpers.GetFiberTestConfig())
  107. fiberReq := httptest.NewRequest(tt.req.method, tt.req.route, nil)
  108. serviceProvider := sp.InitMock(tt.cacheMock(mc), tt.articleMock(mc))
  109. fiberApp.Get("/", MainPageHandler(serviceProvider))
  110. fiberRes, fiberErr := fiberApp.Test(fiberReq)
  111. assert.Equal(t, tt.res, fiberRes.StatusCode)
  112. assert.Equal(t, tt.err, fiberErr)
  113. })
  114. }
  115. }