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.

80 lines
2.3 KiB

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 TestUserRepository(t *testing.T) {
  9. if dbErr != nil {
  10. t.Fatal(dbErr)
  11. }
  12. ctx := context.Background()
  13. testUser1 := models.User{
  14. ID: "U1234",
  15. Name: "Stuffer",
  16. }
  17. testUser2 := models.User{
  18. ID: "U4321",
  19. Name: "Logger",
  20. }
  21. testUser3 := models.User{
  22. ID: "U2396",
  23. Name: "Temper",
  24. }
  25. testUsers := []*models.User{
  26. &testUser1,
  27. &testUser3,
  28. &testUser2,
  29. }
  30. t.Run("Save", func(t *testing.T) {
  31. assert.NoError(t, db.Users().Save(ctx, testUser1), "Save testUser1")
  32. assert.NoError(t, db.Users().Save(ctx, testUser2), "Save testUser2")
  33. assert.NoError(t, db.Users().Save(ctx, testUser3), "Save testUser3")
  34. })
  35. t.Run("List", func(t *testing.T) {
  36. users, err := db.Users().List(ctx)
  37. assert.Equal(t, testUsers, users, "List 3 users")
  38. assert.NoError(t, err, "List 3 users")
  39. })
  40. t.Run("Find", func(t *testing.T) {
  41. user, err := db.Users().FindID(ctx, testUser1.ID)
  42. assert.Equal(t, &testUser1, user, "Find testUser1")
  43. assert.NoError(t, err, "Find testUser1")
  44. user, err = db.Users().FindID(ctx, testUser2.ID)
  45. assert.Equal(t, &testUser2, user, "Find testUser2")
  46. assert.NoError(t, err, "Find testUser2")
  47. user, err = db.Users().FindID(ctx, testUser3.ID)
  48. assert.Equal(t, &testUser3, user, "Find testUser3")
  49. assert.NoError(t, err, "Find testUser3")
  50. user, err = db.Users().FindID(ctx, "UNonExistent")
  51. assert.Nil(t, user, "Find UNonExistent")
  52. assert.Error(t, err, "Find UNonExistent")
  53. })
  54. t.Run("Remove", func(t *testing.T) {
  55. assert.NoError(t, db.Users().Remove(ctx, testUser3), "Remove testUser3")
  56. user, err := db.Users().FindID(ctx, testUser1.ID)
  57. assert.Equal(t, &testUser1, user, "Find non-deleted testUser1")
  58. assert.NoError(t, err, "Find non-deleted testUser1")
  59. user, err = db.Users().FindID(ctx, testUser2.ID)
  60. assert.Equal(t, &testUser2, user, "Find non-deleted testUser2")
  61. assert.NoError(t, err, "Find non-deleted testUser2")
  62. user, err = db.Users().FindID(ctx, testUser3.ID)
  63. assert.Nil(t, user, "Find deleted testUser3")
  64. assert.Error(t, err, "Find deleted testUser3")
  65. users, err := db.Users().List(ctx)
  66. assert.Equal(t, []*models.User{&testUser1, &testUser2}, users, "List 2 remaining users")
  67. assert.NoError(t, err, "List 2 remaining users")
  68. })
  69. }