github.com/paketo-buildpacks/libpak@v1.70.0/bindings/resolve_test.go (about)

     1  /*
     2   * Copyright 2018-2020 the original author or authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      https://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package bindings_test
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/buildpacks/libcnb"
    23  	. "github.com/onsi/gomega"
    24  	"github.com/sclevine/spec"
    25  
    26  	"github.com/paketo-buildpacks/libpak/bindings"
    27  )
    28  
    29  func testResolve(t *testing.T, context spec.G, it spec.S) {
    30  	var (
    31  		Expect = NewWithT(t).Expect
    32  
    33  		binds libcnb.Bindings
    34  	)
    35  
    36  	it.Before(func() {
    37  		binds = []libcnb.Binding{
    38  			{
    39  				Name:     "name1",
    40  				Type:     "some-type",
    41  				Provider: "some-provider",
    42  			},
    43  			{
    44  				Name:     "name2",
    45  				Type:     "some-type",
    46  				Provider: "other-provider",
    47  			},
    48  			{
    49  				Name:     "name3",
    50  				Type:     "other-type",
    51  				Provider: "some-provider",
    52  			},
    53  			{
    54  				Name:     "name1",
    55  				Type:     "unknown",
    56  				Provider: "unknown",
    57  			},
    58  		}
    59  	})
    60  
    61  	context("Resolve", func() {
    62  		context("no predicate", func() {
    63  			it("returns all bindings", func() {
    64  				resolved := bindings.Resolve(binds)
    65  				Expect(resolved).To(Equal(binds))
    66  			})
    67  		})
    68  
    69  		context("ByType", func() {
    70  			it("returns all with matching type", func() {
    71  				resolved := bindings.Resolve(binds,
    72  					bindings.OfType("some-type"),
    73  				)
    74  				Expect(resolved).To(Equal(libcnb.Bindings{
    75  					{
    76  						Name:     "name1",
    77  						Type:     "some-type",
    78  						Provider: "some-provider",
    79  					},
    80  					{
    81  						Name:     "name2",
    82  						Type:     "some-type",
    83  						Provider: "other-provider",
    84  					},
    85  				}))
    86  			})
    87  		})
    88  
    89  		context("ByProvider", func() {
    90  			it("returns all with matching type", func() {
    91  				resolved := bindings.Resolve(binds,
    92  					bindings.OfProvider("some-provider"),
    93  				)
    94  				Expect(resolved).To(Equal(libcnb.Bindings{
    95  					{
    96  						Name:     "name1",
    97  						Type:     "some-type",
    98  						Provider: "some-provider",
    99  					},
   100  					{
   101  						Name:     "name3",
   102  						Type:     "other-type",
   103  						Provider: "some-provider",
   104  					},
   105  				}))
   106  			})
   107  		})
   108  
   109  		context("WithName", func() {
   110  			it("returns all with matching name", func() {
   111  				resolved := bindings.Resolve(binds,
   112  					bindings.WithName("Name1"),
   113  				)
   114  				Expect(resolved).To(Equal(libcnb.Bindings{
   115  					{
   116  						Name:     "name1",
   117  						Type:     "some-type",
   118  						Provider: "some-provider",
   119  					},
   120  					{
   121  						Name:     "name1",
   122  						Type:     "unknown",
   123  						Provider: "unknown",
   124  					},
   125  				}))
   126  			})
   127  		})
   128  
   129  		context("multiple predicates", func() {
   130  			it("returns the intersection", func() {
   131  				resolved := bindings.Resolve(binds,
   132  					bindings.OfType("some-type"),
   133  					bindings.OfProvider("some-provider"),
   134  				)
   135  				Expect(resolved).To(Equal(libcnb.Bindings{
   136  					{
   137  						Name:     "name1",
   138  						Type:     "some-type",
   139  						Provider: "some-provider",
   140  					},
   141  				}))
   142  			})
   143  		})
   144  
   145  		context("zero bindings match", func() {
   146  			it("returns nil", func() {
   147  				resolved := bindings.Resolve(binds,
   148  					bindings.OfType("missing-type"),
   149  				)
   150  				Expect(resolved).To(BeNil())
   151  			})
   152  		})
   153  	})
   154  
   155  	context("ResolveOne", func() {
   156  		context("one binding matches", func() {
   157  			it("returns the binding and true", func() {
   158  				bind, ok, err := bindings.ResolveOne(binds,
   159  					bindings.OfType("some-type"),
   160  					bindings.OfProvider("some-provider"),
   161  				)
   162  				Expect(err).NotTo(HaveOccurred())
   163  				Expect(ok).To(BeTrue())
   164  				Expect(bind).To(Equal(libcnb.Binding{
   165  					Name:     "name1",
   166  					Type:     "some-type",
   167  					Provider: "some-provider",
   168  				}))
   169  			})
   170  		})
   171  
   172  		context("multiples match", func() {
   173  			it("returns an error", func() {
   174  				_, _, err := bindings.ResolveOne(binds,
   175  					bindings.OfType("some-type"),
   176  				)
   177  				Expect(err).To(MatchError(`multiple bindings matched the given predicates [name1 name2]`))
   178  			})
   179  		})
   180  
   181  		context("zero bindings match", func() {
   182  			it("returns the binding and false", func() {
   183  				_, ok, err := bindings.ResolveOne(binds,
   184  					bindings.OfType("missing-type"),
   185  				)
   186  				Expect(err).NotTo(HaveOccurred())
   187  				Expect(ok).To(BeFalse())
   188  			})
   189  		})
   190  	})
   191  }