forked from xmtp/example-notification-server-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.go
More file actions
37 lines (30 loc) · 1.04 KB
/
helpers.go
File metadata and controls
37 lines (30 loc) · 1.04 KB
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
package test
import (
"context"
"testing"
"time"
"github.com/uptrace/bun"
"github.com/uptrace/bun/extra/bundebug"
database "github.com/xmtp/example-notification-server-go/pkg/db"
)
const TEST_DSN = "postgres://postgres:xmtp@localhost:25432/postgres?sslmode=disable"
func createDb() *bun.DB {
db, _ := database.CreateBunDB(TEST_DSN, 5*time.Second)
db.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true)))
return db
}
func CreateTestDb(t *testing.T) *bun.DB {
ctx := t.Context()
db := createDb()
if err := database.Migrate(ctx, db); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
cleanupCtx := context.Background()
_, _ = db.NewTruncateTable().Model((*database.Installation)(nil)).Cascade().Exec(cleanupCtx)
_, _ = db.NewTruncateTable().Model((*database.DeviceDeliveryMechanism)(nil)).Cascade().Exec(cleanupCtx)
_, _ = db.NewTruncateTable().Model((*database.Subscription)(nil)).Cascade().Exec(cleanupCtx)
_, _ = db.NewTruncateTable().Model((*database.SubscriptionHmacKeys)(nil)).Cascade().Exec(cleanupCtx)
})
return db
}