github.com/cloudberrydb/gpbackup@v1.0.3-0.20240118031043-5410fd45eed6/testutils/functions_test.go (about) 1 package testutils_test 2 3 import ( 4 "testing" 5 6 "github.com/cloudberrydb/gpbackup/testutils" 7 "github.com/cloudberrydb/gpbackup/toc" 8 "github.com/cloudberrydb/gpbackup/utils" 9 10 . "github.com/onsi/ginkgo/v2" 11 . "github.com/onsi/gomega" 12 . "github.com/onsi/gomega/gbytes" 13 ) 14 15 func TestTestUtils(t *testing.T) { 16 RegisterFailHandler(Fail) 17 RunSpecs(t, "testutils tests") 18 } 19 20 var _ = Describe("testutils/functions", func() { 21 var buffer *Buffer 22 BeforeEach(func() { 23 buffer = NewBuffer() 24 }) 25 Describe("SliceBufferByEntries()", func() { 26 It("returns a one item slice", func() { 27 bufferLen := utils.MustPrintf(buffer, "CREATE TABLE foo (i int);") 28 entries := []toc.MetadataEntry{{Name: "name", Schema: "schema", ObjectType: "TABLE", StartByte: 0, EndByte: bufferLen}} 29 results, remaining := testutils.SliceBufferByEntries(entries, buffer) 30 Expect(remaining).To(Equal("")) 31 Expect(results).To(HaveLen(1)) 32 Expect(results[0]).To(Equal("CREATE TABLE foo (i int);")) 33 }) 34 It("returns a multi-item slice with spaces and newlines", func() { 35 table1Len := utils.MustPrintf(buffer, "CREATE TABLE foo (i int);") 36 table2Len := utils.MustPrintf(buffer, "CREATE TABLE bar (j int);") 37 entries := []toc.MetadataEntry{{Name: "name", Schema: "schema", ObjectType: "TABLE", StartByte: 0, EndByte: table1Len}, {Name: "name", Schema: "schema", ObjectType: "TABLE", StartByte: table1Len, EndByte: table1Len + table2Len}} 38 results, remaining := testutils.SliceBufferByEntries(entries, buffer) 39 Expect(remaining).To(Equal("")) 40 Expect(results).To(HaveLen(2)) 41 Expect(results[0]).To(Equal("CREATE TABLE foo (i int);")) 42 Expect(results[1]).To(Equal("CREATE TABLE bar (j int);")) 43 }) 44 It("returns a single slice with start within buffer, end outside buffer", func() { 45 bufferLen := utils.MustPrintf(buffer, "CREATE TABLE foo (i int);") 46 entries := []toc.MetadataEntry{{Name: "name", Schema: "schema", ObjectType: "TABLE", StartByte: 0, EndByte: bufferLen + 10}} 47 results, remaining := testutils.SliceBufferByEntries(entries, buffer) 48 Expect(remaining).To(Equal("")) 49 Expect(results).To(HaveLen(1)) 50 Expect(results[0]).To(Equal("CREATE TABLE foo (i int);")) 51 }) 52 It("returns multiple slices with start outside buffer, end outside buffer", func() { 53 bufferLen := utils.MustPrintf(buffer, "CREATE TABLE foo (i int);") 54 entries := []toc.MetadataEntry{{Name: "name", Schema: "schema", ObjectType: "TABLE", StartByte: 0, EndByte: bufferLen + 10}, {Name: "name", Schema: "schema", ObjectType: "TABLE", StartByte: bufferLen + 10, EndByte: bufferLen + 40}} 55 results, remaining := testutils.SliceBufferByEntries(entries, buffer) 56 Expect(remaining).To(Equal("")) 57 Expect(results).To(HaveLen(2)) 58 Expect(results[0]).To(Equal("CREATE TABLE foo (i int);")) 59 Expect(results[1]).To(Equal("")) 60 }) 61 It("returns a single slice with extra buffer contents", func() { 62 bufferLen := utils.MustPrintf(buffer, "CREATE TABLE foo (i int);") 63 utils.MustPrintf(buffer, "More extra stuff") 64 entries := []toc.MetadataEntry{{Name: "name", Schema: "schema", ObjectType: "TABLE", StartByte: 0, EndByte: bufferLen}} 65 _, remaining := testutils.SliceBufferByEntries(entries, buffer) 66 Expect(remaining).To(Equal("More extra stuff")) 67 }) 68 }) 69 Describe("CompareSlicesIgnoringWhitespace()", func() { 70 It("returns true when slices and buffer are equal", func() { 71 actual := []string{"CREATE TABLE foo (i int);"} 72 expected := []string{"CREATE TABLE foo (i int);"} 73 Expect(testutils.CompareSlicesIgnoringWhitespace(actual, expected)).To(BeTrue()) 74 }) 75 It("returns true when slices and buffer are equal other than whitespace", func() { 76 actual := []string{"\n\t CREATE TABLE foo (i int);\n"} 77 expected := []string{"CREATE TABLE foo (i int);"} 78 Expect(testutils.CompareSlicesIgnoringWhitespace(actual, expected)).To(BeTrue()) 79 }) 80 It("returns false when slices are of different lengths", func() { 81 actual := make([]string, 0) 82 expected := []string{"CREATE TABLE foo (i int);"} 83 Expect(testutils.CompareSlicesIgnoringWhitespace(actual, expected)).To(BeFalse()) 84 }) 85 }) 86 })