diff --git a/api/core/administrator/id_test.go b/api/core/administrator/id_test.go index dbd74e4..7549513 100644 --- a/api/core/administrator/id_test.go +++ b/api/core/administrator/id_test.go @@ -10,7 +10,7 @@ import ( func TestGetUserID_Success(t *testing.T) { ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second)) defer cancel() - + defer Flush() c, err := db.Acquire(ctx) diff --git a/api/core/administrator/init_test.go b/api/core/administrator/init_test.go index 21807ad..a29863e 100644 --- a/api/core/administrator/init_test.go +++ b/api/core/administrator/init_test.go @@ -71,7 +71,7 @@ func Teardown() (err error) { defer cancel() defer db.Close() - + c, err := db.Acquire(ctx) if err != nil { return err diff --git a/api/core/administrator/verify_test.go b/api/core/administrator/verify_test.go index ba213fb..6e21672 100644 --- a/api/core/administrator/verify_test.go +++ b/api/core/administrator/verify_test.go @@ -41,7 +41,7 @@ func TestCheckKeyExists_Success(t *testing.T) { func TestCheckKeyExists_Failing(t *testing.T) { ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second)) defer cancel() - + defer Flush() c, err := db.Acquire(ctx) diff --git a/api/core/joke/getter.go b/api/core/joke/getter.go index 4e26768..4ca97f9 100644 --- a/api/core/joke/getter.go +++ b/api/core/joke/getter.go @@ -128,7 +128,7 @@ func GetCachedJokeByID(memory *bigcache.BigCache, id int) (string, error) { var data []schema.Joke err = ffjson.Unmarshal(jokes, &data) if err != nil { - return "", nil + return "", err } // This is a simple solution, might convert it to goroutines and channels sometime soon. @@ -150,8 +150,12 @@ func GetCachedTotalJokes(memory *bigcache.BigCache) (int, error) { } return 0, err } + i, err := strconv.Atoi(string(total)) + if err != nil { + return 0, err + } - return int(total[0]), nil + return i, nil } func CheckJokeExists(db *pgxpool.Pool, ctx context.Context, id string) (bool, error) { @@ -174,7 +178,7 @@ func CheckJokeExists(db *pgxpool.Pool, ctx context.Context, id string) (bool, er var jokeID int err = conn.QueryRow(ctx, sql, args...).Scan(&jokeID) - if err != nil && err != pgx.ErrNoRows { + if err != nil && errors.Is(err, pgx.ErrNoRows) { return false, err } diff --git a/api/core/joke/getter_test.go b/api/core/joke/getter_test.go index 83ca8e5..4de0fda 100644 --- a/api/core/joke/getter_test.go +++ b/api/core/joke/getter_test.go @@ -14,7 +14,7 @@ import ( func TestGetAllJSONJokes(t *testing.T) { ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second)) defer cancel() - + defer Flush() conn, err := db.Acquire(ctx) @@ -69,7 +69,7 @@ func TestGetAllJSONJokes(t *testing.T) { func TestGetRandomJokeFromDB(t *testing.T) { ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second)) defer cancel() - + defer Flush() conn, err := db.Acquire(ctx) @@ -121,7 +121,7 @@ func TestGetRandomJokeFromDB(t *testing.T) { } } -func TestGetRandomJokeFromCache(t *testing.T) { +func TestGetRandomJokeFromCache(t *testing.T) { defer Flush() jokes := []schema.Joke{ @@ -279,7 +279,7 @@ func TestGetCachedTotalJokes(t *testing.T) { func TestCheckJokeExists(t *testing.T) { ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second)) defer cancel() - + defer Flush() conn, err := db.Acquire(ctx) diff --git a/api/core/joke/init_test.go b/api/core/joke/init_test.go index b3b376e..34a4e65 100644 --- a/api/core/joke/init_test.go +++ b/api/core/joke/init_test.go @@ -35,7 +35,7 @@ func TestMain(m *testing.M) { func Setup() { 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) @@ -117,7 +117,7 @@ func Setup() { func Teardown() (err error) { ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second)) defer cancel() - + defer db.Close() conn, err := db.Acquire(ctx) diff --git a/api/core/joke/setter_test.go b/api/core/joke/setter_test.go index f2b4949..eb24245 100644 --- a/api/core/joke/setter_test.go +++ b/api/core/joke/setter_test.go @@ -64,7 +64,7 @@ func TestSetAllJSONJoke(t *testing.T) { func TestSetTotalJoke(t *testing.T) { ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second)) defer cancel() - + defer Flush() conn, err := db.Acquire(ctx) @@ -115,7 +115,7 @@ func TestSetTotalJoke(t *testing.T) { func TestInsertJokeIntoDB(t *testing.T) { ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second)) defer cancel() - + defer Flush() data := schema.Joke{ @@ -132,7 +132,7 @@ func TestInsertJokeIntoDB(t *testing.T) { func TestDeleteSingleJoke(t *testing.T) { ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second)) defer cancel() - + defer Flush() conn, err := db.Acquire(ctx) @@ -183,7 +183,7 @@ func TestDeleteSingleJoke(t *testing.T) { func TestUpdateJoke(t *testing.T) { ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second)) defer cancel() - + defer Flush() conn, err := db.Acquire(ctx) diff --git a/api/core/submit/getter_test.go b/api/core/submit/getter_test.go index c8cbd30..8a5e759 100644 --- a/api/core/submit/getter_test.go +++ b/api/core/submit/getter_test.go @@ -11,7 +11,7 @@ import ( func TestGetSubmittedItems(t *testing.T) { ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second)) defer cancel() - + defer Flush() c, err := db.Acquire(ctx) diff --git a/api/core/submit/init_test.go b/api/core/submit/init_test.go index 08d5f01..2901baa 100644 --- a/api/core/submit/init_test.go +++ b/api/core/submit/init_test.go @@ -112,7 +112,7 @@ func Teardown() (err error) { func Flush() error { ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second)) defer cancel() - + conn, err := db.Acquire(ctx) if err != nil { return err diff --git a/api/core/submit/setter_test.go b/api/core/submit/setter_test.go index b491897..524f18c 100644 --- a/api/core/submit/setter_test.go +++ b/api/core/submit/setter_test.go @@ -11,7 +11,7 @@ import ( func TestSubmitJoke(t *testing.T) { ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second)) defer cancel() - + defer Flush() s, err := submit.SubmitJoke(db, ctx, schema.Submission{Author: "Test "}, "https://example.net/img.png")