Plan stuff. Log stuff.
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.

123 lines
4.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package bolt
  2. import (
  3. "context"
  4. "github.com/gisle/stufflog/models"
  5. "github.com/stretchr/testify/assert"
  6. "testing"
  7. )
  8. func TestActivityRepository(t *testing.T) {
  9. if dbErr != nil {
  10. t.Fatal(dbErr)
  11. }
  12. strPtr := func(s string) *string { return &s }
  13. ctx := context.Background()
  14. testUser1 := models.User{
  15. ID: "U1234",
  16. Name: "Stuffer",
  17. }
  18. testUser2 := models.User{
  19. ID: "U4321",
  20. Name: "Logger",
  21. }
  22. testActivity1 := models.Activity{
  23. ID: "A01",
  24. Name: "3D Modelling",
  25. UserID: testUser1.ID,
  26. Icon: "cube",
  27. SubActivities: []models.SubActivity{
  28. {ID: "S1001", Name: "Practice", UnitName: "minute", Value: 15},
  29. {ID: "S1002", Name: "Tutorial", UnitName: "minute", Value: 20},
  30. {ID: "S1003", Name: "Scene", UnitName: "minute", Value: 30},
  31. },
  32. }
  33. testActivity2 := models.Activity{
  34. ID: "A02",
  35. Name: "Roleplay",
  36. UserID: testUser2.ID,
  37. Icon: "dice-d20",
  38. SubActivities: []models.SubActivity{
  39. {ID: "S2001", Name: "Writing", UnitName: "word", Value: 1},
  40. {ID: "S2002", Name: "Planning", UnitName: "word", Value: 2},
  41. {ID: "S2003", Name: "Publish", UnitName: "word", Value: 1},
  42. {ID: "S2003", Name: "Write & Publish", UnitName: "word", Value: 3},
  43. {ID: "S2004", Name: "IRC RP", UnitName: "minute", Value: 6.6},
  44. },
  45. }
  46. testActivity3 := models.Activity{
  47. ID: "A03",
  48. Name: "Coding",
  49. UserID: testUser1.ID,
  50. Icon: "code",
  51. SubActivities: []models.SubActivity{
  52. {ID: "S3001", Name: "Rust NameGen", UnitName: "minute", Value: 20},
  53. {ID: "S3002", Name: "Learn GoLang", UnitName: "minute", Value: 25},
  54. },
  55. }
  56. testActivities := []*models.Activity{&testActivity1, &testActivity2, &testActivity3}
  57. testActivity3Update := models.ActivityUpdate{
  58. SetName: strPtr("Programming"),
  59. AddSub: &models.SubActivity{Name: "Website Rewrite", UnitName: "minute", Value: 15},
  60. EditSub: &models.SubActivity{ID: "S3001", Name: "Rust SetName Generator", UnitName: "minute", Value: 22},
  61. RemoveSub: strPtr("S3002"),
  62. }
  63. testActivity3Updated := models.Activity{
  64. ID: "A03",
  65. Name: "Programming",
  66. UserID: testUser1.ID,
  67. Icon: "code",
  68. SubActivities: []models.SubActivity{
  69. {ID: "S3001", Name: "Rust SetName Generator", UnitName: "minute", Value: 22},
  70. {Name: "Website Rewrite", UnitName: "minute", Value: 15},
  71. },
  72. }
  73. t.Run("Insert", func(t *testing.T) {
  74. assert.NoError(t, db.Activities().Insert(ctx, testActivity1), "Save testActivity1")
  75. assert.NoError(t, db.Activities().Insert(ctx, testActivity2), "Save testActivity2")
  76. assert.NoError(t, db.Activities().Insert(ctx, testActivity3), "Save testActivity3")
  77. })
  78. t.Run("Find", func(t *testing.T) {
  79. activity, err := db.Activities().FindID(ctx, testActivity1.ID)
  80. assert.NoError(t, err, "Find testActivity1")
  81. assert.Equal(t, &testActivity1, activity, "Find testActivity1")
  82. activity, err = db.Activities().FindID(ctx, testActivity2.ID)
  83. assert.NoError(t, err, "Find testActivity2")
  84. assert.Equal(t, &testActivity2, activity, "Find testActivity2")
  85. activity, err = db.Activities().FindID(ctx, testActivity3.ID)
  86. assert.NoError(t, err, "Find testActivity3")
  87. assert.Equal(t, &testActivity3, activity, "Find testActivity3")
  88. })
  89. t.Run("List", func(t *testing.T) {
  90. activities, err := db.Activities().List(ctx)
  91. assert.Equal(t, testActivities, activities, "List 3 activities")
  92. assert.NoError(t, err, "List 3 activities")
  93. activities, err = db.Activities().ListUser(ctx, testUser1)
  94. assert.Equal(t, []*models.Activity{&testActivity1, &testActivity3}, activities, "List 2 activities for testUser1")
  95. assert.NoError(t, err, "List 2 activities for testUser1")
  96. })
  97. t.Run("Update", func(t *testing.T) {
  98. activity, err := db.Activities().Update(ctx, testActivity3, []*models.ActivityUpdate{&testActivity3Update})
  99. if activity != nil && len(activity.SubActivities) > 1 {
  100. testActivity3Updated.SubActivities[1].ID = activity.SubActivities[1].ID
  101. }
  102. assert.Equal(t, &testActivity3Updated, activity, "Update activity")
  103. assert.NoError(t, err, "Update activity")
  104. })
  105. t.Run("Remove", func(t *testing.T) {
  106. assert.NoError(t, db.Activities().Remove(ctx, testActivity2), "Delete testActivity2")
  107. activities, err := db.Activities().List(ctx)
  108. assert.Equal(t, []*models.Activity{&testActivity1, &testActivity3Updated}, activities, "List 2 remaining activities")
  109. assert.NoError(t, err, "List 2 remaining activities")
  110. })
  111. }