diff options
author | 2023-11-14 13:43:27 +0100 | |
---|---|---|
committer | 2023-11-14 12:43:27 +0000 | |
commit | 0b99f14d64d5d372824c4d7602543610f5c006a1 (patch) | |
tree | 953e9e12fb312184b85dda88e278075ac260dc9f /internal/db/bundb/poll_test.go | |
parent | [feature/performance] Wrap incoming HTTP requests in timeout handler (#2353) (diff) | |
download | gotosocial-0b99f14d64d5d372824c4d7602543610f5c006a1.tar.xz |
[bugfix] Update poll delete/update db queries (#2361)
Diffstat (limited to 'internal/db/bundb/poll_test.go')
-rw-r--r-- | internal/db/bundb/poll_test.go | 54 |
1 files changed, 52 insertions, 2 deletions
diff --git a/internal/db/bundb/poll_test.go b/internal/db/bundb/poll_test.go index 53da2514b..479557c55 100644 --- a/internal/db/bundb/poll_test.go +++ b/internal/db/bundb/poll_test.go @@ -26,6 +26,7 @@ import ( "github.com/stretchr/testify/suite" "github.com/superseriousbusiness/gotosocial/internal/db" + "github.com/superseriousbusiness/gotosocial/internal/gtscontext" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/id" "github.com/superseriousbusiness/gotosocial/internal/util" @@ -304,15 +305,64 @@ func (suite *PollTestSuite) TestDeletePollVotes() { suite.NoError(err) // Fetch latest version of poll from database. - poll, err = suite.db.GetPollByID(ctx, poll.ID) + poll, err = suite.db.GetPollByID( + gtscontext.SetBarebones(ctx), + poll.ID, + ) suite.NoError(err) // Check that poll counts are all zero. suite.Equal(*poll.Voters, 0) - suite.Equal(poll.Votes, make([]int, len(poll.Options))) + suite.Equal(make([]int, len(poll.Options)), poll.Votes) } } +func (suite *PollTestSuite) TestDeletePollVotesNoPoll() { + // Create a new context for this test. + ctx, cncl := context.WithCancel(context.Background()) + defer cncl() + + // Try to delete votes of nonexistent poll. + nonPollID := "01HF6V4XWTSZWJ80JNPPDTD4DB" + + err := suite.db.DeletePollVotes(ctx, nonPollID) + suite.NoError(err) +} + +func (suite *PollTestSuite) TestDeletePollVotesBy() { + ctx, cncl := context.WithCancel(context.Background()) + defer cncl() + + for _, vote := range suite.testPollVotes { + // Fetch before version of pollBefore from database. + pollBefore, err := suite.db.GetPollByID(ctx, vote.PollID) + suite.NoError(err) + + // Delete this poll vote. + err = suite.db.DeletePollVoteBy(ctx, vote.PollID, vote.AccountID) + suite.NoError(err) + + // Fetch after version of poll from database. + pollAfter, err := suite.db.GetPollByID(ctx, vote.PollID) + suite.NoError(err) + + // Voters count should be reduced by 1. + suite.Equal(*pollBefore.Voters-1, *pollAfter.Voters) + } +} + +func (suite *PollTestSuite) TestDeletePollVotesByNoAccount() { + ctx, cncl := context.WithCancel(context.Background()) + defer cncl() + + // Try to delete a poll by nonexisting account. + pollID := suite.testPolls["local_account_1_status_6_poll"].ID + nonAccountID := "01HF6T545G1G8ZNMY1S3ZXJ608" + + err := suite.db.DeletePollVoteBy(ctx, pollID, nonAccountID) + suite.NoError(err) +} + func TestPollTestSuite(t *testing.T) { suite.Run(t, new(PollTestSuite)) } |