github.com/cloudberrydb/gpbackup@v1.0.3-0.20240118031043-5410fd45eed6/utils/set_test.go (about)

     1  package utils_test
     2  
     3  import (
     4  	"github.com/cloudberrydb/gpbackup/utils"
     5  
     6  	. "github.com/onsi/ginkgo/v2"
     7  	. "github.com/onsi/gomega"
     8  )
     9  
    10  var _ = Describe("utils/set tests", func() {
    11  	Describe("NewSet", func() {
    12  		It("can create a set from an empty list", func() {
    13  			emptySet := utils.NewSet([]string{})
    14  
    15  			expectedMap := map[string]bool{}
    16  			Expect(emptySet.Set).To(Equal(expectedMap))
    17  			Expect(emptySet.IsExclude).To(BeFalse())
    18  			Expect(emptySet.AlwaysMatchesFilter).To(BeFalse())
    19  		})
    20  		It("can create a set from a list of strings", func() {
    21  			setWithEntries := utils.NewSet([]string{"foo", "bar"})
    22  
    23  			expectedMap := map[string]bool{"foo": true, "bar": true}
    24  			Expect(setWithEntries.Set).To(Equal(expectedMap))
    25  			Expect(setWithEntries.IsExclude).To(BeFalse())
    26  			Expect(setWithEntries.AlwaysMatchesFilter).To(BeFalse())
    27  		})
    28  	})
    29  	Describe("NewIncludeSet", func() {
    30  		It("can create a set from an empty list", func() {
    31  			emptyInclude := utils.NewIncludeSet([]string{})
    32  
    33  			expectedMap := map[string]bool{}
    34  			Expect(emptyInclude.Set).To(Equal(expectedMap))
    35  			Expect(emptyInclude.IsExclude).To(BeFalse())
    36  			Expect(emptyInclude.AlwaysMatchesFilter).To(BeTrue())
    37  		})
    38  		It("can create a set from a list of strings", func() {
    39  			includeWithEntries := utils.NewIncludeSet([]string{"foo", "bar"})
    40  
    41  			expectedMap := map[string]bool{"foo": true, "bar": true}
    42  			Expect(includeWithEntries.Set).To(Equal(expectedMap))
    43  			Expect(includeWithEntries.IsExclude).To(BeFalse())
    44  			Expect(includeWithEntries.AlwaysMatchesFilter).To(BeFalse())
    45  		})
    46  	})
    47  	Describe("NewExcludeSet", func() {
    48  		It("can create a set from an empty list", func() {
    49  			emptyExclude := utils.NewExcludeSet([]string{})
    50  
    51  			expectedMap := map[string]bool{}
    52  			Expect(emptyExclude.Set).To(Equal(expectedMap))
    53  			Expect(emptyExclude.IsExclude).To(BeTrue())
    54  			Expect(emptyExclude.AlwaysMatchesFilter).To(BeTrue())
    55  		})
    56  		It("can create a set from a list of strings", func() {
    57  			excludeWithEntries := utils.NewExcludeSet([]string{"foo", "bar"})
    58  
    59  			expectedMap := map[string]bool{"foo": true, "bar": true}
    60  			Expect(excludeWithEntries.Set).To(Equal(expectedMap))
    61  			Expect(excludeWithEntries.IsExclude).To(BeTrue())
    62  			Expect(excludeWithEntries.AlwaysMatchesFilter).To(BeFalse())
    63  		})
    64  	})
    65  	Describe("MatchesFilter", func() {
    66  		Context("Include Set", func() {
    67  			It("returns true if an item is in a non-empty include set", func() {
    68  				includeWithEntries := utils.NewIncludeSet([]string{"foo", "bar"})
    69  				Expect(includeWithEntries.MatchesFilter("foo")).To(BeTrue())
    70  			})
    71  			It("returns false if an item is not in a non-empty include set", func() {
    72  				includeWithEntries := utils.NewIncludeSet([]string{"foo", "bar"})
    73  				Expect(includeWithEntries.MatchesFilter("nonexistent")).To(BeFalse())
    74  			})
    75  			It("returns true if an include set is empty", func() {
    76  				emptyInclude := utils.NewIncludeSet([]string{})
    77  				Expect(emptyInclude.MatchesFilter("foo")).To(BeTrue())
    78  			})
    79  		})
    80  		Context("Exclude Set", func() {
    81  			It("returns true if an item is not in a non-empty exclude set", func() {
    82  				excludeWithEntries := utils.NewExcludeSet([]string{"foo", "bar"})
    83  				Expect(excludeWithEntries.MatchesFilter("nonexistent")).To(BeTrue())
    84  			})
    85  			It("returns false if an item is in a non-empty exclude set", func() {
    86  				excludeWithEntries := utils.NewExcludeSet([]string{"foo", "bar"})
    87  				Expect(excludeWithEntries.MatchesFilter("foo")).To(BeFalse())
    88  			})
    89  			It("returns true if an exclude set is empty", func() {
    90  				emptyExclude := utils.NewExcludeSet([]string{})
    91  				Expect(emptyExclude.MatchesFilter("foo")).To(BeTrue())
    92  			})
    93  		})
    94  	})
    95  	Describe("Length", func() {
    96  		It("returns the length of the underlying map", func() {
    97  			setWithEntries := utils.NewSet([]string{"foo", "bar"})
    98  
    99  			Expect(setWithEntries.Length()).To(Equal(2))
   100  		})
   101  	})
   102  	Describe("Equals", func() {
   103  		set1 := utils.NewIncludeSet([]string{"foo", "bar"})
   104  		set1Copy := utils.NewIncludeSet([]string{"foo", "bar"})
   105  		set2 := utils.NewIncludeSet([]string{"foo"})
   106  		set3 := utils.NewIncludeSet([]string{"foo", "baz"})
   107  
   108  		Context("sets have different elements", func() {
   109  			It("returns false if sets have different number of elements", func() {
   110  				Expect(set1.Equals(set2)).To(BeFalse())
   111  			})
   112  			It("returns false if sets have different elements", func() {
   113  				Expect(set1.Equals(set3)).To(BeFalse())
   114  			})
   115  		})
   116  		It("returns true if sets have the same elements", func() {
   117  			Expect(set1.Equals(set1Copy)).To(BeTrue())
   118  		})
   119  		It("returns true if both sets are empty", func() {
   120  			Expect(utils.NewIncludeSet([]string{}).Equals(utils.NewIncludeSet([]string{}))).To(BeTrue())
   121  		})
   122  	})
   123  })