assertEqualSQL for assert that the sql is equal, this method will ignore quote, and dialect specials.
(t *testing.T, expected string, actually string)
| 457 | |
| 458 | // assertEqualSQL for assert that the sql is equal, this method will ignore quote, and dialect specials. |
| 459 | func assertEqualSQL(t *testing.T, expected string, actually string) { |
| 460 | t.Helper() |
| 461 | |
| 462 | // replace SQL quote, convert into postgresql like "" |
| 463 | expected = replaceQuoteInSQL(expected) |
| 464 | actually = replaceQuoteInSQL(actually) |
| 465 | |
| 466 | // ignore updated_at value, because it's generated in Gorm internal, can't to mock value on update. |
| 467 | updatedAtRe := regexp.MustCompile(`(?i)"updated_at"=".+?"`) |
| 468 | actually = updatedAtRe.ReplaceAllString(actually, `"updated_at"=?`) |
| 469 | expected = updatedAtRe.ReplaceAllString(expected, `"updated_at"=?`) |
| 470 | |
| 471 | // ignore RETURNING "id" (only in PostgreSQL) |
| 472 | returningRe := regexp.MustCompile(`(?i)RETURNING "id"`) |
| 473 | actually = returningRe.ReplaceAllString(actually, ``) |
| 474 | expected = returningRe.ReplaceAllString(expected, ``) |
| 475 | |
| 476 | actually = strings.TrimSpace(actually) |
| 477 | expected = strings.TrimSpace(expected) |
| 478 | |
| 479 | if actually != expected { |
| 480 | t.Fatalf("\nexpected: %s\nactually: %s", expected, actually) |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | func replaceQuoteInSQL(sql string) string { |
| 485 | // convert single quote into double quote |
no test coverage detected