Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT committed Nov 26, 2024
1 parent ace6d19 commit a308e23
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
20 changes: 10 additions & 10 deletions pkg/stanza/fileconsumer/matcher/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ func New(c Criteria) (*Matcher, error) {
m.filterOpts = append(m.filterOpts, filter.ExcludeOlderThan(c.ExcludeOlderThan))
}

if c.OrderingCriteria.GroupBy != "" {
r, err := regexp.Compile(c.OrderingCriteria.GroupBy)
if err != nil {
return nil, fmt.Errorf("compile group_by regex: %w", err)
}
m.groupBy = r
}

if len(c.OrderingCriteria.SortBy) == 0 {
return m, nil
}
Expand All @@ -93,14 +101,6 @@ func New(c Criteria) (*Matcher, error) {
c.OrderingCriteria.TopN = defaultOrderingCriteriaTopN
}

if c.OrderingCriteria.GroupBy != "" {
r, err := regexp.Compile(c.OrderingCriteria.GroupBy)
if err != nil {
return nil, fmt.Errorf("compile group_by regex: %w", err)
}
m.groupBy = r
}

if orderingCriteriaNeedsRegex(c.OrderingCriteria.SortBy) {
if c.OrderingCriteria.Regex == "" {
return nil, fmt.Errorf("'regex' must be specified when 'sort_by' is specified")
Expand Down Expand Up @@ -197,8 +197,8 @@ func (m Matcher) MatchFiles() ([]string, error) {
}

var result []string
for _, files := range groups {
groupResult, err := filter.Filter(files, m.regex, m.filterOpts...)
for _, groupedFiles := range groups {
groupResult, err := filter.Filter(groupedFiles, m.regex, m.filterOpts...)
if len(groupResult) == 0 {
return groupResult, errors.Join(err, errs)
}
Expand Down
58 changes: 57 additions & 1 deletion pkg/stanza/fileconsumer/matcher/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ func TestNew(t *testing.T) {
},
expectedErr: "exclude: parse glob: syntax error in pattern",
},
{
name: "GroupBy",
criteria: Criteria{
Include: []string{"*.log"},
OrderingCriteria: OrderingCriteria{
GroupBy: "[a-z]",
},
},
},
{
name: "RegexEmpty",
criteria: Criteria{
Expand Down Expand Up @@ -118,6 +127,16 @@ func TestNew(t *testing.T) {
},
expectedErr: "'top_n' must be a positive integer",
},
{
name: "GroupBy error",
criteria: Criteria{
Include: []string{"*.log"},
OrderingCriteria: OrderingCriteria{
GroupBy: "[a-z",
},
},
expectedErr: "compile group_by regex: error parsing regexp: missing closing ]: `[a-z`",
},
{
name: "SortTypeEmpty",
criteria: Criteria{
Expand Down Expand Up @@ -384,6 +403,43 @@ func TestMatcher(t *testing.T) {
},
expected: []string{"err.123456789.log"},
},
{
name: "Numeric Sorting",
files: []string{"err.a.123456788.log", "err.a.123456789.log", "err.a.123456787.log", "err.a.123456786.log", "err.b.123456788.log", "err.b.123456789.log"},
include: []string{"err.*.log"},
exclude: []string{},
filterCriteria: OrderingCriteria{
TopN: 6,
Regex: `err\.[a-z]\.(?P<value>\d+).*log`,
SortBy: []Sort{
{
SortType: sortTypeNumeric,
RegexKey: "value",
Ascending: false,
},
},
},
expected: []string{"err.a.123456789.log", "err.b.123456789.log", "err.a.123456788.log", "err.b.123456788.log", "err.a.123456787.log", "err.a.123456786.log"},
},
{
name: "Numeric Sorting with grouping",
files: []string{"err.a.123456788.log", "err.a.123456789.log", "err.a.123456787.log", "err.a.123456786.log", "err.b.123456788.log", "err.b.123456789.log"},
include: []string{"err.*.log"},
exclude: []string{},
filterCriteria: OrderingCriteria{
TopN: 6,
GroupBy: `err\.(?P<value>[a-z]+).[0-9]*.*log`,
Regex: `err\.[a-z]\.(?P<value>\d+).*log`,
SortBy: []Sort{
{
SortType: sortTypeNumeric,
RegexKey: "value",
Ascending: false,
},
},
},
expected: []string{"err.a.123456789.log", "err.a.123456788.log", "err.a.123456787.log", "err.a.123456786.log", "err.b.123456789.log", "err.b.123456788.log"},
},
{
name: "Numeric Sorting Ascending",
files: []string{"err.123456789.log", "err.123456788.log", "err.123456786.log", "err.123456787.log"},
Expand Down Expand Up @@ -786,7 +842,7 @@ func TestMatcher(t *testing.T) {
} else {
assert.NoError(t, err)
}
assert.ElementsMatch(t, tc.expected, files)
assert.Equal(t, tc.expected, files)
})
}
}
Expand Down

0 comments on commit a308e23

Please sign in to comment.