feat(tests): more backend tests

This commit is contained in:
Łukasz Mierzwa
2018-09-07 22:03:33 +01:00
parent 6583c844e4
commit cd0ef2421c
2 changed files with 89 additions and 0 deletions

View File

@@ -6,6 +6,7 @@ import (
"time"
"github.com/prymitive/unsee/internal/uri"
"github.com/pmezard/go-difflib/difflib"
log "github.com/sirupsen/logrus"
@@ -207,3 +208,9 @@ func TestUrlSecretTest(t *testing.T) {
}
}
}
// FIXME check logged values
func TestLogValues(t *testing.T) {
Config.Read()
Config.LogValues()
}

View File

@@ -0,0 +1,82 @@
package transform_test
import (
"encoding/json"
"sort"
"testing"
"github.com/prymitive/unsee/internal/models"
"github.com/prymitive/unsee/internal/transform"
"github.com/pmezard/go-difflib/difflib"
)
type acTest struct {
Alerts []models.Alert
Expected []string
}
var acTests = []acTest{
{
Alerts: []models.Alert{},
Expected: []string{
"@age\u003e1h",
"@age\u003c10m",
"@age\u003c1h",
"@age\u003e10m",
"@limit=10",
"@limit=50",
},
},
{
Alerts: []models.Alert{
models.Alert{
Labels: map[string]string{
"foo": "bar",
},
},
},
Expected: []string{
"@age\u003e1h",
"@age\u003c10m",
"@age\u003c1h",
"@age\u003e10m",
"@limit=10",
"@limit=50",
"@state!=",
"@state=",
"foo!=bar",
"foo=bar",
},
},
}
func TestBuildAutocomplete(t *testing.T) {
for _, acTest := range acTests {
result := []string{}
for _, hint := range transform.BuildAutocomplete(acTest.Alerts) {
result = append(result, hint.Value)
}
sort.Strings(result)
sort.Strings(acTest.Expected)
resultJSON, _ := json.Marshal(result)
expectedJSON, _ := json.Marshal(acTest.Expected)
if string(resultJSON) != string(expectedJSON) {
diff := difflib.UnifiedDiff{
A: difflib.SplitLines(string(expectedJSON)),
B: difflib.SplitLines(string(resultJSON)),
FromFile: "Expected",
ToFile: "Returned",
Context: 3,
}
text, err := difflib.GetUnifiedDiffString(diff)
if err != nil {
t.Error(err)
}
t.Errorf("Autocomplete mismatch:\n%s", text)
}
}
}