-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from jasondoc3/add-one-off-tracking
Support tracking queries in a block
- Loading branch information
Showing
5 changed files
with
81 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'test_helper' | ||
|
||
module SqlTracker | ||
class HandlerTest < Minitest::Test | ||
def test_tracking_queries_with_a_block | ||
config = SqlTracker::Config.apply_defaults | ||
config.enabled = true | ||
config.tracked_sql_command = %w(SELECT INSERT) | ||
|
||
expected_queries = [ | ||
'SELECT * FROM users', | ||
'insert into users VALUES (xxx)' | ||
] | ||
|
||
query_data = SqlTracker.track do | ||
expected_queries.each { |q| instrument_query(q) } | ||
instrument_query('DELETE FROM users WHERE id = 1') | ||
end | ||
|
||
instrument_query('SELECT * FROM comments') | ||
|
||
assert_equal( | ||
expected_queries, | ||
query_data.values.map { |v| v[:sql] } | ||
) | ||
end | ||
|
||
private | ||
|
||
def instrument_query(query) | ||
ActiveSupport::Notifications.instrument( | ||
'sql.active_record', | ||
sql: query | ||
) | ||
end | ||
end | ||
end |