update_thing_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. package thing
  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/gofiber/fiber/v2"
  11. "github.com/gojuno/minimock/v3"
  12. "github.com/stretchr/testify/assert"
  13. API "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1"
  14. "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1/thing/mocks"
  15. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  16. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  17. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  18. )
  19. func TestUpdateThingHandler(t *testing.T) {
  20. t.Parallel()
  21. type req struct {
  22. method string
  23. route string
  24. contentType string
  25. body *dto.UpdateThingRequest
  26. }
  27. var (
  28. placeID = gofakeit.Number(1, 1000)
  29. thingID = gofakeit.Number(1, 1000)
  30. title = gofakeit.Phrase()
  31. description = gofakeit.Phrase()
  32. testError = errors.New(gofakeit.Phrase())
  33. layout = "2006-01-02 15:04:05"
  34. correctReq = req{
  35. method: fiber.MethodPut,
  36. route: "/v1/things/" + strconv.Itoa(thingID),
  37. body: &dto.UpdateThingRequest{
  38. PlaceID: placeID,
  39. Title: title,
  40. Description: description,
  41. },
  42. contentType: fiber.MIMEApplicationJSON,
  43. }
  44. repoResBeforeUpdate = models.Thing{
  45. ID: thingID,
  46. Title: gofakeit.Phrase(),
  47. Description: gofakeit.Phrase(),
  48. CreatedAt: gofakeit.Date(),
  49. UpdatedAt: gofakeit.Date(),
  50. }
  51. placeThingRepoResBeforeUpdate = models.PlaceThing{
  52. PlaceID: gofakeit.Number(1, 1000),
  53. ThingID: thingID,
  54. CreatedAt: gofakeit.Date(),
  55. }
  56. repoRes = models.Thing{
  57. ID: thingID,
  58. Title: title,
  59. Description: description,
  60. CreatedAt: gofakeit.Date(),
  61. UpdatedAt: gofakeit.Date(),
  62. }
  63. expectedRes = dto.ThingResponse{
  64. ID: thingID,
  65. Title: title,
  66. Description: description,
  67. CreatedAt: repoRes.CreatedAt.Format(layout),
  68. UpdatedAt: repoRes.UpdatedAt.Format(layout),
  69. }
  70. )
  71. tests := []struct {
  72. name string
  73. req req
  74. resCode int
  75. resBody interface{}
  76. thingRepoMock func(mc *minimock.Controller) ThingRepository
  77. placeThingRepoMock func(mc *minimock.Controller) PlaceThingRepository
  78. }{
  79. {
  80. name: "positive case",
  81. req: correctReq,
  82. resCode: fiber.StatusOK,
  83. resBody: expectedRes,
  84. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  85. mock := mocks.NewThingRepositoryMock(mc)
  86. mock.BeginTxMock.Return(nil, nil)
  87. mock.UpdateMock.Inspect(func(ctx context.Context, req models.UpdateThingRequest, tx *sql.Tx) {
  88. assert.Equal(mc, title, req.Title)
  89. assert.Equal(mc, description, req.Description)
  90. }).Return(nil)
  91. mock.CommitTxMock.Return(nil)
  92. mock.GetMock.Set(func(ctx context.Context, id int) (*models.Thing, error) {
  93. assert.Equal(mc, thingID, id)
  94. if mock.GetAfterCounter() == 0 {
  95. return &repoResBeforeUpdate, nil
  96. }
  97. return &repoRes, nil
  98. })
  99. return mock
  100. },
  101. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  102. mock := mocks.NewPlaceThingRepositoryMock(mc)
  103. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  104. assert.Equal(mc, thingID, id)
  105. }).Return(&placeThingRepoResBeforeUpdate, nil)
  106. mock.UpdatePlaceMock.Inspect(func(ctx context.Context, req models.UpdatePlaceThingRequest, tx *sql.Tx) {
  107. assert.Equal(mc, placeID, req.PlaceID)
  108. assert.Equal(mc, thingID, req.ThingID)
  109. }).Return(nil)
  110. return mock
  111. },
  112. },
  113. {
  114. name: "negative case - bad request",
  115. req: req{
  116. method: fiber.MethodPut,
  117. route: "/v1/things/" + gofakeit.Word(),
  118. },
  119. resCode: fiber.StatusBadRequest,
  120. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  121. return mocks.NewThingRepositoryMock(mc)
  122. },
  123. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  124. return mocks.NewPlaceThingRepositoryMock(mc)
  125. },
  126. },
  127. {
  128. name: "negative case - body parse error",
  129. req: req{
  130. method: fiber.MethodPut,
  131. route: "/v1/things/" + strconv.Itoa(thingID),
  132. },
  133. resCode: fiber.StatusBadRequest,
  134. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  135. return mocks.NewThingRepositoryMock(mc)
  136. },
  137. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  138. return mocks.NewPlaceThingRepositoryMock(mc)
  139. },
  140. },
  141. {
  142. name: "negative case - request without place_id",
  143. req: req{
  144. method: fiber.MethodPut,
  145. route: "/v1/things/" + strconv.Itoa(thingID),
  146. contentType: fiber.MIMEApplicationJSON,
  147. body: &dto.UpdateThingRequest{
  148. Title: title,
  149. },
  150. },
  151. resCode: fiber.StatusBadRequest,
  152. resBody: []*dto.ValidateErrorResponse{
  153. {
  154. Field: "UpdateThingRequest.PlaceID",
  155. Tag: "required",
  156. },
  157. },
  158. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  159. return mocks.NewThingRepositoryMock(mc)
  160. },
  161. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  162. return mocks.NewPlaceThingRepositoryMock(mc)
  163. },
  164. },
  165. {
  166. name: "negative case - request without title",
  167. req: req{
  168. method: fiber.MethodPut,
  169. route: "/v1/things/" + strconv.Itoa(thingID),
  170. contentType: fiber.MIMEApplicationJSON,
  171. body: &dto.UpdateThingRequest{
  172. PlaceID: placeID,
  173. },
  174. },
  175. resCode: fiber.StatusBadRequest,
  176. resBody: []*dto.ValidateErrorResponse{
  177. {
  178. Field: "UpdateThingRequest.Title",
  179. Tag: "required",
  180. },
  181. },
  182. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  183. return mocks.NewThingRepositoryMock(mc)
  184. },
  185. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  186. return mocks.NewPlaceThingRepositoryMock(mc)
  187. },
  188. },
  189. {
  190. name: "negative case - repository error (get thing)",
  191. req: correctReq,
  192. resCode: fiber.StatusBadRequest,
  193. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  194. mock := mocks.NewThingRepositoryMock(mc)
  195. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  196. assert.Equal(mc, thingID, id)
  197. }).Return(nil, sql.ErrNoRows)
  198. return mock
  199. },
  200. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  201. return mocks.NewPlaceThingRepositoryMock(mc)
  202. },
  203. },
  204. {
  205. name: "negative case - repository error (get place thing)",
  206. req: correctReq,
  207. resCode: fiber.StatusBadRequest,
  208. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  209. mock := mocks.NewThingRepositoryMock(mc)
  210. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  211. assert.Equal(mc, thingID, id)
  212. }).Return(&repoResBeforeUpdate, nil)
  213. return mock
  214. },
  215. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  216. mock := mocks.NewPlaceThingRepositoryMock(mc)
  217. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  218. assert.Equal(mc, thingID, id)
  219. }).Return(nil, sql.ErrNoRows)
  220. return mock
  221. },
  222. },
  223. {
  224. name: "negative case - repository error (begin tx)",
  225. req: correctReq,
  226. resCode: fiber.StatusInternalServerError,
  227. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  228. mock := mocks.NewThingRepositoryMock(mc)
  229. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  230. assert.Equal(mc, thingID, id)
  231. }).Return(&repoResBeforeUpdate, nil)
  232. mock.BeginTxMock.Return(nil, testError)
  233. return mock
  234. },
  235. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  236. mock := mocks.NewPlaceThingRepositoryMock(mc)
  237. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  238. assert.Equal(mc, thingID, id)
  239. }).Return(&placeThingRepoResBeforeUpdate, nil)
  240. return mock
  241. },
  242. },
  243. {
  244. name: "negative case - repository error (update thing)",
  245. req: correctReq,
  246. resCode: fiber.StatusInternalServerError,
  247. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  248. mock := mocks.NewThingRepositoryMock(mc)
  249. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  250. assert.Equal(mc, thingID, id)
  251. }).Return(&repoResBeforeUpdate, nil)
  252. mock.BeginTxMock.Return(nil, nil)
  253. mock.UpdateMock.Inspect(func(ctx context.Context, req models.UpdateThingRequest, tx *sql.Tx) {
  254. assert.Equal(mc, title, req.Title)
  255. assert.Equal(mc, description, req.Description)
  256. }).Return(testError)
  257. return mock
  258. },
  259. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  260. mock := mocks.NewPlaceThingRepositoryMock(mc)
  261. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  262. assert.Equal(mc, thingID, id)
  263. }).Return(&placeThingRepoResBeforeUpdate, nil)
  264. return mock
  265. },
  266. },
  267. {
  268. name: "negative case - repository error (update place)",
  269. req: correctReq,
  270. resCode: fiber.StatusInternalServerError,
  271. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  272. mock := mocks.NewThingRepositoryMock(mc)
  273. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  274. assert.Equal(mc, thingID, id)
  275. }).Return(&repoResBeforeUpdate, nil)
  276. mock.BeginTxMock.Return(nil, nil)
  277. mock.UpdateMock.Inspect(func(ctx context.Context, req models.UpdateThingRequest, tx *sql.Tx) {
  278. assert.Equal(mc, title, req.Title)
  279. assert.Equal(mc, description, req.Description)
  280. }).Return(nil)
  281. return mock
  282. },
  283. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  284. mock := mocks.NewPlaceThingRepositoryMock(mc)
  285. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  286. assert.Equal(mc, thingID, id)
  287. }).Return(&placeThingRepoResBeforeUpdate, nil)
  288. mock.UpdatePlaceMock.Inspect(func(ctx context.Context, req models.UpdatePlaceThingRequest, tx *sql.Tx) {
  289. assert.Equal(mc, placeID, req.PlaceID)
  290. assert.Equal(mc, thingID, req.ThingID)
  291. }).Return(testError)
  292. return mock
  293. },
  294. },
  295. {
  296. name: "negative case - repository error (commit tx)",
  297. req: correctReq,
  298. resCode: fiber.StatusInternalServerError,
  299. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  300. mock := mocks.NewThingRepositoryMock(mc)
  301. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  302. assert.Equal(mc, thingID, id)
  303. }).Return(&repoResBeforeUpdate, nil)
  304. mock.BeginTxMock.Return(nil, nil)
  305. mock.UpdateMock.Inspect(func(ctx context.Context, req models.UpdateThingRequest, tx *sql.Tx) {
  306. assert.Equal(mc, title, req.Title)
  307. assert.Equal(mc, description, req.Description)
  308. }).Return(nil)
  309. mock.CommitTxMock.Return(testError)
  310. return mock
  311. },
  312. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  313. mock := mocks.NewPlaceThingRepositoryMock(mc)
  314. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  315. assert.Equal(mc, thingID, id)
  316. }).Return(&placeThingRepoResBeforeUpdate, nil)
  317. mock.UpdatePlaceMock.Inspect(func(ctx context.Context, req models.UpdatePlaceThingRequest, tx *sql.Tx) {
  318. assert.Equal(mc, placeID, req.PlaceID)
  319. assert.Equal(mc, thingID, req.ThingID)
  320. }).Return(nil)
  321. return mock
  322. },
  323. },
  324. {
  325. name: "negative case - repository error (get thing)",
  326. req: correctReq,
  327. resCode: fiber.StatusInternalServerError,
  328. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  329. mock := mocks.NewThingRepositoryMock(mc)
  330. mock.GetMock.Set(func(ctx context.Context, thingID int) (*models.Thing, error) {
  331. if mock.GetAfterCounter() == 0 {
  332. return &repoResBeforeUpdate, nil
  333. }
  334. return nil, sql.ErrNoRows
  335. })
  336. mock.BeginTxMock.Return(nil, nil)
  337. mock.UpdateMock.Inspect(func(ctx context.Context, req models.UpdateThingRequest, tx *sql.Tx) {
  338. assert.Equal(mc, title, req.Title)
  339. assert.Equal(mc, description, req.Description)
  340. }).Return(nil)
  341. mock.CommitTxMock.Return(nil)
  342. return mock
  343. },
  344. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  345. mock := mocks.NewPlaceThingRepositoryMock(mc)
  346. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  347. assert.Equal(mc, thingID, id)
  348. }).Return(&placeThingRepoResBeforeUpdate, nil)
  349. mock.UpdatePlaceMock.Inspect(func(ctx context.Context, req models.UpdatePlaceThingRequest, tx *sql.Tx) {
  350. assert.Equal(mc, placeID, req.PlaceID)
  351. assert.Equal(mc, thingID, req.ThingID)
  352. }).Return(nil)
  353. return mock
  354. },
  355. },
  356. }
  357. for _, tt := range tests {
  358. t.Run(tt.name, func(t *testing.T) {
  359. t.Parallel()
  360. mc := minimock.NewController(t)
  361. fiberApp := fiber.New()
  362. fiberApp.Put("/v1/things/:thingId", UpdateThingHandler(tt.thingRepoMock(mc), tt.placeThingRepoMock(mc)))
  363. fiberReq := httptest.NewRequest(tt.req.method, tt.req.route, helpers.ConvertDataToIOReader(tt.req.body))
  364. fiberReq.Header.Add(fiber.HeaderContentType, tt.req.contentType)
  365. fiberRes, _ := fiberApp.Test(fiberReq, API.DefaultTestTimeOut)
  366. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  367. if tt.resBody != nil {
  368. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  369. }
  370. })
  371. }
  372. }