Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test coverage for generate queries #59

Merged
merged 1 commit into from Mar 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions cmd/tsbs_generate_queries/databases/timescaledb/devops.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
48 changes: 48 additions & 0 deletions cmd/tsbs_generate_queries/databases/timescaledb/devops_test.go
@@ -1,6 +1,8 @@
package timescaledb

import (
"fmt"
"math/rand"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -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
Expand Down