upped test coverage of helpers
Some checks failed
Tests / Run Go Tests (push) Failing after 1m27s

This commit is contained in:
2025-12-03 00:38:26 +01:00
parent 386f11ec7e
commit a6ea625e8f
4 changed files with 240 additions and 1 deletions

View File

@@ -0,0 +1,47 @@
package helper
import "testing"
func TestGetFirst(t *testing.T) {
tests := []struct {
name string
a any
b any
want any
}{
{"ints", 10, 20, 10},
{"strings", "first", "second", "first"},
{"mixed", "abc", 123, "abc"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := GetFirst(tt.a, tt.b)
if got != tt.want {
t.Errorf("GetFirst(%v, %v) = %v, want %v", tt.a, tt.b, got, tt.want)
}
})
}
}
func TestGetSecond(t *testing.T) {
tests := []struct {
name string
a any
b any
want any
}{
{"ints", 10, 20, 20},
{"strings", "first", "second", "second"},
{"mixed", "abc", 123, 123},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := GetSecond(tt.a, tt.b)
if got != tt.want {
t.Errorf("GetSecond(%v, %v) = %v, want %v", tt.a, tt.b, got, tt.want)
}
})
}
}

View File

@@ -26,7 +26,7 @@ func TestGetMonday(t *testing.T) {
}
}
func TestFormatDuration(t *testing.T) {
func TestFormatDurationFill(t *testing.T) {
testCases := []struct {
name string
duration time.Duration
@@ -48,6 +48,22 @@ func TestFormatDuration(t *testing.T) {
}
}
func TestFormatDuration(t *testing.T) {
testCases := []struct {
name string
duration time.Duration
}{
{"", 0},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if FormatDuration(tc.duration) != tc.name {
t.Error("Format missmatch in Formatduration.")
}
})
}
}
func TestIsSameDate(t *testing.T) {
testCases := []struct {
dateA string
@@ -116,3 +132,41 @@ func TestFormatGermanDayOfWeek(t *testing.T) {
})
}
}
func TestGetKW(t *testing.T) {
tests := []struct {
name string
date time.Time
want int
}{
{
name: "First week of year",
date: time.Date(2023, 1, 2, 0, 0, 0, 0, time.UTC), // Monday
want: 1,
},
{
name: "Middle of year",
date: time.Date(2023, 6, 15, 0, 0, 0, 0, time.UTC),
want: 24,
},
{
name: "Last week of year",
date: time.Date(2023, 12, 31, 0, 0, 0, 0, time.UTC),
want: 52,
},
{
name: "ISO week crossing into next year",
date: time.Date(2020, 12, 31, 0, 0, 0, 0, time.UTC),
want: 53,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := GetKW(tt.date)
if got != tt.want {
t.Errorf("GetKW(%v) = %d, want %d", tt.date, got, tt.want)
}
})
}
}

View File

@@ -0,0 +1,26 @@
package helper
import (
"fmt"
"testing"
)
func TestBoolToInt(t *testing.T) {
testCases := []struct {
value bool
res int
res8 int8
}{
{true, 1, 1},
{false, 0, 0},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("BoolToInt value: %t", tc.value), func(t *testing.T) {
if BoolToInt(tc.value) != tc.res || BoolToInt8(tc.value) != tc.res8 {
t.Error("How could you... mess up bool to int")
}
})
}
}

112
Backend/helper/web_test.go Normal file
View File

@@ -0,0 +1,112 @@
package helper
import (
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/alexedwards/scs/v2"
)
func TestSetCors_WhenNoCorsTrue(t *testing.T) {
os.Setenv("NO_CORS", "true")
defer os.Unsetenv("NO_CORS")
rr := httptest.NewRecorder()
SetCors(rr)
h := rr.Header()
if h.Get("Access-Control-Allow-Origin") != "*" {
t.Errorf("expected Access-Control-Allow-Origin to be '*', got %q", h.Get("Access-Control-Allow-Origin"))
}
if h.Get("Access-Control-Allow-Methods") != "*" {
t.Errorf("expected Access-Control-Allow-Methods to be '*', got %q", h.Get("Access-Control-Allow-Methods"))
}
if h.Get("Access-Control-Allow-Headers") != "*" {
t.Errorf("expected Access-Control-Allow-Headers to be '*', got %q", h.Get("Access-Control-Allow-Headers"))
}
}
func TestSetCors_WhenNoCorsFalse(t *testing.T) {
os.Setenv("NO_CORS", "false")
defer os.Unsetenv("NO_CORS")
rr := httptest.NewRecorder()
SetCors(rr)
h := rr.Header()
if h.Get("Access-Control-Allow-Origin") != "" ||
h.Get("Access-Control-Allow-Methods") != "" ||
h.Get("Access-Control-Allow-Headers") != "" {
t.Errorf("CORS headers should not be set when NO_CORS=false")
}
}
func TestRequiresLogin_DebugMode_NoRedirect(t *testing.T) {
os.Setenv("GO_ENV", "debug")
defer os.Unsetenv("GO_ENV")
session := scs.New()
req := httptest.NewRequest("GET", "/", nil)
rr := httptest.NewRecorder()
RequiresLogin(session, rr, req)
if rr.Result().StatusCode == http.StatusSeeOther {
t.Errorf("expected no redirect in debug mode")
}
}
// func TestRequiresLogin_UserExists_NoRedirect(t *testing.T) {
// os.Setenv("GO_ENV", "production")
// defer os.Unsetenv("GO_ENV")
// session := scs.New()
// req := httptest.NewRequest("GET", "/", nil)
// ctx, err := session.Load(req.Context(), "")
// if err != nil {
// t.Fatalf("session load error: %v", err)
// }
// ctx = session.Put(ctx, "user", "123")
// req = req.WithContext(context.WithValue(ctx, "session", session))
// rr := httptest.NewRecorder()
// yourpkg.RequiresLogin(session, rr, req)
// if rr.Result().StatusCode == http.StatusSeeOther {
// t.Errorf("expected no redirect when user exists")
// }
// }
// func TestRequiresLogin_NoUser_Redirects(t *testing.T) {
// os.Setenv("GO_ENV", "production")
// defer os.Unsetenv("GO_ENV")
// session := scs.New()
// req := httptest.NewRequest("GET", "/", nil)
// req = req.WithContext(context.WithValue(req.Context(), "session", session))
// rr := httptest.NewRecorder()
// RequiresLogin(session, rr, req)
// if rr.Result().StatusCode != http.StatusSeeOther {
// t.Errorf("expected redirect when user does not exist, got %d", rr.Result().StatusCode)
// }
// location := rr.Result().Header.Get("Location")
// if location != "/user/login" {
// t.Errorf("expected redirect to /user/login, got %q", location)
// }
// }