From c90c117174fabb35787a98200c03cfcebf0efb48 Mon Sep 17 00:00:00 2001 From: Rob Kiefer Date: Fri, 1 Mar 2019 00:10:04 -0500 Subject: [PATCH] Improve test coverage for generate queries These functions are easy to cover with unit tests, so let's get them in there. --- .../databases/timescaledb/devops.go | 18 ++++--- .../databases/timescaledb/devops_test.go | 48 +++++++++++++++++++ 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/cmd/tsbs_generate_queries/databases/timescaledb/devops.go b/cmd/tsbs_generate_queries/databases/timescaledb/devops.go index 0b43cf459..57fe84594 100644 --- a/cmd/tsbs_generate_queries/databases/timescaledb/devops.go +++ b/cmd/tsbs_generate_queries/databases/timescaledb/devops.go @@ -9,8 +9,13 @@ import ( "github.com/timescale/tsbs/query" ) -const oneMinute = 60 -const oneHour = oneMinute * 60 +const ( + oneMinute = 60 + oneHour = oneMinute * 60 + + timeBucketFmt = "time_bucket('%d seconds', time)" + nonTimeBucketFmt = "to_timestamp(((extract(epoch from time)::int)/%d)*%d)" +) // Devops produces TimescaleDB-specific queries for all the devops query types. type Devops struct { @@ -55,17 +60,16 @@ func (d *Devops) getHostWhereWithHostnames(hostnames []string) string { } // getHostWhereString gets multiple random hostnames and creates a WHERE SQL statement for these hostnames. -func (d *Devops) getHostWhereString(nhosts int) string { - hostnames := d.GetRandomHosts(nhosts) +func (d *Devops) getHostWhereString(nHosts int) string { + hostnames := d.GetRandomHosts(nHosts) return d.getHostWhereWithHostnames(hostnames) } func (d *Devops) getTimeBucket(seconds int) string { if d.UseTimeBucket { - return fmt.Sprintf("time_bucket('%d seconds', time)", seconds) + return fmt.Sprintf(timeBucketFmt, seconds) } - - return fmt.Sprintf("to_timestamp(((extract(epoch from time)::int)/%d)*%d)", seconds, seconds) + return fmt.Sprintf(nonTimeBucketFmt, seconds, seconds) } func (d *Devops) getSelectClausesAggMetrics(agg string, metrics []string) []string { diff --git a/cmd/tsbs_generate_queries/databases/timescaledb/devops_test.go b/cmd/tsbs_generate_queries/databases/timescaledb/devops_test.go index bb27c2991..c435a3a30 100644 --- a/cmd/tsbs_generate_queries/databases/timescaledb/devops_test.go +++ b/cmd/tsbs_generate_queries/databases/timescaledb/devops_test.go @@ -1,6 +1,8 @@ package timescaledb import ( + "fmt" + "math/rand" "strings" "testing" "time" @@ -69,6 +71,52 @@ func TestDevopsGetHostWhereWithHostnames(t *testing.T) { } } +func TestDevopsGetHostWhereString(t *testing.T) { + cases := []struct { + nHosts int + want string + }{ + { + nHosts: 1, + want: "(hostname = 'host_5')", + }, + { + nHosts: 2, + want: "(hostname = 'host_5' OR hostname = 'host_9')", + }, + { + nHosts: 5, + want: "(hostname = 'host_5' OR hostname = 'host_9' OR hostname = 'host_3' OR hostname = 'host_1' OR hostname = 'host_7')", + }, + } + + for _, c := range cases { + rand.Seed(123) + d := NewDevops(time.Now(), time.Now(), 10) + + if got := d.getHostWhereString(c.nHosts); got != c.want { + t.Errorf("incorrect output for %d hosts: got %s want %s", c.nHosts, got, c.want) + } + } +} + +func TestDevopsGetTimeBucket(t *testing.T) { + d := NewDevops(time.Now(), time.Now(), 10) + d.UseTimeBucket = false + + seconds := 60 + want := fmt.Sprintf(nonTimeBucketFmt, seconds, seconds) + if got := d.getTimeBucket(seconds); got != want { + t.Errorf("incorrect non time bucket format: got %s want %s", got, want) + } + + d.UseTimeBucket = true + want = fmt.Sprintf(timeBucketFmt, seconds) + if got := d.getTimeBucket(seconds); got != want { + t.Errorf("incorrect time bucket format: got %s want %s", got, want) + } +} + func TestDevopsGetSelectClausesAggMetrics(t *testing.T) { cases := []struct { desc string