1// Copyright (c) The Thanos Authors.
2// Licensed under the Apache License 2.0.
3
4package downsample
5
6import (
7	"testing"
8
9	"github.com/prometheus/prometheus/tsdb/chunkenc"
10
11	"github.com/thanos-io/thanos/pkg/testutil"
12)
13
14func TestAggrChunk(t *testing.T) {
15	var input [5][]sample
16
17	input[AggrCount] = []sample{{100, 30}, {200, 50}, {300, 60}, {400, 67}}
18	input[AggrSum] = []sample{{100, 130}, {200, 1000}, {300, 2000}, {400, 5555}}
19	input[AggrMin] = []sample{{100, 0}, {200, -10}, {300, 1000}, {400, -9.5}}
20	// Maximum is absent.
21	input[AggrCounter] = []sample{{100, 5}, {200, 10}, {300, 10.1}, {400, 15}, {400, 3}}
22
23	var chks [5]chunkenc.Chunk
24
25	for i, smpls := range input {
26		if len(smpls) == 0 {
27			continue
28		}
29		chks[i] = chunkenc.NewXORChunk()
30		a, err := chks[i].Appender()
31		testutil.Ok(t, err)
32
33		for _, s := range smpls {
34			a.Append(s.t, s.v)
35		}
36	}
37
38	var res [5][]sample
39	ac := EncodeAggrChunk(chks)
40
41	for _, at := range []AggrType{AggrCount, AggrSum, AggrMin, AggrMax, AggrCounter} {
42		if c, err := ac.Get(at); err != ErrAggrNotExist {
43			testutil.Ok(t, err)
44			testutil.Ok(t, expandChunkIterator(c.Iterator(nil), &res[at]))
45		}
46	}
47	testutil.Equals(t, input, res)
48}
49