github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/ast/pipesearch/searchHandler_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 pipesearch
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func Test_parseSearchBody(t *testing.T) {
    26  
    27  	jssrc := make(map[string]interface{})
    28  	jssrc["searchText"] = "abc def"
    29  	jssrc["indexName"] = "svc-2"
    30  	jssrc["size"] = 200
    31  	nowTs := uint64(1659874108987)
    32  	jssrc["startEpoch"] = "now-15m"
    33  	jssrc["endEpoch"] = "now"
    34  	jssrc["scroll"] = 0
    35  
    36  	stext, sepoch, eepoch, fsize, idxname, scroll := ParseSearchBody(jssrc, nowTs)
    37  	assert.Equal(t, "abc def", stext)
    38  	assert.Equal(t, nowTs-15*60_000, sepoch, "expected=%v, actual=%v", nowTs-15*60_000, sepoch)
    39  	assert.Equal(t, nowTs, eepoch, "expected=%v, actual=%v", nowTs, eepoch)
    40  	assert.Equal(t, uint64(200), fsize, "expected=%v, actual=%v", uint64(200), fsize)
    41  	assert.Equal(t, "svc-2", idxname, "expected=%v, actual=%v", "svc-2", idxname)
    42  	assert.Equal(t, 0, scroll, "expected=%v, actual=%v", 0, scroll)
    43  
    44  	jssrc["from"] = 500
    45  	_, _, _, finalSize, _, scroll := ParseSearchBody(jssrc, nowTs)
    46  	assert.Equal(t, uint64(700), finalSize, "expected=%v, actual=%v", 700, scroll)
    47  	assert.Equal(t, 500, scroll, "expected=%v, actual=%v", 500, scroll)
    48  }
    49  
    50  func Test_parseAlphaNumTime(t *testing.T) {
    51  
    52  	nowTs := uint64(1659874108987)
    53  
    54  	defValue := uint64(12345)
    55  
    56  	inp := "now"
    57  	expected := nowTs
    58  	actual := parseAlphaNumTime(nowTs, inp, defValue)
    59  	assert.Equal(t, expected, actual, "expected=%v, actual=%v", expected, actual)
    60  
    61  	inp = "now-1m"
    62  	expected = nowTs - 1*60_000
    63  	actual = parseAlphaNumTime(nowTs, inp, defValue)
    64  	assert.Equal(t, expected, actual, "expected=%v, actual=%v", expected, actual)
    65  
    66  	inp = "now-12345m"
    67  	expected = nowTs - 12345*60_000
    68  	actual = parseAlphaNumTime(nowTs, inp, defValue)
    69  	assert.Equal(t, expected, actual, "expected=%v, actual=%v", expected, actual)
    70  
    71  	inp = "now-1h"
    72  	expected = nowTs - 1*3600_000
    73  	actual = parseAlphaNumTime(nowTs, inp, defValue)
    74  	assert.Equal(t, expected, actual, "expected=%v, actual=%v", expected, actual)
    75  
    76  	inp = "now-365d"
    77  	expected = nowTs - 365*24*3600*1_000
    78  	actual = parseAlphaNumTime(nowTs, inp, defValue)
    79  	assert.Equal(t, expected, actual, "expected=%v, actual=%v", expected, actual)
    80  
    81  }