diff options
author | Joe Chen <jc@unknwon.io> | 2023-02-08 13:55:54 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-08 13:55:54 +0800 |
commit | 8350daf505b837984397679f07ccc2324b4d2451 (patch) | |
tree | 19d3bd7b8dc8370a8973614a74537bddc5f618a6 /internal/db/follows_test.go | |
parent | 133b9d90441008ee175e1f8e6369e06309e1392a (diff) |
refactor(db): merge relation stores into entity stores (#7341)
Diffstat (limited to 'internal/db/follows_test.go')
-rw-r--r-- | internal/db/follows_test.go | 122 |
1 files changed, 0 insertions, 122 deletions
diff --git a/internal/db/follows_test.go b/internal/db/follows_test.go deleted file mode 100644 index ce71fcaa..00000000 --- a/internal/db/follows_test.go +++ /dev/null @@ -1,122 +0,0 @@ -// Copyright 2022 The Gogs Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package db - -import ( - "context" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "gogs.io/gogs/internal/dbtest" -) - -func TestFollows(t *testing.T) { - if testing.Short() { - t.Skip() - } - t.Parallel() - - tables := []any{new(User), new(EmailAddress), new(Follow)} - db := &follows{ - DB: dbtest.NewDB(t, "follows", tables...), - } - - for _, tc := range []struct { - name string - test func(t *testing.T, db *follows) - }{ - {"Follow", followsFollow}, - {"IsFollowing", followsIsFollowing}, - {"Unfollow", followsUnfollow}, - } { - t.Run(tc.name, func(t *testing.T) { - t.Cleanup(func() { - err := clearTables(t, db.DB, tables...) - require.NoError(t, err) - }) - tc.test(t, db) - }) - if t.Failed() { - break - } - } -} - -func followsFollow(t *testing.T, db *follows) { - ctx := context.Background() - - usersStore := NewUsersStore(db.DB) - alice, err := usersStore.Create(ctx, "alice", "alice@example.com", CreateUserOptions{}) - require.NoError(t, err) - bob, err := usersStore.Create(ctx, "bob", "bob@example.com", CreateUserOptions{}) - require.NoError(t, err) - - err = db.Follow(ctx, alice.ID, bob.ID) - require.NoError(t, err) - - // It is OK to follow multiple times and just be noop. - err = db.Follow(ctx, alice.ID, bob.ID) - require.NoError(t, err) - - alice, err = usersStore.GetByID(ctx, alice.ID) - require.NoError(t, err) - assert.Equal(t, 1, alice.NumFollowing) - - bob, err = usersStore.GetByID(ctx, bob.ID) - require.NoError(t, err) - assert.Equal(t, 1, bob.NumFollowers) -} - -func followsIsFollowing(t *testing.T, db *follows) { - ctx := context.Background() - - usersStore := NewUsersStore(db.DB) - alice, err := usersStore.Create(ctx, "alice", "alice@example.com", CreateUserOptions{}) - require.NoError(t, err) - bob, err := usersStore.Create(ctx, "bob", "bob@example.com", CreateUserOptions{}) - require.NoError(t, err) - - got := db.IsFollowing(ctx, alice.ID, bob.ID) - assert.False(t, got) - - err = db.Follow(ctx, alice.ID, bob.ID) - require.NoError(t, err) - got = db.IsFollowing(ctx, alice.ID, bob.ID) - assert.True(t, got) - - err = db.Unfollow(ctx, alice.ID, bob.ID) - require.NoError(t, err) - got = db.IsFollowing(ctx, alice.ID, bob.ID) - assert.False(t, got) -} - -func followsUnfollow(t *testing.T, db *follows) { - ctx := context.Background() - - usersStore := NewUsersStore(db.DB) - alice, err := usersStore.Create(ctx, "alice", "alice@example.com", CreateUserOptions{}) - require.NoError(t, err) - bob, err := usersStore.Create(ctx, "bob", "bob@example.com", CreateUserOptions{}) - require.NoError(t, err) - - err = db.Follow(ctx, alice.ID, bob.ID) - require.NoError(t, err) - - // It is OK to unfollow multiple times and just be noop. - err = db.Unfollow(ctx, alice.ID, bob.ID) - require.NoError(t, err) - err = db.Unfollow(ctx, alice.ID, bob.ID) - require.NoError(t, err) - - alice, err = usersStore.GetByID(ctx, alice.ID) - require.NoError(t, err) - assert.Equal(t, 0, alice.NumFollowing) - - bob, err = usersStore.GetByID(ctx, bob.ID) - require.NoError(t, err) - assert.Equal(t, 0, bob.NumFollowers) -} |