fix: fixing up some logic

This commit is contained in:
Reinaldy Rafli 2021-11-08 22:55:07 +07:00
parent 955313b45e
commit 843f1d77c3
No known key found for this signature in database
GPG Key ID: CFDB9400255D8CB6
10 changed files with 23 additions and 19 deletions

View File

@ -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)

View File

@ -71,7 +71,7 @@ func Teardown() (err error) {
defer cancel()
defer db.Close()
c, err := db.Acquire(ctx)
if err != nil {
return err

View File

@ -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)

View File

@ -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
}

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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 <example@test.com>"}, "https://example.net/img.png")