From 9316eccabede90882fed206aed42f5f0d99f3a9c Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Sat, 9 Jul 2022 08:48:36 -0700 Subject: [PATCH] adding tests for tasks and aggregation queries (temp). --- .../scrutiny_repository_tasks_test.go | 149 ++++++++++++++ .../scrutiny_repository_temperature_test.go | 185 ++++++++++++++++++ 2 files changed, 334 insertions(+) create mode 100644 webapp/backend/pkg/database/scrutiny_repository_tasks_test.go create mode 100644 webapp/backend/pkg/database/scrutiny_repository_temperature_test.go diff --git a/webapp/backend/pkg/database/scrutiny_repository_tasks_test.go b/webapp/backend/pkg/database/scrutiny_repository_tasks_test.go new file mode 100644 index 0000000..487a1bc --- /dev/null +++ b/webapp/backend/pkg/database/scrutiny_repository_tasks_test.go @@ -0,0 +1,149 @@ +package database + +import ( + mock_config "github.com/analogj/scrutiny/webapp/backend/pkg/config/mock" + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/require" + "testing" +) + +func Test_DownsampleScript_Weekly(t *testing.T) { + t.Parallel() + + //setup + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + fakeConfig := mock_config.NewMockInterface(mockCtrl) + fakeConfig.EXPECT().GetString("web.influxdb.bucket").Return("metrics").AnyTimes() + fakeConfig.EXPECT().GetString("web.influxdb.org").Return("scrutiny").AnyTimes() + + deviceRepo := scrutinyRepository{ + appConfig: fakeConfig, + } + + aggregationType := "weekly" + + //test + influxDbScript := deviceRepo.DownsampleScript(aggregationType) + + //assert + require.Equal(t, ` + sourceBucket = "metrics" + rangeStart = -2w + rangeEnd = -1w + aggWindow = 1w + destBucket = "metrics_weekly" + destOrg = "scrutiny" + + from(bucket: sourceBucket) + |> range(start: rangeStart, stop: rangeEnd) + |> filter(fn: (r) => r["_measurement"] == "smart" ) + |> group(columns: ["device_wwn", "_field"]) + |> aggregateWindow(every: aggWindow, fn: last, createEmpty: false) + |> to(bucket: destBucket, org: destOrg) + + temp_data = from(bucket: sourceBucket) + |> range(start: rangeStart, stop: rangeEnd) + |> filter(fn: (r) => r["_measurement"] == "temp") + |> group(columns: ["device_wwn"]) + |> toInt() + + temp_data + |> aggregateWindow(fn: mean, every: aggWindow, createEmpty: false) + |> to(bucket: destBucket, org: destOrg) + `, influxDbScript) +} + +func Test_DownsampleScript_Monthly(t *testing.T) { + t.Parallel() + + //setup + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + fakeConfig := mock_config.NewMockInterface(mockCtrl) + fakeConfig.EXPECT().GetString("web.influxdb.bucket").Return("metrics").AnyTimes() + fakeConfig.EXPECT().GetString("web.influxdb.org").Return("scrutiny").AnyTimes() + + deviceRepo := scrutinyRepository{ + appConfig: fakeConfig, + } + + aggregationType := "monthly" + + //test + influxDbScript := deviceRepo.DownsampleScript(aggregationType) + + //assert + require.Equal(t, ` + sourceBucket = "metrics_weekly" + rangeStart = -2mo + rangeEnd = -1mo + aggWindow = 1mo + destBucket = "metrics_monthly" + destOrg = "scrutiny" + + from(bucket: sourceBucket) + |> range(start: rangeStart, stop: rangeEnd) + |> filter(fn: (r) => r["_measurement"] == "smart" ) + |> group(columns: ["device_wwn", "_field"]) + |> aggregateWindow(every: aggWindow, fn: last, createEmpty: false) + |> to(bucket: destBucket, org: destOrg) + + temp_data = from(bucket: sourceBucket) + |> range(start: rangeStart, stop: rangeEnd) + |> filter(fn: (r) => r["_measurement"] == "temp") + |> group(columns: ["device_wwn"]) + |> toInt() + + temp_data + |> aggregateWindow(fn: mean, every: aggWindow, createEmpty: false) + |> to(bucket: destBucket, org: destOrg) + `, influxDbScript) +} + +func Test_DownsampleScript_Yearly(t *testing.T) { + t.Parallel() + + //setup + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + fakeConfig := mock_config.NewMockInterface(mockCtrl) + fakeConfig.EXPECT().GetString("web.influxdb.bucket").Return("metrics").AnyTimes() + fakeConfig.EXPECT().GetString("web.influxdb.org").Return("scrutiny").AnyTimes() + + deviceRepo := scrutinyRepository{ + appConfig: fakeConfig, + } + + aggregationType := "yearly" + + //test + influxDbScript := deviceRepo.DownsampleScript(aggregationType) + + //assert + require.Equal(t, ` + sourceBucket = "metrics_monthly" + rangeStart = -2y + rangeEnd = -1y + aggWindow = 1y + destBucket = "metrics_yearly" + destOrg = "scrutiny" + + from(bucket: sourceBucket) + |> range(start: rangeStart, stop: rangeEnd) + |> filter(fn: (r) => r["_measurement"] == "smart" ) + |> group(columns: ["device_wwn", "_field"]) + |> aggregateWindow(every: aggWindow, fn: last, createEmpty: false) + |> to(bucket: destBucket, org: destOrg) + + temp_data = from(bucket: sourceBucket) + |> range(start: rangeStart, stop: rangeEnd) + |> filter(fn: (r) => r["_measurement"] == "temp") + |> group(columns: ["device_wwn"]) + |> toInt() + + temp_data + |> aggregateWindow(fn: mean, every: aggWindow, createEmpty: false) + |> to(bucket: destBucket, org: destOrg) + `, influxDbScript) +} diff --git a/webapp/backend/pkg/database/scrutiny_repository_temperature_test.go b/webapp/backend/pkg/database/scrutiny_repository_temperature_test.go new file mode 100644 index 0000000..7751ddf --- /dev/null +++ b/webapp/backend/pkg/database/scrutiny_repository_temperature_test.go @@ -0,0 +1,185 @@ +package database + +import ( + mock_config "github.com/analogj/scrutiny/webapp/backend/pkg/config/mock" + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/require" + "testing" +) + +func Test_aggregateTempQuery_Week(t *testing.T) { + t.Parallel() + + //setup + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + fakeConfig := mock_config.NewMockInterface(mockCtrl) + fakeConfig.EXPECT().GetString("web.influxdb.bucket").Return("metrics").AnyTimes() + fakeConfig.EXPECT().GetString("web.influxdb.org").Return("scrutiny").AnyTimes() + + deviceRepo := scrutinyRepository{ + appConfig: fakeConfig, + } + + aggregationType := DURATION_KEY_WEEK + + //test + influxDbScript := deviceRepo.aggregateTempQuery(aggregationType) + + //assert + require.Equal(t, `import "influxdata/influxdb/schema" +weekData = from(bucket: "metrics") +|> range(start: -1w, stop: now()) +|> filter(fn: (r) => r["_measurement"] == "temp" ) +|> aggregateWindow(every: 1h, fn: mean, createEmpty: false) +|> group(columns: ["device_wwn"]) +|> toInt() + +weekData +|> schema.fieldsAsCols() +|> yield()`, influxDbScript) +} + +func Test_aggregateTempQuery_Month(t *testing.T) { + t.Parallel() + + //setup + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + fakeConfig := mock_config.NewMockInterface(mockCtrl) + fakeConfig.EXPECT().GetString("web.influxdb.bucket").Return("metrics").AnyTimes() + fakeConfig.EXPECT().GetString("web.influxdb.org").Return("scrutiny").AnyTimes() + + deviceRepo := scrutinyRepository{ + appConfig: fakeConfig, + } + + aggregationType := DURATION_KEY_MONTH + + //test + influxDbScript := deviceRepo.aggregateTempQuery(aggregationType) + + //assert + require.Equal(t, `import "influxdata/influxdb/schema" +weekData = from(bucket: "metrics") +|> range(start: -1w, stop: now()) +|> filter(fn: (r) => r["_measurement"] == "temp" ) +|> aggregateWindow(every: 1h, fn: mean, createEmpty: false) +|> group(columns: ["device_wwn"]) +|> toInt() + +monthData = from(bucket: "metrics_weekly") +|> range(start: -1mo, stop: -1w) +|> filter(fn: (r) => r["_measurement"] == "temp" ) +|> aggregateWindow(every: 1h, fn: mean, createEmpty: false) +|> group(columns: ["device_wwn"]) +|> toInt() + +union(tables: [weekData, monthData]) +|> group(columns: ["device_wwn"]) +|> sort(columns: ["_time"], desc: false) +|> schema.fieldsAsCols()`, influxDbScript) +} + +func Test_aggregateTempQuery_Year(t *testing.T) { + t.Parallel() + + //setup + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + fakeConfig := mock_config.NewMockInterface(mockCtrl) + fakeConfig.EXPECT().GetString("web.influxdb.bucket").Return("metrics").AnyTimes() + fakeConfig.EXPECT().GetString("web.influxdb.org").Return("scrutiny").AnyTimes() + + deviceRepo := scrutinyRepository{ + appConfig: fakeConfig, + } + + aggregationType := DURATION_KEY_YEAR + + //test + influxDbScript := deviceRepo.aggregateTempQuery(aggregationType) + + //assert + require.Equal(t, `import "influxdata/influxdb/schema" +weekData = from(bucket: "metrics") +|> range(start: -1w, stop: now()) +|> filter(fn: (r) => r["_measurement"] == "temp" ) +|> aggregateWindow(every: 1h, fn: mean, createEmpty: false) +|> group(columns: ["device_wwn"]) +|> toInt() + +monthData = from(bucket: "metrics_weekly") +|> range(start: -1mo, stop: -1w) +|> filter(fn: (r) => r["_measurement"] == "temp" ) +|> aggregateWindow(every: 1h, fn: mean, createEmpty: false) +|> group(columns: ["device_wwn"]) +|> toInt() + +yearData = from(bucket: "metrics_monthly") +|> range(start: -1y, stop: -1mo) +|> filter(fn: (r) => r["_measurement"] == "temp" ) +|> aggregateWindow(every: 1h, fn: mean, createEmpty: false) +|> group(columns: ["device_wwn"]) +|> toInt() + +union(tables: [weekData, monthData, yearData]) +|> group(columns: ["device_wwn"]) +|> sort(columns: ["_time"], desc: false) +|> schema.fieldsAsCols()`, influxDbScript) +} + +func Test_aggregateTempQuery_Forever(t *testing.T) { + t.Parallel() + + //setup + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + fakeConfig := mock_config.NewMockInterface(mockCtrl) + fakeConfig.EXPECT().GetString("web.influxdb.bucket").Return("metrics").AnyTimes() + fakeConfig.EXPECT().GetString("web.influxdb.org").Return("scrutiny").AnyTimes() + + deviceRepo := scrutinyRepository{ + appConfig: fakeConfig, + } + + aggregationType := DURATION_KEY_FOREVER + + //test + influxDbScript := deviceRepo.aggregateTempQuery(aggregationType) + + //assert + require.Equal(t, `import "influxdata/influxdb/schema" +weekData = from(bucket: "metrics") +|> range(start: -1w, stop: now()) +|> filter(fn: (r) => r["_measurement"] == "temp" ) +|> aggregateWindow(every: 1h, fn: mean, createEmpty: false) +|> group(columns: ["device_wwn"]) +|> toInt() + +monthData = from(bucket: "metrics_weekly") +|> range(start: -1mo, stop: -1w) +|> filter(fn: (r) => r["_measurement"] == "temp" ) +|> aggregateWindow(every: 1h, fn: mean, createEmpty: false) +|> group(columns: ["device_wwn"]) +|> toInt() + +yearData = from(bucket: "metrics_monthly") +|> range(start: -1y, stop: -1mo) +|> filter(fn: (r) => r["_measurement"] == "temp" ) +|> aggregateWindow(every: 1h, fn: mean, createEmpty: false) +|> group(columns: ["device_wwn"]) +|> toInt() + +foreverData = from(bucket: "metrics_yearly") +|> range(start: -10y, stop: -1y) +|> filter(fn: (r) => r["_measurement"] == "temp" ) +|> aggregateWindow(every: 1h, fn: mean, createEmpty: false) +|> group(columns: ["device_wwn"]) +|> toInt() + +union(tables: [weekData, monthData, yearData, foreverData]) +|> group(columns: ["device_wwn"]) +|> sort(columns: ["_time"], desc: false) +|> schema.fieldsAsCols()`, influxDbScript) +}