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.

117 lines
2.9 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 user1 = models.User{
  11. ID: "Test",
  12. Name: "Testy Tester",
  13. Active: true,
  14. Admin: false,
  15. Hash: []byte("$2y$12$tj/R/zDHrGy1Jsi57DUuSeCISYvEHb/F37p.9HGlyf72cIXnppeQK"),
  16. }
  17. var user2 = models.User{
  18. ID: "Admin",
  19. Name: "Administrator",
  20. Active: true,
  21. Admin: true,
  22. Hash: []byte("$2y$12$tj/R/zDHrGy1Jsi57DUuSeCISYvEHb/F37p.9HGlyf72cIXnppeQK"),
  23. }
  24. var user2Updated = models.User{
  25. ID: user2.ID,
  26. Name: "Dethroned Dictator",
  27. Active: false,
  28. Admin: false,
  29. Hash: []byte("$2y$12$7MuqYzV59HCtHJlRJCd/vOQIrFcMEMVhyySzJX.WlVtEJH3qHVPU2\n"),
  30. }
  31. func TestUserRepository(t *testing.T) {
  32. users := testDB.users
  33. ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
  34. defer cancel()
  35. assert.NoError(t, clearTable("user"))
  36. // INSERT
  37. result, err := users.Insert(ctx, user1)
  38. assert.NoError(t, err)
  39. assert.Equal(t, user1, *result)
  40. result, err = users.Insert(ctx, user2)
  41. assert.NoError(t, err)
  42. assert.Equal(t, user2, *result)
  43. // FIND
  44. result, err = users.Find(ctx, user1.ID)
  45. assert.NoError(t, err)
  46. assert.Equal(t, &user1, result)
  47. result, err = users.Find(ctx, user2.ID)
  48. assert.NoError(t, err)
  49. assert.Equal(t, &user2, result)
  50. // FIND't
  51. result, err = users.Find(ctx, "NonExistent")
  52. assert.Error(t, err)
  53. assert.True(t, slerrors.IsNotFound(err))
  54. assert.Nil(t, result)
  55. // LIST
  56. results, err := users.List(ctx, models.UserFilter{})
  57. assert.NoError(t, err)
  58. assert.Equal(t, []*models.User{&user2, &user1}, results)
  59. results, err = users.List(ctx, models.UserFilter{
  60. Admin: ptrBool(true),
  61. })
  62. assert.NoError(t, err)
  63. assert.Equal(t, []*models.User{&user2}, results)
  64. results, err = users.List(ctx, models.UserFilter{
  65. Active: ptrBool(true),
  66. })
  67. assert.NoError(t, err)
  68. assert.Equal(t, []*models.User{&user2, &user1}, results)
  69. results, err = users.List(ctx, models.UserFilter{
  70. Active: ptrBool(false),
  71. })
  72. assert.NoError(t, err)
  73. assert.Equal(t, []*models.User{}, results)
  74. results, err = users.List(ctx, models.UserFilter{
  75. UserIDs: []string{user1.ID},
  76. })
  77. assert.NoError(t, err)
  78. assert.Equal(t, []*models.User{&user1}, results)
  79. results, err = users.List(ctx, models.UserFilter{
  80. Limit: ptrInt(1),
  81. })
  82. assert.NoError(t, err)
  83. assert.Equal(t, []*models.User{&user2}, results)
  84. // UPDATE
  85. err = users.Save(ctx, user2Updated)
  86. assert.NoError(t, err)
  87. // LIST after UPDATE
  88. results, err = users.List(ctx, models.UserFilter{})
  89. assert.NoError(t, err)
  90. assert.Equal(t, []*models.User{&user2Updated, &user1}, results)
  91. // DELETE
  92. err = users.Delete(ctx, user1)
  93. assert.NoError(t, err)
  94. // LIST after DELETE
  95. results, err = users.List(ctx, models.UserFilter{})
  96. assert.NoError(t, err)
  97. assert.Equal(t, []*models.User{&user2Updated}, results)
  98. // INSERT after DELETE
  99. result, err = users.Insert(ctx, user1)
  100. assert.NoError(t, err)
  101. assert.Equal(t, user1, *result)
  102. }