github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/utils/bitutils_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 utils
    18  
    19  import (
    20  	"encoding/json"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func Test_ExtractTimeStamp(t *testing.T) {
    27  	m := make(map[string]interface{})
    28  
    29  	// Case 1: timestamp as json.Number
    30  	m["timestamp"] = json.Number("950823120000")
    31  	rawJson, _ := json.Marshal(m)
    32  	tskeyCfg := "timestamp"
    33  	ts_millis := ExtractTimeStamp(rawJson, &tskeyCfg)
    34  	assert.Equal(t, uint64(950823120000), ts_millis)
    35  
    36  	// Case 2: timestamp as String
    37  	m["timestamp"] = "950823120000"
    38  	rawJson, _ = json.Marshal(m)
    39  	ts_millis = ExtractTimeStamp(rawJson, &tskeyCfg)
    40  	assert.Equal(t, uint64(950823120000), ts_millis)
    41  }
    42  
    43  func Test_ConvertTimestampToMillis(t *testing.T) {
    44  	// valid timestamps
    45  	value := "950823120000"
    46  	ts_millis, _ := convertTimestampToMillis(value)
    47  	assert.Equal(t, uint64(950823120000), ts_millis)
    48  
    49  	value = "950823120"
    50  	ts_millis, _ = convertTimestampToMillis(value)
    51  	assert.Equal(t, uint64(950823120000), ts_millis)
    52  
    53  	value = "2019-06-11T16:33:51Z"
    54  	ts_millis, _ = convertTimestampToMillis(value)
    55  	assert.Equal(t, uint64(1560270831000), ts_millis)
    56  
    57  	value = "2020-08-03T07:10:20.123456+02:00"
    58  	ts_millis, _ = convertTimestampToMillis(value)
    59  	assert.Equal(t, uint64(1596431420123), ts_millis)
    60  
    61  	// invalid timestamps
    62  	value = "random string"
    63  	_, err := convertTimestampToMillis(value)
    64  	assert.NotNil(t, err)
    65  
    66  	value = "20201-08-03T07:10:20.123456+02:00"
    67  	_, err = convertTimestampToMillis(value)
    68  	assert.NotNil(t, err)
    69  }