github.com/govau/cf-common@v0.0.7/env/ups_test.go (about)

     1  package env
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/cloudfoundry-community/go-cfenv"
     7  )
     8  
     9  func TestWithUPSLookup(t *testing.T) {
    10  	services := cfenv.Services{"service-a-label": []cfenv.Service{
    11  		{
    12  			Name: "service-1",
    13  			Credentials: map[string]interface{}{
    14  				"A":     "a",
    15  				"Int":   1,
    16  				"Bool":  true,
    17  				"Float": 0.42,
    18  			},
    19  		},
    20  	}}
    21  
    22  	tests := []struct {
    23  		testName    string
    24  		app         *cfenv.App
    25  		serviceName string
    26  		name        string
    27  		want        string
    28  	}{
    29  		{
    30  			testName:    "app nil",
    31  			app:         nil,
    32  			serviceName: "service-1",
    33  			name:        "A",
    34  			want:        "",
    35  		},
    36  		{
    37  			testName:    "service name empty",
    38  			app:         &cfenv.App{},
    39  			serviceName: "",
    40  			name:        "A",
    41  			want:        "",
    42  		},
    43  		{
    44  			testName: "service not found",
    45  			app: &cfenv.App{
    46  				Services: services,
    47  			},
    48  			serviceName: "service-z",
    49  			name:        "A",
    50  			want:        "",
    51  		},
    52  		{
    53  			testName: "service found, environment variable not found",
    54  			app: &cfenv.App{
    55  				Services: services,
    56  			},
    57  			serviceName: "service-1",
    58  			name:        "Z",
    59  			want:        "",
    60  		},
    61  		{
    62  			testName: "service found, environment variable found",
    63  			app: &cfenv.App{
    64  				Services: services,
    65  			},
    66  			serviceName: "service-1",
    67  			name:        "A",
    68  			want:        "a",
    69  		},
    70  		{
    71  			testName: "service found, environment variable found, value not castable to string (1)",
    72  			app: &cfenv.App{
    73  				Services: services,
    74  			},
    75  			serviceName: "service-1",
    76  			name:        "Int",
    77  			want:        "1",
    78  		},
    79  		{
    80  			testName: "service found, environment variable found, value not castable to string (2)",
    81  			app: &cfenv.App{
    82  				Services: services,
    83  			},
    84  			serviceName: "service-1",
    85  			name:        "Bool",
    86  			want:        "true",
    87  		},
    88  		{
    89  			testName: "service found, environment variable found, value not castable to string (3)",
    90  			app: &cfenv.App{
    91  				Services: services,
    92  			},
    93  			serviceName: "service-1",
    94  			name:        "Float",
    95  			want:        "0.42",
    96  		},
    97  	}
    98  	for _, tt := range tests {
    99  		t.Run(tt.testName, func(t *testing.T) {
   100  			vs := NewVarSet(WithUPSLookup(tt.app, tt.serviceName))
   101  			if got := vs.String(tt.name, ""); got != tt.want {
   102  				t.Errorf("String(%q) = %q, want %q", tt.name, got, tt.want)
   103  			}
   104  		})
   105  	}
   106  }