-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.go
More file actions
92 lines (75 loc) · 1.71 KB
/
testing.go
File metadata and controls
92 lines (75 loc) · 1.71 KB
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
package dbunit
import (
"database/sql"
"fmt"
"os"
"strings"
"github.com/goapt/dbunit/fixtures"
)
type Testing struct {
tdb *database
db *sql.DB
schema string
}
func NewTest(dsn string, schema string) *Testing {
tdb := newDatabase(dsn, schema)
// Open connection to the test database.
// Do NOT import fixtures in a production database!
// Existing data would be deleted.
db, err := tdb.adapter.Open(tdb.DSN())
if err != nil {
panic("test database open fail " + err.Error())
}
return &Testing{
tdb,
db,
schema,
}
}
func (d *Testing) DB() *sql.DB {
return d.db
}
func (d *Testing) Schema() string {
return d.schema
}
func (d *Testing) Drop() {
if d.db != nil {
_ = d.db.Close()
}
err := d.tdb.Drop()
if err != nil {
panic("drop database error " + err.Error())
}
}
func (d *Testing) Load(files ...string) {
options := make([]func(*fixtures.Loader) error, 0)
options = append(options, fixtures.Database(d.db)) // You database connection
options = append(options, fixtures.Dialect(d.tdb.adapter.DriverName()))
fs := make([]string, 0)
for _, file := range files {
if isDir(file) {
options = append(options, fixtures.Directory(file)) // the directory containing the YAML files
} else {
fs = append(fs, file)
}
}
if len(fs) > 0 {
options = append(options, fixtures.Files(fs...)) // Specifies the load data file
}
f, err := fixtures.New(options...)
if err != nil {
panic(err)
}
fmt.Printf("🐳 Load fixtures:%s\n", strings.Join(files, ","))
if err := f.Load(); err != nil {
panic(err)
}
}
// isDir determines whether the specified path is a directory.
func isDir(path string) bool {
fio, err := os.Lstat(path)
if nil != err {
return false
}
return fio.IsDir()
}