diff --git a/render/data/query_test.go b/render/data/query_test.go index 0fc1a97eb..db783b70c 100644 --- a/render/data/query_test.go +++ b/render/data/query_test.go @@ -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), } diff --git a/render/data/targets_test.go b/render/data/targets_test.go new file mode 100644 index 000000000..ca5c53346 --- /dev/null +++ b/render/data/targets_test.go @@ -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) + }) + } +}