github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/integrations/otsdb/query/expqueryparsing_test.go (about)

     1  /*
     2  Copyright 2023.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package otsdbquery
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/siglens/siglens/pkg/segment/structs"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func Test_ExpMetricsQueryParsing(t *testing.T) {
    27  	mainQuery := &structs.OTSDBMetricsQueryExpRequest{
    28  		Time: structs.OTSDBMetricsQueryExpTime{
    29  			Start:      "1h-ago",
    30  			End:        "",
    31  			Timezone:   "UTC",
    32  			Aggregator: "sum",
    33  			Downsampler: structs.OTSDBMetricsQueryExpDownsampler{
    34  				Interval:   "1m",
    35  				Aggregator: "sum",
    36  			},
    37  		},
    38  		Filters: []structs.OTSDBMetricsQueryExpFilter{
    39  			{
    40  				Tags: []structs.OTSDBMetricsQueryExpTags{
    41  					{
    42  						Type:    "literal_or",
    43  						Tagk:    "color",
    44  						Filter:  "*",
    45  						GroupBy: false,
    46  					},
    47  				},
    48  				Id: "f1",
    49  			},
    50  		},
    51  		Metrics: []structs.OTSDBMetricsQueryExpMetric{
    52  			{
    53  				Id:         "m1",
    54  				MetricName: "test.metric.1",
    55  				Filter:     "f1",
    56  			},
    57  		},
    58  		Expressions: []structs.OTSDBMetricsQueryExpressions{},
    59  		Outputs: []structs.OTSDBMetricsQueryExpOutput{
    60  			{
    61  				Id:    "m1",
    62  				Alias: "output-m1",
    63  			},
    64  		},
    65  	}
    66  	var err error
    67  	invalidTimeQuery := *mainQuery
    68  	invalidTimeQuery.Time = structs.OTSDBMetricsQueryExpTime{
    69  		Start:      "",
    70  		End:        "2022-01-01",
    71  		Timezone:   "UTC",
    72  		Aggregator: "sum",
    73  		Downsampler: structs.OTSDBMetricsQueryExpDownsampler{
    74  			Interval:   "1m",
    75  			Aggregator: "sum",
    76  		},
    77  	}
    78  	_, err = MetricsQueryExpressionsParseRequest(&invalidTimeQuery)
    79  	assert.Error(t, err)
    80  
    81  	invalidAggQuery := *mainQuery
    82  	invalidAggQuery.Time.Aggregator = "Multiply"
    83  	_, err = MetricsQueryExpressionsParseRequest(&invalidAggQuery)
    84  	assert.Error(t, err)
    85  
    86  	invalidDownsamplerQuery := *mainQuery
    87  	invalidDownsamplerQuery.Time.Downsampler = structs.OTSDBMetricsQueryExpDownsampler{
    88  		Interval:   "",
    89  		Aggregator: "sum",
    90  	}
    91  	_, err = MetricsQueryExpressionsParseRequest(&invalidDownsamplerQuery)
    92  	assert.Error(t, err)
    93  }