From 30283daf08abbcc331b1ed1e588007afcc4f77be Mon Sep 17 00:00:00 2001 From: THOTHADRI RAJESH VIDYASANKAR Date: Tue, 2 Apr 2024 13:43:23 -0700 Subject: [PATCH 1/3] Update query.go --- query.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/query.go b/query.go index 47d3796..2e584da 100644 --- a/query.go +++ b/query.go @@ -43,6 +43,10 @@ func (f QueryMatcherFunc) Match(expectedSQL, actualSQL string) error { // used by sqlmock. It parses expectedSQL to a regular // expression and attempts to match actualSQL. var QueryMatcherRegexp QueryMatcher = QueryMatcherFunc(func(expectedSQL, actualSQL string) error { + if actual != "" and expect == "" { + return fmt.Errorf("expectedSQL can't be empty") + } + expect := stripQuery(expectedSQL) actual := stripQuery(actualSQL) re, err := regexp.Compile(expect) From faba995e99888e6b7db94df00a4bcf062b1ca0c9 Mon Sep 17 00:00:00 2001 From: THOTHADRI RAJESH VIDYASANKAR Date: Tue, 2 Apr 2024 13:56:51 -0700 Subject: [PATCH 2/3] Update query.go --- query.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/query.go b/query.go index 2e584da..54341a3 100644 --- a/query.go +++ b/query.go @@ -42,13 +42,12 @@ func (f QueryMatcherFunc) Match(expectedSQL, actualSQL string) error { // QueryMatcherRegexp is the default SQL query matcher // used by sqlmock. It parses expectedSQL to a regular // expression and attempts to match actualSQL. -var QueryMatcherRegexp QueryMatcher = QueryMatcherFunc(func(expectedSQL, actualSQL string) error { - if actual != "" and expect == "" { - return fmt.Errorf("expectedSQL can't be empty") - } - +var QueryMatcherRegexp QueryMatcher = QueryMatcherFunc(func(expectedSQL, actualSQL string) error { expect := stripQuery(expectedSQL) actual := stripQuery(actualSQL) + if actual != "" && expect == "" { + return fmt.Errorf("expectedSQL can't be empty") + } re, err := regexp.Compile(expect) if err != nil { return err From f53e637e4259762beecd8c9b12960655c91f257f Mon Sep 17 00:00:00 2001 From: sfc-gh-tvidyasankar Date: Tue, 2 Apr 2024 14:35:59 -0700 Subject: [PATCH 3/3] adding a test case to support empty expectedSQL check --- query_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/query_test.go b/query_test.go index 0ba7bdc..514a6ad 100644 --- a/query_test.go +++ b/query_test.go @@ -69,6 +69,7 @@ func TestQueryMatcherRegexp(t *testing.T) { {"SELECT (.+) FROM users", "SELECT name, email FROM users WHERE id = ?", nil}, {"Select (.+) FROM users", "SELECT name, email FROM users WHERE id = ?", fmt.Errorf(`could not match actual sql: "SELECT name, email FROM users WHERE id = ?" with expected regexp "Select (.+) FROM users"`)}, {"SELECT (.+) FROM\nusers", "SELECT name, email\n FROM users\n WHERE id = ?", nil}, + {"","SELECT from table", fmt.Errorf(`expectedSQL can't be empty`)}, } for i, c := range cases {