...

Source file src/github.com/go-kivik/kivik/v4/options_test.go

Documentation: github.com/go-kivik/kivik/v4

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  package kivik
    14  
    15  import (
    16  	"net/url"
    17  	"testing"
    18  	"time"
    19  
    20  	"github.com/google/go-cmp/cmp"
    21  )
    22  
    23  func TestDuration(t *testing.T) {
    24  	o := Duration("heartbeat", 15*time.Second)
    25  
    26  	query := url.Values{}
    27  	o.Apply(&query)
    28  	want := "15000"
    29  	if got := query.Get("heartbeat"); got != want {
    30  		t.Errorf("Unexpected url query value: %s", got)
    31  	}
    32  
    33  	opts := map[string]interface{}{}
    34  	o.Apply(opts)
    35  	if got := opts["heartbeat"]; got != want {
    36  		t.Errorf("Unexpected map value: %s", got)
    37  	}
    38  }
    39  
    40  func Test_params_Apply(t *testing.T) {
    41  	tests := []struct {
    42  		name   string
    43  		target interface{}
    44  		want   interface{}
    45  		option params
    46  	}{
    47  		{
    48  			name:   "no options",
    49  			target: map[string]interface{}{},
    50  			want:   map[string]interface{}{},
    51  		},
    52  		{
    53  			name:   "map option",
    54  			target: map[string]interface{}{},
    55  			want:   map[string]interface{}{"foo": "bar"},
    56  			option: params{"foo": "bar"},
    57  		},
    58  		{
    59  			name:   "unsupported target type",
    60  			target: "",
    61  			want:   "",
    62  			option: params{"foo": "bar"},
    63  		},
    64  		{
    65  			name:   "query, string",
    66  			target: &url.Values{},
    67  			want:   &url.Values{"foo": []string{"bar"}},
    68  			option: params{"foo": "bar"},
    69  		},
    70  		{
    71  			name:   "query, bool",
    72  			target: &url.Values{},
    73  			want:   &url.Values{"foo": []string{"true"}},
    74  			option: params{"foo": true},
    75  		},
    76  		{
    77  			name:   "query, int",
    78  			target: &url.Values{},
    79  			want:   &url.Values{"foo": []string{"42"}},
    80  			option: params{"foo": 42},
    81  		},
    82  		{
    83  			name:   "query, float64",
    84  			target: &url.Values{},
    85  			want:   &url.Values{"foo": []string{"42.5"}},
    86  			option: params{"foo": 42.5},
    87  		},
    88  		{
    89  			name:   "query, float32",
    90  			target: &url.Values{},
    91  			want:   &url.Values{"foo": []string{"42.5"}},
    92  			option: params{"foo": float32(42.5)},
    93  		},
    94  		{
    95  			name:   "query, []string",
    96  			target: &url.Values{},
    97  			want:   &url.Values{"foo": []string{"bar", "baz"}},
    98  			option: params{"foo": []string{"bar", "baz"}},
    99  		},
   100  	}
   101  
   102  	for _, tt := range tests {
   103  		t.Run(tt.name, func(t *testing.T) {
   104  			tt.option.Apply(tt.target)
   105  			if d := cmp.Diff(tt.want, tt.target); d != "" {
   106  				t.Errorf("Unexpected result (-want, +got):\n%s", d)
   107  			}
   108  		})
   109  	}
   110  }
   111  

View as plain text