github.com/m3db/m3@v1.5.0/src/query/functions/linear/sort_test.go (about)

     1  // Copyright (c) 2020 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package linear
    22  
    23  import (
    24  	"math"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/m3db/m3/src/query/block"
    29  	"github.com/m3db/m3/src/query/executor/transform"
    30  	"github.com/m3db/m3/src/query/models"
    31  	"github.com/m3db/m3/src/query/parser"
    32  	"github.com/m3db/m3/src/query/test"
    33  	"github.com/m3db/m3/src/query/test/compare"
    34  	"github.com/m3db/m3/src/query/test/executor"
    35  	xtime "github.com/m3db/m3/src/x/time"
    36  
    37  	"github.com/stretchr/testify/assert"
    38  	"github.com/stretchr/testify/require"
    39  )
    40  
    41  func TestShouldFailWhenOpTypeIsInvalid(t *testing.T) {
    42  	_, err := NewSortOp("sortAsc")
    43  	require.Error(t, err)
    44  }
    45  
    46  func TestSortAscInstant(t *testing.T) {
    47  	op, err := NewSortOp(SortType)
    48  	require.NoError(t, err)
    49  
    50  	sink := processSortOp(t, op, true)
    51  
    52  	expected := [][]float64{
    53  		{100},
    54  		{200},
    55  		{300},
    56  		{400},
    57  		{500},
    58  		{600},
    59  		{700},
    60  		{800},
    61  		{math.NaN()},
    62  	}
    63  
    64  	assert.Equal(t, seriesMetas, sink.Metas)
    65  	compare.EqualsWithNansWithDelta(t, expected, sink.Values, math.Pow10(-5))
    66  }
    67  
    68  func TestSortDescInstant(t *testing.T) {
    69  	op, err := NewSortOp(SortDescType)
    70  	require.NoError(t, err)
    71  
    72  	sink := processSortOp(t, op, true)
    73  
    74  	expected := [][]float64{
    75  		{800},
    76  		{700},
    77  		{600},
    78  		{500},
    79  		{400},
    80  		{300},
    81  		{200},
    82  		{100},
    83  		{math.NaN()},
    84  	}
    85  
    86  	assert.Equal(t, seriesMetas, sink.Metas)
    87  	compare.EqualsWithNansWithDelta(t, expected, sink.Values, math.Pow10(-5))
    88  }
    89  
    90  func TestSortNop(t *testing.T) {
    91  	op, err := NewSortOp(SortDescType)
    92  	require.NoError(t, err)
    93  
    94  	sink := processSortOp(t, op, false)
    95  
    96  	expected := v
    97  
    98  	assert.Equal(t, seriesMetas, sink.Metas)
    99  	compare.EqualsWithNansWithDelta(t, expected, sink.Values, math.Pow10(-5))
   100  }
   101  
   102  var (
   103  	seriesMetas = []block.SeriesMeta{
   104  		{Tags: test.StringTagsToTags(test.StringTags{{N: "job", V: "api-server"}, {N: "instance", V: "0"}, {N: "group", V: "production"}})},
   105  		{Tags: test.StringTagsToTags(test.StringTags{{N: "job", V: "api-server"}, {N: "instance", V: "1"}, {N: "group", V: "production"}})},
   106  		{Tags: test.StringTagsToTags(test.StringTags{{N: "job", V: "api-server"}, {N: "instance", V: "2"}, {N: "group", V: "production"}})},
   107  		{Tags: test.StringTagsToTags(test.StringTags{{N: "job", V: "api-server"}, {N: "instance", V: "0"}, {N: "group", V: "canary"}})},
   108  		{Tags: test.StringTagsToTags(test.StringTags{{N: "job", V: "api-server"}, {N: "instance", V: "1"}, {N: "group", V: "canary"}})},
   109  		{Tags: test.StringTagsToTags(test.StringTags{{N: "job", V: "app-server"}, {N: "instance", V: "0"}, {N: "group", V: "production"}})},
   110  		{Tags: test.StringTagsToTags(test.StringTags{{N: "job", V: "app-server"}, {N: "instance", V: "1"}, {N: "group", V: "production"}})},
   111  		{Tags: test.StringTagsToTags(test.StringTags{{N: "job", V: "app-server"}, {N: "instance", V: "0"}, {N: "group", V: "canary"}})},
   112  		{Tags: test.StringTagsToTags(test.StringTags{{N: "job", V: "app-server"}, {N: "instance", V: "1"}, {N: "group", V: "canary"}})},
   113  	}
   114  
   115  	v = [][]float64{
   116  		{60, 70, 80, 90, 100},
   117  		{150, 160, 170, 180, 200},
   118  		{180, 210, 240, 270, 300},
   119  		{240, 280, 320, 360, 400},
   120  		{math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN()},
   121  		{300, 350, 400, 450, 500},
   122  		{360, 420, 480, 540, 600},
   123  		{320, 390, 460, 530, 700},
   124  		{480, 560, 640, 720, 800},
   125  	}
   126  
   127  	bounds = models.Bounds{
   128  		Start:    xtime.Now(),
   129  		Duration: time.Minute * 5,
   130  		StepSize: time.Minute,
   131  	}
   132  )
   133  
   134  func processSortOp(t *testing.T, op parser.Params, instant bool) *executor.SinkNode {
   135  	bl := test.NewBlockFromValuesWithSeriesMeta(bounds, seriesMetas, v)
   136  	c, sink := executor.NewControllerWithSink(parser.NodeID(rune(1)))
   137  	node := op.(sortOp).Node(c, transform.Options{})
   138  	queryContext := models.NoopQueryContext()
   139  	queryContext.Options.Instantaneous = instant
   140  	err := node.Process(queryContext, parser.NodeID(rune(0)), bl)
   141  	require.NoError(t, err)
   142  	return sink
   143  }