github.com/cloudfoundry/postgres-release/src/acceptance-tests@v0.0.0-20240511030151-872bdd2e0dba/testing/helpers/postgres_load_sets_test.go (about) 1 package helpers_test 2 3 import ( 4 "github.com/cloudfoundry/postgres-release/src/acceptance-tests/testing/helpers" 5 6 . "github.com/onsi/ginkgo/v2" 7 . "github.com/onsi/gomega" 8 ) 9 10 var _ = Describe("Postgres load sets", func() { 11 Context("Generate valid tables lists", func() { 12 It("Correctly creates a table list", func() { 13 tables := helpers.GetSampleLoadWithPrefix(helpers.Test2Load, "pgats_table") 14 expected := []helpers.PGLoadTable{ 15 helpers.PGLoadTable{ 16 Name: "pgats_table_0", 17 ColumnNames: []string{ 18 "column0", 19 "column1", 20 "column2", 21 "column3", 22 }, 23 ColumnTypes: []string{ 24 helpers.RowSamples[0][0].(string), 25 helpers.RowSamples[1][0].(string), 26 helpers.RowSamples[2][0].(string), 27 helpers.RowSamples[0][0].(string), 28 }, 29 SampleRow: []interface{}{ 30 helpers.RowSamples[0][1], 31 helpers.RowSamples[1][1], 32 helpers.RowSamples[2][1], 33 helpers.RowSamples[0][1], 34 }, 35 NumRows: 5, 36 }, 37 helpers.PGLoadTable{ 38 Name: "pgats_table_1", 39 ColumnNames: []string{ 40 "column0", 41 "column1", 42 "column2", 43 "column3", 44 }, 45 ColumnTypes: []string{ 46 helpers.RowSamples[0][0].(string), 47 helpers.RowSamples[1][0].(string), 48 helpers.RowSamples[2][0].(string), 49 helpers.RowSamples[0][0].(string), 50 }, 51 SampleRow: []interface{}{ 52 helpers.RowSamples[0][1], 53 helpers.RowSamples[1][1], 54 helpers.RowSamples[2][1], 55 helpers.RowSamples[0][1], 56 }, 57 NumRows: 5, 58 }, 59 } 60 Expect(tables).To(Equal(expected)) 61 }) 62 }) 63 Context("With a good table in input", func() { 64 var table helpers.PGLoadTable 65 66 BeforeEach(func() { 67 table = helpers.PGLoadTable{ 68 Name: "test1", 69 ColumnNames: []string{ 70 "column1", 71 "column2", 72 "column3", 73 }, 74 ColumnTypes: []string{ 75 "character varying not null", 76 "integer", 77 "notmanaged", 78 }, 79 SampleRow: []interface{}{ 80 "sample", 81 5, 82 false, 83 }, 84 NumRows: 2, 85 } 86 }) 87 88 It("Corretly prepare CREATE", func() { 89 expected := `CREATE TABLE test1 (column1 character varying not null, 90 column2 integer, 91 column3 notmanaged);` 92 Expect(table.PrepareCreate()).To(Equal(expected)) 93 }) 94 It("Corretly prepare statement", func() { 95 expected := `COPY "test1" ("column1", "column2", "column3") FROM STDIN` 96 Expect(table.PrepareStatement()).To(Equal(expected)) 97 }) 98 It("Corretly prepare row", func() { 99 idx := 2 100 expected := []interface{}{"sample2", 2, false} 101 Expect(table.PrepareRow(idx)).To(Equal(expected)) 102 }) 103 }) 104 Context("With a table without rows", func() { 105 var table helpers.PGLoadTable 106 107 BeforeEach(func() { 108 table.Name = "test1" 109 }) 110 111 It("Corretly prepare CREATE", func() { 112 expected := `CREATE TABLE test1 ();` 113 Expect(table.PrepareCreate()).To(Equal(expected)) 114 }) 115 It("Corretly prepare statement", func() { 116 var expected string 117 Expect(table.PrepareStatement()).To(Equal(expected)) 118 }) 119 It("Corretly prepare row", func() { 120 idx := 0 121 var expected []interface{} 122 Expect(table.PrepareRow(idx)).To(Equal(expected)) 123 }) 124 }) 125 Context("With an inconsistent table in input", func() { 126 127 It("Uses default column type in CREATE if type missing", func() { 128 var table = helpers.PGLoadTable{ 129 Name: "test1", 130 ColumnNames: []string{ 131 "column1", 132 "column2", 133 }, 134 ColumnTypes: []string{ 135 "character varying not null", 136 }, 137 } 138 expected := `CREATE TABLE test1 (column1 character varying not null, 139 column2 character varying);` 140 Expect(table.PrepareCreate()).To(Equal(expected)) 141 }) 142 It("Returns empty prepared statement if table name is missing", func() { 143 var table = helpers.PGLoadTable{ 144 ColumnNames: []string{ 145 "column1", 146 }, 147 ColumnTypes: []string{ 148 "character varying not null", 149 }, 150 } 151 var expected string 152 Expect(table.PrepareStatement()).To(Equal(expected)) 153 }) 154 It("Returns empty row if no colums", func() { 155 var table = helpers.PGLoadTable{ 156 ColumnTypes: []string{ 157 "character varying not null", 158 }, 159 } 160 var expected []interface{} 161 Expect(table.PrepareRow(0)).To(Equal(expected)) 162 }) 163 It("Ignores extra sample values preparing row", func() { 164 var table = helpers.PGLoadTable{ 165 ColumnNames: []string{ 166 "column1", 167 }, 168 SampleRow: []interface{}{ 169 "sample", 170 5, 171 }, 172 NumRows: 2, 173 } 174 idx := 2 175 expected := []interface{}{"sample2"} 176 Expect(table.PrepareRow(idx)).To(Equal(expected)) 177 }) 178 }) 179 })