refactor assert_sql query to reuse capture_sql

Sets assert_sql to reuse the capture_sql method instead of repeating code. 

This PR fixes the refactoring discussed in #14546.