basic_test.go (4457B)
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 bundb_test 19 20 import ( 21 "context" 22 "crypto/rand" 23 "crypto/rsa" 24 "testing" 25 "time" 26 27 "github.com/stretchr/testify/suite" 28 "github.com/superseriousbusiness/gotosocial/internal/db" 29 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 30 ) 31 32 type BasicTestSuite struct { 33 BunDBStandardTestSuite 34 } 35 36 func (suite *BasicTestSuite) TestGetAccountByID() { 37 testAccount := suite.testAccounts["local_account_1"] 38 39 a := >smodel.Account{} 40 err := suite.db.GetByID(context.Background(), testAccount.ID, a) 41 suite.NoError(err) 42 } 43 44 func (suite *BasicTestSuite) TestPutAccountWithBunDefaultFields() { 45 key, err := rsa.GenerateKey(rand.Reader, 2048) 46 if err != nil { 47 suite.FailNow(err.Error()) 48 } 49 50 // Create an account that only just matches constraints. 51 testAccount := >smodel.Account{ 52 ID: "01GADR1AH9VCKH8YYCM86XSZ00", 53 Username: "test", 54 URI: "https://example.org/users/test", 55 URL: "https://example.org/@test", 56 InboxURI: "https://example.org/users/test/inbox", 57 OutboxURI: "https://example.org/users/test/outbox", 58 ActorType: "Person", 59 PublicKeyURI: "https://example.org/test#main-key", 60 PublicKey: &key.PublicKey, 61 } 62 63 if err := suite.db.Put(context.Background(), testAccount); err != nil { 64 suite.FailNow(err.Error()) 65 } 66 67 a := >smodel.Account{} 68 if err := suite.db.GetByID(context.Background(), testAccount.ID, a); err != nil { 69 suite.FailNow(err.Error()) 70 } 71 72 // check all fields are set as expected, including database defaults 73 suite.Equal(testAccount.ID, a.ID) 74 suite.WithinDuration(time.Now(), a.CreatedAt, 5*time.Second) 75 suite.WithinDuration(time.Now(), a.UpdatedAt, 5*time.Second) 76 suite.Equal(testAccount.Username, a.Username) 77 suite.Empty(a.Domain) 78 suite.Empty(a.AvatarMediaAttachmentID) 79 suite.Nil(a.AvatarMediaAttachment) 80 suite.Empty(a.AvatarRemoteURL) 81 suite.Empty(a.HeaderMediaAttachmentID) 82 suite.Nil(a.HeaderMediaAttachment) 83 suite.Empty(a.HeaderRemoteURL) 84 suite.Empty(a.DisplayName) 85 suite.Nil(a.Fields) 86 suite.Empty(a.Note) 87 suite.Empty(a.NoteRaw) 88 suite.False(*a.Memorial) 89 suite.Empty(a.AlsoKnownAs) 90 suite.Empty(a.MovedToAccountID) 91 suite.False(*a.Bot) 92 suite.Empty(a.Reason) 93 // Locked is especially important, since it's a bool that defaults 94 // to true, which is why we use pointers for bools in the first place 95 suite.True(*a.Locked) 96 suite.False(*a.Discoverable) 97 suite.Empty(a.Privacy) 98 suite.False(*a.Sensitive) 99 suite.Equal("en", a.Language) 100 suite.Empty(a.StatusContentType) 101 suite.Equal(testAccount.URI, a.URI) 102 suite.Equal(testAccount.URL, a.URL) 103 suite.Zero(testAccount.FetchedAt) 104 suite.Equal(testAccount.InboxURI, a.InboxURI) 105 suite.Equal(testAccount.OutboxURI, a.OutboxURI) 106 suite.Empty(a.FollowingURI) 107 suite.Empty(a.FollowersURI) 108 suite.Empty(a.FeaturedCollectionURI) 109 suite.Equal(testAccount.ActorType, a.ActorType) 110 suite.Nil(a.PrivateKey) 111 suite.EqualValues(key.PublicKey, *a.PublicKey) 112 suite.Equal(testAccount.PublicKeyURI, a.PublicKeyURI) 113 suite.Zero(a.SensitizedAt) 114 suite.Zero(a.SilencedAt) 115 suite.Zero(a.SuspendedAt) 116 suite.False(*a.HideCollections) 117 suite.Empty(a.SuspensionOrigin) 118 } 119 120 func (suite *BasicTestSuite) TestGetAllStatuses() { 121 s := []*gtsmodel.Status{} 122 err := suite.db.GetAll(context.Background(), &s) 123 suite.NoError(err) 124 suite.Len(s, 17) 125 } 126 127 func (suite *BasicTestSuite) TestGetAllNotNull() { 128 where := []db.Where{{ 129 Key: "domain", 130 Value: nil, 131 Not: true, 132 }} 133 134 a := []*gtsmodel.Account{} 135 136 err := suite.db.GetWhere(context.Background(), where, &a) 137 suite.NoError(err) 138 suite.NotEmpty(a) 139 140 for _, acct := range a { 141 suite.NotEmpty(acct.Domain) 142 } 143 } 144 145 func TestBasicTestSuite(t *testing.T) { 146 suite.Run(t, new(BasicTestSuite)) 147 }