-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.go
56 lines (46 loc) · 1.28 KB
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package db
import (
"context"
"database/sql"
"fmt"
"os"
"strings"
"testing"
"github.com/pkg/errors"
"github.com/Southclaws/storyden/internal/infrastructure/db/model/account"
"github.com/Southclaws/storyden/internal/infrastructure/db/model/category"
"github.com/Southclaws/storyden/internal/infrastructure/db/model/notification"
"github.com/Southclaws/storyden/internal/infrastructure/db/model/post"
"github.com/Southclaws/storyden/internal/infrastructure/db/model/react"
"github.com/Southclaws/storyden/internal/infrastructure/db/model/subscription"
"github.com/Southclaws/storyden/internal/infrastructure/db/model/tag"
)
func TestDB(t *testing.T) {
url := os.Getenv("DATABASE_URL")
if url == "" {
url = "postgresql://default:default@localhost:5432/postgres"
}
c, _, err := connect(context.Background(), url)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
c.Close()
})
}
func Truncate(db *sql.DB) error {
tables := []string{
notification.Table,
subscription.Table,
react.Table,
account.Table,
category.Table,
tag.Table,
post.Table,
}
if _, err := db.Exec(fmt.Sprintf("truncate table %s CASCADE;", strings.Join(tables, ", "))); err != nil {
return errors.Wrap(err, "failed to clean database")
}
fmt.Println("--- Cleaned database")
return nil
}