article_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package handler
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "net/http/httptest"
  7. "strconv"
  8. "testing"
  9. "github.com/brianvoe/gofakeit/v6"
  10. "github.com/dmitriygnatenko/internal/helpers"
  11. "github.com/dmitriygnatenko/internal/interfaces"
  12. "github.com/dmitriygnatenko/internal/models"
  13. repositoryMocks "github.com/dmitriygnatenko/internal/repositories/mocks"
  14. sp "github.com/dmitriygnatenko/internal/service_provider"
  15. cacheMocks "github.com/dmitriygnatenko/internal/services/cache/mocks"
  16. "github.com/gofiber/fiber/v2"
  17. "github.com/gojuno/minimock/v3"
  18. "github.com/stretchr/testify/assert"
  19. )
  20. func Test_ArticleHandler(t *testing.T) {
  21. type cacheMockFunc func(mc *minimock.Controller) interfaces.Cache
  22. type tagMockFunc func(mc *minimock.Controller) interfaces.TagRepository
  23. type articleMockFunc func(mc *minimock.Controller) interfaces.ArticleRepository
  24. type req struct {
  25. method string
  26. route string
  27. }
  28. var (
  29. mc = minimock.NewController(t)
  30. articleID = gofakeit.Number(1, 100)
  31. publishTime = gofakeit.Date()
  32. internalErr = errors.New(gofakeit.Phrase())
  33. article = models.Article{
  34. ID: articleID,
  35. URL: gofakeit.URL(),
  36. Title: gofakeit.Phrase(),
  37. Text: gofakeit.Phrase(),
  38. PublishTime: publishTime,
  39. PreviewText: sql.NullString{Valid: true, String: gofakeit.Phrase()},
  40. Image: sql.NullString{Valid: true, String: gofakeit.URL()},
  41. IsActive: true,
  42. }
  43. notActiveArticle = models.Article{
  44. ID: articleID,
  45. URL: gofakeit.URL(),
  46. Title: gofakeit.Phrase(),
  47. Text: gofakeit.Phrase(),
  48. PublishTime: publishTime,
  49. PreviewText: sql.NullString{Valid: true, String: gofakeit.Phrase()},
  50. Image: sql.NullString{Valid: true, String: gofakeit.URL()},
  51. }
  52. tags = []models.Tag{
  53. {
  54. ID: gofakeit.Number(1, 100),
  55. Tag: gofakeit.Word(),
  56. URL: gofakeit.Word(),
  57. },
  58. {
  59. ID: gofakeit.Number(1, 100),
  60. Tag: gofakeit.Word(),
  61. URL: gofakeit.Word(),
  62. },
  63. }
  64. previewArticles = []models.ArticlePreview{
  65. {
  66. ID: gofakeit.Number(1, 100),
  67. URL: gofakeit.URL(),
  68. Title: gofakeit.Phrase(),
  69. PublishTime: publishTime,
  70. PreviewText: sql.NullString{Valid: true, String: gofakeit.Phrase()},
  71. Image: sql.NullString{Valid: true, String: gofakeit.URL()},
  72. },
  73. {
  74. ID: gofakeit.Number(1, 100),
  75. URL: gofakeit.URL(),
  76. Title: gofakeit.Phrase(),
  77. PublishTime: publishTime,
  78. PreviewText: sql.NullString{Valid: true, String: gofakeit.Phrase()},
  79. Image: sql.NullString{Valid: true, String: gofakeit.URL()},
  80. },
  81. {
  82. ID: gofakeit.Number(1, 100),
  83. URL: gofakeit.URL(),
  84. Title: gofakeit.Phrase(),
  85. PublishTime: publishTime,
  86. PreviewText: sql.NullString{Valid: true, String: gofakeit.Phrase()},
  87. Image: sql.NullString{Valid: true, String: gofakeit.URL()},
  88. },
  89. {
  90. ID: gofakeit.Number(1, 100),
  91. URL: gofakeit.URL(),
  92. Title: gofakeit.Phrase(),
  93. PublishTime: publishTime,
  94. PreviewText: sql.NullString{Valid: true, String: gofakeit.Phrase()},
  95. Image: sql.NullString{Valid: true, String: gofakeit.URL()},
  96. },
  97. }
  98. )
  99. tests := []struct {
  100. name string
  101. req req
  102. res int
  103. err error
  104. cacheMock cacheMockFunc
  105. tagMock tagMockFunc
  106. articleMock articleMockFunc
  107. }{
  108. {
  109. name: "positive case",
  110. req: req{
  111. method: fiber.MethodGet,
  112. route: "/article/" + strconv.Itoa(articleID),
  113. },
  114. res: fiber.StatusOK,
  115. err: nil,
  116. cacheMock: func(mc *minimock.Controller) interfaces.Cache {
  117. mock := cacheMocks.NewCacheMock(mc)
  118. mock.GetMock.Return(nil, false)
  119. mock.SetMock.Return()
  120. return mock
  121. },
  122. tagMock: func(mc *minimock.Controller) interfaces.TagRepository {
  123. mock := repositoryMocks.NewTagRepositoryMock(mc)
  124. mock.GetAllUsedMock.Return(tags, nil)
  125. return mock
  126. },
  127. articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
  128. mock := repositoryMocks.NewArticleRepositoryMock(mc)
  129. mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
  130. assert.Equal(mc, strconv.Itoa(articleID), url)
  131. }).Return(&article, nil)
  132. mock.GetAllPreviewMock.Return(previewArticles, nil)
  133. return mock
  134. },
  135. },
  136. {
  137. name: "negative case - article not found",
  138. req: req{
  139. method: fiber.MethodGet,
  140. route: "/article/" + strconv.Itoa(articleID),
  141. },
  142. res: fiber.StatusNotFound,
  143. err: nil,
  144. cacheMock: func(mc *minimock.Controller) interfaces.Cache {
  145. mock := cacheMocks.NewCacheMock(mc)
  146. mock.GetMock.Return(nil, false)
  147. return mock
  148. },
  149. tagMock: func(mc *minimock.Controller) interfaces.TagRepository {
  150. mock := repositoryMocks.NewTagRepositoryMock(mc)
  151. return mock
  152. },
  153. articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
  154. mock := repositoryMocks.NewArticleRepositoryMock(mc)
  155. mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
  156. assert.Equal(mc, strconv.Itoa(articleID), url)
  157. }).Return(nil, sql.ErrNoRows)
  158. return mock
  159. },
  160. },
  161. {
  162. name: "negative case - article repository error",
  163. req: req{
  164. method: fiber.MethodGet,
  165. route: "/article/" + strconv.Itoa(articleID),
  166. },
  167. res: fiber.StatusInternalServerError,
  168. err: nil,
  169. cacheMock: func(mc *minimock.Controller) interfaces.Cache {
  170. mock := cacheMocks.NewCacheMock(mc)
  171. mock.GetMock.Return(nil, false)
  172. return mock
  173. },
  174. tagMock: func(mc *minimock.Controller) interfaces.TagRepository {
  175. mock := repositoryMocks.NewTagRepositoryMock(mc)
  176. return mock
  177. },
  178. articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
  179. mock := repositoryMocks.NewArticleRepositoryMock(mc)
  180. mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
  181. assert.Equal(mc, strconv.Itoa(articleID), url)
  182. }).Return(nil, internalErr)
  183. return mock
  184. },
  185. },
  186. {
  187. name: "negative case - article not active",
  188. req: req{
  189. method: fiber.MethodGet,
  190. route: "/article/" + strconv.Itoa(articleID),
  191. },
  192. res: fiber.StatusNotFound,
  193. err: nil,
  194. cacheMock: func(mc *minimock.Controller) interfaces.Cache {
  195. mock := cacheMocks.NewCacheMock(mc)
  196. mock.GetMock.Return(nil, false)
  197. return mock
  198. },
  199. tagMock: func(mc *minimock.Controller) interfaces.TagRepository {
  200. mock := repositoryMocks.NewTagRepositoryMock(mc)
  201. return mock
  202. },
  203. articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
  204. mock := repositoryMocks.NewArticleRepositoryMock(mc)
  205. mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
  206. assert.Equal(mc, strconv.Itoa(articleID), url)
  207. }).Return(&notActiveArticle, nil)
  208. return mock
  209. },
  210. },
  211. {
  212. name: "negative case - tags repository error",
  213. req: req{
  214. method: fiber.MethodGet,
  215. route: "/article/" + strconv.Itoa(articleID),
  216. },
  217. res: fiber.StatusInternalServerError,
  218. err: nil,
  219. cacheMock: func(mc *minimock.Controller) interfaces.Cache {
  220. mock := cacheMocks.NewCacheMock(mc)
  221. mock.GetMock.Return(nil, false)
  222. return mock
  223. },
  224. tagMock: func(mc *minimock.Controller) interfaces.TagRepository {
  225. mock := repositoryMocks.NewTagRepositoryMock(mc)
  226. mock.GetAllUsedMock.Return(nil, internalErr)
  227. return mock
  228. },
  229. articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
  230. mock := repositoryMocks.NewArticleRepositoryMock(mc)
  231. mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
  232. assert.Equal(mc, strconv.Itoa(articleID), url)
  233. }).Return(&article, nil)
  234. return mock
  235. },
  236. },
  237. {
  238. name: "negative case - articles repository error",
  239. req: req{
  240. method: fiber.MethodGet,
  241. route: "/article/" + strconv.Itoa(articleID),
  242. },
  243. res: fiber.StatusInternalServerError,
  244. err: nil,
  245. cacheMock: func(mc *minimock.Controller) interfaces.Cache {
  246. mock := cacheMocks.NewCacheMock(mc)
  247. mock.GetMock.Return(nil, false)
  248. return mock
  249. },
  250. tagMock: func(mc *minimock.Controller) interfaces.TagRepository {
  251. mock := repositoryMocks.NewTagRepositoryMock(mc)
  252. mock.GetAllUsedMock.Return(tags, nil)
  253. return mock
  254. },
  255. articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
  256. mock := repositoryMocks.NewArticleRepositoryMock(mc)
  257. mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
  258. assert.Equal(mc, strconv.Itoa(articleID), url)
  259. }).Return(&article, nil)
  260. mock.GetAllPreviewMock.Return(nil, internalErr)
  261. return mock
  262. },
  263. },
  264. }
  265. for _, tt := range tests {
  266. t.Run(tt.name, func(t *testing.T) {
  267. fiberApp := fiber.New(helpers.GetFiberTestConfig())
  268. fiberReq := httptest.NewRequest(tt.req.method, tt.req.route, nil)
  269. serviceProvider := sp.InitMock(tt.cacheMock(mc), tt.tagMock(mc), tt.articleMock(mc))
  270. fiberApp.Get("/article/:article", ArticleHandler(serviceProvider))
  271. fiberRes, fiberErr := fiberApp.Test(fiberReq)
  272. assert.Equal(t, tt.res, fiberRes.StatusCode)
  273. assert.Equal(t, tt.err, fiberErr)
  274. })
  275. }
  276. }