diff --git a/lib/logstorage/pipe_field_names_test.go b/lib/logstorage/pipe_field_names_test.go index 4b19f2e34..72e5d4adc 100644 --- a/lib/logstorage/pipe_field_names_test.go +++ b/lib/logstorage/pipe_field_names_test.go @@ -4,6 +4,64 @@ import ( "testing" ) +func TestParsePipeFieldNamesSuccess(t *testing.T) { + f := func(pipeStr string) { + t.Helper() + expectParsePipeSuccess(t, pipeStr) + } + + f(`field_names as x`) +} + +func TestParsePipeFieldNamesFailure(t *testing.T) { + f := func(pipeStr string) { + t.Helper() + expectParsePipeFailure(t, pipeStr) + } + + f(`field_names`) + f(`field_names(foo)`) + f(`field_names a b`) + f(`field_names as`) +} + +func TestPipeFieldNames(t *testing.T) { + f := func(pipeStr string, rows, rowsExpected [][]Field) { + t.Helper() + expectPipeResults(t, pipeStr, rows, rowsExpected) + } + + // single row, result column doesn't clash with original columns + f("field_names as x", [][]Field{ + { + {"_msg", `{"foo":"bar"}`}, + {"a", `test`}, + }, + }, [][]Field{ + { + {"x", "_msg"}, + }, + { + {"x", "a"}, + }, + }) + + // single row, result column do clashes with original columns + f("field_names as _msg", [][]Field{ + { + {"_msg", `{"foo":"bar"}`}, + {"a", `test`}, + }, + }, [][]Field{ + { + {"_msg", "_msg"}, + }, + { + {"_msg", "a"}, + }, + }) +} + func TestPipeFieldNamesUpdateNeededFields(t *testing.T) { f := func(s string, neededFields, unneededFields, neededFieldsExpected, unneededFieldsExpected string) { t.Helper() diff --git a/lib/logstorage/pipe_filter_test.go b/lib/logstorage/pipe_filter_test.go index 73da2c8bb..99a4e2fbf 100644 --- a/lib/logstorage/pipe_filter_test.go +++ b/lib/logstorage/pipe_filter_test.go @@ -4,6 +4,103 @@ import ( "testing" ) +func TestParsePipeFilterSuccess(t *testing.T) { + f := func(pipeStr string) { + t.Helper() + expectParsePipeSuccess(t, pipeStr) + } + + f(`filter *`) + f(`filter foo bar`) + f(`filter a:b or c:d in(x,y) z:>343`) +} + +func TestParsePipeFilterFailure(t *testing.T) { + f := func(pipeStr string) { + t.Helper() + expectParsePipeFailure(t, pipeStr) + } + + f(`filter`) + f(`filter |`) + f(`filter ()`) +} + +func TestPipeFilter(t *testing.T) { + f := func(pipeStr string, rows, rowsExpected [][]Field) { + t.Helper() + expectPipeResults(t, pipeStr, rows, rowsExpected) + } + + // filter mismatch + f("filter abc", [][]Field{ + { + {"_msg", `{"foo":"bar"}`}, + {"a", `test`}, + }, + }, [][]Field{}) + + // filter match + f("filter _msg:foo", [][]Field{ + { + {"_msg", `{"foo":"bar"}`}, + {"a", `test`}, + }, + }, [][]Field{ + { + {"_msg", `{"foo":"bar"}`}, + {"a", `test`}, + }, + }) + + // multiple rows + f("filter x:foo y:bar", [][]Field{ + { + {"a", "f1"}, + {"x", "foo"}, + {"y", "bar"}, + }, + { + {"a", "f2"}, + {"x", "x foo bar"}, + {"y", "aa bar bbb"}, + {"z", "iwert"}, + }, + { + {"a", "f3"}, + {"x", "x fo bar"}, + {"y", "aa bar bbb"}, + {"z", "it"}, + }, + { + {"a", "f4"}, + {"x", "x foo bar"}, + {"y", "aa ba bbb"}, + {"z", "t"}, + }, + { + {"x", "x foo"}, + {"y", "aa bar"}, + }, + }, [][]Field{ + { + {"a", "f1"}, + {"x", "foo"}, + {"y", "bar"}, + }, + { + {"a", "f2"}, + {"x", "x foo bar"}, + {"y", "aa bar bbb"}, + {"z", "iwert"}, + }, + { + {"x", "x foo"}, + {"y", "aa bar"}, + }, + }) +} + func TestPipeFilterUpdateNeededFields(t *testing.T) { f := func(s string, neededFields, unneededFields, neededFieldsExpected, unneededFieldsExpected string) { t.Helper()