refactor assert_sql query to reuse capture_sql

set assert_sql to reuse the capture_sql method from above
instead of repeating the code

in response to comments on issue #14546
Merge pull request #14548 from eileencodes/refactor_capture_sql_in_tests

refactor assert_sql query to reuse capture_sql