2021-11-06 17:45:18 +00:00
|
|
|
package joke_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"jokes-bapak2-api/core/joke"
|
|
|
|
"jokes-bapak2-api/core/schema"
|
|
|
|
"testing"
|
2021-11-08 12:39:29 +00:00
|
|
|
"time"
|
2021-11-06 17:45:18 +00:00
|
|
|
|
|
|
|
"github.com/jackc/pgx/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetAllJSONJoke(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()
|
2021-11-06 17:45:18 +00:00
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
defer Flush()
|
|
|
|
|
|
|
|
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
|
|
|
err = joke.SetAllJSONJoke(db, ctx, memory)
|
2021-11-06 17:45:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetTotalJoke(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()
|
2021-11-08 15:55:07 +00:00
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
defer Flush()
|
2021-11-06 17:45:18 +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
|
|
|
err = joke.SetTotalJoke(db, ctx, memory)
|
2021-11-06 17:45:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInsertJokeIntoDB(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()
|
2021-11-08 15:55:07 +00:00
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
defer Flush()
|
2021-11-06 17:45:18 +00:00
|
|
|
|
|
|
|
data := schema.Joke{
|
|
|
|
ID: 1,
|
|
|
|
Link: "link1",
|
|
|
|
Creator: 1,
|
|
|
|
}
|
2021-11-08 12:39:29 +00:00
|
|
|
err := joke.InsertJokeIntoDB(db, ctx, data)
|
2021-11-06 17:45:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteSingleJoke(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()
|
2021-11-08 15:55:07 +00:00
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
defer Flush()
|
2021-11-06 17:45:18 +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
|
|
|
err = joke.DeleteSingleJoke(db, ctx, 1)
|
2021-11-06 17:45:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateJoke(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()
|
2021-11-08 15:55:07 +00:00
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
defer Flush()
|
2021-11-06 17:45:18 +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)
|
|
|
|
}
|
|
|
|
|
|
|
|
newJoke := schema.Joke{
|
|
|
|
ID: 1,
|
|
|
|
Link: "link1",
|
|
|
|
Creator: 1,
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:39:29 +00:00
|
|
|
err = joke.UpdateJoke(db, ctx, newJoke)
|
2021-11-06 17:45:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("an error was thrown:", err)
|
|
|
|
}
|
|
|
|
}
|