fix: fixing up some logic
This commit is contained in:
parent
955313b45e
commit
843f1d77c3
|
@ -10,7 +10,7 @@ import (
|
||||||
func TestGetUserID_Success(t *testing.T) {
|
func TestGetUserID_Success(t *testing.T) {
|
||||||
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
defer Flush()
|
defer Flush()
|
||||||
|
|
||||||
c, err := db.Acquire(ctx)
|
c, err := db.Acquire(ctx)
|
||||||
|
|
|
@ -71,7 +71,7 @@ func Teardown() (err error) {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
c, err := db.Acquire(ctx)
|
c, err := db.Acquire(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -41,7 +41,7 @@ func TestCheckKeyExists_Success(t *testing.T) {
|
||||||
func TestCheckKeyExists_Failing(t *testing.T) {
|
func TestCheckKeyExists_Failing(t *testing.T) {
|
||||||
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
defer Flush()
|
defer Flush()
|
||||||
|
|
||||||
c, err := db.Acquire(ctx)
|
c, err := db.Acquire(ctx)
|
||||||
|
|
|
@ -128,7 +128,7 @@ func GetCachedJokeByID(memory *bigcache.BigCache, id int) (string, error) {
|
||||||
var data []schema.Joke
|
var data []schema.Joke
|
||||||
err = ffjson.Unmarshal(jokes, &data)
|
err = ffjson.Unmarshal(jokes, &data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is a simple solution, might convert it to goroutines and channels sometime soon.
|
// 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
|
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) {
|
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
|
var jokeID int
|
||||||
err = conn.QueryRow(ctx, sql, args...).Scan(&jokeID)
|
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
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ import (
|
||||||
func TestGetAllJSONJokes(t *testing.T) {
|
func TestGetAllJSONJokes(t *testing.T) {
|
||||||
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
defer Flush()
|
defer Flush()
|
||||||
|
|
||||||
conn, err := db.Acquire(ctx)
|
conn, err := db.Acquire(ctx)
|
||||||
|
@ -69,7 +69,7 @@ func TestGetAllJSONJokes(t *testing.T) {
|
||||||
func TestGetRandomJokeFromDB(t *testing.T) {
|
func TestGetRandomJokeFromDB(t *testing.T) {
|
||||||
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
defer Flush()
|
defer Flush()
|
||||||
|
|
||||||
conn, err := db.Acquire(ctx)
|
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()
|
defer Flush()
|
||||||
|
|
||||||
jokes := []schema.Joke{
|
jokes := []schema.Joke{
|
||||||
|
@ -279,7 +279,7 @@ func TestGetCachedTotalJokes(t *testing.T) {
|
||||||
func TestCheckJokeExists(t *testing.T) {
|
func TestCheckJokeExists(t *testing.T) {
|
||||||
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
defer Flush()
|
defer Flush()
|
||||||
|
|
||||||
conn, err := db.Acquire(ctx)
|
conn, err := db.Acquire(ctx)
|
||||||
|
|
|
@ -35,7 +35,7 @@ func TestMain(m *testing.M) {
|
||||||
func Setup() {
|
func Setup() {
|
||||||
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(1*time.Minute))
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(1*time.Minute))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
poolConfig, err := pgxpool.ParseConfig(os.Getenv("DATABASE_URL"))
|
poolConfig, err := pgxpool.ParseConfig(os.Getenv("DATABASE_URL"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -117,7 +117,7 @@ func Setup() {
|
||||||
func Teardown() (err error) {
|
func Teardown() (err error) {
|
||||||
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
conn, err := db.Acquire(ctx)
|
conn, err := db.Acquire(ctx)
|
||||||
|
|
|
@ -64,7 +64,7 @@ func TestSetAllJSONJoke(t *testing.T) {
|
||||||
func TestSetTotalJoke(t *testing.T) {
|
func TestSetTotalJoke(t *testing.T) {
|
||||||
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
defer Flush()
|
defer Flush()
|
||||||
|
|
||||||
conn, err := db.Acquire(ctx)
|
conn, err := db.Acquire(ctx)
|
||||||
|
@ -115,7 +115,7 @@ func TestSetTotalJoke(t *testing.T) {
|
||||||
func TestInsertJokeIntoDB(t *testing.T) {
|
func TestInsertJokeIntoDB(t *testing.T) {
|
||||||
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
defer Flush()
|
defer Flush()
|
||||||
|
|
||||||
data := schema.Joke{
|
data := schema.Joke{
|
||||||
|
@ -132,7 +132,7 @@ func TestInsertJokeIntoDB(t *testing.T) {
|
||||||
func TestDeleteSingleJoke(t *testing.T) {
|
func TestDeleteSingleJoke(t *testing.T) {
|
||||||
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
defer Flush()
|
defer Flush()
|
||||||
|
|
||||||
conn, err := db.Acquire(ctx)
|
conn, err := db.Acquire(ctx)
|
||||||
|
@ -183,7 +183,7 @@ func TestDeleteSingleJoke(t *testing.T) {
|
||||||
func TestUpdateJoke(t *testing.T) {
|
func TestUpdateJoke(t *testing.T) {
|
||||||
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
defer Flush()
|
defer Flush()
|
||||||
|
|
||||||
conn, err := db.Acquire(ctx)
|
conn, err := db.Acquire(ctx)
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
func TestGetSubmittedItems(t *testing.T) {
|
func TestGetSubmittedItems(t *testing.T) {
|
||||||
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
defer Flush()
|
defer Flush()
|
||||||
|
|
||||||
c, err := db.Acquire(ctx)
|
c, err := db.Acquire(ctx)
|
||||||
|
|
|
@ -112,7 +112,7 @@ func Teardown() (err error) {
|
||||||
func Flush() error {
|
func Flush() error {
|
||||||
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
conn, err := db.Acquire(ctx)
|
conn, err := db.Acquire(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
func TestSubmitJoke(t *testing.T) {
|
func TestSubmitJoke(t *testing.T) {
|
||||||
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
defer Flush()
|
defer Flush()
|
||||||
|
|
||||||
s, err := submit.SubmitJoke(db, ctx, schema.Submission{Author: "Test <example@test.com>"}, "https://example.net/img.png")
|
s, err := submit.SubmitJoke(db, ctx, schema.Submission{Author: "Test <example@test.com>"}, "https://example.net/img.png")
|
||||||
|
|
Loading…
Reference in New Issue