Skip to content

Commit

Permalink
Merge pull request #5 from srushe/add-thresholds-for-model-reporting
Browse files Browse the repository at this point in the history
Allow models to have thresholds for reporting
  • Loading branch information
brrygrdn committed Apr 4, 2016
2 parents f290680 + 39ee760 commit 6d3172e
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 8 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,22 @@ config.after(:all) do
end
```

This is also useful if your test suite is problem-free and you would like ensure it
stays that way.
### Setting thresholds for Models

You may have some models you would like to report on, but which should also have
entries in the database, for example a table that is seeded or loaded with fixtures.
In order to allow this you can provide a threshold for a Model, which is the
maximum number of entries allowed in the database for the Model before it is
regarded as leaky.

To provide a threshold for a Model, you can can the following:

```ruby
config.after(:all) do
# Perform the report after each example group
DatabasePlumber.inspect model_thresholds: { Bar => 3 }
end


## Contributing

Expand Down
7 changes: 6 additions & 1 deletion lib/database_plumber/leak_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ def self.inspect(options = {})
def initialize(options)
@ignored_models = (options[:ignored_models] || []) + IGNORED_AR_INTERNALS
@ignored_adapters = options[:ignored_adapters] || []
@model_thresholds = options[:model_thresholds] || {}
end

def inspect
filtered_models.each_with_object({}) do |model, results|
records = count_for(model)
if records > 0
if records > threshold_for(model)
results[model.to_s] = records
mop_up(model)
end
Expand All @@ -34,6 +35,10 @@ def count_for(model)
raise InvalidModelError, "#{model} does not have a valid table definition"
end

def threshold_for(model)
@model_thresholds.key?(model) ? @model_thresholds[model].to_i : 0
end

def mop_up(model)
model.destroy_all
end
Expand Down
2 changes: 1 addition & 1 deletion lib/database_plumber/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module DatabasePlumber
VERSION = '1.0.0'
VERSION = '1.1.0'
end
88 changes: 84 additions & 4 deletions spec/lib/database_plumber/leak_finder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
let(:ignored_connection) { double(:connection, adapter_name: 'SQLite') }

let(:happy_model) { double(:happy, name: 'Happy', abstract_class?: nil, connection: normal_connection, count: 0) }
let(:leaky_model) { double(:leaky, name: 'Leaky', abstract_class?: nil, connection: normal_connection, count: 1) }
let(:leaky_model) { double(:leaky, name: 'Leaky', abstract_class?: nil, connection: normal_connection, count: 2) }
let(:abstract_model) { double(:abstract, name: 'Abstract', abstract_class?: true, connection: normal_connection, count: 2) }
let(:ignored_model) { double(:ignored, name: 'Ignored', abstract_class?: nil, connection: normal_connection, count: 3) }
let(:ignored_adapter_model) { double(:anon, name: 'Anon', abstract_class?: nil, connection: ignored_connection, count: 4) }
Expand All @@ -24,7 +24,7 @@
context 'with no params' do
let(:expected_leaks) do
{
leaky_model.to_s => 1,
leaky_model.to_s => 2,
ignored_model.to_s => 3,
ignored_adapter_model.to_s => 4
}
Expand Down Expand Up @@ -54,7 +54,7 @@

let(:expected_leaks) do
{
leaky_model.to_s => 1,
leaky_model.to_s => 2,
ignored_adapter_model.to_s => 4
}
end
Expand Down Expand Up @@ -83,7 +83,7 @@

let(:expected_leaks) do
{
leaky_model.to_s => 1,
leaky_model.to_s => 2,
ignored_model.to_s => 3
}
end
Expand All @@ -103,6 +103,86 @@
it { expect(join_model_stub).not_to have_received(:destroy_all) }
end

context 'with a threshold' do
context 'with a leaky model at the threshold' do
let(:options_params) do
{
ignored_models: [ignored_model],
ignored_adapters: [:sqlite],
model_thresholds: { leaky_model => 2 }
}
end

before(:each) { @leaks = described_class.inspect(options_params) }

it { expect(@leaks).to be_empty }

it { expect(ActiveRecord::SchemaMigration).not_to have_received(:destroy_all) }

it { expect(happy_model).not_to have_received(:destroy_all) }
it { expect(leaky_model).not_to have_received(:destroy_all) }

it { expect(ignored_model).not_to have_received(:destroy_all) }
it { expect(ignored_adapter_model).not_to have_received(:destroy_all) }

it { expect(join_model_stub).not_to have_received(:destroy_all) }
end

context 'with a leaky model below the threshold' do
let(:options_params) do
{
ignored_models: [ignored_model],
ignored_adapters: [:sqlite],
model_thresholds: { leaky_model => 5 }
}
end

before(:each) { @leaks = described_class.inspect(options_params) }

it { expect(@leaks).to be_empty }

it { expect(ActiveRecord::SchemaMigration).not_to have_received(:destroy_all) }

it { expect(happy_model).not_to have_received(:destroy_all) }
it { expect(leaky_model).not_to have_received(:destroy_all) }

it { expect(ignored_model).not_to have_received(:destroy_all) }
it { expect(ignored_adapter_model).not_to have_received(:destroy_all) }

it { expect(join_model_stub).not_to have_received(:destroy_all) }
end

context 'with a leaky model above the threshold' do
let(:options_params) do
{
ignored_models: [ignored_model],
ignored_adapters: [:sqlite],
model_thresholds: { leaky_model => 1 }
}
end

let(:expected_leaks) do
{
leaky_model.to_s => 2
}
end

before(:each) { @leaks = described_class.inspect(options_params) }

it { expect(@leaks).to eql(expected_leaks) }

it { expect(ActiveRecord::SchemaMigration).not_to have_received(:destroy_all) }

it { expect(happy_model).not_to have_received(:destroy_all) }
it { expect(leaky_model).to have_received(:destroy_all) }

it { expect(ignored_model).not_to have_received(:destroy_all) }
it { expect(ignored_adapter_model).not_to have_received(:destroy_all) }

it { expect(join_model_stub).not_to have_received(:destroy_all) }
end
end

context 'with no leaking models in scope' do
let(:options_params) do
{
Expand Down

0 comments on commit 6d3172e

Please sign in to comment.