follows_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2022 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package db
  5. import (
  6. "context"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. "gogs.io/gogs/internal/dbtest"
  11. )
  12. func TestFollows(t *testing.T) {
  13. if testing.Short() {
  14. t.Skip()
  15. }
  16. t.Parallel()
  17. tables := []interface{}{new(User), new(EmailAddress), new(Follow)}
  18. db := &follows{
  19. DB: dbtest.NewDB(t, "follows", tables...),
  20. }
  21. for _, tc := range []struct {
  22. name string
  23. test func(t *testing.T, db *follows)
  24. }{
  25. {"Follow", followsFollow},
  26. {"IsFollowing", followsIsFollowing},
  27. {"Unfollow", followsUnfollow},
  28. } {
  29. t.Run(tc.name, func(t *testing.T) {
  30. t.Cleanup(func() {
  31. err := clearTables(t, db.DB, tables...)
  32. require.NoError(t, err)
  33. })
  34. tc.test(t, db)
  35. })
  36. if t.Failed() {
  37. break
  38. }
  39. }
  40. }
  41. func followsFollow(t *testing.T, db *follows) {
  42. ctx := context.Background()
  43. usersStore := NewUsersStore(db.DB)
  44. alice, err := usersStore.Create(ctx, "alice", "[email protected]", CreateUserOptions{})
  45. require.NoError(t, err)
  46. bob, err := usersStore.Create(ctx, "bob", "[email protected]", CreateUserOptions{})
  47. require.NoError(t, err)
  48. err = db.Follow(ctx, alice.ID, bob.ID)
  49. require.NoError(t, err)
  50. // It is OK to follow multiple times and just be noop.
  51. err = db.Follow(ctx, alice.ID, bob.ID)
  52. require.NoError(t, err)
  53. alice, err = usersStore.GetByID(ctx, alice.ID)
  54. require.NoError(t, err)
  55. assert.Equal(t, 1, alice.NumFollowing)
  56. bob, err = usersStore.GetByID(ctx, bob.ID)
  57. require.NoError(t, err)
  58. assert.Equal(t, 1, bob.NumFollowers)
  59. }
  60. func followsIsFollowing(t *testing.T, db *follows) {
  61. ctx := context.Background()
  62. usersStore := NewUsersStore(db.DB)
  63. alice, err := usersStore.Create(ctx, "alice", "[email protected]", CreateUserOptions{})
  64. require.NoError(t, err)
  65. bob, err := usersStore.Create(ctx, "bob", "[email protected]", CreateUserOptions{})
  66. require.NoError(t, err)
  67. got := db.IsFollowing(ctx, alice.ID, bob.ID)
  68. assert.False(t, got)
  69. err = db.Follow(ctx, alice.ID, bob.ID)
  70. require.NoError(t, err)
  71. got = db.IsFollowing(ctx, alice.ID, bob.ID)
  72. assert.True(t, got)
  73. err = db.Unfollow(ctx, alice.ID, bob.ID)
  74. require.NoError(t, err)
  75. got = db.IsFollowing(ctx, alice.ID, bob.ID)
  76. assert.False(t, got)
  77. }
  78. func followsUnfollow(t *testing.T, db *follows) {
  79. ctx := context.Background()
  80. usersStore := NewUsersStore(db.DB)
  81. alice, err := usersStore.Create(ctx, "alice", "[email protected]", CreateUserOptions{})
  82. require.NoError(t, err)
  83. bob, err := usersStore.Create(ctx, "bob", "[email protected]", CreateUserOptions{})
  84. require.NoError(t, err)
  85. err = db.Follow(ctx, alice.ID, bob.ID)
  86. require.NoError(t, err)
  87. // It is OK to unfollow multiple times and just be noop.
  88. err = db.Unfollow(ctx, alice.ID, bob.ID)
  89. require.NoError(t, err)
  90. err = db.Unfollow(ctx, alice.ID, bob.ID)
  91. require.NoError(t, err)
  92. alice, err = usersStore.GetByID(ctx, alice.ID)
  93. require.NoError(t, err)
  94. assert.Equal(t, 0, alice.NumFollowing)
  95. bob, err = usersStore.GetByID(ctx, bob.ID)
  96. require.NoError(t, err)
  97. assert.Equal(t, 0, bob.NumFollowers)
  98. }