Skip to content

Commit

Permalink
Add Targets.selectDataTable tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Felixoid committed Apr 23, 2021
1 parent 6bf90a3 commit 61b8c70
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 4 deletions.
6 changes: 2 additions & 4 deletions render/data/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,8 @@ func ageToTimestamp(age int64) int64 {
func newCondition(fromAge, untilAge, maxDataPoints int64) *conditions {
tf := TimeFrame{ageToTimestamp(fromAge), ageToTimestamp(untilAge), maxDataPoints}
tt := Targets{
List: []string{"*.name.*"},
AM: newAM(),
// selectDataTable is not tested
// TODO add tests for selectDataTable
List: []string{"*.name.*"},
AM: newAM(),
pointsTable: "graphite.data",
rollupRules: newRules(false),
}
Expand Down
155 changes: 155 additions & 0 deletions render/data/targets_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package data

import (
"fmt"
"testing"
"time"

"github.com/lomik/graphite-clickhouse/config"
"github.com/stretchr/testify/assert"
)

func dur(d time.Duration) *config.Duration {
return &config.Duration{Duration: d}
}

func TestSelectDataTableTime(t *testing.T) {
cfg := config.New()
cfg.DataTable = []config.DataTable{
{
Table: "first_day",
MaxAge: dur(24 * time.Hour),
},
{
Table: "second_day",
MinAge: dur(24 * time.Hour),
MaxAge: dur(48 * time.Hour),
},
{
Table: "two_days_min_interval",
MaxAge: dur(48 * time.Hour),
MinInterval: dur(2 * time.Hour),
},
{
Table: "two_days_min_max_interval",
MaxAge: dur(48 * time.Hour),
MinInterval: dur(30 * time.Minute),
MaxInterval: dur(1 * time.Hour),
},
{
Table: "two_days_max_interval",
MaxAge: dur(48 * time.Hour),
MaxInterval: dur(2 * time.Hour),
},
{
Table: "three_days",
MaxAge: dur(72 * time.Hour),
},
{
Table: "unlimited",
},
}
err := cfg.ProcessDataTables()
assert.NoError(t, err)
tg := Targets{}

tests := []struct {
*TimeFrame
config.DataTable
err error
}{
{
&TimeFrame{ageToTimestamp(3600*24 - 1), ageToTimestamp(1800), 1},
cfg.DataTable[0],
nil,
},
{
&TimeFrame{ageToTimestamp(3600*48 - 1), ageToTimestamp(24*3600 + 1), 1},
cfg.DataTable[1],
nil,
},
{
&TimeFrame{ageToTimestamp(3600 * 26), ageToTimestamp(3600 * 23), 1},
cfg.DataTable[2],
nil,
},
{
&TimeFrame{ageToTimestamp(3600*24 + 1600), ageToTimestamp(3600*24 - 1600), 1},
cfg.DataTable[3],
nil,
},
{
&TimeFrame{ageToTimestamp(3600*24 + 2000), ageToTimestamp(3600*24 - 2000), 1},
cfg.DataTable[4],
nil,
},
{
&TimeFrame{ageToTimestamp(3600*72 - 1), ageToTimestamp(3600*11 - 1), 1},
cfg.DataTable[5],
nil,
},
{
&TimeFrame{ageToTimestamp(3600 * 100), ageToTimestamp(3600*11 - 1), 1},
cfg.DataTable[6],
nil,
},
}

for i, test := range tests {
t.Run(fmt.Sprintf("%d-%s", i+1, test.DataTable.Table), func(t *testing.T) {
err := tg.selectDataTable(cfg, test.TimeFrame, config.ContextGraphite)
assert.Equal(t, test.err, err)
assert.Equal(t, test.DataTable.Table, tg.pointsTable)
})
}
}

func TestSelectDataTableMatch(t *testing.T) {
cfg := config.New()
cfg.DataTable = []config.DataTable{
{
Table: "all",
TargetMatchAll: "^all.*avg",
},
{
Table: "any",
TargetMatchAny: "^any.*avg",
},
{
Table: "unlimited",
},
}
err := cfg.ProcessDataTables()
assert.NoError(t, err)
tf := &TimeFrame{ageToTimestamp(3600*24 - 1), ageToTimestamp(1800), 1}

tests := []struct {
*Targets
config.DataTable
err error
}{
{
&Targets{List: []string{"allinclucive.in.avg", "all.metrics.for.avg"}},
cfg.DataTable[0],
nil,
},
{
&Targets{List: []string{"allinclucive.in.avg", "any.metrics.for.avg"}},
cfg.DataTable[1],
nil,
},
{
&Targets{List: []string{"allinclucive.in.avg", "some.metrics.for.avg"}},
cfg.DataTable[2],
nil,
},
}

for i, test := range tests {
t.Run(fmt.Sprintf("%d-%s", i+1, test.DataTable.Table), func(t *testing.T) {
err := test.Targets.selectDataTable(cfg, tf, config.ContextGraphite)
assert.Equal(t, test.err, err)
assert.Equal(t, test.DataTable.Table, test.Targets.pointsTable)
})
}
}

0 comments on commit 61b8c70

Please sign in to comment.