-
Notifications
You must be signed in to change notification settings - Fork 1
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 #2 from openfoodfoundation/summary-stats
Add query summary matching
- Loading branch information
Showing
5 changed files
with
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
module RSpec | ||
module Sql | ||
class QuerySummary | ||
QUERY_TYPES = [:delete, :insert, :select, :update].freeze | ||
|
||
attr_reader :summary | ||
|
||
def initialize(queries) | ||
@summary = {} | ||
queries.each do |payload| | ||
type = get_type(payload[:sql]) | ||
next if QUERY_TYPES.exclude?(type) || pg_query?(payload[:sql]) | ||
|
||
table = get_table(payload[:sql]) | ||
@summary[type] ||= {} | ||
@summary[type][table] ||= 0 | ||
@summary[type][table] += 1 | ||
end | ||
end | ||
|
||
private | ||
|
||
def get_table(sql) | ||
sql_parts = sql.split | ||
case get_type(sql) | ||
when :insert | ||
sql_parts[2] | ||
when :update | ||
sql_parts[1] | ||
else | ||
table_index = sql_parts.index("FROM") | ||
sql_parts[table_index + 1] | ||
end.gsub(/(\\|")/, "").to_sym | ||
end | ||
|
||
def get_type(sql) | ||
sql.split[0].downcase.to_sym | ||
end | ||
|
||
def pg_query?(sql) | ||
sql.include?("SELECT a.attname") || sql.include?("pg_attribute") | ||
end | ||
end | ||
end | ||
end |
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ Gem::Specification.new do |s| | |
s.version = "0.0.0" | ||
s.summary = "RSpec::Sql matcher" | ||
s.description = "RSpec matcher for database queries." | ||
s.authors = ["Maikel Linke"] | ||
s.authors = ["Maikel Linke", "Open Food Network contributors"] | ||
s.email = "[email protected]" | ||
s.files = Dir["lib/**/*.rb"] | ||
s.homepage = "https://github.com/openfoodfoundation/rspec-sql" | ||
|
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