Error: Query: could not match actual sql #189
Replies: 11 comments
-
I don't check it yet but I see you're using multiline string constant. And I think you miss the point you will have line endings there. And these two lines are certainly not equal: `
SELECT acct_status
FROM ccm.cc_account_mx
WHERE acct_uuid = $1;
`
vs
"SELECT acct_status FROM ccm.cc_account_mx WHERE acct_uuid = $1;" In first case you have at least 4 |
Beta Was this translation helpful? Give feedback.
-
Please try this one: pool.ExpectQuery("SELECT acct_status").WillReturnRows(rows) That's usually enough |
Beta Was this translation helpful? Give feedback.
-
package main
import "fmt"
func main() {
sql := `
SELECT acct_status
FROM ccm.cc_account_mx
WHERE acct_uuid = $1;
`
sql2 := "SELECT acct_status FROM ccm.cc_account_mx WHERE acct_uuid = $1;"
fmt.Printf("%+q\n%+q", sql, sql2)
} Output:
|
Beta Was this translation helpful? Give feedback.
-
Interesting, in my code both are multiline strings. // in the test
sql := `
SELECT acct_status
FROM ccm.cc_account_mx
WHERE acct_uuid = $1;
`
// in the actual code
sql := `
SELECT acct_status
FROM ccm.cc_account_mx
WHERE acct_uuid = $1;
` |
Beta Was this translation helpful? Give feedback.
-
When I try this, I get this error:
|
Beta Was this translation helpful? Give feedback.
-
Your Try to add some, e.g. Please, read the example carefully. |
Beta Was this translation helpful? Give feedback.
-
Ok thank you, that does clear the errors. But then I'm not clear on how to explicitly return no rows. I'm attempting to test against a situation where |
Beta Was this translation helpful? Give feedback.
-
Sorry, I cannot follow your code really, but I've added test case returning no rows to the func TestNoPostsReturned(t *testing.T) {
mock, err := pgxmock.NewConn()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer mock.Close(context.Background())
// create app with mocked db, request and response to test
app := &api{mock}
req, err := http.NewRequest("GET", "http://localhost/posts", nil)
if err != nil {
t.Fatalf("an error '%s' was not expected while creating request", err)
}
w := httptest.NewRecorder()
mock.ExpectQuery("^SELECT (.+) FROM posts$").WillReturnRows(mock.NewRows([]string{"id", "title", "body"}))
// now we execute our request
app.posts(w, req)
if w.Code != 200 {
t.Fatalf("expected status code to be 200, but got: %d\nBody: %v", w.Code, w.Body)
}
// we make sure that all expectations were met
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
} |
Beta Was this translation helpful? Give feedback.
-
Hi @pashagolub I am having a similar issue. My setup : Code:
Whenever i try to mock this I get error:
|
Beta Was this translation helpful? Give feedback.
-
@niting3c Do you remember that by default mockDB.ExpectQuery("INSERT") |
Beta Was this translation helpful? Give feedback.
-
Hello, I raised a question about this in #51 (reply in thread), but am still encountering this error in another environment.
This is in the test I've written
And within my function the result of
pgxmockPool.QueryRow().Scan()
is"SELECT acct_status FROM ccm.cc_account_mx WHERE acct_uuid = $1;" with expected regexp "SELECT acct_status FROM ccm.cc_account_mx WHERE acct_uuid = $1;"
It seems to me that these two SQL strings should match, and do match, so I do not understand how this error is happening.
Beta Was this translation helpful? Give feedback.
All reactions