github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/configs/legacy_promql/functions_test.go (about)

     1  // Copyright 2015 The Prometheus Authors
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package promql
    15  
    16  import (
    17  	"context"
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/prometheus/prometheus/pkg/labels"
    22  	"github.com/prometheus/prometheus/pkg/timestamp"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestDeriv(t *testing.T) {
    27  	// https://github.com/prometheus/prometheus/issues/2674#issuecomment-315439393
    28  	// This requires more precision than the usual test system offers,
    29  	// so we test it by hand.
    30  	storage := NewStorage(t)
    31  	defer storage.Close()
    32  	engine := NewEngine(nil, nil, 10, 10*time.Second)
    33  
    34  	a := storage.Appender(context.Background())
    35  
    36  	metric := labels.FromStrings("__name__", "foo")
    37  	_, err := a.Append(0, metric, 1493712816939, 1.0)
    38  	assert.NoError(t, err)
    39  
    40  	_, err = a.Append(0, metric, 1493712846939, 1.0)
    41  	assert.NoError(t, err)
    42  
    43  	err = a.Commit()
    44  	assert.NoError(t, err)
    45  
    46  	query, err := engine.NewInstantQuery(storage, "deriv(foo[30m])", timestamp.Time(1493712846939))
    47  	assert.NoError(t, err)
    48  
    49  	result := query.Exec(context.Background())
    50  	assert.NoError(t, result.Err)
    51  
    52  	vec, _ := result.Vector()
    53  	assert.True(t, len(vec) == 1, "Expected 1 result, got %d", len(vec))
    54  	assert.True(t, vec[0].V == 0.0, "Expected 0.0 as value, got %f", vec[0].V)
    55  }