Skip to content

Commit

Permalink
test: add test cases of monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
Redish101 committed Sep 20, 2024
1 parent 051b33a commit f622a17
Show file tree
Hide file tree
Showing 3 changed files with 427 additions and 0 deletions.
68 changes: 68 additions & 0 deletions monitor/assertions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io

package monitor

import (
"bytes"
"fmt"
"log"
"path/filepath"
"reflect"
"runtime"
"testing"
"text/tabwriter"
)

// AssertEqual checks if values are equal
func AssertEqual(tb testing.TB, expected, actual interface{}, description ...string) { //nolint:thelper // TODO: Verify if tb can be nil
if tb != nil {
tb.Helper()
}

if reflect.DeepEqual(expected, actual) {
return
}

aType := "<nil>"
bType := "<nil>"

if expected != nil {
aType = reflect.TypeOf(expected).String()
}
if actual != nil {
bType = reflect.TypeOf(actual).String()
}

testName := "AssertEqual"
if tb != nil {
testName = tb.Name()
}

_, file, line, _ := runtime.Caller(1)

var buf bytes.Buffer
const pad = 5
w := tabwriter.NewWriter(&buf, 0, 0, pad, ' ', 0)
_, _ = fmt.Fprintf(w, "\nTest:\t%s", testName)
_, _ = fmt.Fprintf(w, "\nTrace:\t%s:%d", filepath.Base(file), line)
if len(description) > 0 {
_, _ = fmt.Fprintf(w, "\nDescription:\t%s", description[0])
}
_, _ = fmt.Fprintf(w, "\nExpect:\t%v\t(%s)", expected, aType)
_, _ = fmt.Fprintf(w, "\nResult:\t%v\t(%s)", actual, bType)

var result string
if err := w.Flush(); err != nil {
result = err.Error()
} else {
result = buf.String()
}

if tb != nil {
tb.Fatal(result)
} else {
log.Fatal(result) //nolint:revive // tb might be nil, so we need a fallback
}
}
162 changes: 162 additions & 0 deletions monitor/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package monitor

import (
"testing"
"time"

"github.com/gofiber/fiber/v3"
)

func Test_Config_Default(t *testing.T) {
t.Parallel()

t.Run("use default", func(t *testing.T) {
t.Parallel()
cfg := configDefault()

AssertEqual(t, defaultTitle, cfg.Title)
AssertEqual(t, defaultRefresh, cfg.Refresh)
AssertEqual(t, defaultFontURL, cfg.FontURL)
AssertEqual(t, defaultChartJSURL, cfg.ChartJSURL)
AssertEqual(t, defaultCustomHead, cfg.CustomHead)
AssertEqual(t, false, cfg.APIOnly)
AssertEqual(t, (func(*fiber.Ctx) bool)(nil), cfg.Next)
AssertEqual(t, newIndex(viewBag{defaultTitle, defaultRefresh, defaultFontURL, defaultChartJSURL, defaultCustomHead}), cfg.index)
})

t.Run("set title", func(t *testing.T) {
t.Parallel()
title := "title"
cfg := configDefault(Config{
Title: title,
})

AssertEqual(t, title, cfg.Title)
AssertEqual(t, defaultRefresh, cfg.Refresh)
AssertEqual(t, defaultFontURL, cfg.FontURL)
AssertEqual(t, defaultChartJSURL, cfg.ChartJSURL)
AssertEqual(t, defaultCustomHead, cfg.CustomHead)
AssertEqual(t, false, cfg.APIOnly)
AssertEqual(t, (func(*fiber.Ctx) bool)(nil), cfg.Next)
AssertEqual(t, newIndex(viewBag{title, defaultRefresh, defaultFontURL, defaultChartJSURL, defaultCustomHead}), cfg.index)
})

t.Run("set refresh less than default", func(t *testing.T) {
t.Parallel()
cfg := configDefault(Config{
Refresh: 100 * time.Millisecond,
})

AssertEqual(t, defaultTitle, cfg.Title)
AssertEqual(t, minRefresh, cfg.Refresh)
AssertEqual(t, defaultFontURL, cfg.FontURL)
AssertEqual(t, defaultChartJSURL, cfg.ChartJSURL)
AssertEqual(t, defaultCustomHead, cfg.CustomHead)
AssertEqual(t, false, cfg.APIOnly)
AssertEqual(t, (func(*fiber.Ctx) bool)(nil), cfg.Next)
AssertEqual(t, newIndex(viewBag{defaultTitle, minRefresh, defaultFontURL, defaultChartJSURL, defaultCustomHead}), cfg.index)
})

t.Run("set refresh", func(t *testing.T) {
t.Parallel()
refresh := time.Second
cfg := configDefault(Config{
Refresh: refresh,
})

AssertEqual(t, defaultTitle, cfg.Title)
AssertEqual(t, refresh, cfg.Refresh)
AssertEqual(t, defaultFontURL, cfg.FontURL)
AssertEqual(t, defaultChartJSURL, cfg.ChartJSURL)
AssertEqual(t, defaultCustomHead, cfg.CustomHead)
AssertEqual(t, false, cfg.APIOnly)
AssertEqual(t, (func(*fiber.Ctx) bool)(nil), cfg.Next)
AssertEqual(t, newIndex(viewBag{defaultTitle, refresh, defaultFontURL, defaultChartJSURL, defaultCustomHead}), cfg.index)
})

t.Run("set font url", func(t *testing.T) {
t.Parallel()
fontURL := "https://example.com"
cfg := configDefault(Config{
FontURL: fontURL,
})

AssertEqual(t, defaultTitle, cfg.Title)
AssertEqual(t, defaultRefresh, cfg.Refresh)
AssertEqual(t, fontURL, cfg.FontURL)
AssertEqual(t, defaultChartJSURL, cfg.ChartJSURL)
AssertEqual(t, defaultCustomHead, cfg.CustomHead)
AssertEqual(t, false, cfg.APIOnly)
AssertEqual(t, (func(*fiber.Ctx) bool)(nil), cfg.Next)
AssertEqual(t, newIndex(viewBag{defaultTitle, defaultRefresh, fontURL, defaultChartJSURL, defaultCustomHead}), cfg.index)
})

t.Run("set chart js url", func(t *testing.T) {
t.Parallel()
chartURL := "http://example.com"
cfg := configDefault(Config{
ChartJSURL: chartURL,
})

AssertEqual(t, defaultTitle, cfg.Title)
AssertEqual(t, defaultRefresh, cfg.Refresh)
AssertEqual(t, defaultFontURL, cfg.FontURL)
AssertEqual(t, chartURL, cfg.ChartJSURL)
AssertEqual(t, defaultCustomHead, cfg.CustomHead)
AssertEqual(t, false, cfg.APIOnly)
AssertEqual(t, (func(*fiber.Ctx) bool)(nil), cfg.Next)
AssertEqual(t, newIndex(viewBag{defaultTitle, defaultRefresh, defaultFontURL, chartURL, defaultCustomHead}), cfg.index)
})

t.Run("set custom head", func(t *testing.T) {
t.Parallel()
head := "head"
cfg := configDefault(Config{
CustomHead: head,
})

AssertEqual(t, defaultTitle, cfg.Title)
AssertEqual(t, defaultRefresh, cfg.Refresh)
AssertEqual(t, defaultFontURL, cfg.FontURL)
AssertEqual(t, defaultChartJSURL, cfg.ChartJSURL)
AssertEqual(t, head, cfg.CustomHead)
AssertEqual(t, false, cfg.APIOnly)
AssertEqual(t, (func(*fiber.Ctx) bool)(nil), cfg.Next)
AssertEqual(t, newIndex(viewBag{defaultTitle, defaultRefresh, defaultFontURL, defaultChartJSURL, head}), cfg.index)
})

t.Run("set api only", func(t *testing.T) {
t.Parallel()
cfg := configDefault(Config{
APIOnly: true,
})

AssertEqual(t, defaultTitle, cfg.Title)
AssertEqual(t, defaultRefresh, cfg.Refresh)
AssertEqual(t, defaultFontURL, cfg.FontURL)
AssertEqual(t, defaultChartJSURL, cfg.ChartJSURL)
AssertEqual(t, defaultCustomHead, cfg.CustomHead)
AssertEqual(t, true, cfg.APIOnly)
AssertEqual(t, (func(*fiber.Ctx) bool)(nil), cfg.Next)
AssertEqual(t, newIndex(viewBag{defaultTitle, defaultRefresh, defaultFontURL, defaultChartJSURL, defaultCustomHead}), cfg.index)
})

t.Run("set next", func(t *testing.T) {
t.Parallel()
f := func(c *fiber.Ctx) bool {
return true
}
cfg := configDefault(Config{
Next: f,
})

AssertEqual(t, defaultTitle, cfg.Title)
AssertEqual(t, defaultRefresh, cfg.Refresh)
AssertEqual(t, defaultFontURL, cfg.FontURL)
AssertEqual(t, defaultChartJSURL, cfg.ChartJSURL)
AssertEqual(t, defaultCustomHead, cfg.CustomHead)
AssertEqual(t, false, cfg.APIOnly)
AssertEqual(t, f(nil), cfg.Next(nil))
AssertEqual(t, newIndex(viewBag{defaultTitle, defaultRefresh, defaultFontURL, defaultChartJSURL, defaultCustomHead}), cfg.index)
})
}
Loading

0 comments on commit f622a17

Please # to comment.