Class: ActiveRecordFormatterHelpers::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/activerecord/helpers/collector.rb

Constant Summary collapse

SKIP_QUERIES =
["SELECT tablename FROM pg_tables", "select sum(ct) from (select count(*) ct from"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCollector

Returns a new instance of Collector.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rspec/activerecord/helpers/collector.rb', line 10

def initialize
  #@unnamed_queries = []
  @query_count    = 0
  @groups_encountered = 0
  @objects_count  = 0
  @total_queries  = 0
  @total_objects  = 0
  @query_names    = Hash.new(0)
  @group_counts   = Hash.new(0)
  @active_groups  = []

  ActiveSupport::Notifications.subscribe("sql.active_record", method(:record_query))
end

Instance Attribute Details

#active_groupsObject (readonly)

Returns the value of attribute active_groups.



5
6
7
# File 'lib/rspec/activerecord/helpers/collector.rb', line 5

def active_groups
  @active_groups
end

#group_countsObject (readonly)

Returns the value of attribute group_counts.



5
6
7
# File 'lib/rspec/activerecord/helpers/collector.rb', line 5

def group_counts
  @group_counts
end

#groups_encounteredObject (readonly)

Returns the value of attribute groups_encountered.



5
6
7
# File 'lib/rspec/activerecord/helpers/collector.rb', line 5

def groups_encountered
  @groups_encountered
end

#objects_countObject (readonly)

Returns the value of attribute objects_count.



5
6
7
# File 'lib/rspec/activerecord/helpers/collector.rb', line 5

def objects_count
  @objects_count
end

#query_countObject (readonly)

Returns the value of attribute query_count.



5
6
7
# File 'lib/rspec/activerecord/helpers/collector.rb', line 5

def query_count
  @query_count
end

#query_namesObject (readonly)

Returns the value of attribute query_names.



5
6
7
# File 'lib/rspec/activerecord/helpers/collector.rb', line 5

def query_names
  @query_names
end

#total_objectsObject (readonly)

Returns the value of attribute total_objects.



5
6
7
# File 'lib/rspec/activerecord/helpers/collector.rb', line 5

def total_objects
  @total_objects
end

#total_queriesObject (readonly)

Returns the value of attribute total_queries.



5
6
7
# File 'lib/rspec/activerecord/helpers/collector.rb', line 5

def total_queries
  @total_queries
end

Instance Method Details

#group_finished(group) ⇒ Object



53
54
55
# File 'lib/rspec/activerecord/helpers/collector.rb', line 53

def group_finished(group)
  active_groups.delete(group_path(group))
end

#group_started(group) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/rspec/activerecord/helpers/collector.rb', line 45

def group_started(group)
  @groups_encountered += 1

  return unless group.parent_groups.length > 1

  active_groups.push(group_path(group))
end

#most_common_query_namesObject



32
33
34
# File 'lib/rspec/activerecord/helpers/collector.rb', line 32

def most_common_query_names
  query_names.sort_by(&:last).reverse
end

#most_expensive_groupsObject



36
37
38
# File 'lib/rspec/activerecord/helpers/collector.rb', line 36

def most_expensive_groups
  group_counts.sort_by(&:last).reverse
end

#record_query(*_unused, data) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/rspec/activerecord/helpers/collector.rb', line 24

def record_query(*_unused, data)
  return if SKIP_QUERIES.any? { |q| data[:sql].index(q) == 0 }

  inc_query
  inc_object if query_is_an_insert?(data[:sql])
  inc_query_name(data)
end

#reset_example(_) ⇒ Object



40
41
42
43
# File 'lib/rspec/activerecord/helpers/collector.rb', line 40

def reset_example(_)
  @query_count   = 0
  @objects_count = 0
end