...

Source file src/cloud.google.com/go/storage/option_test.go

Documentation: cloud.google.com/go/storage

     1  // Copyright 2023 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package storage
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/google/go-cmp/cmp"
    21  	"google.golang.org/api/option"
    22  )
    23  
    24  func TestApplyStorageOpt(t *testing.T) {
    25  	for _, test := range []struct {
    26  		desc string
    27  		opts []option.ClientOption
    28  		want storageConfig
    29  	}{
    30  		{
    31  			desc: "set JSON option",
    32  			opts: []option.ClientOption{WithJSONReads()},
    33  			want: storageConfig{
    34  				useJSONforReads: true,
    35  				readAPIWasSet:   true,
    36  			},
    37  		},
    38  		{
    39  			desc: "set XML option",
    40  			opts: []option.ClientOption{WithXMLReads()},
    41  			want: storageConfig{
    42  				useJSONforReads: false,
    43  				readAPIWasSet:   true,
    44  			},
    45  		},
    46  		{
    47  			desc: "set conflicting options, last option set takes precedence",
    48  			opts: []option.ClientOption{WithJSONReads(), WithXMLReads()},
    49  			want: storageConfig{
    50  				useJSONforReads: false,
    51  				readAPIWasSet:   true,
    52  			},
    53  		},
    54  		{
    55  			desc: "empty options",
    56  			opts: []option.ClientOption{},
    57  			want: storageConfig{
    58  				useJSONforReads: false,
    59  				readAPIWasSet:   false,
    60  			},
    61  		},
    62  		{
    63  			desc: "set Google API option",
    64  			opts: []option.ClientOption{option.WithEndpoint("")},
    65  			want: storageConfig{
    66  				useJSONforReads: false,
    67  				readAPIWasSet:   false,
    68  			},
    69  		},
    70  	} {
    71  		t.Run(test.desc, func(t *testing.T) {
    72  			var got storageConfig
    73  			for _, opt := range test.opts {
    74  				if storageOpt, ok := opt.(storageClientOption); ok {
    75  					storageOpt.ApplyStorageOpt(&got)
    76  				}
    77  			}
    78  			if !cmp.Equal(got, test.want, cmp.AllowUnexported(storageConfig{})) {
    79  				t.Errorf(cmp.Diff(got, test.want, cmp.AllowUnexported(storageConfig{})))
    80  			}
    81  		})
    82  	}
    83  }
    84  

View as plain text