Repository: prometheus/prometheus
File: promql/engine_test.go (Go)

Code before:
			expected: promql.Matrix{
				{
					Metric: labels.FromStrings("__name__", "some_metric_with_stale_marker"),
					Floats: []promql.FPoint{
						{T: timestamp.FromTime(baseT.Add(time.Minute)), F: 1},
						{T: timestamp.FromTime(baseT.Add(3 * time.Minute)), F: 3},
					},
				},
			},
		},
	}

	for name, testCase := range testCases {
		t.Run(name, func(t *testing.T) {
			q, err := engine.NewInstantQuery(context.Background(), storage, nil, testCase.expr, testCase.ts)
			require.NoError(t, err)
			defer q.Close()

			res := q.Exec(context.Background())
			require.NoError(t, res.Err)
			testutil.RequireEqual(t, testCase.expected, res.Value)
		})
	}
}

func TestNativeHistogram_Sum_Count_Add_AvgOperator(t *testing.T) {
	// TODO(codesome): Integrate histograms into the PromQL testing framework
	// and write more tests there.
	cases := []struct {
		histograms  []histogram.Histogram
		expected    histogram.FloatHistogram
		expectedAvg histogram.FloatHistogram
	}{
		{
			histograms: []histogram.Histogram{
				{
					CounterResetHint: histogram.GaugeType,
					Schema:           0,
					Count:            25,
					Sum:              1234.5,
					ZeroThreshold:    0.001,
					ZeroCount:        4,
					PositiveSpans: []histogram.Span{
						{Offset: 0, Length: 2},
						{Offset: 1, Length: 2},
					},
					PositiveBuckets: []int64{1, 1, -1, 0},
					NegativeSpans: []histogram.Span{
						{Offset: 0, Length: 2},
						{Offset: 2, Length: 2},
					},

Review comment (suggestion):
I first was surprised why this got removed here, while no framework-based test is added in lieu of it. But then I saw that those framework tests were added earlier. I'll update the commit description to include that fact to avoid future surprises.

Code after:
					Metric: labels.FromStrings("__name__", "some_metric_with_stale_marker"),
					Floats: []promql.FPoint{
						{T: timestamp.FromTime(baseT.Add(time.Minute)), F: 1},
						{T: timestamp.FromTime(baseT.Add(3 * time.Minute)), F: 3},
					},
				},
			},
		},
	}

	for name, testCase := range testCases {
		t.Run(name, func(t *testing.T) {
			q, err := engine.NewInstantQuery(context.Background(), storage, nil, testCase.expr, testCase.ts)
			require.NoError(t, err)
			defer q.Close()

			res := q.Exec(context.Background())
			require.NoError(t, res.Err)
			testutil.RequireEqual(t, testCase.expected, res.Value)
		})
	}
}

func TestQueryLookbackDelta(t *testing.T) {
	var (
		load = `load 5m
metric 0 1 2
`
		query           = "metric"
		lastDatapointTs = time.Unix(600, 0)
	)

	cases := []struct {
		name                          string
		ts                            time.Time
		engineLookback, queryLookback time.Duration
		expectSamples                 bool
	}{
		{
			name:          "default lookback delta",
			ts:            lastDatapointTs.Add(defaultLookbackDelta - time.Millisecond),
			expectSamples: true,
		},
		{
			name:          "outside default lookback delta",
			ts:            lastDatapointTs.Add(defaultLookbackDelta),
			expectSamples: false,
		},
		{
			name:           "custom engine lookback delta",
			ts:             lastDatapointTs.Add(10*time.Minute - time.Millisecond),