-
Notifications
You must be signed in to change notification settings - Fork 0
/
structs.go
53 lines (43 loc) · 1.19 KB
/
structs.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
/*
gofixtures is a package for creating fixture data for tests. It has to be used with https://github.com/jinzhu/gorm.
You can simply setup a group of tables data by using Go syntax, and reuse those data in other test cases with it's simple dependency management.
*/
package gofixtures
import (
"database/sql"
"fmt"
)
type dataContext struct {
lists []interface{}
}
func (mc *dataContext) TruncatePut(db *sql.DB) {
mc.truncatePutOnce(db, make(map[string]bool))
}
type firstTimeTruncate interface {
truncatePutOnce(db *sql.DB, truncatedTables map[string]bool)
}
func truncateTablesIfNotYet(db *sql.DB, tableNames []string, truncatedTables map[string]bool) {
for _, tableName := range tableNames {
if truncatedTables[tableName] {
continue
}
_, err := db.Exec(fmt.Sprintf("DELETE FROM %s", tableName))
if err != nil {
panic(err)
}
truncatedTables[tableName] = true
}
}
func (mc *dataContext) truncatePutOnce(db *sql.DB, truncatedTables map[string]bool) {
for _, l := range mc.lists {
if c, ok := l.(firstTimeTruncate); ok {
c.truncatePutOnce(db, truncatedTables)
continue
}
}
return
}
func Data(lists ...interface{}) (c *dataContext) {
c = &dataContext{lists: lists}
return
}