Module: AssertEfficientSql
- Defined in:
- lib/assert_efficient_sql.rb,
lib/assert_efficient_sql.rb
Defined Under Namespace
Classes: BufferStdout, SqlEfficiencyAsserter
Class Method Summary collapse
-
._decorate_explainers(explains) ⇒ Object
:nodoc:.
-
._multiples ⇒ Object
:nodoc:.
-
._singulars ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#_exec(cmd) ⇒ Object
:nodoc:.
- #assert_efficient_sql(options = {}, &block) ⇒ Object
-
#assert_latest(*models, &block) ⇒ Object
This collects every model in the given class that appears while its block runs.
-
#assert_stdout(matcher = nil, diagnostic = nil) ⇒ Object
:nodoc:.
- #deny_latest(*models, &block) ⇒ Object
-
#deny_stdout(unmatcher, diagnostic = nil, &block) ⇒ Object
:nodoc:.
- #get_latest(models, &block) ⇒ Object
-
#inspect_sql(options = {}, &block) ⇒ Object
def test_order_transactions_by_id sqls = inspect_sql :match => /SELECT conversations.*/ do get_sales_after_transaction_id end statement = sqls.first assert{ statement =~ /ORDER BY id asc/ } end.
Class Method Details
._decorate_explainers(explains) ⇒ Object
:nodoc:
328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/assert_efficient_sql.rb', line 328 def self._decorate_explainers(explains) #:nodoc: def explains.find_statements(regexp) return AssertEfficientSql._decorate_explainers(select{|explanation| explanation[:statement] =~ regexp }) end (_multiples + _singulars).each do |rollup, explainer| eval "def explains.#{rollup}; map{|q|q[:#{rollup}]}.flatten.uniq; end" end return explains end |
._multiples ⇒ Object
:nodoc:
279 280 281 282 |
# File 'lib/assert_efficient_sql.rb', line 279 def self._multiples #:nodoc: return [ [:extras, :Extra ], [:possible_keys, :possible_keys] ] end |
._singulars ⇒ Object
:nodoc:
272 273 274 275 276 277 |
# File 'lib/assert_efficient_sql.rb', line 272 def self._singulars #:nodoc: return [ [:ids, :id ], [:key_lens, :key_len ], [:keys, :key ], [:refs, :ref ], [:rows, :rows ], [:select_types, :select_type], [:tables, :table ], [:types, :type ] ] end |
Instance Method Details
#_exec(cmd) ⇒ Object
:nodoc:
248 249 250 |
# File 'lib/assert_efficient_sql.rb', line 248 def _exec(cmd) #:nodoc: ActiveRecord::Base.connection.execute(cmd) end |
#assert_efficient_sql(options = {}, &block) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/assert_efficient_sql.rb', line 253 def assert_efficient_sql( = {}, &block) = { :verbose => true } if == :verbose if .class == Hash .reverse_merge! if current_adapter?(:MysqlAdapter) return assert_efficient_mysql(, &block) else warn_adapter_required() block.call if block end else print_syntax end return [] end |
#assert_latest(*models, &block) ⇒ Object
This collects every model in the given class that appears while its block runs
187 188 189 190 191 192 193 |
# File 'lib/assert_efficient_sql.rb', line 187 def assert_latest(*models, &block) models, diagnostic = _get_latest_args(models, 'assert') latests = get_latest(models, &block) latests.compact.length == models.length or _flunk_latest(models, latests, diagnostic, true, block) return *latests end |
#assert_stdout(matcher = nil, diagnostic = nil) ⇒ Object
:nodoc:
349 350 351 352 353 354 355 356 357 |
# File 'lib/assert_efficient_sql.rb', line 349 def assert_stdout(matcher = nil, diagnostic = nil) #:nodoc: waz = $stdout $stdout = BufferStdout.new yield assert_match matcher, $stdout.output, diagnostic if matcher return $stdout.output ensure $stdout = waz end |
#deny_latest(*models, &block) ⇒ Object
208 209 210 211 212 213 214 |
# File 'lib/assert_efficient_sql.rb', line 208 def deny_latest(*models, &block) models, diagnostic = _get_latest_args(models, 'deny') latests = get_latest(models, &block) return if latests.compact.empty? models = [latests].flatten.compact.map(&:class) _flunk_latest(models, latests, diagnostic, false, block) end |
#deny_stdout(unmatcher, diagnostic = nil, &block) ⇒ Object
:nodoc:
359 360 361 362 |
# File 'lib/assert_efficient_sql.rb', line 359 def deny_stdout(unmatcher, diagnostic = nil, &block) #:nodoc: got = assert_stdout(nil, nil, &block) assert_no_match unmatcher, got, diagnostic end |
#get_latest(models, &block) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/assert_efficient_sql.rb', line 216 def get_latest(models, &block) max_ids = models.map{|model| model.maximum(:id) || 0 } block.call index = -1 return models.map{|model| all = *model.find( :all, :conditions => "id > #{max_ids[index += 1]}", :order => "id asc" ) all # * returns nil for [], # one object for [x], # or an array with more than one item } end |
#inspect_sql(options = {}, &block) ⇒ Object
def test_order_transactions_by_id
sqls = inspect_sql :match => /SELECT conversations.\*/ do
get_sales_after_transaction_id
end
statement = sqls.first[:statement]
assert{ statement =~ /ORDER BY id asc/ }
end
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/assert_efficient_sql.rb', line 292 def inspect_sql( = {}, &block) verbose = .fetch(:verbose, false) match = .fetch(:match, /./) args = { :ALL => true, :Using_filesort => true, :verbose => verbose } inspections = assert_efficient_sql(args, &block) # pp inspections.first explains = inspections.inject([]) do |a, inspection| if inspection.first =~ match explanations = inspection.last h = { :statement => inspection.first, :explanations => explanations } AssertEfficientSql._singulars.each do |rollup, explainer| h[rollup] = explanations.map{|e| q = e[explainer] and q.to_sym } end AssertEfficientSql._multiples.each do |rollup, explainer| h[rollup] = explanations.map do |e| q = e[explainer] [q].flatten.map do |e| e and e.any? and (rollup == :extras ? e.gsub!(' ', '_') : e ) and e.to_sym end end end a << h end a end return AssertEfficientSql._decorate_explainers(explains) end |