Module: TestProf::AnyFixture
- Extended by:
- Logging
- Defined in:
- lib/test_prof/any_fixture.rb,
lib/test_prof/any_fixture/dsl.rb
Overview
Make DB fixtures from blocks.
Defined Under Namespace
Constant Summary collapse
- INSERT_RXP =
/^INSERT INTO ([\S]+)/.freeze
Constants included from Logging
Class Attribute Summary collapse
-
.reporting_enabled ⇒ Object
Returns the value of attribute reporting_enabled.
Class Method Summary collapse
-
.clean ⇒ Object
Clean all affected tables (but do not reset cache).
-
.register(id) ⇒ Object
Register a block of code as a fixture, returns the result of the block execution.
- .report_stats ⇒ Object
- .reporting_enabled? ⇒ Boolean
-
.reset ⇒ Object
Reset all information and clean tables.
- .subscriber(_event, _start, _finish, _id, data) ⇒ Object
Methods included from Logging
Class Attribute Details
.reporting_enabled ⇒ Object
Returns the value of attribute reporting_enabled.
43 44 45 |
# File 'lib/test_prof/any_fixture.rb', line 43 def reporting_enabled @reporting_enabled end |
Class Method Details
.clean ⇒ Object
Clean all affected tables (but do not reset cache)
60 61 62 63 64 65 66 67 68 |
# File 'lib/test_prof/any_fixture.rb', line 60 def clean disable_referential_integrity do tables_cache.keys.reverse_each do |table| ActiveRecord::Base.connection.execute %( DELETE FROM #{table} ) end end end |
.register(id) ⇒ Object
Register a block of code as a fixture, returns the result of the block execution
51 52 53 54 55 56 57 |
# File 'lib/test_prof/any_fixture.rb', line 51 def register(id) cache.fetch(id) do ActiveSupport::Notifications.subscribed(method(:subscriber), "sql.active_record") do yield end end end |
.report_stats ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/test_prof/any_fixture.rb', line 82 def report_stats msgs = [] msgs << <<~MSG AnyFixture usage stats: MSG first_column = cache.stats.keys.map(&:size).max + 2 msgs << format( "%#{first_column}s %12s %9s %12s", 'key', 'build time', 'hit count', 'saved time' ) msgs << "" total_spent = 0.0 total_saved = 0.0 total_miss = 0.0 cache.stats.to_a.sort_by { |(_, v)| -v[:hit] }.each do |(key, stats)| total_spent += stats[:time] saved = stats[:time] * stats[:hit] total_saved += saved total_miss += stats[:time] if stats[:hit].zero? msgs << format( "%#{first_column}s %12s %9d %12s", key, stats[:time].duration, stats[:hit], saved.duration ) end msgs << <<~MSG Total time spent: #{total_spent.duration} Total time saved: #{total_saved.duration} Total time wasted: #{total_miss.duration} MSG log :info, msgs.join("\n") end |
.reporting_enabled? ⇒ Boolean
45 46 47 |
# File 'lib/test_prof/any_fixture.rb', line 45 def reporting_enabled? reporting_enabled == true end |
.reset ⇒ Object
Reset all information and clean tables
71 72 73 74 75 |
# File 'lib/test_prof/any_fixture.rb', line 71 def reset clean tables_cache.clear cache.clear end |
.subscriber(_event, _start, _finish, _id, data) ⇒ Object
77 78 79 80 |
# File 'lib/test_prof/any_fixture.rb', line 77 def subscriber(_event, _start, _finish, _id, data) matches = data.fetch(:sql).match(INSERT_RXP) tables_cache[matches[1]] = true if matches end |