forked from go-testfixtures/testfixtures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testfixtures.go
119 lines (104 loc) · 2.59 KB
/
testfixtures.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package testfixtures
import (
"database/sql"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"gopkg.in/yaml.v2"
)
type fixtureFile struct {
path string
fileName string
content []byte
}
func (f *fixtureFile) fileNameWithoutExtension() string {
return strings.Replace(f.fileName, filepath.Ext(f.fileName), "", 1)
}
func (f *fixtureFile) delete(tx *sql.Tx, h DataBaseHelper) error {
_, err := tx.Exec(fmt.Sprintf("DELETE FROM %s", h.quoteKeyword(f.fileNameWithoutExtension())))
return err
}
func (f *fixtureFile) insert(tx *sql.Tx, h DataBaseHelper) error {
var rows []interface{}
err := yaml.Unmarshal(f.content, &rows)
if err != nil {
return err
}
for _, row := range rows {
record := row.(map[interface{}]interface{})
var values []interface{}
sqlColumns := ""
sqlValues := ""
i := 1
for key, value := range record {
if len(sqlColumns) > 0 {
sqlColumns += ", "
sqlValues += ", "
}
sqlColumns = sqlColumns + h.quoteKeyword(key.(string))
if h.paramType() == paramTypeDollar {
sqlValues = fmt.Sprintf("%s$%d", sqlValues, i)
} else {
sqlValues = fmt.Sprintf("%s?", sqlValues)
}
i++
values = append(values, value)
}
sql := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", h.quoteKeyword(f.fileNameWithoutExtension()), sqlColumns, sqlValues)
_, err = tx.Exec(sql, values...)
if err != nil {
return err
}
}
return nil
}
func getYmlFiles(foldername string) ([]*fixtureFile, error) {
var files []*fixtureFile
fileinfos, err := ioutil.ReadDir(foldername)
if err != nil {
return nil, err
}
for _, fileinfo := range fileinfos {
if !fileinfo.IsDir() && filepath.Ext(fileinfo.Name()) == ".yml" {
fixture := &fixtureFile{
path: foldername + "/" + fileinfo.Name(),
fileName: fileinfo.Name(),
}
fixture.content, err = ioutil.ReadFile(fixture.path)
if err != nil {
return nil, err
}
files = append(files, fixture)
}
}
return files, nil
}
// LoadFixtures loads all fixtures in a given folder in the database
func LoadFixtures(foldername string, db *sql.DB, h DataBaseHelper) error {
if !skipDatabaseNameCheck {
if !dbnameRegexp.MatchString(h.databaseName(db)) {
return errNotTestDatabase
}
}
files, err := getYmlFiles(foldername)
if err != nil {
return err
}
err = h.disableReferentialIntegrity(db, func(tx *sql.Tx) error {
for _, file := range files {
err = file.delete(tx, h)
if err != nil {
return err
}
err = h.whileInsertOnTable(tx, file.fileNameWithoutExtension(), func() error {
return file.insert(tx, h)
})
if err != nil {
return err
}
}
return nil
})
return err
}