chore: extract getEnv in integration test #181
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"me-fit/types"
|
||||
"me-fit/utils"
|
||||
|
||||
"database/sql"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"me-fit/utils"
|
||||
|
||||
"database/sql"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
2
main.go
2
main.go
@@ -4,13 +4,13 @@ import (
|
||||
"me-fit/handler"
|
||||
"me-fit/types"
|
||||
"me-fit/utils"
|
||||
"os"
|
||||
|
||||
"context"
|
||||
"database/sql"
|
||||
"log"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"me-fit/service"
|
||||
"me-fit/types"
|
||||
"me-fit/utils"
|
||||
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
@@ -43,21 +44,7 @@ func TestHandleSignIn(t *testing.T) {
|
||||
t.Fatalf("Error inserting user: %v", err)
|
||||
}
|
||||
|
||||
go run(ctx, db, func(key string) string {
|
||||
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 ""
|
||||
}
|
||||
})
|
||||
go run(ctx, db, getEnv("8080"))
|
||||
|
||||
err = waitForReady(ctx, 5*time.Second, "http://localhost:8080")
|
||||
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
|
||||
// response or until the context is cancelled or the timeout is
|
||||
// reached.
|
||||
@@ -107,16 +112,17 @@ func waitForReady(
|
||||
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)
|
||||
if err != nil {
|
||||
fmt.Printf("Error making request: %s\n", err.Error())
|
||||
slog.Info("Error making request: " + err.Error())
|
||||
continue
|
||||
}
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
fmt.Println("Endpoint is ready!")
|
||||
slog.Info("Endpoint is ready!")
|
||||
resp.Body.Close()
|
||||
return nil
|
||||
}
|
||||
@@ -127,7 +133,8 @@ func waitForReady(
|
||||
return ctx.Err()
|
||||
default:
|
||||
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
|
||||
time.Sleep(250 * time.Millisecond)
|
||||
Reference in New Issue
Block a user