51
// RunSetupTest exercises the SetupSchema task
52
func (tb *SetupSchemaTestBase) RunSetupTest(
53
>
app *cli.App, db DB, dbNameFlag string, sqlFileContent string, expectedTables []string) {
setuptest.go
54
>
// test command fails without required arguments
55
>
command := append(tb.getCommandBase(), []string{
56
>
dbNameFlag, tb.DBName,
57
>
"-q",
58
>
"setup-schema",
59
>
}...)
60
>
tb.NoError(app.Run(command))
61
>
tables, err := db.ListTables()
62
>
tb.NoError(err)
63
>
tb.Empty(tables)
64
>
65
>
tmpDir := testutils.MkdirTemp(tb.T(), "", "setupSchemaTestDir")
66
>
sqlFile := testutils.CreateTemp(tb.T(), tmpDir, "setupSchema.cliOptionsTest")
67
>
68
>
_, err = sqlFile.WriteString(sqlFileContent)
69
>
tb.NoError(err)
70
>
71
>
// make sure command doesn't succeed without version or disable-version
72
>
command = append(tb.getCommandBase(), []string{
73
>
dbNameFlag, tb.DBName,
74
>
"-q",
75
>
"setup-schema",
76
>
"-f", sqlFile.Name(),
77
>
}...)
78
>
tb.NoError(app.Run(command))
79
>
tables, err = db.ListTables()
80
>
tb.NoError(err)
81
>
tb.Empty(tables)
82
>
83
>
for i := range 4 {
84
>
85
>
ver := convert.Int32ToString(tb.rand.Int31())
86
>
versioningEnabled := (i%2 == 0)
87
>
88
>
// test overwrite with versioning works
89
>
if versioningEnabled {
90
>
command = append(tb.getCommandBase(), []string{
91
>
dbNameFlag, tb.DBName,
92
>
"-q",
93
>
"setup-schema",
94
>
"-f", sqlFile.Name(),
95
>
"-version", ver,
96
>
"-o",
97
>
}...)
98
>
tb.NoError(app.Run(command))
99
>
} else {
100
>
command = append(tb.getCommandBase(), []string{
101
>
dbNameFlag, tb.DBName,
102
>
"-q",
103
>
"setup-schema",
104
>
"-f", sqlFile.Name(),
105
>
"-d",
106
>
"-o",
107
>
}...)
108
>
tb.NoError(app.Run(command))
109
>
}
110
111
>
expectedTables := getExpectedTables(versioningEnabled, expectedTables)
setuptest.go
112
>
tables, err = db.ListTables()
113
>
tb.NoError(err)
114
>
tb.Len(tables, len(expectedTables))
115
>
116
>
for _, t := range tables {
117
>
_, ok := expectedTables[t]
118
>
tb.True(ok)
119
>
delete(expectedTables, t)
120
>
}
121
>
tb.Empty(expectedTables)
122
>
123
>
gotVer, err := db.ReadSchemaVersion()
124
>
if versioningEnabled {
125
>
tb.NoError(err)
126
>
tb.Equal(ver, gotVer)
127
>
} else {
128
>
tb.Error(err)
129
>
}
130
}
131
}
132
133
>
func (tb *SetupSchemaTestBase) getCommandBase() []string {
setuptest.go
134
>
command := []string{"./tool"}
135
>
if tb.pluginName != "" {
136
command = append(command, "-pl", tb.pluginName)
137
}
138
>
return append(command, tb.conn.CLIFlags()...)
setuptest.go
139
}
140