forked from cloudspannerecosystem/spanner-truncate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoordinator_test.go
247 lines (234 loc) · 7.39 KB
/
coordinator_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package main
import (
"github.com/google/go-cmp/cmp"
"testing"
)
func TestFindDeletableTables(t *testing.T) {
for _, tt := range []struct {
desc string
tablesFunc func() []*table
want []string
}{
{
desc: "Flatten tables",
tablesFunc: func() []*table {
tableA := &table{tableName: "A", deleter: &deleter{}}
tableB := &table{tableName: "B", deleter: &deleter{}}
return []*table{tableA, tableB}
},
want: []string{"A", "B"},
},
{
desc: "Parent-child tables with cascade-delete",
tablesFunc: func() []*table {
tableA := &table{tableName: "A", deleter: &deleter{}}
tableB := &table{tableName: "B", deleter: &deleter{}}
tableC := &table{tableName: "C", deleter: &deleter{}}
tableB.childTables = []*table{tableC}
tableC.parentTableName = "B"
tableC.parentOnDeleteAction = deleteActionCascadeDelete
return []*table{tableA, tableB}
},
want: []string{"A", "B"},
},
{
desc: "Parent-child tables with delete-no-action",
tablesFunc: func() []*table {
tableA := &table{tableName: "A", deleter: &deleter{}}
tableB := &table{tableName: "B", deleter: &deleter{}}
tableC := &table{tableName: "C", deleter: &deleter{}}
tableB.childTables = []*table{tableC}
tableC.parentTableName = "B"
tableC.parentOnDeleteAction = deleteActionNoAction
return []*table{tableA, tableB}
},
want: []string{"A", "C"},
},
{
desc: "Parent-child tables with delete-no-action, but already child was deleted",
tablesFunc: func() []*table {
tableA := &table{tableName: "A", deleter: &deleter{}}
tableB := &table{tableName: "B", deleter: &deleter{}}
tableC := &table{tableName: "C", deleter: &deleter{status: statusCompleted}}
tableB.childTables = []*table{tableC}
tableC.parentTableName = "B"
tableC.parentOnDeleteAction = deleteActionNoAction
return []*table{tableA, tableB}
},
want: []string{"A", "B"},
},
{
desc: "Parent-child tables with cascade-delete & delete-no-action",
tablesFunc: func() []*table {
tableA := &table{tableName: "A", deleter: &deleter{}}
tableB := &table{tableName: "B", deleter: &deleter{}}
tableC := &table{tableName: "C", deleter: &deleter{}}
tableD := &table{tableName: "D", deleter: &deleter{}}
// A -- B
tableA.childTables = []*table{tableB}
tableB.parentTableName = "A"
tableB.parentOnDeleteAction = deleteActionCascadeDelete
// B -- C
tableB.childTables = []*table{tableC}
tableC.parentTableName = "B"
tableC.parentOnDeleteAction = deleteActionNoAction
// C -- D
tableC.childTables = []*table{tableD}
tableD.parentTableName = "C"
tableD.parentOnDeleteAction = deleteActionCascadeDelete
return []*table{tableA}
},
want: []string{"C"},
},
{
desc: "Foreign key references",
tablesFunc: func() []*table {
tableA := &table{tableName: "A", deleter: &deleter{}}
tableB := &table{tableName: "B", deleter: &deleter{}}
tableA.referencedBy = []*table{tableB}
return []*table{tableA, tableB}
},
want: []string{"B"},
},
{
desc: "Foreign key references, but referencing table was already deleted",
tablesFunc: func() []*table {
tableA := &table{tableName: "A", deleter: &deleter{}}
tableB := &table{tableName: "B", deleter: &deleter{status: statusCompleted}}
tableA.referencedBy = []*table{tableB}
return []*table{tableA, tableB}
},
want: []string{"A"},
},
{
desc: "Parent-child tables with foreign key references",
tablesFunc: func() []*table {
tableA := &table{tableName: "A", deleter: &deleter{}}
tableB := &table{tableName: "B", deleter: &deleter{}}
tableC := &table{tableName: "C", deleter: &deleter{}}
tableD := &table{tableName: "D", deleter: &deleter{}}
// A -- B
tableA.childTables = []*table{tableB}
tableB.parentTableName = "A"
tableB.parentOnDeleteAction = deleteActionCascadeDelete
// C -- D
tableC.childTables = []*table{tableD}
tableD.parentTableName = "C"
tableD.parentOnDeleteAction = deleteActionCascadeDelete
// Foreign key
tableB.referencedBy = []*table{tableD}
return []*table{tableA, tableC}
},
want: []string{"C"},
},
} {
t.Run(tt.desc, func(t *testing.T) {
got := findDeletableTables(tt.tablesFunc())
gotNames := extractTableNames(got)
if !cmp.Equal(gotNames, tt.want) {
t.Errorf("diff(+got, -want) = %v", cmp.Diff(gotNames, tt.want))
}
})
}
}
func TestTableIsDeletable(t *testing.T) {
for _, tt := range []struct {
desc string
tableFunc func() *table
want bool
}{
{
desc: "Simple table",
tableFunc: func() *table {
tableA := &table{tableName: "A", deleter: &deleter{}}
return tableA
},
want: true,
},
{
desc: "Child table has cascade-delete action",
tableFunc: func() *table {
parent := &table{tableName: "Parent", deleter: &deleter{}}
child := &table{tableName: "Child", deleter: &deleter{}}
parent.childTables = []*table{child}
child.parentTableName = "Parent"
child.parentOnDeleteAction = deleteActionCascadeDelete
return parent
},
want: true,
},
{
desc: "Child table has no-action action",
tableFunc: func() *table {
parent := &table{tableName: "Parent", deleter: &deleter{}}
child := &table{tableName: "Child", deleter: &deleter{}}
parent.childTables = []*table{child}
child.parentTableName = "Parent"
child.parentOnDeleteAction = deleteActionNoAction
return parent
},
want: false,
},
{
desc: "Child table has no-action action, but it was already deleted",
tableFunc: func() *table {
parent := &table{tableName: "Parent", deleter: &deleter{}}
child := &table{tableName: "Child", deleter: &deleter{status: statusCompleted}}
parent.childTables = []*table{child}
child.parentTableName = "Parent"
child.parentOnDeleteAction = deleteActionNoAction
return parent
},
want: true,
},
{
desc: "Foreign key references",
tableFunc: func() *table {
tableA := &table{tableName: "A", deleter: &deleter{}}
tableB := &table{tableName: "B", deleter: &deleter{}}
tableA.referencedBy = []*table{tableB}
return tableA
},
want: false,
},
{
desc: "Foreign key references, but referencing table was already deleted",
tableFunc: func() *table {
tableA := &table{tableName: "A", deleter: &deleter{}}
tableB := &table{tableName: "B", deleter: &deleter{status: statusCompleted}}
tableA.referencedBy = []*table{tableB}
return tableA
},
want: true,
},
} {
t.Run(tt.desc, func(t *testing.T) {
table := tt.tableFunc()
if got := table.isDeletable(); got != tt.want {
t.Errorf("isDeletable(%v) = %v, but want = %v", table, got, tt.want)
}
})
}
}
func extractTableNames(tables []*table) []string {
names := make([]string, len(tables))
for i, table := range tables {
names[i] = table.tableName
}
return names
}