database_plumber 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/README.md +16 -2
- data/lib/database_plumber/leak_finder.rb +6 -1
- data/lib/database_plumber/version.rb +1 -1
- data/spec/lib/database_plumber/leak_finder_spec.rb +84 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: daf1a5f4a3baccca239f4278220bb38c6bf23e03
|
4
|
+
data.tar.gz: 411fe8bed18fb3edf20ea16a7be0d20e8ef71cac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 78f57584b621b51e8f8d52e2c554c1a5d1216f7a2f8e84ed57bced09bd6c770144db8ac546851c5e0c832f8a788c51450473a82a2e7b77cf6afd02b46067125d
|
7
|
+
data.tar.gz: ae1e8ffec52530b5bc86534cb6fce7dbd98a283bd9b3f745639fbc80bcaa902b21e56205970a384874ae0f4ad66c1f010075ecb8da204e42c122757bd8f0bf21
|
data/README.md
CHANGED
@@ -133,8 +133,22 @@ config.after(:all) do
|
|
133
133
|
end
|
134
134
|
```
|
135
135
|
|
136
|
-
|
137
|
-
|
136
|
+
### Setting thresholds for Models
|
137
|
+
|
138
|
+
You may have some models you would like to report on, but which should also have
|
139
|
+
entries in the database, for example a table that is seeded or loaded with fixtures.
|
140
|
+
In order to allow this you can provide a threshold for a Model, which is the
|
141
|
+
maximum number of entries allowed in the database for the Model before it is
|
142
|
+
regarded as leaky.
|
143
|
+
|
144
|
+
To provide a threshold for a Model, you can can the following:
|
145
|
+
|
146
|
+
```ruby
|
147
|
+
config.after(:all) do
|
148
|
+
# Perform the report after each example group
|
149
|
+
DatabasePlumber.inspect model_thresholds: { Bar => 3 }
|
150
|
+
end
|
151
|
+
|
138
152
|
|
139
153
|
## Contributing
|
140
154
|
|
@@ -12,12 +12,13 @@ module DatabasePlumber
|
|
12
12
|
def initialize(options)
|
13
13
|
@ignored_models = (options[:ignored_models] || []) + IGNORED_AR_INTERNALS
|
14
14
|
@ignored_adapters = options[:ignored_adapters] || []
|
15
|
+
@model_thresholds = options[:model_thresholds] || {}
|
15
16
|
end
|
16
17
|
|
17
18
|
def inspect
|
18
19
|
filtered_models.each_with_object({}) do |model, results|
|
19
20
|
records = count_for(model)
|
20
|
-
if records >
|
21
|
+
if records > threshold_for(model)
|
21
22
|
results[model.to_s] = records
|
22
23
|
mop_up(model)
|
23
24
|
end
|
@@ -34,6 +35,10 @@ module DatabasePlumber
|
|
34
35
|
raise InvalidModelError, "#{model} does not have a valid table definition"
|
35
36
|
end
|
36
37
|
|
38
|
+
def threshold_for(model)
|
39
|
+
@model_thresholds.key?(model) ? @model_thresholds[model].to_i : 0
|
40
|
+
end
|
41
|
+
|
37
42
|
def mop_up(model)
|
38
43
|
model.destroy_all
|
39
44
|
end
|
@@ -3,7 +3,7 @@ RSpec.describe DatabasePlumber::LeakFinder do
|
|
3
3
|
let(:ignored_connection) { double(:connection, adapter_name: 'SQLite') }
|
4
4
|
|
5
5
|
let(:happy_model) { double(:happy, name: 'Happy', abstract_class?: nil, connection: normal_connection, count: 0) }
|
6
|
-
let(:leaky_model) { double(:leaky, name: 'Leaky', abstract_class?: nil, connection: normal_connection, count:
|
6
|
+
let(:leaky_model) { double(:leaky, name: 'Leaky', abstract_class?: nil, connection: normal_connection, count: 2) }
|
7
7
|
let(:abstract_model) { double(:abstract, name: 'Abstract', abstract_class?: true, connection: normal_connection, count: 2) }
|
8
8
|
let(:ignored_model) { double(:ignored, name: 'Ignored', abstract_class?: nil, connection: normal_connection, count: 3) }
|
9
9
|
let(:ignored_adapter_model) { double(:anon, name: 'Anon', abstract_class?: nil, connection: ignored_connection, count: 4) }
|
@@ -24,7 +24,7 @@ RSpec.describe DatabasePlumber::LeakFinder do
|
|
24
24
|
context 'with no params' do
|
25
25
|
let(:expected_leaks) do
|
26
26
|
{
|
27
|
-
leaky_model.to_s =>
|
27
|
+
leaky_model.to_s => 2,
|
28
28
|
ignored_model.to_s => 3,
|
29
29
|
ignored_adapter_model.to_s => 4
|
30
30
|
}
|
@@ -54,7 +54,7 @@ RSpec.describe DatabasePlumber::LeakFinder do
|
|
54
54
|
|
55
55
|
let(:expected_leaks) do
|
56
56
|
{
|
57
|
-
leaky_model.to_s =>
|
57
|
+
leaky_model.to_s => 2,
|
58
58
|
ignored_adapter_model.to_s => 4
|
59
59
|
}
|
60
60
|
end
|
@@ -83,7 +83,7 @@ RSpec.describe DatabasePlumber::LeakFinder do
|
|
83
83
|
|
84
84
|
let(:expected_leaks) do
|
85
85
|
{
|
86
|
-
leaky_model.to_s =>
|
86
|
+
leaky_model.to_s => 2,
|
87
87
|
ignored_model.to_s => 3
|
88
88
|
}
|
89
89
|
end
|
@@ -103,6 +103,86 @@ RSpec.describe DatabasePlumber::LeakFinder do
|
|
103
103
|
it { expect(join_model_stub).not_to have_received(:destroy_all) }
|
104
104
|
end
|
105
105
|
|
106
|
+
context 'with a threshold' do
|
107
|
+
context 'with a leaky model at the threshold' do
|
108
|
+
let(:options_params) do
|
109
|
+
{
|
110
|
+
ignored_models: [ignored_model],
|
111
|
+
ignored_adapters: [:sqlite],
|
112
|
+
model_thresholds: { leaky_model => 2 }
|
113
|
+
}
|
114
|
+
end
|
115
|
+
|
116
|
+
before(:each) { @leaks = described_class.inspect(options_params) }
|
117
|
+
|
118
|
+
it { expect(@leaks).to be_empty }
|
119
|
+
|
120
|
+
it { expect(ActiveRecord::SchemaMigration).not_to have_received(:destroy_all) }
|
121
|
+
|
122
|
+
it { expect(happy_model).not_to have_received(:destroy_all) }
|
123
|
+
it { expect(leaky_model).not_to have_received(:destroy_all) }
|
124
|
+
|
125
|
+
it { expect(ignored_model).not_to have_received(:destroy_all) }
|
126
|
+
it { expect(ignored_adapter_model).not_to have_received(:destroy_all) }
|
127
|
+
|
128
|
+
it { expect(join_model_stub).not_to have_received(:destroy_all) }
|
129
|
+
end
|
130
|
+
|
131
|
+
context 'with a leaky model below the threshold' do
|
132
|
+
let(:options_params) do
|
133
|
+
{
|
134
|
+
ignored_models: [ignored_model],
|
135
|
+
ignored_adapters: [:sqlite],
|
136
|
+
model_thresholds: { leaky_model => 5 }
|
137
|
+
}
|
138
|
+
end
|
139
|
+
|
140
|
+
before(:each) { @leaks = described_class.inspect(options_params) }
|
141
|
+
|
142
|
+
it { expect(@leaks).to be_empty }
|
143
|
+
|
144
|
+
it { expect(ActiveRecord::SchemaMigration).not_to have_received(:destroy_all) }
|
145
|
+
|
146
|
+
it { expect(happy_model).not_to have_received(:destroy_all) }
|
147
|
+
it { expect(leaky_model).not_to have_received(:destroy_all) }
|
148
|
+
|
149
|
+
it { expect(ignored_model).not_to have_received(:destroy_all) }
|
150
|
+
it { expect(ignored_adapter_model).not_to have_received(:destroy_all) }
|
151
|
+
|
152
|
+
it { expect(join_model_stub).not_to have_received(:destroy_all) }
|
153
|
+
end
|
154
|
+
|
155
|
+
context 'with a leaky model above the threshold' do
|
156
|
+
let(:options_params) do
|
157
|
+
{
|
158
|
+
ignored_models: [ignored_model],
|
159
|
+
ignored_adapters: [:sqlite],
|
160
|
+
model_thresholds: { leaky_model => 1 }
|
161
|
+
}
|
162
|
+
end
|
163
|
+
|
164
|
+
let(:expected_leaks) do
|
165
|
+
{
|
166
|
+
leaky_model.to_s => 2
|
167
|
+
}
|
168
|
+
end
|
169
|
+
|
170
|
+
before(:each) { @leaks = described_class.inspect(options_params) }
|
171
|
+
|
172
|
+
it { expect(@leaks).to eql(expected_leaks) }
|
173
|
+
|
174
|
+
it { expect(ActiveRecord::SchemaMigration).not_to have_received(:destroy_all) }
|
175
|
+
|
176
|
+
it { expect(happy_model).not_to have_received(:destroy_all) }
|
177
|
+
it { expect(leaky_model).to have_received(:destroy_all) }
|
178
|
+
|
179
|
+
it { expect(ignored_model).not_to have_received(:destroy_all) }
|
180
|
+
it { expect(ignored_adapter_model).not_to have_received(:destroy_all) }
|
181
|
+
|
182
|
+
it { expect(join_model_stub).not_to have_received(:destroy_all) }
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
106
186
|
context 'with no leaking models in scope' do
|
107
187
|
let(:options_params) do
|
108
188
|
{
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: database_plumber
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Barry Gordon
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-
|
12
|
+
date: 2016-04-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|