-
Notifications
You must be signed in to change notification settings - Fork 2
/
populator_test.go
89 lines (74 loc) · 1.8 KB
/
populator_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"fmt"
"io/ioutil"
"net/url"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
const (
dbURL = "sqlite3://:memory:"
schemaFileTemplate = "./db/tables.%s.sql"
)
var populator *Populator
func assertCount(t *testing.T, table string, length int) {
res, err := populator.DB.Query("SELECT COUNT(*) FROM " + table)
assert.Nil(t, err)
defer res.Close()
for res.Next() {
var count int
assert.Nil(t, res.Scan(&count))
assert.Equal(t, length, count)
}
}
func showErrAndExitTest(err error) {
fmt.Println(err.Error())
os.Exit(1)
}
func TestMain(m *testing.M) {
var err error
connectionURL := dbURL
if envURL := os.Getenv("DATABASE_URL"); envURL != "" {
connectionURL = envURL
}
if populator, err = NewPopulator(connectionURL); err != nil {
showErrAndExitTest(err)
}
uri, err := url.Parse(connectionURL)
if err != nil {
showErrAndExitTest(err)
}
schemaFile := fmt.Sprintf(schemaFileTemplate, uri.Scheme)
createTablesStmt, err := ioutil.ReadFile(schemaFile)
if err != nil {
showErrAndExitTest(err)
}
statements := strings.Split(string(createTablesStmt), ";")
for _, statement := range statements {
if strings.TrimSpace(statement) == "" {
continue
}
if _, err = populator.DB.Exec(statement); err != nil {
showErrAndExitTest(err)
}
}
os.Exit(m.Run())
}
func TestPopulateFixture(t *testing.T) {
fixtures, err := LoadFile("./fixtures/001_countries.yml")
assert.Nil(t, err)
err = populator.PopulateFixture(fixtures[0])
assert.Nil(t, err)
assertCount(t, "countries", 2)
}
func TestPopulateData(t *testing.T) {
fixtures, err := LoadDirectory("./fixtures")
assert.Nil(t, err)
err = populator.PopulateData(fixtures)
assert.Nil(t, err)
assertCount(t, "countries", 2)
assertCount(t, "regions", 2)
assertCount(t, "prefectures", 1)
}