runScript runs the script given on the postgres connection provided
(t *testing.T, ctx context.Context, script ScriptTest, conn *Connection, normalizeRows bool)
| 195 | |
| 196 | // runScript runs the script given on the postgres connection provided |
| 197 | func runScript(t *testing.T, ctx context.Context, script ScriptTest, conn *Connection, normalizeRows bool) { |
| 198 | if script.Skip { |
| 199 | t.Skip("Skip has been set in the script") |
| 200 | } |
| 201 | |
| 202 | // Run the setup |
| 203 | for _, query := range script.SetUpScript { |
| 204 | _, err := conn.Exec(ctx, query) |
| 205 | require.NoError(t, err, "error running setup query: %s", query) |
| 206 | } |
| 207 | |
| 208 | assertions := script.Assertions |
| 209 | // First, we'll run through the scripts to check for the Focus variable. If it's true, then append it to the new slice. |
| 210 | focusAssertions := make([]ScriptTestAssertion, 0, len(script.Assertions)) |
| 211 | for _, assertion := range script.Assertions { |
| 212 | if assertion.Focus { |
| 213 | // If this is running in GitHub Actions, then we'll panic, because someone forgot to disable it before committing |
| 214 | if _, ok := os.LookupEnv("GITHUB_ACTION"); ok { |
| 215 | panic(fmt.Sprintf("The assertion `%s` has Focus set to `true`. GitHub Actions requires that "+ |
| 216 | "all tests are run, which Focus circumvents, leading to this error. Please disable Focus on "+ |
| 217 | "all tests.", assertion.Query)) |
| 218 | } |
| 219 | focusAssertions = append(focusAssertions, assertion) |
| 220 | } |
| 221 | } |
| 222 | // If we have assertions with Focus set, then we replace the normal script slice with the new slice. |
| 223 | if len(focusAssertions) > 0 { |
| 224 | assertions = focusAssertions |
| 225 | } |
| 226 | |
| 227 | // Run the assertions |
| 228 | for _, assertion := range assertions { |
| 229 | t.Run(assertion.Query, func(t *testing.T) { |
| 230 | if assertion.Skip { |
| 231 | t.Skip("Skip has been set in the assertion") |
| 232 | } |
| 233 | |
| 234 | // Clear out any previously received notices |
| 235 | receivedNotices = nil |
| 236 | |
| 237 | // Use the provided username and password to create a new connection (if a username has been specified). |
| 238 | // This will automatically handle connection reuse, using the default connection is no user is specified, etc. |
| 239 | if err := conn.Connect(ctx, assertion.Username, assertion.Password); err != nil { |
| 240 | if assertion.ExpectedErr != "" { |
| 241 | require.Error(t, err) |
| 242 | assert.Contains(t, err.Error(), assertion.ExpectedErr) |
| 243 | } else { |
| 244 | require.NoError(t, err) |
| 245 | } |
| 246 | return |
| 247 | } |
| 248 | // If we're skipping the results check, then we call Execute, as it uses a simplified message model. |
| 249 | if assertion.CopyFromStdInFile != "" { |
| 250 | copyFromStdin(t, conn.Current, assertion.Query, assertion.CopyFromStdInFile) |
| 251 | } else if assertion.SkipResultsCheck || assertion.ExpectedErr != "" { |
| 252 | _, err := conn.Exec(ctx, assertion.Query, assertion.BindVars...) |
| 253 | if assertion.ExpectedErr != "" { |
| 254 | require.Error(t, err) |