-
-
Notifications
You must be signed in to change notification settings - Fork 403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
escaping subqueries and other chars #6
Comments
hi, sorry for the late answer. could you give an example of the query you had troubles with? |
Well, the issue you had, most probably is because you haven't escaped regular expression special characters. See the commit reference above. And it would be a good practice to keep your query matching short, because sqlmock matches them in the given order. If you expect a query "SELECT" and an update query is fired, then a test will fail, because it matches queries in order. So you can keep them like: sqlmock.ExpectQuery("SELECT (.*) FROM orders")
sqlmock.ExpectQuery("SELECT (.*) FROM users")
sqlmock.ExpectQuery("SELECT (.*) FROM orders")
sqlmock.ExpectExec("UPDATE orders")
sqlmock.ExpectExec("INSERT INTO products") If queries are not fired in the same order, the test would fail. |
closing it, if there are still some questions - reopen |
fyi for future readers: you may find regexp.QuoteMeta helpful. It will automatically escape special regexp characters. |
Hi,
when I started adding tests for one of my db package I had the problem that my queries mostly all contained sub queries (and thus
(
and)
) were spread across multiple lines or contained other regex-relevant characters which then were treated by the regex engine which then resulted in lots ofexec query 'rawQuery', does not match regex 'rawQuery'
After escaping the first bunch by hand I hacked together this small helper:
I wonder if there is a smarter solution for this problem and if not would have no problem with you guys including this in go-sqlmock. It's an awesome package. Thanks!
The text was updated successfully, but these errors were encountered: