mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2025-06-18 18:16:26 +00:00
Add more backend unit tests
This commit is contained in:
33
backend/internal/util/interfaces_test.go
Normal file
33
backend/internal/util/interfaces_test.go
Normal file
@ -0,0 +1,33 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestFindItemInInterface(t *testing.T) {
|
||||
obj := map[string]interface{}{
|
||||
"key1": "value1",
|
||||
"key2": 10,
|
||||
"key3": map[string]interface{}{
|
||||
"nestedKey": "nestedValue",
|
||||
},
|
||||
"key4": []interface{}{"item1", "item2"},
|
||||
}
|
||||
|
||||
// Test case 1: Key exists at the top level
|
||||
result, found := FindItemInInterface("key1", obj)
|
||||
assert.Equal(t, true, found)
|
||||
assert.Equal(t, "value1", result)
|
||||
|
||||
// Test case 2: Key exists at a nested level
|
||||
result, found = FindItemInInterface("nestedKey", obj)
|
||||
assert.Equal(t, true, found)
|
||||
assert.Equal(t, "nestedValue", result)
|
||||
|
||||
// Test case 3: Key does not exist
|
||||
result, found = FindItemInInterface("nonExistentKey", obj)
|
||||
assert.Equal(t, false, found)
|
||||
assert.Equal(t, nil, result)
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@ -90,3 +91,21 @@ func TestConvertIntSliceToString(t *testing.T) {
|
||||
str := ConvertIntSliceToString(items)
|
||||
assert.Equal(t, expectedStr, str)
|
||||
}
|
||||
|
||||
func TestConvertStringSliceToInterface(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input []string
|
||||
expected []interface{}
|
||||
}{
|
||||
{[]string{"hello", "world"}, []interface{}{"hello", "world"}},
|
||||
{[]string{"apple", "banana", "cherry"}, []interface{}{"apple", "banana", "cherry"}},
|
||||
{[]string{}, []interface{}{}}, // Empty slice should return an empty slice
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
result := ConvertStringSliceToInterface(tc.input)
|
||||
if !reflect.DeepEqual(result, tc.expected) {
|
||||
t.Errorf("Expected: %v, Got: %v", tc.expected, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -49,3 +49,19 @@ upstream npm_upstream_5 {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrettyPrintJSON(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{`{"name":"John","age":30,"city":"New York"}`, "{\n \"name\": \"John\",\n \"age\": 30,\n \"city\": \"New York\"\n}"},
|
||||
{`{"fruit":"apple","color":"red"}`, "{\n \"fruit\": \"apple\",\n \"color\": \"red\"\n}"},
|
||||
{"invalid-json", "invalid-json"}, // non-JSON input should return the original string unchanged
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
result := PrettyPrintJSON(tc.input)
|
||||
assert.Equal(t, tc.expected, result)
|
||||
}
|
||||
}
|
||||
|
25
backend/internal/util/time_test.go
Normal file
25
backend/internal/util/time_test.go
Normal file
@ -0,0 +1,25 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestUnixMilliToNiceFormat(t *testing.T) {
|
||||
tests := []struct {
|
||||
input int64
|
||||
expected string
|
||||
}{
|
||||
{0, "1970-01-01 10:00:00"}, // Unix epoch time
|
||||
{1568000000000, "2019-09-09 13:33:20"}, // Arbitrary millisecond timestamp
|
||||
{1636598400000, "2021-11-11 12:40:00"}, // Another arbitrary millisecond timestamp
|
||||
{-1000000000000, "1938-04-25 08:13:20"}, // Negative millisecond timestamp
|
||||
{9223372036854775807, "1970-01-01 09:59:59"}, // Maximum representable millisecond timestamp
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
output := UnixMilliToNiceFormat(test.input)
|
||||
assert.Equal(t, test.expected, output)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user