stufflog graphql server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
5.5 KiB

  1. package mysqldriver
  2. import (
  3. "context"
  4. "git.aiterp.net/stufflog/server/internal/xlerrors"
  5. "git.aiterp.net/stufflog/server/models"
  6. "github.com/stretchr/testify/assert"
  7. "testing"
  8. "time"
  9. )
  10. var issue1 = models.Issue{
  11. ProjectID: "STUFF",
  12. OwnerID: "Admin",
  13. AssigneeID: "",
  14. StatusStage: models.IssueStageActive,
  15. StatusName: "IN PROGRESS",
  16. DueTime: time.Now().UTC().Add(time.Hour * 72).Truncate(time.Hour * 24).Add(time.Hour * 16),
  17. Name: "Do Stuff",
  18. Title: "Do some important stuff.",
  19. Description: "Stuff and things, items and artifacts, objects and creations.",
  20. }
  21. var issue2 = models.Issue{
  22. ProjectID: "MODELING",
  23. OwnerID: "Test",
  24. AssigneeID: "Test",
  25. StatusStage: models.IssueStagePostponed,
  26. StatusName: "TOO HARD",
  27. Name: "Hard Surface Course",
  28. Title: "Run through the Hard Surface modeling course.",
  29. Description: "maek robit",
  30. }
  31. var issue3 = models.Issue{
  32. ProjectID: "MODELING",
  33. OwnerID: "Test",
  34. AssigneeID: "",
  35. StatusStage: models.IssueStagePending,
  36. StatusName: "TO DO",
  37. Name: "Isometric Room Scene",
  38. Title: "Create an isometric room-box scene.",
  39. Description: "furniture and stuff.",
  40. }
  41. var issue3Updated = models.Issue{
  42. ProjectID: "MODELING",
  43. OwnerID: "Test",
  44. AssigneeID: "Admin",
  45. StatusStage: models.IssueStageActive,
  46. StatusName: "WORK IN PROGRESS",
  47. Name: "Room Scene",
  48. Title: "Create a room-box scene.",
  49. Description: "THREE DIMENSIONAL DESK CLUTTER",
  50. }
  51. var issue4 = models.Issue{
  52. ProjectID: "DINNER",
  53. OwnerID: "Test",
  54. AssigneeID: "Test",
  55. StatusStage: models.IssueStagePending,
  56. StatusName: "SHOPPING LIST",
  57. DueTime: mustParseTime("2020-04-22T17:30:00.000+02:00"),
  58. Name: "Spaghetti Carbonara",
  59. Title: "WEDNESDAY 2020-04-22: Spaghetti Carbonara",
  60. Description: "See shopping list",
  61. }
  62. func TestIssueRepository(t *testing.T) {
  63. issues := testDB.issues
  64. ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
  65. defer cancel()
  66. assert.NoError(t, clearTable("issue"))
  67. // INSERT
  68. result, err := issues.Insert(ctx, issue1)
  69. assert.NoError(t, err)
  70. assert.Equal(t, "STUFF-1", result.ID)
  71. if result != nil {
  72. issue1.ID = result.ID
  73. issue1.CreatedTime = result.CreatedTime.UTC()
  74. issue1.UpdatedTime = result.UpdatedTime.UTC()
  75. }
  76. result, err = issues.Insert(ctx, issue2)
  77. assert.NoError(t, err)
  78. assert.Equal(t, "MODELING-1", result.ID)
  79. if result != nil {
  80. issue2.ID = result.ID
  81. issue2.CreatedTime = result.CreatedTime.UTC()
  82. issue2.UpdatedTime = result.UpdatedTime.UTC()
  83. }
  84. result, err = issues.Insert(ctx, issue3)
  85. assert.NoError(t, err)
  86. assert.Equal(t, "MODELING-2", result.ID)
  87. if result != nil {
  88. issue3.ID = result.ID
  89. issue3.CreatedTime = result.CreatedTime.UTC()
  90. issue3.UpdatedTime = result.UpdatedTime.UTC()
  91. issue3Updated.ID = result.ID
  92. issue3Updated.CreatedTime = result.CreatedTime.UTC()
  93. issue3Updated.UpdatedTime = result.UpdatedTime.UTC()
  94. }
  95. result, err = issues.Insert(ctx, issue4)
  96. assert.NoError(t, err)
  97. assert.Equal(t, "DINNER-1", result.ID)
  98. if result != nil {
  99. issue4.ID = result.ID
  100. issue4.CreatedTime = result.CreatedTime.UTC()
  101. issue4.UpdatedTime = result.UpdatedTime.UTC()
  102. }
  103. // FIND
  104. result, err = issues.Find(ctx, issue1.ID)
  105. assert.NoError(t, err)
  106. assert.Equal(t, &issue1, result)
  107. result, err = issues.Find(ctx, issue3.ID)
  108. assert.NoError(t, err)
  109. assert.Equal(t, &issue3, result)
  110. // FIND't
  111. result, err = issues.Find(ctx, "NONEXISTENT-666")
  112. assert.Error(t, err)
  113. assert.Nil(t, result)
  114. assert.True(t, xlerrors.IsNotFound(err))
  115. // LIST
  116. results, err := issues.List(ctx, models.IssueFilter{})
  117. assert.NoError(t, err)
  118. assert.Equal(t, []*models.Issue{&issue4, &issue2, &issue3, &issue1}, results)
  119. results, err = issues.List(ctx, models.IssueFilter{
  120. ProjectIDs: []string{"DINNER", "MODELING"},
  121. })
  122. assert.NoError(t, err)
  123. assert.Equal(t, []*models.Issue{&issue4, &issue2, &issue3}, results)
  124. results, err = issues.List(ctx, models.IssueFilter{
  125. IssueIDs: []string{"MODELING-2", "DINNER-1"},
  126. })
  127. assert.NoError(t, err)
  128. assert.Equal(t, []*models.Issue{&issue4, &issue3}, results)
  129. results, err = issues.List(ctx, models.IssueFilter{
  130. OwnerIDs: []string{"Test"},
  131. })
  132. assert.NoError(t, err)
  133. assert.Equal(t, []*models.Issue{&issue4, &issue2, &issue3}, results)
  134. results, err = issues.List(ctx, models.IssueFilter{
  135. AssigneeIDs: []string{"", "Admin"},
  136. })
  137. assert.NoError(t, err)
  138. assert.Equal(t, []*models.Issue{&issue3, &issue1}, results)
  139. results, err = issues.List(ctx, models.IssueFilter{
  140. MinStage: ptrInt(models.IssueStageActive),
  141. MaxStage: ptrInt(models.IssueStageReview),
  142. })
  143. assert.NoError(t, err)
  144. assert.Equal(t, []*models.Issue{&issue1}, results)
  145. results, err = issues.List(ctx, models.IssueFilter{
  146. Limit: ptrInt(2),
  147. })
  148. assert.NoError(t, err)
  149. assert.Equal(t, []*models.Issue{&issue4, &issue2}, results)
  150. results, err = issues.List(ctx, models.IssueFilter{
  151. OwnerIDs: []string{"Admin"},
  152. AssigneeIDs: []string{""},
  153. Search: ptrString("stuff"),
  154. })
  155. assert.NoError(t, err)
  156. assert.Equal(t, []*models.Issue{&issue1}, results)
  157. // SAVE
  158. issue3Updated.UpdatedTime = time.Now().UTC().Truncate(time.Second)
  159. err = issues.Save(ctx, issue3Updated)
  160. assert.NoError(t, err)
  161. // FIND after SAVE
  162. result, err = issues.Find(ctx, issue3.ID)
  163. assert.NoError(t, err)
  164. assert.Equal(t, &issue3Updated, result)
  165. // DELETE
  166. err = issues.Delete(ctx, issue4)
  167. assert.NoError(t, err)
  168. // LIST after DELETE and SAVE
  169. results, err = issues.List(ctx, models.IssueFilter{})
  170. assert.NoError(t, err)
  171. assert.Equal(t, []*models.Issue{&issue2, &issue3Updated, &issue1}, results)
  172. }