chore: extract getEnv in integration test #181
All checks were successful
Build Docker Image / Explore-Gitea-Actions (push) Successful in 49s

This commit is contained in:
2024-10-03 12:51:47 +02:00
parent f2a98e5f49
commit 915c9238f6
4 changed files with 33 additions and 24 deletions

View File

@@ -1,10 +1,11 @@
package db package db
import ( import (
"database/sql"
"errors"
"me-fit/types" "me-fit/types"
"me-fit/utils" "me-fit/utils"
"database/sql"
"errors"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"

View File

@@ -1,8 +1,9 @@
package db package db
import ( import (
"database/sql"
"me-fit/utils" "me-fit/utils"
"database/sql"
"reflect" "reflect"
"testing" "testing"
"time" "time"

View File

@@ -4,13 +4,13 @@ import (
"me-fit/handler" "me-fit/handler"
"me-fit/types" "me-fit/types"
"me-fit/utils" "me-fit/utils"
"os"
"context" "context"
"database/sql" "database/sql"
"log" "log"
"log/slog" "log/slog"
"net/http" "net/http"
"os"
"os/signal" "os/signal"
"sync" "sync"
"syscall" "syscall"

View File

@@ -1,12 +1,13 @@
package main package main
import ( import (
"log/slog"
"me-fit/service" "me-fit/service"
"me-fit/types"
"me-fit/utils" "me-fit/utils"
"context" "context"
"database/sql" "database/sql"
"fmt"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@@ -43,21 +44,7 @@ func TestHandleSignIn(t *testing.T) {
t.Fatalf("Error inserting user: %v", err) t.Fatalf("Error inserting user: %v", err)
} }
go run(ctx, db, func(key string) string { go run(ctx, db, getEnv("8080"))
if key == "PORT" {
return "8080"
} else if key == "SMTP_ENABLED" {
return "false"
} else if key == "PROMETHEUS_ENABLED" {
return "false"
} else if key == "BASE_URL" {
return "https://localhost:8080"
} else if key == "ENVIRONMENT" {
return "test"
} else {
return ""
}
})
err = waitForReady(ctx, 5*time.Second, "http://localhost:8080") err = waitForReady(ctx, 5*time.Second, "http://localhost:8080")
if err != nil { if err != nil {
@@ -89,6 +76,24 @@ func TestHandleSignIn(t *testing.T) {
}) })
} }
func getEnv(port string) func(string) string {
return func(key string) string {
if key == "PORT" {
return port
} else if key == "SMTP_ENABLED" {
return "false"
} else if key == "PROMETHEUS_ENABLED" {
return "false"
} else if key == "BASE_URL" {
return "https://localhost:8080"
} else if key == "ENVIRONMENT" {
return "test"
} else {
return ""
}
}
}
// waitForReady calls the specified endpoint until it gets a 200 // waitForReady calls the specified endpoint until it gets a 200
// response or until the context is cancelled or the timeout is // response or until the context is cancelled or the timeout is
// reached. // reached.
@@ -107,16 +112,17 @@ func waitForReady(
nil, nil,
) )
if err != nil { if err != nil {
return fmt.Errorf("failed to create request: %w", err) slog.Error("failed to create request: " + err.Error())
return err
} }
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
fmt.Printf("Error making request: %s\n", err.Error()) slog.Info("Error making request: " + err.Error())
continue continue
} }
if resp.StatusCode == http.StatusOK { if resp.StatusCode == http.StatusOK {
fmt.Println("Endpoint is ready!") slog.Info("Endpoint is ready!")
resp.Body.Close() resp.Body.Close()
return nil return nil
} }
@@ -127,7 +133,8 @@ func waitForReady(
return ctx.Err() return ctx.Err()
default: default:
if time.Since(startTime) >= timeout { if time.Since(startTime) >= timeout {
return fmt.Errorf("timeout reached while waiting for endpoint") slog.Error("timeout reached while waiting for endpoint")
return types.ErrInternal
} }
// wait a little while between checks // wait a little while between checks
time.Sleep(250 * time.Millisecond) time.Sleep(250 * time.Millisecond)