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.

165 lines
4.9 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 projectStatus1 = models.ProjectStatus{
  11. ProjectID: project1.ID,
  12. Stage: models.IssueStagePending,
  13. Name: "Ready",
  14. Description: "This task is ready to be done.",
  15. }
  16. var projectStatus2 = models.ProjectStatus{
  17. ProjectID: project1.ID,
  18. Stage: models.IssueStageInactive,
  19. Name: "Idea",
  20. Description: "Maybe do this?",
  21. }
  22. var projectStatus3 = models.ProjectStatus{
  23. ProjectID: project2.ID,
  24. Stage: models.IssueStageActive,
  25. Name: "In Progress",
  26. Description: "It is being done.",
  27. }
  28. var projectStatus4 = models.ProjectStatus{
  29. ProjectID: project2.ID,
  30. Stage: models.IssueStageReview,
  31. Name: "Rendered",
  32. Description: "It is rendered and awaiting feedback.",
  33. }
  34. var projectStatus5 = models.ProjectStatus{
  35. ProjectID: project3.ID,
  36. Stage: models.IssueStageCompleted,
  37. Name: "Om Nom Nom",
  38. Description: "Sustenance intake complete.",
  39. }
  40. var projectStatus6 = models.ProjectStatus{
  41. ProjectID: project1.ID,
  42. Stage: models.IssueStageFailed,
  43. Name: "Bad Stuff",
  44. Description: "Stuff could not be done.",
  45. }
  46. var projectStatus7 = models.ProjectStatus{
  47. ProjectID: project2.ID,
  48. Stage: models.IssueStagePostponed,
  49. Name: "Too Hard",
  50. Description: "Do it later.",
  51. }
  52. var projectStatus7Updated = models.ProjectStatus{
  53. ProjectID: project2.ID,
  54. Stage: models.IssueStageInactive,
  55. Name: "Too Hard",
  56. Description: "Do it later, when you're less of newbie.",
  57. }
  58. func TestProjectStatusRepository(t *testing.T) {
  59. statuses := testDB.projectStatuses
  60. ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
  61. defer cancel()
  62. assert.NoError(t, clearTable("project_status"))
  63. // INSERT
  64. assert.NoError(t, statuses.Save(ctx, projectStatus1))
  65. assert.NoError(t, statuses.Save(ctx, projectStatus2))
  66. assert.NoError(t, statuses.Save(ctx, projectStatus3))
  67. assert.NoError(t, statuses.Save(ctx, projectStatus4))
  68. assert.NoError(t, statuses.Save(ctx, projectStatus5))
  69. assert.NoError(t, statuses.Save(ctx, projectStatus6))
  70. assert.NoError(t, statuses.Save(ctx, projectStatus7))
  71. // FIND
  72. result, err := statuses.Find(ctx, project2.ID, projectStatus3.Name)
  73. assert.NoError(t, err)
  74. assert.Equal(t, &projectStatus3, result)
  75. result, err = statuses.Find(ctx, project1.ID, projectStatus6.Name)
  76. assert.NoError(t, err)
  77. assert.Equal(t, &projectStatus6, result)
  78. // FINDn't
  79. result, err = statuses.Find(ctx, project3.ID, projectStatus6.Name)
  80. assert.Error(t, err)
  81. assert.True(t, xlerrors.IsNotFound(err))
  82. assert.Nil(t, result)
  83. result, err = statuses.Find(ctx, project2.ID, "Non-existent Name")
  84. assert.Error(t, err)
  85. assert.True(t, xlerrors.IsNotFound(err))
  86. assert.Nil(t, result)
  87. // LIST
  88. results, err := statuses.List(ctx, models.ProjectStatusFilter{})
  89. assert.NoError(t, err)
  90. assert.Equal(t, []*models.ProjectStatus{
  91. &projectStatus5,
  92. &projectStatus3,
  93. &projectStatus4,
  94. &projectStatus7,
  95. &projectStatus2,
  96. &projectStatus1,
  97. &projectStatus6,
  98. }, results)
  99. results, err = statuses.List(ctx, models.ProjectStatusFilter{ProjectID: &project1.ID})
  100. assert.NoError(t, err)
  101. assert.Equal(t, []*models.ProjectStatus{
  102. &projectStatus2,
  103. &projectStatus1,
  104. &projectStatus6,
  105. }, results)
  106. results, err = statuses.List(ctx, models.ProjectStatusFilter{ProjectID: &project2.ID, MinStage: ptrInt(models.IssueStageReview)})
  107. assert.NoError(t, err)
  108. assert.Equal(t, []*models.ProjectStatus{
  109. &projectStatus4,
  110. &projectStatus7,
  111. }, results)
  112. results, err = statuses.List(ctx, models.ProjectStatusFilter{ProjectID: &project2.ID, MaxStage: ptrInt(models.IssueStageReview)})
  113. assert.NoError(t, err)
  114. assert.Equal(t, []*models.ProjectStatus{
  115. &projectStatus3,
  116. &projectStatus4,
  117. }, results)
  118. results, err = statuses.List(ctx, models.ProjectStatusFilter{ProjectID: &project3.ID, MaxStage: ptrInt(models.IssueStagePending)})
  119. assert.NoError(t, err)
  120. assert.Equal(t, []*models.ProjectStatus{}, results)
  121. // UPDATE
  122. assert.NoError(t, statuses.Save(ctx, projectStatus7Updated))
  123. // FIND after UPDATE
  124. result, err = statuses.Find(ctx, project2.ID, projectStatus7.Name)
  125. assert.NoError(t, err)
  126. assert.Equal(t, &projectStatus7Updated, result)
  127. // LIST after UPDATE
  128. results, err = statuses.List(ctx, models.ProjectStatusFilter{})
  129. assert.NoError(t, err)
  130. assert.Equal(t, []*models.ProjectStatus{
  131. &projectStatus5,
  132. &projectStatus7Updated,
  133. &projectStatus3,
  134. &projectStatus4,
  135. &projectStatus2,
  136. &projectStatus1,
  137. &projectStatus6,
  138. }, results)
  139. // DELETE
  140. assert.NoError(t, statuses.Delete(ctx, projectStatus7Updated))
  141. // LIST after DELETE
  142. results, err = statuses.List(ctx, models.ProjectStatusFilter{})
  143. assert.NoError(t, err)
  144. assert.Equal(t, []*models.ProjectStatus{
  145. &projectStatus5,
  146. &projectStatus3,
  147. &projectStatus4,
  148. &projectStatus2,
  149. &projectStatus1,
  150. &projectStatus6,
  151. }, results)
  152. }