github.com/google/osv-scalibr@v0.4.1/clients/depsdev/v1alpha1/grpcclient/grpcclient_test.go (about)

     1  // Copyright 2025 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 grpcclient_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	pb "deps.dev/api/v3alpha"
    21  	"github.com/google/go-cmp/cmp"
    22  	"github.com/google/go-cmp/cmp/cmpopts"
    23  	"github.com/google/osv-scalibr/clients/depsdev/v1alpha1/grpcclient"
    24  )
    25  
    26  func TestDefaultConfig(t *testing.T) {
    27  	tests := []struct {
    28  		name string
    29  		want *grpcclient.Config
    30  	}{
    31  		{
    32  			name: "default_config",
    33  			want: &grpcclient.Config{
    34  				Address: "api.deps.dev:443",
    35  			},
    36  		},
    37  	}
    38  
    39  	for _, tc := range tests {
    40  		t.Run(tc.name, func(t *testing.T) {
    41  			got := grpcclient.DefaultConfig()
    42  			if diff := cmp.Diff(tc.want, got); diff != "" {
    43  				t.Errorf("DefaultConfig() returned an unexpected diff (-want +got): %v", diff)
    44  			}
    45  		})
    46  	}
    47  }
    48  
    49  func TestNew(t *testing.T) {
    50  	tests := []struct {
    51  		name    string
    52  		cfg     *grpcclient.Config
    53  		want    pb.InsightsClient
    54  		wantErr error
    55  	}{
    56  		{
    57  			name:    "nil config",
    58  			cfg:     nil,
    59  			wantErr: grpcclient.ErrMalformedConfig,
    60  		},
    61  		{
    62  			name:    "empty address",
    63  			cfg:     &grpcclient.Config{Address: ""},
    64  			wantErr: grpcclient.ErrMalformedConfig,
    65  		},
    66  	}
    67  
    68  	for _, tc := range tests {
    69  		t.Run(tc.name, func(t *testing.T) {
    70  			got, err := grpcclient.New(tc.cfg)
    71  			if !cmp.Equal(err, tc.wantErr, cmpopts.EquateErrors()) {
    72  				t.Errorf("New(%v) returned an unexpected error: %v", tc.cfg, err)
    73  			}
    74  			if diff := cmp.Diff(tc.want, got); diff != "" {
    75  				t.Errorf("New(%v) returned an unexpected diff (-want +got): %v", tc.cfg, diff)
    76  			}
    77  		})
    78  	}
    79  }