RunScriptN runs the assertions of the given script n times using the same connection
(t *testing.T, script ScriptTest, n int)
| 1496 | |
| 1497 | // RunScriptN runs the assertions of the given script n times using the same connection |
| 1498 | func RunScriptN(t *testing.T, script ScriptTest, n int) { |
| 1499 | scriptDatabase := script.Database |
| 1500 | if len(scriptDatabase) == 0 { |
| 1501 | scriptDatabase = "postgres" |
| 1502 | } |
| 1503 | ctx, conn, controller := CreateServer(t, scriptDatabase) |
| 1504 | defer func() { |
| 1505 | conn.Close(ctx) |
| 1506 | controller.Stop() |
| 1507 | err := controller.WaitForStop() |
| 1508 | require.NoError(t, err) |
| 1509 | }() |
| 1510 | |
| 1511 | // Run the setup |
| 1512 | for _, query := range script.SetUpScript { |
| 1513 | rows, err := conn.Query(ctx, query) |
| 1514 | require.NoError(t, err) |
| 1515 | _, _, err = ReadRows(rows, true) |
| 1516 | assert.NoError(t, err) |
| 1517 | } |
| 1518 | |
| 1519 | for i := 0; i < n; i++ { |
| 1520 | t.Run(script.Name, func(t *testing.T) { |
| 1521 | // Run the assertions |
| 1522 | for _, assertion := range script.Assertions { |
| 1523 | t.Run(assertion.Query, func(t *testing.T) { |
| 1524 | if assertion.Skip { |
| 1525 | t.Skip("Skip has been set in the assertion") |
| 1526 | } |
| 1527 | rows, err := conn.Query(ctx, assertion.Query) |
| 1528 | if err == nil { |
| 1529 | defer rows.Close() |
| 1530 | } |
| 1531 | |
| 1532 | var errorSeen string |
| 1533 | |
| 1534 | if assertion.ExpectedErr == "" { |
| 1535 | require.NoError(t, err) |
| 1536 | } else if err != nil { |
| 1537 | errorSeen = err.Error() |
| 1538 | } |
| 1539 | |
| 1540 | if errorSeen == "" { |
| 1541 | foundRows, foundRawRows, err := ReadRows(rows, true) |
| 1542 | if assertion.ExpectedErr == "" { |
| 1543 | require.NoError(t, err) |
| 1544 | if assertion.ExpectedRaw != nil { |
| 1545 | assert.Equal(t, assertion.ExpectedRaw, foundRawRows) |
| 1546 | } else { |
| 1547 | assert.Equal(t, NormalizeExpectedRow(rows.FieldDescriptions(), assertion.Expected), foundRows) |
| 1548 | } |
| 1549 | } else if err != nil { |
| 1550 | errorSeen = err.Error() |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | if assertion.ExpectedErr != "" { |
| 1555 | require.False(t, errorSeen == "", "Expected error but got none") |
no test coverage detected