github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/util/query_string_builder_test.go (about)

     1  package util
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestQueryStringBuilder(t *testing.T) {
    12  	t.Parallel()
    13  
    14  	tests := map[string]struct {
    15  		input           map[string]interface{}
    16  		expectedEncoded string
    17  	}{
    18  		"should return an empty query string on no params": {
    19  			input:           map[string]interface{}{},
    20  			expectedEncoded: "",
    21  		},
    22  		"should return the URL encoded query string parameters": {
    23  			input: map[string]interface{}{
    24  				"float32":    float32(123.456),
    25  				"float64":    float64(123.456),
    26  				"float64int": float64(12345.0),
    27  				"int32":      32,
    28  				"int64":      int64(64),
    29  				"string":     "foo",
    30  			},
    31  			expectedEncoded: "float32=123.456&float64=123.456&float64int=12345&int32=32&int64=64&string=foo",
    32  		},
    33  	}
    34  
    35  	for testName, testData := range tests {
    36  		testData := testData
    37  
    38  		t.Run(testName, func(t *testing.T) {
    39  			params := NewQueryStringBuilder()
    40  
    41  			for name, value := range testData.input {
    42  				switch value := value.(type) {
    43  				case string:
    44  					params.SetString(name, value)
    45  				case float32:
    46  					params.SetFloat32(name, value)
    47  				case float64:
    48  					params.SetFloat(name, value)
    49  				case int:
    50  					params.SetInt32(name, value)
    51  				case int64:
    52  					params.SetInt(name, value)
    53  				default:
    54  					require.Fail(t, fmt.Sprintf("Unknown data type for test fixture with name '%s'", name))
    55  				}
    56  			}
    57  
    58  			assert.Equal(t, testData.expectedEncoded, params.Encode())
    59  		})
    60  	}
    61  }