jokes-bapak2/api/core/administrator/init_test.go

125 lines
2.1 KiB
Go
Raw Normal View History

package administrator_test
import (
"context"
"os"
"testing"
"time"
"github.com/jackc/pgx/v4/pgxpool"
)
var db *pgxpool.Pool
var administratorsData = []interface{}{
1, "very secure", "not the real one", time.Now().Format(time.RFC3339),
}
func TestMain(m *testing.M) {
defer Teardown()
Setup()
os.Exit(m.Run())
}
func Setup() {
2021-11-08 12:39:29 +00:00
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(1*time.Minute))
defer cancel()
poolConfig, err := pgxpool.ParseConfig(os.Getenv("DATABASE_URL"))
if err != nil {
panic(err)
}
2021-11-08 12:39:29 +00:00
db, err = pgxpool.ConnectConfig(ctx, poolConfig)
if err != nil {
panic(err)
}
2021-11-08 12:39:29 +00:00
conn, err := db.Acquire(ctx)
if err != nil {
panic(err)
}
defer conn.Release()
2021-11-08 12:39:29 +00:00
tx, err := conn.Begin(ctx)
if err != nil {
panic(err)
}
2021-11-08 12:39:29 +00:00
defer tx.Rollback(ctx)
_, err = tx.Exec(
2021-11-08 12:39:29 +00:00
ctx,
`CREATE TABLE IF NOT EXISTS administrators (
id SERIAL PRIMARY KEY,
key VARCHAR(255) NOT NULL UNIQUE,
token TEXT,
last_used VARCHAR(255)
)`,
)
if err != nil {
panic(err)
}
2021-11-08 12:39:29 +00:00
err = tx.Commit(ctx)
if err != nil {
panic(err)
}
}
func Teardown() (err error) {
2021-11-08 12:39:29 +00:00
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(1*time.Minute))
defer cancel()
defer db.Close()
c, err := db.Acquire(ctx)
if err != nil {
return err
}
2021-11-08 12:39:29 +00:00
defer c.Release()
2021-11-08 12:39:29 +00:00
tx, err := c.Begin(ctx)
if err != nil {
return err
}
2021-11-08 12:39:29 +00:00
defer tx.Rollback(ctx)
_, err = tx.Exec(ctx, "TRUNCATE TABLE submission RESTART IDENTITY CASCADE")
if err != nil {
return err
}
2021-11-08 12:39:29 +00:00
_, err = tx.Exec(ctx, "TRUNCATE TABLE jokesbapak2 RESTART IDENTITY CASCADE")
if err != nil {
return err
}
_, err = tx.Exec(ctx, "TRUNCATE TABLE administrators RESTART IDENTITY CASCADE")
if err != nil {
return err
}
2021-11-08 12:39:29 +00:00
err = tx.Commit(ctx)
if err != nil {
return err
}
return
}
func Flush() error {
2021-11-08 12:39:29 +00:00
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
defer cancel()
conn, err := db.Acquire(ctx)
if err != nil {
return err
}
defer conn.Release()
2021-11-08 12:39:29 +00:00
_, err = conn.Exec(ctx, "TRUNCATE TABLE administrators RESTART IDENTITY CASCADE")
if err != nil {
return err
}
return nil
}