-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
63 lines (50 loc) · 1.13 KB
/
config.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
package fixture
import (
"fmt"
"log"
"strings"
)
type Config struct {
DatabaseURL *DatabaseURL
FixtureDataDir string
SchemaFilepath string
}
func (c *Config) Validate() error {
if c.DatabaseURL == nil {
return ErrMissingDBRawURL
}
if !isPathExist(c.FixtureDataDir) {
return ErrFixtureDataDirNotFound
}
if !isPathExist(c.SchemaFilepath) {
return ErrSchemaFileNotFound
}
return nil
}
type Option func(*TestFixture)
func SchemaFilepath(p string) Option {
return func(tf *TestFixture) {
tf.config.SchemaFilepath = p
}
}
func DataDir(dir string) Option {
return func(tf *TestFixture) {
tf.config.FixtureDataDir = dir
}
}
func Database(rawurl string) Option {
return func(tf *TestFixture) {
url, err := Parse(rawurl)
panicOnErr(err)
// parse test db name from url
dbName := url.DBName()
if dbName == "" {
panic(fmt.Sprintf("db name not found in '%s'", rawurl))
}
if !strings.HasPrefix(dbName, "test_") {
panic(fmt.Sprintf("invalid db name '%s': test db name must starts with 'test_'", dbName))
}
log.Printf("config.Database: database config is '%s'", url)
tf.config.DatabaseURL = url
}
}