gotosocial/testrig/db.go

100 lines
2 KiB
Go
Raw Normal View History

2021-04-05 14:21:38 +02:00
package testrig
import (
2021-04-10 13:34:41 +02:00
"context"
"github.com/sirupsen/logrus"
2021-04-05 14:21:38 +02:00
"github.com/superseriousbusiness/gotosocial/internal/db"
2021-04-08 23:29:35 +02:00
"github.com/superseriousbusiness/gotosocial/internal/db/gtsmodel"
2021-04-05 14:21:38 +02:00
"github.com/superseriousbusiness/gotosocial/internal/oauth"
)
var testModels []interface{} = []interface{}{
2021-04-08 23:29:35 +02:00
&gtsmodel.Account{},
&gtsmodel.Application{},
&gtsmodel.Block{},
&gtsmodel.DomainBlock{},
&gtsmodel.EmailDomainBlock{},
&gtsmodel.Follow{},
&gtsmodel.FollowRequest{},
&gtsmodel.MediaAttachment{},
&gtsmodel.Mention{},
&gtsmodel.Status{},
&gtsmodel.Tag{},
&gtsmodel.User{},
2021-04-05 14:21:38 +02:00
&oauth.Token{},
&oauth.Client{},
}
2021-04-10 13:34:41 +02:00
// NewTestDB returns a new initialized, empty database for testing
func NewTestDB() db.DB {
config := NewTestConfig()
l := logrus.New()
l.SetLevel(logrus.TraceLevel)
testDB, err := db.New(context.Background(), config, l)
if err != nil {
panic(err)
}
return testDB
}
2021-04-05 14:21:38 +02:00
// StandardDBSetup populates a given db with all the necessary tables/models for perfoming tests.
2021-04-10 13:34:41 +02:00
func StandardDBSetup(db db.DB) {
2021-04-05 14:21:38 +02:00
for _, m := range testModels {
if err := db.CreateTable(m); err != nil {
2021-04-10 13:34:41 +02:00
panic(err)
2021-04-05 14:21:38 +02:00
}
}
2021-04-06 16:31:57 +02:00
2021-04-10 13:34:41 +02:00
for _, v := range NewTestTokens() {
2021-04-06 16:31:57 +02:00
if err := db.Put(v); err != nil {
2021-04-10 13:34:41 +02:00
panic(err)
2021-04-06 16:31:57 +02:00
}
}
2021-04-10 13:34:41 +02:00
for _, v := range NewTestClients() {
2021-04-06 16:31:57 +02:00
if err := db.Put(v); err != nil {
2021-04-10 13:34:41 +02:00
panic(err)
2021-04-06 16:31:57 +02:00
}
}
2021-04-10 13:34:41 +02:00
for _, v := range NewTestApplications() {
2021-04-06 16:31:57 +02:00
if err := db.Put(v); err != nil {
2021-04-10 13:34:41 +02:00
panic(err)
2021-04-06 16:31:57 +02:00
}
}
2021-04-10 13:34:41 +02:00
for _, v := range NewTestUsers() {
2021-04-06 16:31:57 +02:00
if err := db.Put(v); err != nil {
2021-04-10 13:34:41 +02:00
panic(err)
2021-04-06 16:31:57 +02:00
}
}
2021-04-10 13:34:41 +02:00
for _, v := range NewTestAccounts() {
2021-04-06 16:31:57 +02:00
if err := db.Put(v); err != nil {
2021-04-10 13:34:41 +02:00
panic(err)
2021-04-06 16:31:57 +02:00
}
}
2021-04-10 13:34:41 +02:00
for _, v := range NewTestAttachments() {
2021-04-09 17:21:53 +02:00
if err := db.Put(v); err != nil {
2021-04-10 13:34:41 +02:00
panic(err)
2021-04-09 17:21:53 +02:00
}
}
2021-04-10 13:34:41 +02:00
for _, v := range NewTestStatuses() {
2021-04-08 23:29:35 +02:00
if err := db.Put(v); err != nil {
2021-04-10 13:34:41 +02:00
panic(err)
2021-04-08 23:29:35 +02:00
}
}
2021-04-05 14:21:38 +02:00
}
// StandardDBTeardown drops all the standard testing tables/models from the database to ensure it's clean for the next test.
2021-04-10 13:34:41 +02:00
func StandardDBTeardown(db db.DB) {
2021-04-05 14:21:38 +02:00
for _, m := range testModels {
if err := db.DropTable(m); err != nil {
2021-04-10 13:34:41 +02:00
panic(err)
2021-04-05 14:21:38 +02:00
}
}
}