grafana/pkg/util/strings_test.go
Marcus Efraimsson 8927a3ca20
Chore: Query endpoint refactor (#41637)
Get rid of using legacydata.RequestHandler in HTTPServer, /api/tsdb/query and pkg/expr 
with the goal of deprecating /api/tsdb/query and remove it completely eventually. This is 
the first step of cleaning up the HTTP API query endpoint.

Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com>
Co-authored-by: Alexander Emelin <frvzmb@gmail.com>
2021-11-29 14:21:54 +01:00

108 lines
2.5 KiB
Go

package util
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestStringsFallback2(t *testing.T) {
tests := []struct {
val1 string
val2 string
expected string
}{
// testing every scenario
{"", "", ""},
{"1", "", "1"},
{"1", "2", "1"},
{"", "2", "2"},
}
for _, testcase := range tests {
assert.EqualValues(t, testcase.expected, StringsFallback2(testcase.val1, testcase.val2))
}
}
func TestStringsFallback3(t *testing.T) {
tests := []struct {
val1 string
val2 string
val3 string
expected string
}{
{"", "", "", ""},
{"1", "", "", "1"},
{"1", "2", "", "1"},
{"1", "2", "3", "1"},
{"", "2", "", "2"},
{"", "2", "3", "2"},
{"", "", "3", "3"},
}
for _, testcase := range tests {
assert.EqualValues(t, testcase.expected, StringsFallback3(testcase.val1, testcase.val2, testcase.val3))
}
}
func TestSplitString(t *testing.T) {
tests := map[string][]string{
"": {},
"test": {"test"},
"test1 test2 test3": {"test1", "test2", "test3"},
"test1,test2,test3": {"test1", "test2", "test3"},
"test1, test2, test3": {"test1", "test2", "test3"},
"test1 , test2 test3": {"test1", "test2", "test3"},
}
for input, expected := range tests {
assert.EqualValues(t, expected, SplitString(input))
}
}
func TestDateAge(t *testing.T) {
assert.Equal(t, "?", GetAgeString(time.Time{})) // base case
tests := map[time.Duration]string{
-1 * time.Hour: "< 1 minute", // one hour in the future
0: "< 1 minute",
2 * time.Second: "< 1 minute",
2 * time.Minute: "2 minutes",
2 * time.Hour: "2 hours",
3 * 24 * time.Hour: "3 days",
67 * 24 * time.Hour: "2 months",
409 * 24 * time.Hour: "1 year",
}
for elapsed, expected := range tests {
assert.Equalf(
t,
expected,
GetAgeString(time.Now().Add(-elapsed)),
"duration '%s'",
elapsed.String(),
)
}
}
func TestToCamelCase(t *testing.T) {
tests := map[string]string{
"kebab-case-string": "kebabCaseString",
"snake_case_string": "snakeCaseString",
"mixed-case_string": "mixedCaseString",
"alreadyCamelCase": "alreadyCamelCase",
"": "",
}
for input, expected := range tests {
assert.Equal(t, expected, ToCamelCase(input))
}
}
func TestCapitalize(t *testing.T) {
tests := map[string]string{
"properly capitalizes": "Properly capitalizes",
"Already capitalized": "Already capitalized",
"": "",
}
for input, expected := range tests {
assert.Equal(t, expected, Capitalize(input))
}
}