Skip to content

Commit

Permalink
unit tests: promQL query with/without range.
Browse files Browse the repository at this point in the history
Added unit tests for building promQL query string with and without
range.
  • Loading branch information
pradykaushik committed Jun 26, 2020
1 parent b3d11ac commit 0651ba5
Showing 1 changed file with 75 additions and 36 deletions.
111 changes: 75 additions & 36 deletions query/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,85 @@ import (
"testing"
)

var testBuilder *Builder
var testBuilderWithRangeQuery *Builder
var testBuilderWithoutRangeQuery *Builder

func TestMain(m *testing.M) {
testBuilder = NewBuilder(
WithMetric("test_metric"),
WithLabelMatchers(
&LabelMatcher{
Label: "test_label1",
Operator: Equal,
Value: "test_value1",
},
&LabelMatcher{
Label: "test_label2",
Operator: Equal,
Value: "test_value2",
}),
WithRange(Seconds, 5))
func TestNewBuilder(t *testing.T) {
t.Run("with-range-query", func(t *testing.T) {
testBuilderWithRangeQuery = NewBuilder(
WithMetric("test_metric"),
WithLabelMatchers(
&LabelMatcher{
Label: "test_label1",
Operator: Equal,
Value: "test_value1",
},
&LabelMatcher{
Label: "test_label2",
Operator: Equal,
Value: "test_value2",
}),
WithRange(Seconds, 5))

m.Run()
}
assert.NotNil(t, testBuilderWithRangeQuery)
assert.Equal(t, "test_metric", testBuilderWithRangeQuery.metric)
assert.Len(t, testBuilderWithRangeQuery.labelMatchers, 2)
assert.ObjectsAreEqualValues(&LabelMatcher{
Label: "test_label1",
Operator: Equal,
Value: "test_value1",
}, testBuilderWithRangeQuery.labelMatchers[0])
assert.ObjectsAreEqualValues(&LabelMatcher{
Label: "test_label2",
Operator: Equal,
Value: "test_value2",
}, testBuilderWithRangeQuery.labelMatchers[1])
assert.Equal(t, Seconds, testBuilderWithRangeQuery.timeUnit)
assert.Equal(t, uint(5), testBuilderWithRangeQuery.timeDuration)
})

func TestNewBuilder(t *testing.T) {
assert.NotNil(t, testBuilder)
assert.Equal(t, "test_metric", testBuilder.metric)
assert.Len(t, testBuilder.labelMatchers, 2)
assert.ObjectsAreEqualValues(&LabelMatcher{
Label: "test_label1",
Operator: Equal,
Value: "test_value1",
}, testBuilder.labelMatchers[0])
assert.ObjectsAreEqualValues(&LabelMatcher{
Label: "test_label2",
Operator: Equal,
Value: "test_value2",
}, testBuilder.labelMatchers[1])
assert.Equal(t, Seconds, testBuilder.timeUnit)
assert.Equal(t, uint(5), testBuilder.timeDuration)
t.Run("without-range-query", func(t *testing.T) {
testBuilderWithoutRangeQuery = NewBuilder(
WithMetric("test_metric"),
WithLabelMatchers(
&LabelMatcher{
Label: "test_label1",
Operator: Equal,
Value: "test_value1",
},
&LabelMatcher{
Label: "test_label2",
Operator: Equal,
Value: "test_value2",
}),
WithRange(None, 0))

assert.NotNil(t, testBuilderWithoutRangeQuery)
assert.Equal(t, "test_metric", testBuilderWithoutRangeQuery.metric)
assert.Len(t, testBuilderWithoutRangeQuery.labelMatchers, 2)
assert.ObjectsAreEqualValues(&LabelMatcher{
Label: "test_label1",
Operator: Equal,
Value: "test_value1",
}, testBuilderWithoutRangeQuery.labelMatchers[0])
assert.ObjectsAreEqualValues(&LabelMatcher{
Label: "test_label2",
Operator: Equal,
Value: "test_value2",
}, testBuilderWithoutRangeQuery.labelMatchers[1])
assert.Equal(t, None, testBuilderWithoutRangeQuery.timeUnit)
assert.Equal(t, uint(0), testBuilderWithoutRangeQuery.timeDuration)
})
}

func TestBuilder_BuildQuery(t *testing.T) {
const expectedQueryString = "test_metric{test_label1=\"test_value1\",test_label2=\"test_value2\"}[5s]"
assert.Equal(t, expectedQueryString, testBuilder.BuildQuery())
t.Run("with-range-query", func(t *testing.T) {
const expectedQueryStringWithRange = "test_metric{test_label1=\"test_value1\",test_label2=\"test_value2\"}[5s]"
assert.Equal(t, expectedQueryStringWithRange, testBuilderWithRangeQuery.BuildQuery())
})

t.Run("without-range-query", func(t *testing.T) {
const expectedQueryStringWithoutRange = "test_metric{test_label1=\"test_value1\",test_label2=\"test_value2\"}"
assert.Equal(t, expectedQueryStringWithoutRange, testBuilderWithoutRangeQuery.BuildQuery())
})
}

0 comments on commit 0651ba5

Please sign in to comment.