2023-03-12 16:00:57 +01:00
|
|
|
// GoToSocial
|
|
|
|
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2021-08-20 12:26:56 +02:00
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
package bundb_test
|
2021-08-20 12:26:56 +02:00
|
|
|
|
|
|
|
import (
|
2021-08-25 15:34:33 +02:00
|
|
|
"context"
|
2021-08-20 12:26:56 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
2022-09-21 19:55:52 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2023-03-02 16:58:23 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2021-08-20 12:26:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type StatusTestSuite struct {
|
2021-08-25 15:34:33 +02:00
|
|
|
BunDBStandardTestSuite
|
2021-08-20 12:26:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *StatusTestSuite) TestGetStatusByID() {
|
2021-08-25 15:34:33 +02:00
|
|
|
status, err := suite.db.GetStatusByID(context.Background(), suite.testStatuses["local_account_1_status_1"].ID)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
|
|
|
suite.NotNil(status)
|
|
|
|
suite.NotNil(status.Account)
|
|
|
|
suite.NotNil(status.CreatedWithApplication)
|
|
|
|
suite.Nil(status.BoostOf)
|
|
|
|
suite.Nil(status.BoostOfAccount)
|
|
|
|
suite.Nil(status.InReplyTo)
|
|
|
|
suite.Nil(status.InReplyToAccount)
|
2022-08-15 12:35:05 +02:00
|
|
|
suite.True(*status.Federated)
|
|
|
|
suite.True(*status.Boostable)
|
|
|
|
suite.True(*status.Replyable)
|
|
|
|
suite.True(*status.Likeable)
|
2021-08-20 12:26:56 +02:00
|
|
|
}
|
|
|
|
|
2023-07-24 14:14:13 +02:00
|
|
|
func (suite *StatusTestSuite) TestGetStatusesByIDs() {
|
2023-01-10 15:19:05 +01:00
|
|
|
ids := []string{
|
|
|
|
suite.testStatuses["local_account_1_status_1"].ID,
|
|
|
|
suite.testStatuses["local_account_2_status_3"].ID,
|
|
|
|
}
|
|
|
|
|
2023-07-24 14:14:13 +02:00
|
|
|
statuses, err := suite.db.GetStatusesByIDs(context.Background(), ids)
|
2023-01-10 15:19:05 +01:00
|
|
|
if err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(statuses) != 2 {
|
|
|
|
suite.FailNow("expected 2 statuses in slice")
|
|
|
|
}
|
|
|
|
|
|
|
|
status1 := statuses[0]
|
|
|
|
suite.NotNil(status1)
|
|
|
|
suite.NotNil(status1.Account)
|
|
|
|
suite.NotNil(status1.CreatedWithApplication)
|
|
|
|
suite.Nil(status1.BoostOf)
|
|
|
|
suite.Nil(status1.BoostOfAccount)
|
|
|
|
suite.Nil(status1.InReplyTo)
|
|
|
|
suite.Nil(status1.InReplyToAccount)
|
|
|
|
suite.True(*status1.Federated)
|
|
|
|
suite.True(*status1.Boostable)
|
|
|
|
suite.True(*status1.Replyable)
|
|
|
|
suite.True(*status1.Likeable)
|
|
|
|
|
|
|
|
status2 := statuses[1]
|
|
|
|
suite.NotNil(status2)
|
|
|
|
suite.NotNil(status2.Account)
|
|
|
|
suite.NotNil(status2.CreatedWithApplication)
|
|
|
|
suite.Nil(status2.BoostOf)
|
|
|
|
suite.Nil(status2.BoostOfAccount)
|
|
|
|
suite.Nil(status2.InReplyTo)
|
|
|
|
suite.Nil(status2.InReplyToAccount)
|
|
|
|
suite.True(*status2.Federated)
|
|
|
|
suite.True(*status2.Boostable)
|
|
|
|
suite.False(*status2.Replyable)
|
|
|
|
suite.False(*status2.Likeable)
|
|
|
|
}
|
|
|
|
|
2021-08-20 12:26:56 +02:00
|
|
|
func (suite *StatusTestSuite) TestGetStatusByURI() {
|
2021-09-09 16:15:25 +02:00
|
|
|
status, err := suite.db.GetStatusByURI(context.Background(), suite.testStatuses["local_account_2_status_3"].URI)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
|
|
|
suite.NotNil(status)
|
|
|
|
suite.NotNil(status.Account)
|
|
|
|
suite.NotNil(status.CreatedWithApplication)
|
|
|
|
suite.Nil(status.BoostOf)
|
|
|
|
suite.Nil(status.BoostOfAccount)
|
|
|
|
suite.Nil(status.InReplyTo)
|
|
|
|
suite.Nil(status.InReplyToAccount)
|
2022-08-15 12:35:05 +02:00
|
|
|
suite.True(*status.Federated)
|
|
|
|
suite.True(*status.Boostable)
|
|
|
|
suite.False(*status.Replyable)
|
|
|
|
suite.False(*status.Likeable)
|
2021-08-20 12:26:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *StatusTestSuite) TestGetStatusWithExtras() {
|
2021-08-25 15:34:33 +02:00
|
|
|
status, err := suite.db.GetStatusByID(context.Background(), suite.testStatuses["admin_account_status_1"].ID)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
|
|
|
suite.NotNil(status)
|
|
|
|
suite.NotNil(status.Account)
|
|
|
|
suite.NotNil(status.CreatedWithApplication)
|
|
|
|
suite.NotEmpty(status.Tags)
|
|
|
|
suite.NotEmpty(status.Attachments)
|
|
|
|
suite.NotEmpty(status.Emojis)
|
2022-08-15 12:35:05 +02:00
|
|
|
suite.True(*status.Federated)
|
|
|
|
suite.True(*status.Boostable)
|
|
|
|
suite.True(*status.Replyable)
|
|
|
|
suite.True(*status.Likeable)
|
2021-08-20 12:26:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *StatusTestSuite) TestGetStatusWithMention() {
|
2021-08-25 15:34:33 +02:00
|
|
|
status, err := suite.db.GetStatusByID(context.Background(), suite.testStatuses["local_account_2_status_5"].ID)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
|
|
|
suite.NotNil(status)
|
|
|
|
suite.NotNil(status.Account)
|
|
|
|
suite.NotNil(status.CreatedWithApplication)
|
|
|
|
suite.NotEmpty(status.MentionIDs)
|
2021-09-01 11:08:21 +02:00
|
|
|
suite.NotEmpty(status.InReplyToID)
|
|
|
|
suite.NotEmpty(status.InReplyToAccountID)
|
2022-08-15 12:35:05 +02:00
|
|
|
suite.True(*status.Federated)
|
|
|
|
suite.True(*status.Boostable)
|
|
|
|
suite.True(*status.Replyable)
|
|
|
|
suite.True(*status.Likeable)
|
2021-08-20 12:26:56 +02:00
|
|
|
}
|
|
|
|
|
2024-02-18 11:04:02 +01:00
|
|
|
// The below test was originally used to ensure that a second
|
|
|
|
// fetch is faster than a first fetch of a status, because in
|
|
|
|
// the second fetch it should be cached. However because we
|
|
|
|
// always run in-memory tests anyway, sometimes the first fetch
|
|
|
|
// is actually faster, which causes CI/CD to fail unpredictably.
|
|
|
|
// Since we know by now (Feb 2024) that the cache works fine,
|
|
|
|
// the test is commented out.
|
|
|
|
/*
|
2021-08-20 12:26:56 +02:00
|
|
|
func (suite *StatusTestSuite) TestGetStatusTwice() {
|
|
|
|
before1 := time.Now()
|
2021-08-25 15:34:33 +02:00
|
|
|
_, err := suite.db.GetStatusByURI(context.Background(), suite.testStatuses["local_account_1_status_1"].URI)
|
2021-08-20 12:26:56 +02:00
|
|
|
suite.NoError(err)
|
|
|
|
after1 := time.Now()
|
|
|
|
duration1 := after1.Sub(before1)
|
2022-10-08 13:50:48 +02:00
|
|
|
fmt.Println(duration1.Microseconds())
|
2021-08-20 12:26:56 +02:00
|
|
|
|
|
|
|
before2 := time.Now()
|
2021-08-25 15:34:33 +02:00
|
|
|
_, err = suite.db.GetStatusByURI(context.Background(), suite.testStatuses["local_account_1_status_1"].URI)
|
2021-08-20 12:26:56 +02:00
|
|
|
suite.NoError(err)
|
|
|
|
after2 := time.Now()
|
|
|
|
duration2 := after2.Sub(before2)
|
2022-10-08 13:50:48 +02:00
|
|
|
fmt.Println(duration2.Microseconds())
|
2021-08-20 12:26:56 +02:00
|
|
|
|
|
|
|
// second retrieval should be several orders faster since it will be cached now
|
|
|
|
suite.Less(duration2, duration1)
|
|
|
|
}
|
2024-02-18 11:04:02 +01:00
|
|
|
*/
|
2021-08-20 12:26:56 +02:00
|
|
|
|
2023-11-20 13:22:28 +01:00
|
|
|
func (suite *StatusTestSuite) TestGetStatusReplies() {
|
|
|
|
targetStatus := suite.testStatuses["local_account_1_status_1"]
|
|
|
|
children, err := suite.db.GetStatusReplies(context.Background(), targetStatus.ID)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.Len(children, 2)
|
|
|
|
for _, c := range children {
|
|
|
|
suite.Equal(targetStatus.URI, c.InReplyToURI)
|
|
|
|
suite.Equal(targetStatus.AccountID, c.InReplyToAccountID)
|
|
|
|
suite.Equal(targetStatus.ID, c.InReplyToID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-09 16:15:25 +02:00
|
|
|
func (suite *StatusTestSuite) TestGetStatusChildren() {
|
|
|
|
targetStatus := suite.testStatuses["local_account_1_status_1"]
|
2023-11-20 13:22:28 +01:00
|
|
|
children, err := suite.db.GetStatusChildren(context.Background(), targetStatus.ID)
|
2021-09-09 16:15:25 +02:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.Len(children, 2)
|
|
|
|
for _, c := range children {
|
|
|
|
suite.Equal(targetStatus.URI, c.InReplyToURI)
|
|
|
|
suite.Equal(targetStatus.AccountID, c.InReplyToAccountID)
|
|
|
|
suite.Equal(targetStatus.ID, c.InReplyToID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:55:52 +02:00
|
|
|
func (suite *StatusTestSuite) TestDeleteStatus() {
|
2023-03-02 16:58:23 +01:00
|
|
|
// Take a copy of the status.
|
|
|
|
targetStatus := >smodel.Status{}
|
|
|
|
*targetStatus = *suite.testStatuses["admin_account_status_1"]
|
|
|
|
|
2022-09-21 19:55:52 +02:00
|
|
|
err := suite.db.DeleteStatusByID(context.Background(), targetStatus.ID)
|
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
_, err = suite.db.GetStatusByID(context.Background(), targetStatus.ID)
|
|
|
|
suite.ErrorIs(err, db.ErrNoEntries)
|
|
|
|
}
|
|
|
|
|
2023-03-02 16:58:23 +01:00
|
|
|
// This test was added specifically to ensure that Postgres wasn't getting upset
|
|
|
|
// about trying to use a transaction in which an error has already occurred, which
|
|
|
|
// was previously leading to errors like 'current transaction is aborted, commands
|
|
|
|
// ignored until end of transaction block' when updating a status that already had
|
|
|
|
// emojis or tags set on it.
|
|
|
|
//
|
|
|
|
// To run this test for postgres specifically, start a postgres container on localhost
|
|
|
|
// and then run:
|
|
|
|
//
|
|
|
|
// GTS_DB_TYPE=postgres GTS_DB_ADDRESS=localhost go test ./internal/db/bundb -run '^TestStatusTestSuite$' -testify.m '^(TestUpdateStatus)$' github.com/superseriousbusiness/gotosocial/internal/db/bundb
|
|
|
|
func (suite *StatusTestSuite) TestUpdateStatus() {
|
|
|
|
// Take a copy of the status.
|
|
|
|
targetStatus := >smodel.Status{}
|
|
|
|
*targetStatus = *suite.testStatuses["admin_account_status_1"]
|
|
|
|
|
|
|
|
targetStatus.PinnedAt = time.Time{}
|
|
|
|
|
|
|
|
err := suite.db.UpdateStatus(context.Background(), targetStatus, "pinned_at")
|
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
updated, err := suite.db.GetStatusByID(context.Background(), targetStatus.ID)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.True(updated.PinnedAt.IsZero())
|
|
|
|
}
|
|
|
|
|
2023-11-27 16:39:44 +01:00
|
|
|
func (suite *StatusTestSuite) TestPutPopulatedStatus() {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
targetStatus := >smodel.Status{}
|
|
|
|
*targetStatus = *suite.testStatuses["admin_account_status_1"]
|
|
|
|
|
|
|
|
// Populate fields on the target status.
|
|
|
|
if err := suite.db.PopulateStatus(ctx, targetStatus); err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete it from the database.
|
|
|
|
if err := suite.db.DeleteStatusByID(ctx, targetStatus.ID); err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reinsert the populated version
|
|
|
|
// so that it becomes cached.
|
|
|
|
if err := suite.db.PutStatus(ctx, targetStatus); err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the status owner's
|
|
|
|
// account with a new bio.
|
|
|
|
account := >smodel.Account{}
|
|
|
|
*account = *targetStatus.Account
|
|
|
|
account.Note = "new note for this test"
|
|
|
|
if err := suite.db.UpdateAccount(ctx, account, "note"); err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
dbStatus, err := suite.db.GetStatusByID(ctx, targetStatus.ID)
|
|
|
|
if err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Account note should be updated,
|
|
|
|
// even though we stored this
|
|
|
|
// status with the old note.
|
|
|
|
suite.Equal(
|
|
|
|
"new note for this test",
|
|
|
|
dbStatus.Account.Note,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-08-20 12:26:56 +02:00
|
|
|
func TestStatusTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(StatusTestSuite))
|
|
|
|
}
|