[🤖] Add Unit-Tests
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m34s

This commit is contained in:
2026-04-27 10:46:08 +02:00
parent dad0e3240d
commit 02d6894ec6
116 changed files with 18795 additions and 1 deletions
+121
View File
@@ -0,0 +1,121 @@
package ginext
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/gin-gonic/gin"
)
func TestCreateBackgroundAppContext(t *testing.T) {
ac := CreateBackgroundAppContext()
if ac == nil {
t.Fatalf("expected non-nil context")
}
if ac.GinContext != nil {
t.Fatalf("background context should have no gin context")
}
if ac.Err() != nil {
t.Fatalf("expected no error")
}
if _, ok := ac.Deadline(); ok {
t.Fatalf("background context should have no deadline")
}
}
func TestCreateAppContext_CopiesGinKeys(t *testing.T) {
rec := httptest.NewRecorder()
g, _ := gin.CreateTestContext(rec)
g.Request = httptest.NewRequest(http.MethodGet, "/", nil)
g.Set("foo", "bar")
g.Set("num", 42)
inner, cancel := context.WithCancel(context.Background())
defer cancel()
ac := CreateAppContext(g, inner, cancel)
if ac.Value("foo") != "bar" {
t.Fatalf("expected key foo to be copied")
}
if ac.Value("num") != 42 {
t.Fatalf("expected key num to be copied")
}
if ac.GinContext != g {
t.Fatalf("expected GinContext to be set")
}
}
func TestAppContext_Set(t *testing.T) {
ac := CreateBackgroundAppContext()
ac.Set("k", "v")
if ac.Value("k") != "v" {
t.Fatalf("expected Set to store value")
}
}
func TestAppContext_Cancel(t *testing.T) {
called := false
cancel := func() { called = true }
ac := &AppContext{
inner: context.Background(),
cancelFunc: cancel,
}
ac.Cancel()
if !called {
t.Fatalf("expected cancel function to be invoked")
}
if !ac.cancelled {
t.Fatalf("expected cancelled flag set")
}
}
func TestAppContext_DeadlineDoneErr(t *testing.T) {
deadline := time.Now().Add(1 * time.Hour)
inner, cancel := context.WithDeadline(context.Background(), deadline)
defer cancel()
ac := &AppContext{inner: inner, cancelFunc: cancel}
d, ok := ac.Deadline()
if !ok {
t.Fatalf("expected deadline ok")
}
if !d.Equal(deadline) {
t.Fatalf("deadline mismatch")
}
if ac.Done() == nil {
t.Fatalf("expected non-nil Done channel")
}
if ac.Err() != nil {
t.Fatalf("expected no err yet")
}
cancel()
// After cancel, Err should return Canceled
if !errors.Is(ac.Err(), context.Canceled) {
t.Fatalf("expected context.Canceled, got %v", ac.Err())
}
}
func TestAppContext_RequestURI(t *testing.T) {
bg := CreateBackgroundAppContext()
if bg.RequestURI() != "" {
t.Fatalf("expected empty for background context")
}
rec := httptest.NewRecorder()
g, _ := gin.CreateTestContext(rec)
g.Request = httptest.NewRequest(http.MethodPost, "/foo/bar", nil)
inner, cancel := context.WithCancel(context.Background())
defer cancel()
ac := CreateAppContext(g, inner, cancel)
uri := ac.RequestURI()
if uri != "POST :: /foo/bar" {
t.Fatalf("expected POST :: /foo/bar, got %q", uri)
}
}