2021-11-08 09:58:41 +00:00
|
|
|
package submit_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2021-11-08 12:39:29 +00:00
|
|
|
"time"
|
2021-11-08 09:58:41 +00:00
|
|
|
|
|
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
|
|
|
)
|
|
|
|
|
|
|
|
var db *pgxpool.Pool
|
|
|
|
|
|
|
|
var submissionData = []interface{}{
|
|
|
|
1, "https://via.placeholder.com/300/01f/fff.png", "2021-08-03T18:20:38Z", "Test <test@example.com>", 0,
|
|
|
|
2, "https://via.placeholder.com/300/02f/fff.png", "2021-08-04T18:20:38Z", "Test <test@example.com>", 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
defer Teardown()
|
|
|
|
Setup()
|
2021-11-08 12:39:29 +00:00
|
|
|
time.Sleep(3 * time.Second)
|
2021-11-08 09:58:41 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2021-11-08 09:58:41 +00:00
|
|
|
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)
|
2021-11-08 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
conn, err := db.Acquire(ctx)
|
2021-11-08 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer conn.Release()
|
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
tx, err := conn.Begin(ctx)
|
2021-11-08 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-11-08 12:39:29 +00:00
|
|
|
defer tx.Rollback(ctx)
|
2021-11-08 09:58:41 +00:00
|
|
|
|
|
|
|
_, err = tx.Exec(
|
2021-11-08 12:39:29 +00:00
|
|
|
ctx,
|
2021-11-08 09:58:41 +00:00
|
|
|
`CREATE TABLE IF NOT EXISTS submission (
|
|
|
|
id SERIAL PRIMARY KEY,
|
|
|
|
link VARCHAR(255) UNIQUE NOT NULL,
|
|
|
|
created_at VARCHAR(255),
|
|
|
|
author VARCHAR(255) NOT NULL,
|
|
|
|
status SMALLINT DEFAULT 0
|
2021-11-08 12:39:29 +00:00
|
|
|
)`,
|
2021-11-08 09:58:41 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
err = tx.Commit(ctx)
|
2021-11-08 09:58:41 +00:00
|
|
|
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(30*time.Second))
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
conn, err := db.Acquire(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Release()
|
|
|
|
|
|
|
|
tx, err := conn.Begin(ctx)
|
2021-11-08 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-08 12:39:29 +00:00
|
|
|
defer tx.Rollback(ctx)
|
2021-11-08 09:58:41 +00:00
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
_, err = tx.Exec(ctx, "DROP TABLE IF EXISTS submission CASCADE")
|
2021-11-08 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-08 12:39:29 +00:00
|
|
|
_, err = tx.Exec(ctx, "DROP TABLE IF EXISTS jokesbapak2 CASCADE")
|
2021-11-08 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-08 12:39:29 +00:00
|
|
|
_, err = tx.Exec(ctx, "DROP TABLE IF EXISTS administrators CASCADE")
|
2021-11-08 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
err = tx.Commit(ctx)
|
2021-11-08 09:58:41 +00:00
|
|
|
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()
|
2021-11-08 15:55:07 +00:00
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
conn, err := db.Acquire(ctx)
|
2021-11-08 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Release()
|
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
_, err = conn.Exec(ctx, "TRUNCATE TABLE submission RESTART IDENTITY CASCADE")
|
2021-11-08 09:58:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|