-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_test.go
72 lines (56 loc) · 1.94 KB
/
config_test.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
package fixture
import (
"fmt"
"path"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type SuiteConfigTester struct {
suite.Suite
tf *TestFixture
}
func (s *SuiteConfigTester) SetupTest() {
s.tf = &TestFixture{config: new(Config)}
}
func (s *SuiteConfigTester) Test_ConfigSchemaFilepath() {
pth := path.Join(testDataDir, "schema.sql")
SchemaFilepath(pth)(s.tf)
assert.Equal(s.T(), pth, s.tf.Config().SchemaFilepath)
}
func (s *SuiteConfigTester) Test_ConfigDataDir() {
DataDir(testDataDir)(s.tf)
assert.Equal(s.T(), testDataDir, s.tf.Config().FixtureDataDir)
}
func (s *SuiteConfigTester) Test_ConfigDatabase() {
rawurl := "mysql://user:password@localhost:1234/test_db?loc=Asia/Shanghai&parseTime=true"
Database(rawurl)(s.tf)
assert.Equal(s.T(), rawurl, s.tf.Config().DatabaseURL.String())
rawurl = "user:password@localhost:1234/test_db?loc=Asia/Shanghai&parseTime=true"
assert.Panics(s.T(), func() {
Database(rawurl)(s.tf)
})
rawurl = "mysql://user:password@localhost:1234?loc=Asia/Shanghai&parseTime=true"
assert.PanicsWithValue(s.T(), fmt.Sprintf("db name not found in '%s'", rawurl), func() {
Database(rawurl)(s.tf)
})
rawurl = "mysql://user:password@localhost:1234/product?loc=Asia/Shanghai&parseTime=true"
assert.PanicsWithValue(s.T(), "invalid db name 'product': test db name must starts with 'test_'", func() {
Database(rawurl)(s.tf)
})
}
func TestSuiteConfig(t *testing.T) {
suite.Run(t, new(SuiteConfigTester))
}
func Test_ConfigValidate(t *testing.T) {
conf := &Config{}
assert.Equal(t, ErrMissingDBRawURL, conf.Validate())
conf.DatabaseURL = new(DatabaseURL)
conf.FixtureDataDir = "/path/to/foo"
assert.Equal(t, ErrFixtureDataDirNotFound, conf.Validate())
conf.FixtureDataDir = fixtureDataDir
conf.SchemaFilepath = "/path/to/bazz.sql"
assert.Equal(t, ErrSchemaFileNotFound, conf.Validate())
conf.SchemaFilepath = path.Join(testDataDir, "schema.sql")
assert.Nil(t, conf.Validate())
}