2021-10-18 08:31:17 +00:00
|
|
|
package joke_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-06 17:45:18 +00:00
|
|
|
"encoding/json"
|
2021-10-30 03:24:53 +00:00
|
|
|
"jokes-bapak2-api/core/joke"
|
2021-11-06 17:45:18 +00:00
|
|
|
"jokes-bapak2-api/core/schema"
|
2021-10-18 08:31:17 +00:00
|
|
|
"testing"
|
2021-11-08 12:39:29 +00:00
|
|
|
"time"
|
2021-10-18 08:31:17 +00:00
|
|
|
|
|
|
|
"github.com/jackc/pgx/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetAllJSONJokes(t *testing.T) {
|
2021-11-08 12:39:29 +00:00
|
|
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
defer Flush()
|
2021-11-06 17:45:18 +00:00
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
conn, err := db.Acquire(ctx)
|
2021-10-18 08:31:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
2021-11-06 17:45:18 +00:00
|
|
|
defer conn.Release()
|
2021-10-18 08:31:17 +00:00
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
err = conn.BeginFunc(ctx, func(t pgx.Tx) error {
|
2021-11-06 17:45:18 +00:00
|
|
|
_, err := t.Exec(
|
2021-11-08 12:39:29 +00:00
|
|
|
ctx,
|
2021-11-06 17:45:18 +00:00
|
|
|
`INSERT INTO "administrators"
|
|
|
|
(id, key, token, last_used)
|
|
|
|
VALUES
|
|
|
|
($1, $2, $3, $4),
|
2021-11-08 12:39:29 +00:00
|
|
|
($5, $6, $7, $8)`,
|
2021-11-06 17:45:18 +00:00
|
|
|
administratorsData...,
|
|
|
|
)
|
2021-10-18 08:31:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-06 17:45:18 +00:00
|
|
|
_, err = t.Exec(
|
2021-11-08 12:39:29 +00:00
|
|
|
ctx,
|
2021-11-06 17:45:18 +00:00
|
|
|
`INSERT INTO "jokesbapak2"
|
|
|
|
(id, link, creator)
|
|
|
|
VALUES
|
|
|
|
($1, $2, $3),
|
|
|
|
($4, $5, $6),
|
2021-11-08 12:39:29 +00:00
|
|
|
($7, $8, $9)`,
|
2021-11-06 17:45:18 +00:00
|
|
|
jokesData...,
|
|
|
|
)
|
2021-10-18 08:31:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
j, err := joke.GetAllJSONJokes(db, ctx)
|
2021-10-18 08:31:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(j) == "" {
|
|
|
|
t.Error("j should not be empty")
|
|
|
|
}
|
2021-11-06 17:45:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRandomJokeFromDB(t *testing.T) {
|
2021-11-08 12:39:29 +00:00
|
|
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
defer Flush()
|
2021-11-08 09:58:41 +00:00
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
conn, err := db.Acquire(ctx)
|
2021-11-06 17:45:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
defer conn.Release()
|
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
err = conn.BeginFunc(ctx, func(t pgx.Tx) error {
|
2021-11-06 17:45:18 +00:00
|
|
|
_, err := t.Exec(
|
2021-11-08 12:39:29 +00:00
|
|
|
ctx,
|
2021-11-06 17:45:18 +00:00
|
|
|
`INSERT INTO "administrators"
|
|
|
|
(id, key, token, last_used)
|
|
|
|
VALUES
|
|
|
|
($1, $2, $3, $4),
|
2021-11-08 12:39:29 +00:00
|
|
|
($5, $6, $7, $8)`,
|
2021-11-06 17:45:18 +00:00
|
|
|
administratorsData...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = t.Exec(
|
2021-11-08 12:39:29 +00:00
|
|
|
ctx,
|
2021-11-06 17:45:18 +00:00
|
|
|
`INSERT INTO "jokesbapak2"
|
|
|
|
(id, link, creator)
|
|
|
|
VALUES
|
|
|
|
($1, $2, $3),
|
|
|
|
($4, $5, $6),
|
2021-11-08 12:39:29 +00:00
|
|
|
($7, $8, $9)`,
|
2021-11-06 17:45:18 +00:00
|
|
|
jokesData...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
2021-10-18 08:31:17 +00:00
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
j, err := joke.GetRandomJokeFromDB(db, ctx)
|
2021-11-06 17:45:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if j == "" {
|
|
|
|
t.Error("j should not be empty")
|
|
|
|
}
|
2021-10-18 08:31:17 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
func TestGetRandomJokeFromCache(t *testing.T) {
|
|
|
|
defer Flush()
|
2021-11-08 09:58:41 +00:00
|
|
|
|
2021-11-06 17:45:18 +00:00
|
|
|
jokes := []schema.Joke{
|
|
|
|
{ID: 1, Link: "link1", Creator: 1},
|
|
|
|
{ID: 2, Link: "link2", Creator: 1},
|
|
|
|
{ID: 3, Link: "link3", Creator: 1},
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(jokes)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = memory.Set("jokes", data)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
j, err := joke.GetRandomJokeFromCache(memory)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if j == "" {
|
|
|
|
t.Error("j should not be empty")
|
|
|
|
}
|
2021-10-18 08:31:17 +00:00
|
|
|
}
|
|
|
|
|
2021-11-06 17:45:18 +00:00
|
|
|
func TestCheckJokesCache_True(t *testing.T) {
|
2021-11-08 12:39:29 +00:00
|
|
|
defer Flush()
|
2021-11-06 17:45:18 +00:00
|
|
|
|
|
|
|
jokes := []schema.Joke{
|
|
|
|
{ID: 1, Link: "link1", Creator: 1},
|
|
|
|
{ID: 2, Link: "link2", Creator: 1},
|
|
|
|
{ID: 3, Link: "link3", Creator: 1},
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(jokes)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = memory.Set("jokes", data)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
j, err := joke.CheckJokesCache(memory)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if j == false {
|
|
|
|
t.Error("j should not be false")
|
|
|
|
}
|
2021-10-18 08:31:17 +00:00
|
|
|
}
|
|
|
|
|
2021-11-06 17:45:18 +00:00
|
|
|
func TestCheckJokesCache_False(t *testing.T) {
|
2021-11-08 12:39:29 +00:00
|
|
|
defer Flush()
|
2021-11-08 09:58:41 +00:00
|
|
|
|
2021-11-06 17:45:18 +00:00
|
|
|
j, err := joke.CheckJokesCache(memory)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if j == true {
|
|
|
|
t.Error("j should not be true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckTotalJokesCache_True(t *testing.T) {
|
2021-11-08 12:39:29 +00:00
|
|
|
defer Flush()
|
2021-11-06 17:45:18 +00:00
|
|
|
|
|
|
|
err := memory.Set("total", []byte("10"))
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
j, err := joke.CheckTotalJokesCache(memory)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if j == false {
|
|
|
|
t.Error("j should not be false")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckTotalJokesCache_False(t *testing.T) {
|
2021-11-08 12:39:29 +00:00
|
|
|
defer Flush()
|
2021-11-08 09:58:41 +00:00
|
|
|
|
2021-11-06 17:45:18 +00:00
|
|
|
j, err := joke.CheckTotalJokesCache(memory)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if j == true {
|
|
|
|
t.Error("j should not be true")
|
|
|
|
}
|
2021-10-18 08:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetCachedJokeByID(t *testing.T) {
|
2021-11-08 12:39:29 +00:00
|
|
|
defer Flush()
|
2021-11-06 17:45:18 +00:00
|
|
|
|
|
|
|
jokes := []schema.Joke{
|
|
|
|
{ID: 1, Link: "link1", Creator: 1},
|
|
|
|
{ID: 2, Link: "link2", Creator: 1},
|
|
|
|
{ID: 3, Link: "link3", Creator: 1},
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(jokes)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = memory.Set("jokes", data)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
j, err := joke.GetCachedJokeByID(memory, 1)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if j != "link1" {
|
|
|
|
t.Error("j should be link1, got:", j)
|
|
|
|
}
|
|
|
|
|
|
|
|
k, err := joke.GetCachedJokeByID(memory, 4)
|
|
|
|
if err == nil {
|
2021-11-08 12:39:29 +00:00
|
|
|
t.Error("an error was not thrown, k:", k)
|
2021-11-06 17:45:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if k != "" {
|
|
|
|
t.Error("k should be empty, got:", k)
|
|
|
|
}
|
2021-10-18 08:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetCachedTotalJokes(t *testing.T) {
|
2021-11-08 12:39:29 +00:00
|
|
|
defer Flush()
|
2021-11-06 17:45:18 +00:00
|
|
|
|
|
|
|
err := memory.Set("total", []byte("10"))
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
j, err := joke.GetCachedTotalJokes(memory)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if j != 10 {
|
|
|
|
t.Error("j should be 10, got:", j)
|
|
|
|
}
|
2021-10-18 08:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckJokeExists(t *testing.T) {
|
2021-11-08 12:39:29 +00:00
|
|
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
defer Flush()
|
2021-11-08 09:58:41 +00:00
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
conn, err := db.Acquire(ctx)
|
2021-11-06 17:45:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
defer conn.Release()
|
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
err = conn.BeginFunc(ctx, func(t pgx.Tx) error {
|
2021-11-06 17:45:18 +00:00
|
|
|
_, err := t.Exec(
|
2021-11-08 12:39:29 +00:00
|
|
|
ctx,
|
2021-11-06 17:45:18 +00:00
|
|
|
`INSERT INTO "administrators"
|
|
|
|
(id, key, token, last_used)
|
|
|
|
VALUES
|
|
|
|
($1, $2, $3, $4),
|
2021-11-08 12:39:29 +00:00
|
|
|
($5, $6, $7, $8)`,
|
2021-11-06 17:45:18 +00:00
|
|
|
administratorsData...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = t.Exec(
|
2021-11-08 12:39:29 +00:00
|
|
|
ctx,
|
2021-11-06 17:45:18 +00:00
|
|
|
`INSERT INTO "jokesbapak2"
|
|
|
|
(id, link, creator)
|
|
|
|
VALUES
|
|
|
|
($1, $2, $3),
|
|
|
|
($4, $5, $6),
|
2021-11-08 12:39:29 +00:00
|
|
|
($7, $8, $9)`,
|
2021-11-06 17:45:18 +00:00
|
|
|
jokesData...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
j, err := joke.CheckJokeExists(db, ctx, "1")
|
2021-11-06 17:45:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if j == false {
|
|
|
|
t.Error("j should not be false")
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
k, err := joke.CheckJokeExists(db, ctx, "4")
|
2021-11-06 17:45:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if k == true {
|
|
|
|
t.Error("k should not be true")
|
|
|
|
}
|
2021-10-18 08:31:17 +00:00
|
|
|
}
|