-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmigration_file_test.go
140 lines (121 loc) · 3.23 KB
/
migration_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package migration
import (
"github.com/aleksanderaleksic/tgmigrate/test"
"github.com/stretchr/testify/assert"
"path/filepath"
"testing"
)
func TestGetMigrationFilesFromEmptyDirectory(t *testing.T) {
ass := assert.New(t)
testDir := t.TempDir()
files, err := GetMigrationFiles(testDir)
ass.Nil(err)
ass.Len(*files, 0)
}
func TestHandleInvalidMigrationFilesError(t *testing.T) {
ass := assert.New(t)
testDir := t.TempDir()
test.CopyTestData(t, "empty_migration", testDir)
files, err := GetMigrationFiles(filepath.Join(testDir, "migrations"))
ass.NotNil(err)
ass.Nil(files)
}
func TestHandleInvalidMoveBlock(t *testing.T) {
ass := assert.New(t)
testDir := t.TempDir()
test.CopyTestData(t, "invalid_move_block", testDir)
files, err := GetMigrationFiles(filepath.Join(testDir, "migrations"))
ass.NotNil(err)
ass.Nil(files)
}
func TestHandleInvalidRemoveBlock(t *testing.T) {
ass := assert.New(t)
testDir := t.TempDir()
test.CopyTestData(t, "invalid_remove_block", testDir)
files, err := GetMigrationFiles(filepath.Join(testDir, "migrations"))
ass.NotNil(err)
ass.Nil(files)
}
func TestHandleMissingVersionPrefix(t *testing.T) {
ass := assert.New(t)
testDir := t.TempDir()
test.CopyTestData(t, "missing_version", testDir)
files, err := GetMigrationFiles(filepath.Join(testDir, "migrations"))
ass.NotNil(err)
ass.Nil(files)
}
func TestHandleDuplicateVersionPrefix(t *testing.T) {
ass := assert.New(t)
testDir := t.TempDir()
test.CopyTestData(t, "duplicate_version", testDir)
files, err := GetMigrationFiles(filepath.Join(testDir, "migrations"))
ass.NotNil(err)
ass.Nil(files)
}
func TestHandleFilePermissionDenied(t *testing.T) {
ass := assert.New(t)
testDir := t.TempDir()
test.CopyTestData(t, "duplicate_version", testDir)
files, err := GetMigrationFiles(filepath.Join(testDir, "migrations"))
ass.NotNil(err)
ass.Nil(files)
}
func TestValidMigration(t *testing.T) {
ass := assert.New(t)
testDir := t.TempDir()
test.CopyTestData(t, "simple", testDir)
f, err := GetMigrationFiles(filepath.Join(testDir, "migrations"))
ass.Nil(err)
files := *f
ass.Len(files, 2)
ass.Equal(files[0], File{
Metadata: FileMetadata{
FileName: "V1__move.hcl",
FileHash: "5f18eb04c47a8f7ead44b7e727e1d21f9c07954c3143180383a6cc2280e2fc0d",
Version: 1,
},
Config: Config{
Environments: []string{"test"},
Description: " - Move rest_api lambda to rest_v2\n",
},
Migrations: []Migration{
{
Type: "move",
Name: "rest_api",
Move: &MoveBlock{
From: MoveFromBlock{
State: "us-east-1/apis/rest",
Resource: "aws_lambda_function.rest_api",
},
To: MoveFromBlock{
State: "us-east-1/apis/rest_v2",
Resource: "aws_lambda_function.rest_api",
},
},
Remove: nil,
},
},
})
ass.Equal(files[1], File{
Metadata: FileMetadata{
FileName: "V2__remove.hcl",
FileHash: "92cb5ae4c96519f4213a6ee2d0ada9c360866ea81cd7977f2caabbebd736b598",
Version: 2,
},
Config: Config{
Environments: []string{"test"},
Description: " - Remove testfile from files module\n",
},
Migrations: []Migration{
{
Type: "remove",
Name: "file",
Move: nil,
Remove: &RemoveBlock{
State: "us-east-1/files",
Resource: "file.test_file",
},
},
},
})
}