VictoriaMetrics/lib/logstorage/json_parser_test.go

74 lines
1.1 KiB
Go
Raw Permalink Normal View History

2024-05-20 02:08:30 +00:00
package logstorage
import (
"reflect"
"testing"
)
func TestJSONParserFailure(t *testing.T) {
f := func(data string) {
t.Helper()
p := GetJSONParser()
2024-05-22 19:01:20 +00:00
err := p.ParseLogMessage([]byte(data))
2024-05-20 02:08:30 +00:00
if err == nil {
t.Fatalf("expecting non-nil error")
}
PutJSONParser(p)
}
f("")
f("{foo")
f("[1,2,3]")
f(`{"foo",}`)
}
func TestJSONParserSuccess(t *testing.T) {
2024-05-22 19:01:20 +00:00
f := func(data string, fieldsExpected []Field) {
2024-05-20 02:08:30 +00:00
t.Helper()
p := GetJSONParser()
2024-05-22 19:01:20 +00:00
err := p.ParseLogMessage([]byte(data))
2024-05-20 02:08:30 +00:00
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !reflect.DeepEqual(p.Fields, fieldsExpected) {
t.Fatalf("unexpected fields;\ngot\n%s\nwant\n%s", p.Fields, fieldsExpected)
}
PutJSONParser(p)
}
2024-05-22 19:01:20 +00:00
f("{}", nil)
f(`{"foo":"bar"}`, []Field{
2024-05-20 02:08:30 +00:00
{
Name: "foo",
Value: "bar",
},
})
2024-05-22 19:01:20 +00:00
f(`{"foo":{"bar":{"x":"y","z":["foo"]}},"a":1,"b":true,"c":[1,2],"d":false}`, []Field{
2024-05-20 02:08:30 +00:00
{
2024-05-22 19:01:20 +00:00
Name: "foo.bar.x",
Value: "y",
2024-05-20 02:08:30 +00:00
},
{
2024-05-22 19:01:20 +00:00
Name: "foo.bar.z",
Value: `["foo"]`,
2024-05-20 02:08:30 +00:00
},
{
Name: "a",
Value: "1",
},
{
Name: "b",
Value: "true",
},
{
Name: "c",
Value: "[1,2]",
},
{
Name: "d",
Value: "false",
},
})
}