-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_test.go
102 lines (90 loc) · 2.91 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
94
95
96
97
98
99
100
101
102
package pgxschema
import (
"errors"
"os"
"testing"
)
func TestMigrationFromFilePath(t *testing.T) {
migration, err := MigrationFromFilePath("./test-migrations/saas/2019-01-01 0900 Create Users.sql")
if migration.Script != "CREATE TABLE users (id INTEGER NOT NULL PRIMARY KEY);" {
t.Error("Failed to get correct contents of migration")
}
if err != nil {
t.Error(err)
}
}
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)
}
if migration.ID != "2019-01-01 0900 Create Users" {
t.Errorf("Incorrect ID: %s", migration.ID)
}
if migration.Script != "CREATE TABLE users (id INTEGER NOT NULL PRIMARY KEY);" {
t.Errorf("Incorrect Script: %s", migration.Script)
}
}
func TestMigrationsFromDirectoryPath(t *testing.T) {
migrations, err := MigrationsFromDirectoryPath("./test-migrations/saas")
SortMigrations(migrations)
if err != nil {
t.Error(err)
}
if migrations[0].ID != "2019-01-01 0900 Create Users" {
t.Errorf("Incorrect ID: %s", migrations[0].ID)
}
if migrations[1].ID != "2019-01-03 1000 Create Affiliates" {
t.Errorf("Incorrect ID: %s", migrations[1].ID)
}
}
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")
}
}