10 examples of 'golang table driven tests' in Go

Every line of 'golang table driven tests' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
72func ListTableSample(client *tablestore.TableStoreClient) {
73 fmt.Println("Begin to list table")
74 listtables, err := client.ListTable()
75 if err != nil {
76 fmt.Println("Failed to list table")
77 } else {
78 fmt.Println("List table result is")
79 for _, table := range listtables.TableNames {
80 fmt.Println("TableName: ", table)
81 }
82 }
83}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

34func main() {
35 table := simpletable.New()
36
37 table.Header = &simpletable.Header{
38 Cells: []*simpletable.Cell{
39 {Align: simpletable.AlignCenter, Text: "#"},
40 {Align: simpletable.AlignCenter, Text: "NAME"},
41 {Align: simpletable.AlignCenter, Text: "PHONE"},
42 {Align: simpletable.AlignCenter, Text: "EMAIL"},
43 {Align: simpletable.AlignCenter, Text: "QTTY"},
44 },
45 }
46
47 subtotal := 0
48 for _, row := range data {
49 r := []*simpletable.Cell{
50 {Align: simpletable.AlignRight, Text: fmt.Sprintf("%d", row[0].(int))},
51 {Text: row[1].(string)},
52 {Text: row[2].(string)},
53 {Text: row[3].(string)},
54 {Align: simpletable.AlignRight, Text: fmt.Sprintf("%d", row[4])},
55 }
56
57 table.Body.Cells = append(table.Body.Cells, r)
58 subtotal += row[4].(int)
59 }
60
61 table.Footer = &simpletable.Footer{
62 Cells: []*simpletable.Cell{
63 {Align: simpletable.AlignRight, Span: 4, Text: "Subtotal"},
64 {Align: simpletable.AlignRight, Text: fmt.Sprintf("%d", subtotal)},
65 },
66 }
67
68 for n, s := range styles {
69 fmt.Println(n)
70
71 table.SetStyle(s)
72 table.Println()
73
74 fmt.Println()
75 }
76}
7func main() {
8 table := simpletable.New()
9
10 table.Header = &simpletable.Header{
11 Cells: []*simpletable.Cell{
12 {Align: simpletable.AlignCenter, Text: "FOO"},
13 {Align: simpletable.AlignCenter, Text: "BAR"},
14 {Align: simpletable.AlignCenter, Text: "BAZ"},
15 },
16 }
17
18 table.Body = &simpletable.Body{
19 Cells: [][]*simpletable.Cell{
20 {
21 &simpletable.Cell{Align: simpletable.AlignCenter, Span: 2, Text: "11111"},
22 &simpletable.Cell{Align: simpletable.AlignCenter, Text: "11111"},
23 },
24 {
25 &simpletable.Cell{Align: simpletable.AlignCenter, Text: "22222"},
26 &simpletable.Cell{Align: simpletable.AlignCenter, Span: 2, Text: "22222"},
27 },
28 {
29 &simpletable.Cell{Align: simpletable.AlignCenter, Span: 3, Text: "33333"},
30 },
31 {
32 &simpletable.Cell{Align: simpletable.AlignCenter, Text: "44444"},
33 &simpletable.Cell{Align: simpletable.AlignCenter, Text: "44444"},
34 &simpletable.Cell{Align: simpletable.AlignCenter, Text: "44444"},
35 },
36 },
37 }
38
39 table.Println()
40}
19func main() {
20 ctx := context.Background()
21
22 // Sets your Google Cloud Platform project ID.
23 projectID := "YOUR_PROJECT_ID"
24
25 // Creates a client.
26 client, err := bigquery.NewClient(ctx, projectID)
27 if err != nil {
28 log.Fatalf("Failed to create client: %v", err)
29 }
30
31 // Sets the name for the new dataset.
32 datasetName := "my_new_dataset"
33
34 // Creates the new BigQuery dataset.
35 if err := client.Dataset(datasetName).Create(ctx, &bigquery.DatasetMetadata{}); err != nil {
36 log.Fatalf("Failed to create dataset: %v", err)
37 }
38
39 fmt.Printf("Dataset created\n")
40}
752func (s *Schema) genDeleteRows(t *Table, g *Generator, r *rand.Rand, p PartitionRangeConfig) (*Stmt, error) {
753 var (
754 typs []Type
755 )
756 builder := qb.Delete(s.Keyspace.Name + "." + t.Name)
757 for _, pk := range t.PartitionKeys {
758 builder = builder.Where(qb.Eq(pk.Name))
759 typs = append(typs, pk.Type)
760 }
761
762 vs, ok := g.Get()
763 if !ok {
764 return nil, nil
765 }
766 values := vs.Value
767 if len(t.ClusteringKeys) > 0 {
768 ck := t.ClusteringKeys[0]
769 builder = builder.Where(qb.GtOrEq(ck.Name)).Where(qb.LtOrEq(ck.Name))
770 values = appendValue(ck.Type, r, p, values)
771 values = appendValue(ck.Type, r, p, values)
772 typs = append(typs, ck.Type, ck.Type)
773 }
774 return &Stmt{
775 Query: builder,
776 Values: func() (uint64, []interface{}) {
777 return vs.Token, values
778 },
779 Types: typs,
780 QueryType: DeleteStatementType,
781 }, nil
782}
37func main() {
38 db, dbrConn, err := csdb.Connect()
39 codegen.LogFatal(err)
40 defer db.Close()
41 for _, tStruct := range codegen.ConfigTableToStruct {
42 generateStructures(tStruct, db, dbrConn)
43 }
44}
189func TableTest(w io.Writer, width int) {
190 table := NewTable(width)
191 table.ShowHeader = true
192
193 table.AddCol("name").SetColor("red").SetTextAlign(TextCenter).SetBgColor("dgreen")
194 table.AddCol("rank").SetColor("blue").SetTextAlign(TextLeft).SetBgColor("dgreen")
195 table.AddCol("time").SetColor("green").SetTextAlign(TextRight).SetBgColor("dgreen")
196
197 // fmt.Println(table)
198 for i := 0; i <= 100; i++ {
199 tmp := NewCol()
200 tmp.Data = fmt.Sprintf("name %d", i)
201 tmp.TextAlign = TextCenter
202 tmp.Color = "blue"
203 table.AddRow(0, tmp)
204 rank := NewCol()
205 rank.Data = fmt.Sprintf("rank %d", i)
206 rank.TextAlign = TextCenter
207 rank.Color = "white"
208 table.AddRow(1, rank)
209 time := NewCol()
210 time.Data = fmt.Sprintf("time %d", i)
211 time.TextAlign = TextCenter
212 time.Color = "yellow"
213 table.AddRow(2, time)
214 }
215 table.CalColumnWidths()
216 table.Fprint(w)
217}
39func cleanupTableForTest(t *testing.T, tableName string, client *dynamodb.DynamoDB) {
40 err := DeleteTable(tableName, client)
41 assert.Nil(t, err, "Unexpected error: %v", err)
42}
223func importTables(c *cli.Context) {
224 ttt := template.Must(template.New("st").Funcs(funcMap).Parse(structTemplate))
225 cxn, err := sql.Open(driver(c), conn(c))
226 if err != nil {
227 cxdie(c, err)
228 }
229 // Many drivers defer connections until the first statement. We test
230 // that here.
231 if err := cxn.Ping(); err != nil {
232 cxdie(c, err)
233 }
234 defer cxn.Close()
235
236 // Set up Squirrel
237 stmts := squirrel.NewStmtCacher(cxn)
238 bldr := squirrel.StatementBuilder.RunWith(stmts)
239 if driver(c) == "postgres" {
240 bldr = bldr.PlaceholderFormat(squirrel.Dollar)
241 }
242
243 // Set up destination
244 out := dest(c)
245 fmt.Fprintf(out, fileHeader, c.String("package"))
246
247 tables := tableList(c)
248
249 if len(tables) == 0 {
250 tables, err = publicTables(bldr)
251 if err != nil {
252 fmt.Fprintf(os.Stderr, "Cannot fetch list of tables: %s\n", err)
253 os.Exit(2)
254 }
255 }
256
257 for _, t := range tables {
258 f, err := importTable(t, bldr, driver(c))
259 if err != nil {
260 fmt.Fprintf(os.Stderr, "Failed to import table %s: %s", t, err)
261 }
262
263 //fmt.Fprintf(out, "%s %s %s\n", f.StructName, f.TableName, f.Fields)
264 ttt.Execute(out, f)
265 }
266}
95func createTable(ctx context.Context, table *bigquery.Table) error {
96 schema, err := bigquery.InferSchema(Usage{})
97 if err != nil {
98 return fmt.Errorf("unable to infer schema")
99 }
100 err = table.Create(ctx, &bigquery.TableMetadata{Schema: schema})
101 if err == nil {
102 log.Println("Created table")
103 return nil
104 }
105 if e, ok := err.(*googleapi.Error); ok {
106 if e.Code == 409 {
107 // Table already exists. This is fine.
108 return nil
109 }
110 }
111 return err
112}

Related snippets