bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/bosun/expr/map_test.go (about)

     1  package expr
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"bosun.org/opentsdb"
     8  )
     9  
    10  func TestMap(t *testing.T) {
    11  	err := testExpression(exprInOut{
    12  		`map(series("test=test", 0, 1, 1, 3), expr(v()+1))`,
    13  		Results{
    14  			Results: ResultSlice{
    15  				&Result{
    16  					Value: Series{
    17  						time.Unix(0, 0): 2,
    18  						time.Unix(1, 0): 4,
    19  					},
    20  					Group: opentsdb.TagSet{"test": "test"},
    21  				},
    22  			},
    23  		},
    24  		false,
    25  	}, t)
    26  	if err != nil {
    27  		t.Error(err)
    28  	}
    29  
    30  	err = testExpression(exprInOut{
    31  		`avg(map(series("test=test", 0, 1, 1, 3), expr(v()+1)))`,
    32  		Results{
    33  			Results: ResultSlice{
    34  				&Result{
    35  					Value: Number(3),
    36  					Group: opentsdb.TagSet{"test": "test"},
    37  				},
    38  			},
    39  		},
    40  		false,
    41  	}, t)
    42  	if err != nil {
    43  		t.Error(err)
    44  	}
    45  
    46  	err = testExpression(exprInOut{
    47  		`1 + avg(map(series("test=test", 0, 1, 1, 3), expr(v()+1))) + 1`,
    48  		Results{
    49  			Results: ResultSlice{
    50  				&Result{
    51  					Value: Number(5),
    52  					Group: opentsdb.TagSet{"test": "test"},
    53  				},
    54  			},
    55  		},
    56  		false,
    57  	}, t)
    58  	if err != nil {
    59  		t.Error(err)
    60  	}
    61  
    62  	err = testExpression(exprInOut{
    63  		`max(map(series("test=test", 0, 1, 1, 3), expr(v()+v())))`,
    64  		Results{
    65  			Results: ResultSlice{
    66  				&Result{
    67  					Value: Number(6),
    68  					Group: opentsdb.TagSet{"test": "test"},
    69  				},
    70  			},
    71  		},
    72  		false,
    73  	}, t)
    74  	if err != nil {
    75  		t.Error(err)
    76  	}
    77  
    78  	err = testExpression(exprInOut{
    79  		`map(series("test=test", 0, -2, 1, 3), expr(1+1))`,
    80  		Results{
    81  			Results: ResultSlice{
    82  				&Result{
    83  					Value: Series{
    84  						time.Unix(0, 0): 2,
    85  						time.Unix(1, 0): 2,
    86  					},
    87  					Group: opentsdb.TagSet{"test": "test"},
    88  				},
    89  			},
    90  		},
    91  		false,
    92  	}, t)
    93  	if err != nil {
    94  		t.Error(err)
    95  	}
    96  
    97  	err = testExpression(exprInOut{
    98  		`map(series("test=test", 0, -2, 1, 3), expr(abs(v())))`,
    99  		Results{
   100  			Results: ResultSlice{
   101  				&Result{
   102  					Value: Series{
   103  						time.Unix(0, 0): 2,
   104  						time.Unix(1, 0): 3,
   105  					},
   106  					Group: opentsdb.TagSet{"test": "test"},
   107  				},
   108  			},
   109  		},
   110  		false,
   111  	}, t)
   112  	if err != nil {
   113  		t.Error(err)
   114  	}
   115  
   116  	err = testExpression(exprInOut{
   117  		`map(series("test=test", 0, -2, 1, 3), expr(series("test=test", 0, v())))`,
   118  		Results{
   119  			Results: ResultSlice{
   120  				&Result{
   121  					Value: Series{},
   122  					Group: opentsdb.TagSet{"test": "test"},
   123  				},
   124  			},
   125  		},
   126  		true, // expect parse error here, series result not valid as TypeNumberExpr
   127  	}, t)
   128  	if err != nil {
   129  		t.Error(err)
   130  	}
   131  
   132  	err = testExpression(exprInOut{
   133  		`v()`,
   134  		Results{
   135  			Results: ResultSlice{
   136  				&Result{
   137  					Value: Series{},
   138  					Group: opentsdb.TagSet{"test": "test"},
   139  				},
   140  			},
   141  		},
   142  		true, // v() is not valid outside a map expression
   143  	}, t)
   144  	if err != nil {
   145  		t.Error(err)
   146  	}
   147  
   148  	err = testExpression(exprInOut{
   149  		`map(series("test=test", 0, -2, 1, 0), expr(!v()))`,
   150  		Results{
   151  			Results: ResultSlice{
   152  				&Result{
   153  					Value: Series{
   154  						time.Unix(0, 0): 0,
   155  						time.Unix(1, 0): 1,
   156  					},
   157  					Group: opentsdb.TagSet{"test": "test"},
   158  				},
   159  			},
   160  		},
   161  		false,
   162  	}, t)
   163  	if err != nil {
   164  		t.Error(err)
   165  	}
   166  }