Module: TestProf::FactoryDefault

Extended by:
Logging
Defined in:
lib/test_prof/factory_default.rb,
lib/test_prof/factory_default/factory_bot_patch.rb

Overview

:nodoc: all

Defined Under Namespace

Modules: DefaultSyntax, RunnerExt, StrategyExt Classes: Configuration, NoopProfiler, Profiler

Constant Summary

Constants included from Logging

Logging::COLORS

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Logging

log

Class Attribute Details

.current_contextObject

Returns the value of attribute current_context.



196
197
198
# File 'lib/test_prof/factory_default.rb', line 196

def current_context
  @current_context
end

.profilerObject (readonly)

Returns the value of attribute profiler.



197
198
199
# File 'lib/test_prof/factory_default.rb', line 197

def profiler
  @profiler
end

.statsObject (readonly)

Returns the value of attribute stats.



197
198
199
# File 'lib/test_prof/factory_default.rb', line 197

def stats
  @stats
end

Class Method Details

.configObject



211
212
213
# File 'lib/test_prof/factory_default.rb', line 211

def config
  @config ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Yields:



215
216
217
# File 'lib/test_prof/factory_default.rb', line 215

def configure
  yield config
end

.disable!Object



301
302
303
304
305
306
307
308
# File 'lib/test_prof/factory_default.rb', line 301

def disable!
  was_enabled = @enabled
  @enabled = false
  return unless block_given?
  yield
ensure
  @enabled = was_enabled
end

.enable!Object



292
293
294
295
296
297
298
299
# File 'lib/test_prof/factory_default.rb', line 292

def enable!
  was_enabled = @enabled
  @enabled = true
  return unless block_given?
  yield
ensure
  @enabled = was_enabled
end

.enabled?Boolean

Returns:

  • (Boolean)


288
289
290
# File 'lib/test_prof/factory_default.rb', line 288

def enabled?
  @enabled
end

.get(name, traits = nil, overrides = nil) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/test_prof/factory_default.rb', line 239

def get(name, traits = nil, overrides = nil)
  return unless enabled?

  record = store[name]
  return unless record

  if traits && (trait_key = record[:traits][traits])
    name = trait_key
    record = store[name]
    traits = nil
  end

  stats[name][:miss] += 1

  if traits && !traits.empty? && record[:preserve_traits]
    return
  end

  object = record[:object]

  if overrides && !overrides.empty? && record[:preserve_attributes]
    overrides.each do |name, value|
      return unless object.respond_to?(name) # rubocop:disable Lint/NonLocalExitFromIterator
      return if object.public_send(name) != value # rubocop:disable Lint/NonLocalExitFromIterator
    end
  end

  stats[name][:miss] -= 1
  stats[name][:hit] += 1

  if record[:context] && (record[:context] != :example)
    object.refind
  else
    object
  end
end

.initObject



199
200
201
202
203
204
205
206
207
208
209
# File 'lib/test_prof/factory_default.rb', line 199

def init
  TestProf::FactoryBot::Syntax::Methods.include DefaultSyntax
  TestProf::FactoryBot.extend DefaultSyntax
  TestProf::FactoryBot::Strategy::Create.prepend StrategyExt
  TestProf::FactoryBot::Strategy::Build.prepend StrategyExt
  TestProf::FactoryBot::Strategy::Stub.prepend StrategyExt

  @profiler = config.profiling_enabled? ? Profiler.new : NoopProfiler.new
  @enabled = ENV["FACTORY_DEFAULT_DISABLED"] != "1"
  @stats = {}
end

.preserve_attributes=(val) ⇒ Object



224
225
226
# File 'lib/test_prof/factory_default.rb', line 224

def preserve_attributes=(val)
  config.preserve_attributes = val
end

.preserve_traits=(val) ⇒ Object

TODO(v2): drop



220
221
222
# File 'lib/test_prof/factory_default.rb', line 220

def preserve_traits=(val)
  config.preserve_traits = val
end


310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/test_prof/factory_default.rb', line 310

def print_report
  profiler.print_report
  return unless config.report_stats || config.report_summary

  if stats.empty?
    log :info, "FactoryDefault has not been used"
    return
  end

  msgs = []

  if config.report_stats
    msgs <<
      <<~MSG
        FactoryDefault usage stats:
      MSG

    first_column = stats.keys.map(&:size).max + 2

    msgs << format(
      "%#{first_column}s  %9s  %9s",
      "factory", "hit", "miss"
    )

    msgs << ""
  end

  total_hit = 0
  total_miss = 0

  stats.to_a.sort_by { |(_, v)| -v[:hit] }.each do |(key, record_stats)|
    total_hit += record_stats[:hit]
    total_miss += record_stats[:miss]

    if config.report_stats
      msgs << format(
        "%#{first_column}s  %9d  %9d",
        key, record_stats[:hit], record_stats[:miss]
      )
    end
  end

  msgs << "" if config.report_stats

  msgs <<
    <<~MSG
      FactoryDefault summary: hit=#{total_hit} miss=#{total_miss}
    MSG

  log :info, msgs.join("\n")
end

.register(name, obj, **options) ⇒ Object



228
229
230
231
232
233
234
235
236
237
# File 'lib/test_prof/factory_default.rb', line 228

def register(name, obj, **options)
  # Name with traits
  if name.is_a?(Array)
    register_traited_record(*name, obj, **options)
  else
    register_default_record(name, obj, **options)
  end

  obj
end

.remove(name) ⇒ Object



276
277
278
# File 'lib/test_prof/factory_default.rb', line 276

def remove(name)
  store.delete(name)
end

.reset(context: nil) ⇒ Object



280
281
282
283
284
285
286
# File 'lib/test_prof/factory_default.rb', line 280

def reset(context: nil)
  return store.clear unless context

  store.delete_if do |_name, |
    [:context] == context
  end
end