github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/ast/common_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 ast
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/siglens/siglens/pkg/segment/structs"
    23  	"github.com/siglens/siglens/pkg/segment/utils"
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func Test_GetDefaultTimechartSpanOptions(t *testing.T) {
    28  	type args struct {
    29  		startEpoch uint64
    30  		endEpoch   uint64
    31  		qid        uint64
    32  	}
    33  	tests := []struct {
    34  		name    string
    35  		args    args
    36  		want    *structs.SpanOptions
    37  		wantErr bool
    38  	}{
    39  		{"startEpoch = 0 should be error", args{0, 1, 1}, nil, true},
    40  		{"endEpoch = 0 should be error", args{1, 0, 1}, nil, true},
    41  		{"<15*60*1000 should be TMSecond with Num = 10",
    42  			args{1, 5*60*1000 + 1, 1},
    43  			&structs.SpanOptions{SpanLength: &structs.SpanLength{Num: 10, TimeScalr: utils.TMSecond}, DefaultSettings: false},
    44  			false},
    45  		{"15*60*1000 should be TMSecond with Num = 10",
    46  			args{1, 15*60*1000 + 1, 1},
    47  			&structs.SpanOptions{SpanLength: &structs.SpanLength{Num: 10, TimeScalr: utils.TMSecond}, DefaultSettings: false},
    48  			false},
    49  		{"<60*60*1000 should be TMMinute with Num = 1",
    50  			args{1, 30*60*1000 + 1, 1},
    51  			&structs.SpanOptions{SpanLength: &structs.SpanLength{Num: 1, TimeScalr: utils.TMMinute}, DefaultSettings: false},
    52  			false},
    53  		{"60*60*1000 should be TMMinute with Num = 1",
    54  			args{1, 60*60*1000 + 1, 1},
    55  			&structs.SpanOptions{SpanLength: &structs.SpanLength{Num: 1, TimeScalr: utils.TMMinute}, DefaultSettings: false},
    56  			false},
    57  		{"<4*60*60*1000 should be TMMinute with Num = 5",
    58  			args{1, 2*60*60*1000 + 1, 1},
    59  			&structs.SpanOptions{SpanLength: &structs.SpanLength{Num: 5, TimeScalr: utils.TMMinute}, DefaultSettings: false},
    60  			false},
    61  		{"4*60*60*1000 should be TMMinute with Num = 5",
    62  			args{1, 4*60*60*1000 + 1, 1},
    63  			&structs.SpanOptions{SpanLength: &structs.SpanLength{Num: 5, TimeScalr: utils.TMMinute}, DefaultSettings: false},
    64  			false},
    65  		{"<24*60*60*1000 should be TMMinute with Num = 30",
    66  			args{1, 20*60*60*1000 + 1, 1},
    67  			&structs.SpanOptions{SpanLength: &structs.SpanLength{Num: 30, TimeScalr: utils.TMMinute}, DefaultSettings: false},
    68  			false},
    69  		{"24*60*60*1000 should be TMMinute with Num = 30",
    70  			args{1, 24*60*60*1000 + 1, 1},
    71  			&structs.SpanOptions{SpanLength: &structs.SpanLength{Num: 30, TimeScalr: utils.TMMinute}, DefaultSettings: false},
    72  			false},
    73  		{"<7*24*60*60*1000 should be TMHour with Num = 1",
    74  			args{1, 6*24*60*60*1000 + 1, 1},
    75  			&structs.SpanOptions{SpanLength: &structs.SpanLength{Num: 1, TimeScalr: utils.TMHour}, DefaultSettings: false},
    76  			false},
    77  		{"7*24*60*60*1000 should be TMHour with Num = 1",
    78  			args{1, 7*24*60*60*1000 + 1, 1},
    79  			&structs.SpanOptions{SpanLength: &structs.SpanLength{Num: 1, TimeScalr: utils.TMHour}, DefaultSettings: false},
    80  			false},
    81  		{"<180*24*60*60*1000 should be TMDay with Num = 1",
    82  			args{1, 179*24*60*60*1000 + 1, 1},
    83  			&structs.SpanOptions{SpanLength: &structs.SpanLength{Num: 1, TimeScalr: utils.TMDay}, DefaultSettings: false},
    84  			false},
    85  		{"180*24*60*60*1000 should be TMDay with Num = 1",
    86  			args{1, 180*24*60*60*1000 + 1, 1},
    87  			&structs.SpanOptions{SpanLength: &structs.SpanLength{Num: 1, TimeScalr: utils.TMDay}, DefaultSettings: false},
    88  			false},
    89  		{">180*24*60*60*1000 should be TMDay with Num = 1",
    90  			args{1, 181*24*60*60*1000 + 1, 1},
    91  			&structs.SpanOptions{SpanLength: &structs.SpanLength{Num: 1, TimeScalr: utils.TMMonth}, DefaultSettings: false},
    92  			false},
    93  	}
    94  	for _, tt := range tests {
    95  		t.Run(tt.name, func(t *testing.T) {
    96  			got, err := GetDefaultTimechartSpanOptions(tt.args.startEpoch, tt.args.endEpoch, tt.args.qid)
    97  			assert.Equal(t, err != nil, tt.wantErr)
    98  			assert.Equal(t, got, tt.want)
    99  		})
   100  	}
   101  }