-
Notifications
You must be signed in to change notification settings - Fork 5
/
file_test.go
93 lines (81 loc) · 2.65 KB
/
file_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
90
91
92
93
package schema
import (
"errors"
"os"
"testing"
)
func TestMigrationFromFilePath(t *testing.T) {
migration, err := MigrationFromFilePath("./test-migrations/saas/2019-01-01 0900 Create Users.sql")
if err != nil {
t.Error(err)
}
expectID(t, migration, "2019-01-01 0900 Create Users")
expectScriptMatch(t, migration, `^CREATE TABLE users`)
}
func TestMigrationFromFilePathWithInvalidPath(t *testing.T) {
_, err := MigrationFromFilePath("./test-migrations/saas/nonexistent-file.sql")
if err == nil {
t.Errorf("Expected failure when reading from nonexistent file")
}
}
func TestMigrationFromFile(t *testing.T) {
file, err := os.Open("./test-migrations/saas/2019-01-01 0900 Create Users.sql")
if err != nil {
t.Error(err)
}
migration, err := MigrationFromFile(file)
if err != nil {
t.Error(err)
}
expectID(t, migration, "2019-01-01 0900 Create Users")
expectScriptMatch(t, migration, `^CREATE TABLE users`)
}
func TestMigrationsFromDirectoryPath(t *testing.T) {
migrations, err := MigrationsFromDirectoryPath("./test-migrations/saas")
if err != nil {
t.Error(err)
}
SortMigrations(migrations)
expectID(t, migrations[0], "2019-01-01 0900 Create Users")
expectID(t, migrations[1], "2019-01-03 1000 Create Affiliates")
}
func TestMigrationsFromDirectoryPathThrowsErrorForInvalidDirectory(t *testing.T) {
migrations, err := MigrationsFromDirectoryPath("/a/totally/made/up/directory/path")
if err == nil {
t.Error("Expected an error trying to load migrations from a fake directory")
}
if len(migrations) > 0 {
t.Errorf("Expected an empty list of migrations. Got %d", len(migrations))
}
}
func TestMigrationsFromDirectoryPathThrowsErrorForInvalidGlob(t *testing.T) {
_, err := MigrationsFromDirectoryPath("/a/path[]with/bad/glob/pattern")
if err == nil {
t.Error("Expected an error trying to load migrations from a fake directory")
}
}
func TestMigrationsFromDirectoryPathThrowsErrorWithUnreadableFiles(t *testing.T) {
err := os.Chmod("./test-migrations/unreadable/unreadable.sql", 0200)
if err != nil {
t.Error(err)
}
_, err = MigrationsFromDirectoryPath("./test-migrations/unreadable")
if err == nil {
t.Error("Expected a failure when trying to read unreadable file")
}
_ = os.Chmod("./test-migrations/unreadable/unreadable.sql", 0644) // #nosec
}
type failedReader int
func (fr failedReader) Name() string {
return "fakeFile.sql"
}
func (fr failedReader) Read(p []byte) (n int, err error) {
return 0, errors.New("this reader always fails")
}
func TestMigrationFromFileWithUnreadableFile(t *testing.T) {
var fr failedReader
_, err := MigrationFromFile(fr)
if err == nil {
t.Error("Expected MigrationFromFile to fail when given erroneous file")
}
}