2021-08-03 18:14:32 +00:00
|
|
|
package health_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
v1 "jokes-bapak2-api/app/v1"
|
|
|
|
"jokes-bapak2-api/app/v1/platform/database"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-08-04 05:56:14 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
|
|
|
_ "github.com/joho/godotenv/autoload"
|
2021-08-03 18:14:32 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-08-04 05:56:14 +00:00
|
|
|
var db *pgxpool.Pool = database.New()
|
2021-08-04 02:51:22 +00:00
|
|
|
var jokesData = []interface{}{1, "https://via.placeholder.com/300/06f/fff.png", 1, 2, "https://via.placeholder.com/300/07f/fff.png", 1, 3, "https://via.placeholder.com/300/08f/fff.png", 1}
|
2021-08-04 05:56:14 +00:00
|
|
|
var app *fiber.App = v1.New()
|
2021-08-04 02:51:22 +00:00
|
|
|
|
|
|
|
func cleanup() {
|
2021-08-04 05:56:14 +00:00
|
|
|
j, err := db.Query(context.Background(), "DROP TABLE \"jokesbapak2\"")
|
2021-08-04 02:51:22 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-08-04 05:56:14 +00:00
|
|
|
a, err := db.Query(context.Background(), "DROP TABLE \"administrators\"")
|
2021-08-04 02:51:22 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-08-04 05:56:14 +00:00
|
|
|
|
|
|
|
defer j.Close()
|
|
|
|
defer a.Close()
|
2021-08-04 02:51:22 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 05:56:14 +00:00
|
|
|
func setup() error {
|
2021-08-03 18:14:32 +00:00
|
|
|
err := database.Setup()
|
|
|
|
if err != nil {
|
2021-08-04 05:56:14 +00:00
|
|
|
return err
|
2021-08-03 18:14:32 +00:00
|
|
|
}
|
2021-08-04 05:56:14 +00:00
|
|
|
a, err := db.Query(context.Background(), "INSERT INTO \"administrators\" (id, key, token, last_used) VALUES ($1, $2, $3, $4);", 1, "very secure", "not the real one", time.Now().Format(time.RFC3339))
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
2021-08-04 05:56:14 +00:00
|
|
|
return err
|
2021-08-03 18:14:32 +00:00
|
|
|
}
|
2021-08-04 05:56:14 +00:00
|
|
|
j, err := db.Query(context.Background(), "INSERT INTO \"jokesbapak2\" (id, link, creator) VALUES ($1, $2, $3), ($4, $5, $6), ($7, $8, $9);", jokesData...)
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
2021-08-04 05:56:14 +00:00
|
|
|
return err
|
2021-08-03 18:14:32 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 05:56:14 +00:00
|
|
|
defer a.Close()
|
|
|
|
defer j.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHealth(t *testing.T) {
|
|
|
|
err := setup()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-03 18:14:32 +00:00
|
|
|
|
2021-08-04 05:56:14 +00:00
|
|
|
defer cleanup()
|
2021-08-03 18:14:32 +00:00
|
|
|
|
2021-08-04 05:56:14 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/health", nil)
|
|
|
|
res, err := app.Test(req, -1)
|
2021-08-03 18:14:32 +00:00
|
|
|
|
2021-08-04 05:56:14 +00:00
|
|
|
assert.Equalf(t, false, err != nil, "health")
|
|
|
|
assert.Equalf(t, 200, res.StatusCode, "health")
|
|
|
|
assert.NotEqualf(t, 0, res.ContentLength, "health")
|
|
|
|
_, err = ioutil.ReadAll(res.Body)
|
|
|
|
assert.Nilf(t, err, "health")
|
2021-08-03 18:14:32 +00:00
|
|
|
}
|