ar_left_over_guardian 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b56afe1b169a361ea5471b5e4778c769a4317c3e
4
+ data.tar.gz: 102d2623d61281142d585e05e5405ed7e2d0020b
5
+ SHA512:
6
+ metadata.gz: 7b23e92f0c9c24c04fd37aa181491e3a19961b47c4a1f6d4e07a9083a56574e946d81d63248dd7704257eeb534530373036a75c3180c4be6c189da34a4f42532
7
+ data.tar.gz: a60f06c169dda54985b74b09a94e49be9dae217793900324cd3c0b0d7d30ca1f1b28c2e0040e0b04ad8ea2fd170b9fc8a12a434f7eef4a3b14edd3943a97ec7a
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ar_left_over_guardian.gemspec
4
+ gemspec
5
+
6
+ gem "rspec"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Alexey Fedorov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # ARLeftOverGuardian - Active Record Left Over Guardian
2
+
3
+ Meant to be run after each test. Fails your test suite, if test doesn't clean up any database records after itself. Assumes all your models are descendants of ActiveRecord::Base.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ar_left_over_guardian'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ar_left_over_guardian
20
+
21
+ ## Usage
22
+
23
+ Just make sure that it is run in the end of each of your tests.
24
+
25
+ ### Usage with rspec
26
+
27
+ ```ruby
28
+ require 'ar_left_over_guardian'
29
+
30
+ RSpec.configure do |config|
31
+ left_over_guardian = ARLeftOverGuardian.init(ActiveRecord.descendants)
32
+
33
+ # ...
34
+
35
+ # last .after block
36
+ config.after do
37
+ left_over_guardian.verify
38
+ end
39
+ end
40
+ ```
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it ( https://github.com/waterlink/ar_left_over_guardian/fork )
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ar_left_over_guardian/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ar_left_over_guardian"
8
+ spec.version = ARLeftOverGuardian::VERSION
9
+ spec.authors = ["Alexey Fedorov"]
10
+ spec.email = ["waterlink000@gmail.com"]
11
+ spec.summary = %q{Active Record Left Over Guardian.}
12
+ spec.description = %q{Active Record Left Over Guardian. Meant to be run after each test. Fails your test suite, if test doesn't clean up any database records after itself. Assumes all your models are descendants of ActiveRecord::Base}
13
+ spec.homepage = "https://github.com/waterlink/ar_left_over_guardian"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,3 @@
1
+ module ARLeftOverGuardian
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,47 @@
1
+ require "ar_left_over_guardian/version"
2
+
3
+ module ARLeftOverGuardian
4
+ def self.init(models)
5
+ Instance.new(models)
6
+ end
7
+
8
+ class DidntCleanup < StandardError; end
9
+
10
+ class Instance
11
+ def initialize(models)
12
+ @models = models.reject(&:abstract_class?)
13
+ @counts = current_counts
14
+ end
15
+
16
+ def verify
17
+ left_overs = get_left_overs
18
+ return if left_overs.empty?
19
+ model_list = left_overs.map { |model| "- #{model}" }.join("\n")
20
+ raise DidntCleanup.new("Following models has left over records:\n#{model_list}")
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :counts, :models
26
+
27
+ def get_left_overs
28
+ current_counts.each_with_index.inject([]) do |acc, (count, index)|
29
+ if count != counts[index]
30
+ acc << models[index]
31
+ end
32
+ acc
33
+ end
34
+ end
35
+
36
+ def current_counts
37
+ models.map { |model|
38
+ reset_model(model).count
39
+ }
40
+ end
41
+
42
+ def reset_model(model)
43
+ RSpec::Mocks.space.proxy_for(model).reset if defined?(RSpec::Mocks)
44
+ model
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,64 @@
1
+ class FakeWithCount
2
+ class << self
3
+ attr_writer :count
4
+ attr_accessor :abstract_class
5
+ alias_method :abstract_class?, :abstract_class
6
+
7
+ def count
8
+ return @count if @count
9
+ raise NotImplementedError.new("You should fake count out")
10
+ end
11
+ end
12
+
13
+ self.abstract_class = true
14
+ end
15
+
16
+ class FakeModel < FakeWithCount; self.abstract_class = false; end
17
+ class OtherFakeModel < FakeWithCount; self.abstract_class = false; end
18
+
19
+ MODELS = [FakeWithCount, FakeModel, OtherFakeModel]
20
+
21
+ module CountFaker
22
+ def fake_count(count, opts={})
23
+ on = opts.fetch(:on)
24
+ on.count = count
25
+ end
26
+ end
27
+
28
+ RSpec.describe ARLeftOverGuardian do
29
+ include CountFaker
30
+
31
+ describe ".init" do
32
+ it "returns new instance of guardian" do
33
+ fake_count(0, on: FakeModel)
34
+ fake_count(3, on: OtherFakeModel)
35
+ expect(ARLeftOverGuardian.init(MODELS)).to be_a(ARLeftOverGuardian::Instance)
36
+ end
37
+ end
38
+ end
39
+
40
+ RSpec.describe ARLeftOverGuardian::Instance do
41
+ include CountFaker
42
+
43
+ describe "#verify" do
44
+ it "doesn't fail if counts are the same as before" do
45
+ fake_count(5, on: FakeModel)
46
+ fake_count(1, on: OtherFakeModel)
47
+
48
+ instance = ARLeftOverGuardian.init(MODELS)
49
+
50
+ expect { instance.verify }.not_to raise_error
51
+ end
52
+
53
+ it "fails if counts are different" do
54
+ fake_count(5, on: FakeModel)
55
+ fake_count(1, on: OtherFakeModel)
56
+
57
+ instance = ARLeftOverGuardian.init(MODELS)
58
+
59
+ fake_count(7, on: FakeModel)
60
+
61
+ expect { instance.verify }.to raise_error(ARLeftOverGuardian::DidntCleanup, /- FakeModel/)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,28 @@
1
+ require "ar_left_over_guardian"
2
+
3
+ RSpec.configure do |config|
4
+ config.expect_with :rspec do |expectations|
5
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
6
+ end
7
+
8
+ config.mock_with :rspec do |mocks|
9
+ mocks.verify_partial_doubles = true
10
+ end
11
+
12
+ config.filter_run :focus
13
+ config.run_all_when_everything_filtered = true
14
+
15
+ config.disable_monkey_patching!
16
+
17
+ config.warnings = true
18
+
19
+ if config.files_to_run.one?
20
+ config.default_formatter = 'doc'
21
+ end
22
+
23
+ config.profile_examples = 10
24
+
25
+ config.order = :random
26
+
27
+ Kernel.srand config.seed
28
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ar_left_over_guardian
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexey Fedorov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Active Record Left Over Guardian. Meant to be run after each test. Fails
42
+ your test suite, if test doesn't clean up any database records after itself. Assumes
43
+ all your models are descendants of ActiveRecord::Base
44
+ email:
45
+ - waterlink000@gmail.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - ".rspec"
52
+ - Gemfile
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - ar_left_over_guardian.gemspec
57
+ - lib/ar_left_over_guardian.rb
58
+ - lib/ar_left_over_guardian/version.rb
59
+ - spec/ar_left_over_guardian_spec.rb
60
+ - spec/spec_helper.rb
61
+ homepage: https://github.com/waterlink/ar_left_over_guardian
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.2.2
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Active Record Left Over Guardian.
85
+ test_files:
86
+ - spec/ar_left_over_guardian_spec.rb
87
+ - spec/spec_helper.rb