gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

statusfave_test.go (4502B)


      1 /*
      2    GoToSocial
      3    Copyright (C) 2021-2023 GoToSocial Authors admin@gotosocial.org
      4 
      5    This program is free software: you can redistribute it and/or modify
      6    it under the terms of the GNU Affero General Public License as published by
      7    the Free Software Foundation, either version 3 of the License, or
      8    (at your option) any later version.
      9 
     10    This program is distributed in the hope that it will be useful,
     11    but WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13    GNU Affero General Public License for more details.
     14 
     15    You should have received a copy of the GNU Affero General Public License
     16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 */
     18 
     19 package bundb_test
     20 
     21 import (
     22 	"context"
     23 	"errors"
     24 	"testing"
     25 
     26 	"github.com/stretchr/testify/suite"
     27 	"github.com/superseriousbusiness/gotosocial/internal/db"
     28 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
     29 )
     30 
     31 type StatusFaveTestSuite struct {
     32 	BunDBStandardTestSuite
     33 }
     34 
     35 func (suite *StatusFaveTestSuite) TestGetStatusFaves() {
     36 	testStatus := suite.testStatuses["admin_account_status_1"]
     37 
     38 	faves, err := suite.db.GetStatusFavesForStatus(context.Background(), testStatus.ID)
     39 	if err != nil {
     40 		suite.FailNow(err.Error())
     41 	}
     42 
     43 	suite.NotEmpty(faves)
     44 	for _, fave := range faves {
     45 		suite.NotNil(fave.Account)
     46 		suite.NotNil(fave.TargetAccount)
     47 		suite.NotNil(fave.Status)
     48 	}
     49 }
     50 
     51 func (suite *StatusFaveTestSuite) TestGetStatusFavesNone() {
     52 	testStatus := suite.testStatuses["admin_account_status_4"]
     53 
     54 	faves, err := suite.db.GetStatusFavesForStatus(context.Background(), testStatus.ID)
     55 	if err != nil {
     56 		suite.FailNow(err.Error())
     57 	}
     58 
     59 	suite.Empty(faves)
     60 }
     61 
     62 func (suite *StatusFaveTestSuite) TestGetStatusFaveByAccountID() {
     63 	testAccount := suite.testAccounts["local_account_1"]
     64 	testStatus := suite.testStatuses["admin_account_status_1"]
     65 
     66 	fave, err := suite.db.GetStatusFave(context.Background(), testAccount.ID, testStatus.ID)
     67 	suite.NoError(err)
     68 	suite.NotNil(fave)
     69 }
     70 
     71 func (suite *StatusFaveTestSuite) TestDeleteStatusFavesOriginatingFromAccount() {
     72 	testAccount := suite.testAccounts["local_account_1"]
     73 
     74 	if err := suite.db.DeleteStatusFaves(context.Background(), "", testAccount.ID); err != nil {
     75 		suite.FailNow(err.Error())
     76 	}
     77 
     78 	faves := []*gtsmodel.StatusFave{}
     79 	if err := suite.db.GetAll(context.Background(), &faves); err != nil && !errors.Is(err, db.ErrNoEntries) {
     80 		suite.FailNow(err.Error())
     81 	}
     82 
     83 	for _, b := range faves {
     84 		if b.AccountID == testAccount.ID {
     85 			suite.FailNowf("", "no StatusFaves with account id %s should remain", testAccount.ID)
     86 		}
     87 	}
     88 }
     89 
     90 func (suite *StatusFaveTestSuite) TestDeleteStatusFavesTargetingAccount() {
     91 	testAccount := suite.testAccounts["local_account_1"]
     92 
     93 	if err := suite.db.DeleteStatusFaves(context.Background(), testAccount.ID, ""); err != nil {
     94 		suite.FailNow(err.Error())
     95 	}
     96 
     97 	faves := []*gtsmodel.StatusFave{}
     98 	if err := suite.db.GetAll(context.Background(), &faves); err != nil && !errors.Is(err, db.ErrNoEntries) {
     99 		suite.FailNow(err.Error())
    100 	}
    101 
    102 	for _, b := range faves {
    103 		if b.TargetAccountID == testAccount.ID {
    104 			suite.FailNowf("", "no StatusFaves with target account id %s should remain", testAccount.ID)
    105 		}
    106 	}
    107 }
    108 
    109 func (suite *StatusFaveTestSuite) TestDeleteStatusFavesTargetingStatus() {
    110 	testStatus := suite.testStatuses["local_account_1_status_1"]
    111 
    112 	if err := suite.db.DeleteStatusFavesForStatus(context.Background(), testStatus.ID); err != nil {
    113 		suite.FailNow(err.Error())
    114 	}
    115 
    116 	faves := []*gtsmodel.StatusFave{}
    117 	if err := suite.db.GetAll(context.Background(), &faves); err != nil && !errors.Is(err, db.ErrNoEntries) {
    118 		suite.FailNow(err.Error())
    119 	}
    120 
    121 	for _, b := range faves {
    122 		if b.StatusID == testStatus.ID {
    123 			suite.FailNowf("", "no StatusFaves with status id %s should remain", testStatus.ID)
    124 		}
    125 	}
    126 }
    127 
    128 func (suite *StatusFaveTestSuite) TestDeleteStatusFave() {
    129 	testFave := suite.testFaves["local_account_1_admin_account_status_1"]
    130 	ctx := context.Background()
    131 
    132 	if err := suite.db.DeleteStatusFaveByID(ctx, testFave.ID); err != nil {
    133 		suite.FailNow(err.Error())
    134 	}
    135 
    136 	fave, err := suite.db.GetStatusFaveByID(ctx, testFave.ID)
    137 	suite.ErrorIs(err, db.ErrNoEntries)
    138 	suite.Nil(fave)
    139 }
    140 
    141 func (suite *StatusFaveTestSuite) TestDeleteStatusFaveNonExisting() {
    142 	err := suite.db.DeleteStatusFaveByID(context.Background(), "01GVAV715K6Y2SG9ZKS9ZA8G7G")
    143 	suite.NoError(err)
    144 }
    145 
    146 func TestStatusFaveTestSuite(t *testing.T) {
    147 	suite.Run(t, new(StatusFaveTestSuite))
    148 }