Skip to content

Commit 1013ad9

Browse files
committed
Cleanups of rubocop issues
1 parent a26be13 commit 1013ad9

File tree

4 files changed

+47
-48
lines changed

4 files changed

+47
-48
lines changed

lib/ar_query_matchers.rb

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ class FieldModels
241241
include MatcherErrors
242242

243243
# Convert the map of expected values to a Hash of all arrays
244-
expected = expected.map { |k, v| [k, v.kind_of?(Array) ? v : [v]] }.to_h
244+
expected = expected.transform_values { |v| v.is_a(Array) ? v : [v] }
245245

246246
match do |block|
247247
@query_stats = Queries::FieldCounter.instrument(&block)
@@ -267,11 +267,11 @@ def failure_text
267267
include MatcherErrors
268268

269269
# Convert the map of expected values to a Hash of all arrays
270-
_expected = expected.map { |k, v| [k, v.kind_of?(Array) ? v : [v]] }.to_h
270+
temp_expected = expected.transform_values { |v| v.is_a?(Array) ? v : [v] }
271271

272272
match do |block|
273273
@query_stats = Queries::FieldCounter.instrument(&block)
274-
_expected == @query_stats.query_values.select{ |k,v| _expected.keys.include?(k) }
274+
temp_expected == @query_stats.query_values.select { |k, _| temp_expected.keys.include?(k) }
275275
end
276276

277277
def failure_text
@@ -301,29 +301,24 @@ def supports_block_expectations?
301301
end
302302

303303
module MatcherErrors
304+
def create_display_string(max_key_length, key, left, right, values_only)
305+
"#{key.rjust(max_key_length, ' ')} – expected: #{left}, got: #{right} #{"(#{'+' if right > left}#{right - left})" if values_only}"
306+
end
307+
304308
# Show the difference between expected and actual values with one value
305309
# per line. This is done by hand because as of this writing the author
306310
# doesn't understand how RSpec does its nice hash diff printing.
307-
def difference(keys, values_only=false)
311+
def difference(keys, values_only: false)
308312
max_key_length = keys.reduce(0) { |max, key| [max, key.size].max }
309313

310314
keys.map do |key|
311315
left = expected.fetch(key, values_only ? [] : 0)
312-
right = @query_stats.queries.fetch(key, {})
313-
if values_only
314-
left = [left] if not left.kind_of?(Array)
315-
right = right.fetch(:values, [])
316-
else
317-
right = right.fetch(:count, 0)
318-
end
316+
left = [left] unless left.is_a?(Array) || !values_only
319317

320-
if values_only
321-
"#{key.rjust(max_key_length, ' ')} – expected: #{left}, got: #{right}"
322-
else
323-
diff = "#{'+' if right > left}#{right - left}"
324-
"#{key.rjust(max_key_length, ' ')} – expected: #{left}, got: #{right} (#{diff})"
325-
end
318+
right = @query_stats.queries.fetch(key, {})
319+
right = values_only ? right.fetch(:values, []) : right.fetch(:count, 0)
326320

321+
create_display_string(max_key_length, key, left, right, values_only)
327322
end.compact
328323
end
329324

@@ -346,24 +341,30 @@ def no_queries_fail_message(crud_operation)
346341
"Expected ActiveRecord to not #{crud_operation} any records, got #{@query_stats.query_counts}\n\nWhere unexpected queries came from:\n\n#{source_lines(@query_stats.query_counts.keys).join("\n")}"
347342
end
348343

349-
def expectation_failed_message(crud_operation, values_only=false, subset=false)
350-
if values_only
351-
_expected = expected.map { |k, v| [k, v.kind_of?(Array) ? v : [v]] }.to_h
352-
expected = _expected
344+
def reject_record(subset, expected, key)
345+
if subset && !expected[key].nil?
346+
(expected[key] - @query_stats.queries[key][:values]).empty?
347+
else
348+
@query_stats.queries[key][:values] == expected[key]
353349
end
350+
end
351+
352+
def filter_model_names(expected, subset, values_only)
354353
all_model_names = expected.keys + @query_stats.queries.keys
355354
if values_only
356-
model_names_with_wrong_count = all_model_names.reject { |key|
357-
if subset && !expected[key].nil?
358-
(expected[key] - @query_stats.queries[key][:values]).empty?
359-
else
360-
@query_stats.queries[key][:values] == expected[key]
361-
end
362-
}.uniq
355+
all_model_names.reject { |key| reject_record(subset, expected, key) }.uniq
363356
else
364-
model_names_with_wrong_count = all_model_names.reject { |key| expected[key] == @query_stats.queries[key][:count] }.uniq
357+
all_model_names.reject { |key| expected[key] == @query_stats.queries[key][:count] }.uniq
358+
end
359+
end
360+
361+
def expectation_failed_message(crud_operation, values_only: false, subset: false)
362+
if values_only
363+
expected = expected.transform_values { |v| v.is_a(Array) ? v : [v] }
365364
end
366-
"Expected ActiveRecord to #{crud_operation} #{expected}, got #{values_only ? @query_stats.query_values : @query_stats.query_counts}\nExpectations that differed:\n#{difference(model_names_with_wrong_count, values_only).join("\n")}\n\nWhere unexpected queries came from:\n\n#{source_lines(model_names_with_wrong_count).join("\n")}"
365+
model_names_with_wrong_count = filter_model_names(expected, subset, values_only)
366+
"Expected ActiveRecord to #{crud_operation} #{expected}, got #{values_only ? @query_stats.query_values : @query_stats.query_counts}\n"\
367+
"Expectations that differed:\n#{difference(model_names_with_wrong_count, values_only: values_only).join("\n")}\n\nWhere unexpected queries came from:\n\n#{source_lines(model_names_with_wrong_count).join("\n")}"
367368
end
368369
end
369370
end

lib/ar_query_matchers/queries/field_counter.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ def self.instrument(&block)
1313
QueryCounter.new(FieldCounterFilter.new).instrument(&block)
1414
end
1515

16+
# Filters queries for counting purposes
1617
class FieldCounterFilter < Queries::QueryFilter
1718
# We need to look for a few things:
1819
# Anything with ` {field} = {value}` (this could be a select, update, delete)
19-
MODEL_FIELDS_PATTERN = /\.`(?<field_name>[\w]+)` = (?<field_value>[\w"`]+)/
20+
MODEL_FIELDS_PATTERN = /\.`(?<field_name>\w+)` = (?<field_value>[\w"`]+)/
2021

2122
# Anything with ` {field} IN ({value})` (this could be a select, update, delete)
22-
MODEL_FIELDS_IN_PATTERN = /\.`(?<field_name>[\w]+)` IN \((?<field_value>[\w"`]+)\)/
23+
MODEL_FIELDS_IN_PATTERN = /\.`(?<field_name>\w+)` IN \((?<field_value>[\w"`]+)\)/
2324

2425
# Anything with `, field,` in an INSERT (we need to check the values)
2526
MODEL_INSERT_PATTERN = /INSERT INTO (?<table_name>[^`"]+) ... VALUES .../
@@ -28,21 +29,17 @@ def cleanup(value)
2829
cleaned_value = value.gsub '`', ''
2930

3031
# If this is an integer, we'll cast it automatically
31-
if cleaned_value == value
32-
cleaned_value = value.to_i
33-
end
32+
cleaned_value = value.to_i if cleaned_value == value
3433

3534
cleaned_value
3635
end
3736

38-
def filter_map(name, sql)
37+
def filter_map(_name, sql)
3938
# We need to look for a few things:
4039
# - Anything with ` {field} = ` (this could be a select, update, delete)
4140
# - Anything with `, field,` in an INSERT (we need to check the values)
4241
select_field_query = sql.match(MODEL_FIELDS_PATTERN)
43-
if sql.match(/INSERT/)
44-
debugger
45-
end
42+
# debugger if sql.match(/INSERT/)
4643

4744
FieldName.new(select_field_query[:field_name], cleanup(select_field_query[:field_value])) if select_field_query
4845
end

lib/ar_query_matchers/queries/field_name.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ module Queries
55
# An instance of this class is one of the values that could be returned from the QueryFilter#filter_map.
66
# its accepts a name of an ActiveRecord model, for example: 'Company'.
77
class FieldName
8-
attr_reader(:model_name)
9-
attr_reader(:model_value)
8+
attr_reader(:model_name, :model_value)
109

1110
def initialize(model_name, model_value)
1211
@model_name = model_name

lib/ar_query_matchers/queries/query_counter.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ def instrument(&block)
8282
MARGINALIA_SQL_COMMENT_PATTERN = %r{/*line:(?<line>.*)'*/}
8383
private_constant :MARGINALIA_SQL_COMMENT_PATTERN
8484

85+
def add_to_query(queries, payload, model_obj, finish, start)
86+
comment = payload[:sql].match(MARGINALIA_SQL_COMMENT_PATTERN)
87+
queries[model_name][:lines] << comment[:line] if comment
88+
queries[model_name][:count] += 1
89+
queries[model_name][:values].append(model_obj&.model_value) if model_obj.respond_to?(:model_value)
90+
queries[model_name][:time] += (finish - start).round(6) # Round to microseconds
91+
end
92+
8593
def to_proc(queries)
8694
lambda do |_name, start, finish, _message_id, payload|
8795
return if payload[:cached]
@@ -92,13 +100,7 @@ def to_proc(queries)
92100
model_obj = @query_filter.filter_map(payload[:name] || '', payload[:sql] || '')
93101
model_name = model_obj&.model_name
94102

95-
if model_name
96-
comment = payload[:sql].match(MARGINALIA_SQL_COMMENT_PATTERN)
97-
queries[model_name][:lines] << comment[:line] if comment
98-
queries[model_name][:count] += 1
99-
queries[model_name][:values].append(model_obj&.model_value) if model_obj.respond_to?(:model_value)
100-
queries[model_name][:time] += (finish - start).round(6) # Round to microseconds
101-
end
103+
add_to_query(queries, payload, model_obj, finish, start) if model_name
102104
end
103105
end
104106
end

0 commit comments

Comments
 (0)