github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi/opensearchapi_internal_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  //
     3  // The OpenSearch Contributors require contributions made to
     4  // this file be licensed under the Apache-2.0 license or a
     5  // compatible open source license.
     6  //
     7  // Modifications Copyright OpenSearch Contributors. See
     8  // GitHub history for details.
     9  
    10  // Licensed to Elasticsearch B.V. under one or more contributor
    11  // license agreements. See the NOTICE file distributed with
    12  // this work for additional information regarding copyright
    13  // ownership. Elasticsearch B.V. licenses this file to you under
    14  // the Apache License, Version 2.0 (the "License"); you may
    15  // not use this file except in compliance with the License.
    16  // You may obtain a copy of the License at
    17  //
    18  //    http://www.apache.org/licenses/LICENSE-2.0
    19  //
    20  // Unless required by applicable law or agreed to in writing,
    21  // software distributed under the License is distributed on an
    22  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    23  // KIND, either express or implied.  See the License for the
    24  // specific language governing permissions and limitations
    25  // under the License.
    26  
    27  // +build !integration
    28  
    29  package opensearchapi
    30  
    31  import (
    32  	"testing"
    33  	"time"
    34  )
    35  
    36  func TestAPIHelpers(t *testing.T) {
    37  	t.Run("BoolPtr", func(t *testing.T) {
    38  		v := BoolPtr(false)
    39  		if v == nil || *v != false {
    40  			t.Errorf("Expected false, got: %v", v)
    41  		}
    42  
    43  		v = BoolPtr(true)
    44  		if v == nil || *v != true {
    45  			t.Errorf("Expected true, got: %v", v)
    46  		}
    47  	})
    48  
    49  	t.Run("IntPtr", func(t *testing.T) {
    50  		v := IntPtr(0)
    51  		if v == nil || *v != 0 {
    52  			t.Errorf("Expected 0, got: %v", v)
    53  		}
    54  	})
    55  
    56  	t.Run("FormatDuration", func(t *testing.T) {
    57  		var tt = []struct {
    58  			duration time.Duration
    59  			expected string
    60  		}{
    61  			{1 * time.Nanosecond, "1nanos"},
    62  			{100 * time.Nanosecond, "100nanos"},
    63  			{1 * time.Microsecond, "1000nanos"},
    64  			{1 * time.Millisecond, "1ms"},
    65  			{100 * time.Millisecond, "100ms"},
    66  			{1 * time.Minute, "60000ms"},
    67  			{10 * time.Minute, "600000ms"},
    68  			{1 * time.Hour, "3600000ms"},
    69  			{10 * time.Hour, "36000000ms"},
    70  		}
    71  
    72  		for _, tc := range tt {
    73  			actual := formatDuration(tc.duration)
    74  			if actual != tc.expected {
    75  				t.Errorf("Unexpected output: got=%s, want=%s", actual, tc.expected)
    76  			}
    77  		}
    78  	})
    79  }