diff --git a/lib/logstorage/pipe_fields_test.go b/lib/logstorage/pipe_fields_test.go index a7b1e792b..000072081 100644 --- a/lib/logstorage/pipe_fields_test.go +++ b/lib/logstorage/pipe_fields_test.go @@ -4,6 +4,122 @@ import ( "testing" ) +func TestParsePipeFieldsSuccess(t *testing.T) { + f := func(pipeStr string) { + t.Helper() + expectParsePipeSuccess(t, pipeStr) + } + + f(`fields *`) + f(`fields f1`) + f(`fields f1, f2, f3`) +} + +func TestParsePipeFieldsFailure(t *testing.T) { + f := func(pipeStr string) { + t.Helper() + expectParsePipeFailure(t, pipeStr) + } + + f(`fields`) + f(`fields x y`) +} + +func TestPipeFields(t *testing.T) { + f := func(pipeStr string, rows, rowsExpected [][]Field) { + t.Helper() + expectPipeResults(t, pipeStr, rows, rowsExpected) + } + + // single row, star + f("fields *", [][]Field{ + { + {"_msg", `{"foo":"bar"}`}, + {"a", `test`}, + }, + }, [][]Field{ + { + {"_msg", `{"foo":"bar"}`}, + {"a", `test`}, + }, + }) + + // single row, leave existing field + f("fields a", [][]Field{ + { + {"_msg", `{"foo":"bar"}`}, + {"a", `test`}, + }, + }, [][]Field{ + { + {"a", `test`}, + }, + }) + + // single row, mention existing field multiple times + f("fileds a, a", [][]Field{ + { + {"_msg", `{"foo":"bar"}`}, + {"a", `test`}, + }, + }, [][]Field{ + { + {"a", `test`}, + }, + }) + + // mention non-existing fields + f("fields foo, a, bar", [][]Field{ + { + {"_msg", `{"foo":"bar"}`}, + {"a", `test`}, + }, + }, [][]Field{ + { + {"foo", ""}, + {"bar", ""}, + {"a", `test`}, + }, + }) + + // Multiple rows + f("fields a, b", [][]Field{ + { + {"_msg", `{"foo":"bar"}`}, + {"a", `test`}, + }, + { + {"a", `foobar`}, + }, + { + {"b", `baz`}, + {"c", "d"}, + {"e", "afdf"}, + }, + { + {"c", "dss"}, + {"b", "df"}, + }, + }, [][]Field{ + { + {"a", `test`}, + {"b", `test`}, + }, + { + {"a", `foobar`}, + {"b", `foobar`}, + }, + { + {"a", ""}, + {"b", ""}, + }, + { + {"a", ""}, + {"b", "df"}, + }, + }) +} + func TestPipeFieldsUpdateNeededFields(t *testing.T) { f := func(s, neededFields, unneededFields, neededFieldsExpected, unneededFieldsExpected string) { t.Helper()