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.3 KiB

  1. package mysqldriver
  2. import (
  3. "context"
  4. "git.aiterp.net/stufflog/server/internal/slerrors"
  5. "git.aiterp.net/stufflog/server/models"
  6. "github.com/stretchr/testify/assert"
  7. "testing"
  8. "time"
  9. )
  10. var project1 = models.Project{
  11. ID: "STUFF",
  12. Name: "Stuff and things",
  13. Description: "Items and artifacts",
  14. DailyPoints: 200,
  15. }
  16. var project2 = models.Project{
  17. ID: "MODELING",
  18. Name: "3D Modelling",
  19. Description: "Making stuff.",
  20. DailyPoints: 250,
  21. }
  22. var project3 = models.Project{
  23. ID: "DINNER",
  24. Name: "Dinner",
  25. Description: "Shopping lists and meals.",
  26. DailyPoints: 0,
  27. }
  28. var projectSearch1 = "stuff"
  29. func TestProjectRepository(t *testing.T) {
  30. projects := testDB.projects
  31. ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
  32. defer cancel()
  33. assert.NoError(t, clearTable("project"))
  34. // INSERT
  35. result, err := projects.Insert(ctx, project1)
  36. assert.NoError(t, err)
  37. assert.Equal(t, project1, *result)
  38. result, err = projects.Insert(ctx, project2)
  39. assert.NoError(t, err)
  40. assert.Equal(t, project2, *result)
  41. result, err = projects.Insert(ctx, project3)
  42. assert.NoError(t, err)
  43. assert.Equal(t, project3, *result)
  44. if t.Failed() {
  45. return
  46. }
  47. // FIND
  48. result, err = projects.Find(ctx, "MODELING")
  49. assert.NoError(t, err)
  50. assert.Equal(t, &project2, result)
  51. // FIND't
  52. result, err = projects.Find(ctx, "BLARGH")
  53. assert.Error(t, err)
  54. assert.True(t, slerrors.IsNotFound(err))
  55. assert.Nil(t, result)
  56. // LIST
  57. results, err := projects.List(ctx, models.ProjectFilter{})
  58. assert.NoError(t, err)
  59. assert.Equal(t, []*models.Project{&project3, &project2, &project1}, results)
  60. results, err = projects.List(ctx, models.ProjectFilter{
  61. Search: &projectSearch1,
  62. })
  63. assert.NoError(t, err)
  64. assert.Equal(t, []*models.Project{&project2, &project1}, results)
  65. results, err = projects.List(ctx, models.ProjectFilter{
  66. ProjectIDs: []string{"DINNER", "MODELING"},
  67. })
  68. assert.NoError(t, err)
  69. assert.Equal(t, []*models.Project{&project3, &project2}, results)
  70. if t.Failed() {
  71. return
  72. }
  73. // UPDATE
  74. project2fix := project2
  75. project2fix.Name = "3D Modeling"
  76. project2fix.Description = "Modeling 3D stuff."
  77. project2fix.DailyPoints = 150
  78. err = projects.Save(ctx, project2fix)
  79. assert.NoError(t, err)
  80. if t.Failed() {
  81. return
  82. }
  83. // FIND after UPDATE
  84. result, err = projects.Find(ctx, "MODELING")
  85. assert.NoError(t, err)
  86. assert.Equal(t, &project2fix, result)
  87. // DELETE
  88. err = projects.Delete(ctx, project3)
  89. assert.NoError(t, err)
  90. if t.Failed() {
  91. return
  92. }
  93. // LIST after DELETE + UPDATE
  94. results, err = projects.List(ctx, models.ProjectFilter{})
  95. assert.NoError(t, err)
  96. assert.Equal(t, []*models.Project{&project2fix, &project1}, results)
  97. // SET PERMISSION
  98. err = projects.SetPermission(ctx, models.ProjectPermission{
  99. ProjectID: project1.ID,
  100. UserID: user1.ID,
  101. Level: models.ProjectPermissionLevelOwner,
  102. })
  103. assert.NoError(t, err)
  104. err = projects.SetPermission(ctx, models.ProjectPermission{
  105. ProjectID: project2fix.ID,
  106. UserID: user1.ID,
  107. Level: models.ProjectPermissionLevelMember,
  108. })
  109. assert.NoError(t, err)
  110. // GET PERMISSIONS
  111. permission, err := projects.GetPermission(ctx, project1, user1)
  112. assert.NoError(t, err)
  113. assert.Equal(t, project1.ID, permission.ProjectID)
  114. assert.Equal(t, user1.ID, permission.UserID)
  115. assert.Equal(t, models.ProjectPermissionLevelOwner, permission.Level)
  116. permission, err = projects.GetPermission(ctx, project2fix, user1)
  117. assert.NoError(t, err)
  118. assert.Equal(t, models.ProjectPermissionLevelMember, permission.Level)
  119. // GET PERMISSION (default)
  120. permission, err = projects.GetPermission(ctx, project1, user2)
  121. assert.NoError(t, err)
  122. assert.Equal(t, project1.ID, permission.ProjectID)
  123. assert.Equal(t, user2.ID, permission.UserID)
  124. assert.Equal(t, models.ProjectPermissionLevelNoAccess, permission.Level)
  125. // LIST after SET PERMISSION
  126. results, err = projects.List(ctx, models.ProjectFilter{
  127. Permission: &models.ProjectFilterPermission{
  128. UserID: user1.ID,
  129. MinLevel: models.ProjectPermissionLevelMember,
  130. },
  131. })
  132. assert.NoError(t, err)
  133. assert.Equal(t, []*models.Project{&project2fix, &project1}, results)
  134. results, err = projects.List(ctx, models.ProjectFilter{
  135. Permission: &models.ProjectFilterPermission{
  136. UserID: user2.ID,
  137. MinLevel: models.ProjectPermissionLevelMember,
  138. },
  139. })
  140. assert.NoError(t, err)
  141. assert.Equal(t, []*models.Project{}, results)
  142. }