MCPcopy
hub / github.com/go-gorm/gorm / assertEqualSQL

Function assertEqualSQL

tests/sql_builder_test.go:459–482  ·  view source on GitHub ↗

assertEqualSQL for assert that the sql is equal, this method will ignore quote, and dialect specials.

(t *testing.T, expected string, actually string)

Source from the content-addressed store, hash-verified

457
458// assertEqualSQL for assert that the sql is equal, this method will ignore quote, and dialect specials.
459func 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
484func replaceQuoteInSQL(sql string) string {
485 // convert single quote into double quote

Callers 2

TestComplexScopesFunction · 0.85
TestToSQLFunction · 0.85

Calls 1

replaceQuoteInSQLFunction · 0.85

Tested by

no test coverage detected