unprepare_test.go (3670B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 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 package timeline_test 19 20 import ( 21 "context" 22 "testing" 23 24 "github.com/stretchr/testify/suite" 25 26 apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" 27 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 28 "github.com/superseriousbusiness/gotosocial/internal/id" 29 ) 30 31 type UnprepareTestSuite struct { 32 TimelineStandardTestSuite 33 } 34 35 func (suite *UnprepareTestSuite) TestUnprepareFromFave() { 36 var ( 37 ctx = context.Background() 38 testAccount = suite.testAccounts["local_account_1"] 39 maxID = "" 40 sinceID = "" 41 minID = "" 42 limit = 1 43 local = false 44 ) 45 46 suite.fillTimeline(testAccount.ID) 47 48 // Get first status from the top (no params). 49 statuses, err := suite.state.Timelines.Home.GetTimeline( 50 ctx, 51 testAccount.ID, 52 maxID, 53 sinceID, 54 minID, 55 limit, 56 local, 57 ) 58 if err != nil { 59 suite.FailNow(err.Error()) 60 } 61 62 if len(statuses) != 1 { 63 suite.FailNow("couldn't get top status") 64 } 65 66 targetStatus := statuses[0].(*apimodel.Status) 67 68 // Check fave stats of the top status. 69 suite.Equal(0, targetStatus.FavouritesCount) 70 suite.False(targetStatus.Favourited) 71 72 // Fave the top status from testAccount. 73 if err := suite.state.DB.PutStatusFave(ctx, >smodel.StatusFave{ 74 ID: id.NewULID(), 75 AccountID: testAccount.ID, 76 TargetAccountID: targetStatus.Account.ID, 77 StatusID: targetStatus.ID, 78 URI: "https://example.org/some/activity/path", 79 }); err != nil { 80 suite.FailNow(err.Error()) 81 } 82 83 // Repeat call to get first status from the top. 84 // Get first status from the top (no params). 85 statuses, err = suite.state.Timelines.Home.GetTimeline( 86 ctx, 87 testAccount.ID, 88 maxID, 89 sinceID, 90 minID, 91 limit, 92 local, 93 ) 94 if err != nil { 95 suite.FailNow(err.Error()) 96 } 97 98 if len(statuses) != 1 { 99 suite.FailNow("couldn't get top status") 100 } 101 102 targetStatus = statuses[0].(*apimodel.Status) 103 104 // We haven't yet uncached/unprepared the status, 105 // we've only inserted the fave, so counts should 106 // stay the same... 107 suite.Equal(0, targetStatus.FavouritesCount) 108 suite.False(targetStatus.Favourited) 109 110 // Now call unprepare. 111 suite.state.Timelines.Home.UnprepareItemFromAllTimelines(ctx, targetStatus.ID) 112 113 // Now a Get should trigger a fresh prepare of the 114 // target status, and the counts should be updated. 115 // Repeat call to get first status from the top. 116 // Get first status from the top (no params). 117 statuses, err = suite.state.Timelines.Home.GetTimeline( 118 ctx, 119 testAccount.ID, 120 maxID, 121 sinceID, 122 minID, 123 limit, 124 local, 125 ) 126 if err != nil { 127 suite.FailNow(err.Error()) 128 } 129 130 if len(statuses) != 1 { 131 suite.FailNow("couldn't get top status") 132 } 133 134 targetStatus = statuses[0].(*apimodel.Status) 135 136 suite.Equal(1, targetStatus.FavouritesCount) 137 suite.True(targetStatus.Favourited) 138 } 139 140 func TestUnprepareTestSuite(t *testing.T) { 141 suite.Run(t, new(UnprepareTestSuite)) 142 }