-
Notifications
You must be signed in to change notification settings - Fork 5
/
scenario.go
66 lines (63 loc) · 1.55 KB
/
scenario.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package sqltestutil
import (
"context"
"fmt"
"io/ioutil"
"strings"
"github.com/go-yaml/yaml"
"github.com/jmoiron/sqlx"
)
// LoadScenario reads a YAML "scenario" file and uses it to populate the given
// db. Top-level keys in the YAML are treated as table names having repeated
// rows, where keys on each row are column names. For example:
//
// users:
// - id: 1
// name: Alice
// email: [email protected]
// - id: 2
// name: Bob
// email: [email protected]
//
// posts:
// - user_id: 1
// title: Hello, world!
// - user_id: 2
// title: Goodbye, world!
// is_draft: true
//
// The above would populate the users and posts tables. Fields that are missing
// from the YAML are left out of the INSERT statement, and so are populated with
// the default value for that column.
func LoadScenario(ctx context.Context, db sqlx.ExtContext, filename string) error {
data, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
var result map[string][]map[string]interface{}
err = yaml.Unmarshal(data, &result)
if err != nil {
return err
}
for table, rows := range result {
for _, row := range rows {
var columns []string
var placeholders []string
for column := range row {
columns = append(columns, column)
placeholders = append(placeholders, ":"+column)
}
sql := fmt.Sprintf(
"INSERT INTO %q (%s) VALUES (%s)",
table,
strings.Join(columns, ", "),
strings.Join(placeholders, ", "),
)
_, err = sqlx.NamedExecContext(ctx, db, sql, row)
if err != nil {
return err
}
}
}
return nil
}