diff options
author | 2022-10-08 13:50:48 +0200 | |
---|---|---|
committer | 2022-10-08 13:50:48 +0200 | |
commit | aa07750bdb4dacdb1be39d765114915bba3fc29f (patch) | |
tree | 30e9e5052f607f8c8e4f7d518559df8706275e0f /internal/db/bundb/relationship_test.go | |
parent | [performance] cache domains after max retries in transport (#884) (diff) | |
download | gotosocial-aa07750bdb4dacdb1be39d765114915bba3fc29f.tar.xz |
[chore] Standardize database queries, use `bun.Ident()` properly (#886)
* use bun.Ident for user queries
* use bun.Ident for account queries
* use bun.Ident for media queries
* add DeleteAccount func
* remove CaseInsensitive in Where+use Ident ipv Safe
* update admin db
* update domain, use ident
* update emoji, use ident
* update instance queries, use bun.Ident
* fix media
* update mentions, use bun ident
* update relationship + tests
* use tableexpr
* add test follows to bun db test suite
* update notifications
* updatebyprimarykey => updatebyid
* fix session
* prefer explicit ID to pk
* fix little fucky wucky
* remove workaround
* use proper db func for attachment selection
* update status db
* add m2m entries in test rig
* fix up timeline
* go fmt
* fix status put issue
* update GetAccountStatuses
Diffstat (limited to 'internal/db/bundb/relationship_test.go')
-rw-r--r-- | internal/db/bundb/relationship_test.go | 249 |
1 files changed, 215 insertions, 34 deletions
diff --git a/internal/db/bundb/relationship_test.go b/internal/db/bundb/relationship_test.go index 34fe85a57..3df16e2f3 100644 --- a/internal/db/bundb/relationship_test.go +++ b/internal/db/bundb/relationship_test.go @@ -20,7 +20,6 @@ package bundb_test import ( "context" - "errors" "testing" "github.com/stretchr/testify/suite" @@ -48,12 +47,14 @@ func (suite *RelationshipTestSuite) TestIsBlocked() { suite.False(blocked) // have account1 block account2 - suite.db.Put(ctx, >smodel.Block{ + if err := suite.db.Put(ctx, >smodel.Block{ ID: "01G202BCSXXJZ70BHB5KCAHH8C", URI: "http://localhost:8080/some_block_uri_1", AccountID: account1, TargetAccountID: account2, - }) + }); err != nil { + suite.FailNow(err.Error()) + } // account 1 now blocks account 2 blocked, err = suite.db.IsBlocked(ctx, account1, account2, false) @@ -75,62 +76,242 @@ func (suite *RelationshipTestSuite) TestIsBlocked() { } func (suite *RelationshipTestSuite) TestGetBlock() { - suite.Suite.T().Skip("TODO: implement") + ctx := context.Background() + + account1 := suite.testAccounts["local_account_1"].ID + account2 := suite.testAccounts["local_account_2"].ID + + if err := suite.db.Put(ctx, >smodel.Block{ + ID: "01G202BCSXXJZ70BHB5KCAHH8C", + URI: "http://localhost:8080/some_block_uri_1", + AccountID: account1, + TargetAccountID: account2, + }); err != nil { + suite.FailNow(err.Error()) + } + + block, err := suite.db.GetBlock(ctx, account1, account2) + suite.NoError(err) + suite.NotNil(block) + suite.Equal("01G202BCSXXJZ70BHB5KCAHH8C", block.ID) } func (suite *RelationshipTestSuite) TestGetRelationship() { - suite.Suite.T().Skip("TODO: implement") + requestingAccount := suite.testAccounts["local_account_1"] + targetAccount := suite.testAccounts["admin_account"] + + relationship, err := suite.db.GetRelationship(context.Background(), requestingAccount.ID, targetAccount.ID) + suite.NoError(err) + suite.NotNil(relationship) + + suite.True(relationship.Following) + suite.True(relationship.ShowingReblogs) + suite.False(relationship.Notifying) + suite.True(relationship.FollowedBy) + suite.False(relationship.Blocking) + suite.False(relationship.BlockedBy) + suite.False(relationship.Muting) + suite.False(relationship.MutingNotifications) + suite.False(relationship.Requested) + suite.False(relationship.DomainBlocking) + suite.False(relationship.Endorsed) + suite.Empty(relationship.Note) +} + +func (suite *RelationshipTestSuite) TestIsFollowingYes() { + requestingAccount := suite.testAccounts["local_account_1"] + targetAccount := suite.testAccounts["admin_account"] + isFollowing, err := suite.db.IsFollowing(context.Background(), requestingAccount, targetAccount) + suite.NoError(err) + suite.True(isFollowing) } -func (suite *RelationshipTestSuite) TestIsFollowing() { - suite.Suite.T().Skip("TODO: implement") +func (suite *RelationshipTestSuite) TestIsFollowingNo() { + requestingAccount := suite.testAccounts["admin_account"] + targetAccount := suite.testAccounts["local_account_2"] + isFollowing, err := suite.db.IsFollowing(context.Background(), requestingAccount, targetAccount) + suite.NoError(err) + suite.False(isFollowing) } func (suite *RelationshipTestSuite) TestIsMutualFollowing() { - suite.Suite.T().Skip("TODO: implement") + requestingAccount := suite.testAccounts["local_account_1"] + targetAccount := suite.testAccounts["admin_account"] + isMutualFollowing, err := suite.db.IsMutualFollowing(context.Background(), requestingAccount, targetAccount) + suite.NoError(err) + suite.True(isMutualFollowing) +} + +func (suite *RelationshipTestSuite) TestIsMutualFollowingNo() { + requestingAccount := suite.testAccounts["local_account_1"] + targetAccount := suite.testAccounts["local_account_2"] + isMutualFollowing, err := suite.db.IsMutualFollowing(context.Background(), requestingAccount, targetAccount) + suite.NoError(err) + suite.True(isMutualFollowing) } -func (suite *RelationshipTestSuite) AcceptFollowRequest() { - for _, account := range suite.testAccounts { - _, err := suite.db.AcceptFollowRequest(context.Background(), account.ID, "NON-EXISTENT-ID") - if err != nil && !errors.Is(err, db.ErrNoEntries) { - suite.Suite.Fail("error accepting follow request: %v", err) - } +func (suite *RelationshipTestSuite) TestAcceptFollowRequestOK() { + ctx := context.Background() + account := suite.testAccounts["admin_account"] + targetAccount := suite.testAccounts["local_account_2"] + + followRequest := >smodel.FollowRequest{ + ID: "01GEF753FWHCHRDWR0QEHBXM8W", + URI: "http://localhost:8080/weeeeeeeeeeeeeeeee", + AccountID: account.ID, + TargetAccountID: targetAccount.ID, } + + if err := suite.db.Put(ctx, followRequest); err != nil { + suite.FailNow(err.Error()) + } + + follow, err := suite.db.AcceptFollowRequest(ctx, account.ID, targetAccount.ID) + suite.NoError(err) + suite.NotNil(follow) + suite.Equal(followRequest.URI, follow.URI) } -func (suite *RelationshipTestSuite) GetAccountFollowRequests() { - suite.Suite.T().Skip("TODO: implement") +func (suite *RelationshipTestSuite) TestAcceptFollowRequestNotExisting() { + ctx := context.Background() + account := suite.testAccounts["admin_account"] + targetAccount := suite.testAccounts["local_account_2"] + + follow, err := suite.db.AcceptFollowRequest(ctx, account.ID, targetAccount.ID) + suite.ErrorIs(err, db.ErrNoEntries) + suite.Nil(follow) } -func (suite *RelationshipTestSuite) GetAccountFollows() { - suite.Suite.T().Skip("TODO: implement") +func (suite *RelationshipTestSuite) TestAcceptFollowRequestFollowAlreadyExists() { + ctx := context.Background() + account := suite.testAccounts["local_account_1"] + targetAccount := suite.testAccounts["admin_account"] + + // follow already exists in the db from local_account_1 -> admin_account + existingFollow := >smodel.Follow{} + if err := suite.db.GetByID(ctx, suite.testFollows["local_account_1_admin_account"].ID, existingFollow); err != nil { + suite.FailNow(err.Error()) + } + + followRequest := >smodel.FollowRequest{ + ID: "01GEF753FWHCHRDWR0QEHBXM8W", + URI: "http://localhost:8080/weeeeeeeeeeeeeeeee", + AccountID: account.ID, + TargetAccountID: targetAccount.ID, + } + + if err := suite.db.Put(ctx, followRequest); err != nil { + suite.FailNow(err.Error()) + } + + follow, err := suite.db.AcceptFollowRequest(ctx, account.ID, targetAccount.ID) + suite.NoError(err) + suite.NotNil(follow) + + // uri should be equal to value of new/overlapping follow request + suite.NotEqual(followRequest.URI, existingFollow.URI) + suite.Equal(followRequest.URI, follow.URI) } -func (suite *RelationshipTestSuite) CountAccountFollows() { - suite.Suite.T().Skip("TODO: implement") +func (suite *RelationshipTestSuite) TestRejectFollowRequestOK() { + ctx := context.Background() + account := suite.testAccounts["admin_account"] + targetAccount := suite.testAccounts["local_account_2"] + + followRequest := >smodel.FollowRequest{ + ID: "01GEF753FWHCHRDWR0QEHBXM8W", + URI: "http://localhost:8080/weeeeeeeeeeeeeeeee", + AccountID: account.ID, + TargetAccountID: targetAccount.ID, + } + + if err := suite.db.Put(ctx, followRequest); err != nil { + suite.FailNow(err.Error()) + } + + rejectedFollowRequest, err := suite.db.RejectFollowRequest(ctx, account.ID, targetAccount.ID) + suite.NoError(err) + suite.NotNil(rejectedFollowRequest) } -func (suite *RelationshipTestSuite) GetAccountFollowedBy() { - // TODO: more comprehensive tests here +func (suite *RelationshipTestSuite) TestRejectFollowRequestNotExisting() { + ctx := context.Background() + account := suite.testAccounts["admin_account"] + targetAccount := suite.testAccounts["local_account_2"] - for _, account := range suite.testAccounts { - var err error + rejectedFollowRequest, err := suite.db.RejectFollowRequest(ctx, account.ID, targetAccount.ID) + suite.ErrorIs(err, db.ErrNoEntries) + suite.Nil(rejectedFollowRequest) +} - _, err = suite.db.GetAccountFollowedBy(context.Background(), account.ID, false) - if err != nil { - suite.Suite.Fail("error checking accounts followed by: %v", err) - } +func (suite *RelationshipTestSuite) TestGetAccountFollowRequests() { + ctx := context.Background() + account := suite.testAccounts["admin_account"] + targetAccount := suite.testAccounts["local_account_2"] - _, err = suite.db.GetAccountFollowedBy(context.Background(), account.ID, true) - if err != nil { - suite.Suite.Fail("error checking localOnly accounts followed by: %v", err) - } + followRequest := >smodel.FollowRequest{ + ID: "01GEF753FWHCHRDWR0QEHBXM8W", + URI: "http://localhost:8080/weeeeeeeeeeeeeeeee", + AccountID: account.ID, + TargetAccountID: targetAccount.ID, } + + if err := suite.db.Put(ctx, followRequest); err != nil { + suite.FailNow(err.Error()) + } + + followRequests, err := suite.db.GetAccountFollowRequests(ctx, targetAccount.ID) + suite.NoError(err) + suite.Len(followRequests, 1) } -func (suite *RelationshipTestSuite) CountAccountFollowedBy() { - suite.Suite.T().Skip("TODO: implement") +func (suite *RelationshipTestSuite) TestGetAccountFollows() { + account := suite.testAccounts["local_account_1"] + follows, err := suite.db.GetAccountFollows(context.Background(), account.ID) + suite.NoError(err) + suite.Len(follows, 2) +} + +func (suite *RelationshipTestSuite) TestCountAccountFollowsLocalOnly() { + account := suite.testAccounts["local_account_1"] + followsCount, err := suite.db.CountAccountFollows(context.Background(), account.ID, true) + suite.NoError(err) + suite.Equal(2, followsCount) +} + +func (suite *RelationshipTestSuite) TestCountAccountFollows() { + account := suite.testAccounts["local_account_1"] + followsCount, err := suite.db.CountAccountFollows(context.Background(), account.ID, false) + suite.NoError(err) + suite.Equal(2, followsCount) +} + +func (suite *RelationshipTestSuite) TestGetAccountFollowedBy() { + account := suite.testAccounts["local_account_1"] + follows, err := suite.db.GetAccountFollowedBy(context.Background(), account.ID, false) + suite.NoError(err) + suite.Len(follows, 2) +} + +func (suite *RelationshipTestSuite) TestGetAccountFollowedByLocalOnly() { + account := suite.testAccounts["local_account_1"] + follows, err := suite.db.GetAccountFollowedBy(context.Background(), account.ID, true) + suite.NoError(err) + suite.Len(follows, 2) +} + +func (suite *RelationshipTestSuite) TestCountAccountFollowedBy() { + account := suite.testAccounts["local_account_1"] + followsCount, err := suite.db.CountAccountFollowedBy(context.Background(), account.ID, false) + suite.NoError(err) + suite.Equal(2, followsCount) +} + +func (suite *RelationshipTestSuite) TestCountAccountFollowedByLocalOnly() { + account := suite.testAccounts["local_account_1"] + followsCount, err := suite.db.CountAccountFollowedBy(context.Background(), account.ID, true) + suite.NoError(err) + suite.Equal(2, followsCount) } func TestRelationshipTestSuite(t *testing.T) { |