skip_database 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1 @@
1
+ v0.1.0. initial version
data/Manifest ADDED
@@ -0,0 +1,8 @@
1
+ CHANGELOG
2
+ Manifest
3
+ README
4
+ Rakefile
5
+ lib/active_support/test_case.rb
6
+ lib/skip_database.rb
7
+ lib/skip_database/runner.rb
8
+ lib/test/unit/unit.rb
data/README ADDED
@@ -0,0 +1,90 @@
1
+ SkipDatabase
2
+ ============
3
+
4
+ Adds functionality to Test::Unit tests descending from ActiveSupport::TestCase to toggle database execution within a single test suite.
5
+
6
+ Installation
7
+ ------------
8
+
9
+ gem install skip_database
10
+
11
+ Usage
12
+ -----
13
+
14
+ In order to toggle database skipping, run tests with the --skip-database command-line option.
15
+
16
+ Within test classes descending from ActiveSupport::TestCase, commands executed within the skip_database block are only executed
17
+ when the skip-database option is set. Commands within the with_database block are only executed for tests run with database execution.
18
+
19
+ SkipDatabase uses "UnitRecord":http://github.com/dan-manges/unit-record to raise exceptions when database statements are executed. By default, ActiveRecord::Base is disconnected with the options :stub_associations set to true and :strategy set to :raise.
20
+
21
+ Immediately after ActiveRecord::Base is disconnected, SkipDatabase requires any files found in test/skip_database/ under the Rails root directory.
22
+
23
+ Example
24
+ -------
25
+
26
+ class UserTest < ActiveSupport::TestCase
27
+
28
+ def test_user_name_must_be_unique
29
+
30
+ user = User.new(:first => "Elwin", :last => "Ransom")
31
+
32
+ # this block won't be executed unless we're skipping the database
33
+ skip_database do
34
+ User.expects(:create!).with(:first => user.first, :last => user.last)
35
+ user.expects(:save).returns false
36
+ user.errors.add(:name, "must be unique")
37
+ end
38
+
39
+ User.create!(:first => "Elwin", :last => "Ransom")
40
+ assert !user.save
41
+
42
+ assert_equal user.errors.on(:name), "must be unique"
43
+ end
44
+
45
+ # Do not run the following tests when skipping database execution
46
+ with_database do
47
+
48
+ def test_complex_methods
49
+ Company.build_by_factory(:users => 3, :admin => 1)
50
+
51
+ users = User.find_by_sql <<-SQL
52
+ select * from users
53
+ join permissions on permissions.user_id = users.id
54
+ join companies on companies.id = users.company_id
55
+ where permissions.role = 'user' and companies.active is true
56
+ SQL
57
+
58
+ admins = User.find_by_sql <<-SQL
59
+ select * from users
60
+ join permissions on permissions.user_id = users.id
61
+ join companies on companies.id = users.company_id
62
+ where permissions.role = 'admin' and companies.active is true
63
+ SQL
64
+
65
+ assert_equal 2, users.size
66
+ assert_equal 1, admins.size
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+
73
+ $ ruby test/unit/user_test.rb --skip-database
74
+ Loaded suite test/unit/user_test
75
+ Started
76
+ .
77
+ Finished in 0.01123 seconds.
78
+
79
+ 1 tests, 4 assertions, 0 failures, 0 errors
80
+
81
+ $ ruby test/unit/user_test.rb
82
+ Loaded suite test/unit/user_test
83
+ Started
84
+ ..
85
+ Finished in 0.31526 seconds.
86
+
87
+ 2 tests, 4 assertions, 0 failures, 0 errors
88
+
89
+
90
+ Copyright (c) 2010 Mike Bradford <mbradford@mumboe.com>, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "rubygems"
2
+ require "rake"
3
+ require "echoe"
4
+
5
+ Echoe.new("skip_database", "0.1.0") do |p|
6
+ p.description = "Database skipping unility for Test::Unit and ActiveSupport::TestCase"
7
+ p.author = "Mike Bradford"
8
+ p.email = "mbradford@mumboe.com"
9
+ p.url = "http://development.mumboe.com/"
10
+ p.development_dependencies = ["rails","active_record","active_support","unit_record"]
11
+ end
12
+
13
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,35 @@
1
+ module ActiveSupport
2
+ class TestCase
3
+
4
+ class << self
5
+ # the block will be called only if database execution is skipped
6
+ def skip_database(&block)
7
+ block.bind(self).call if SkipDatabase::Runner.skipping_database
8
+ end
9
+
10
+ # the block will not be called if database execution is skipped
11
+ def with_database(&block)
12
+ block.bind(self).call unless SkipDatabase::Runner.skipping_database
13
+ end
14
+
15
+ def skipping_database?
16
+ SkipDatabase::Runner.skipping_database
17
+ end
18
+ end
19
+
20
+ def skipping_database?
21
+ SkipDatabase::Runner.skipping_database
22
+ end
23
+
24
+ # the block will be called only if database execution is skipped
25
+ def skip_database(&block)
26
+ block.bind(self).call if SkipDatabase::Runner.skipping_database
27
+ end
28
+
29
+ # the block will not be called if database execution is skipped
30
+ def with_database(&block)
31
+ block.bind(self).call unless SkipDatabase::Runner.skipping_database
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,11 @@
1
+ require File.join File.dirname(__FILE__), "test/unit/unit"
2
+ require File.join File.dirname(__FILE__), "skip_database/runner"
3
+ require File.join File.dirname(__FILE__), "active_support/test_case"
4
+
5
+ OptionParser.new do |opts|
6
+ opts.on("-s", "--skip-database") do |e|
7
+ SkipDatabase::Runner.skipping_database = true
8
+ end
9
+ end.parse! [ARGV.delete("-s") || ARGV.delete("--skip-database")]
10
+
11
+ at_exit { exit SkipDatabase::Runner.run unless $! || Test::Unit.run? }
@@ -0,0 +1,23 @@
1
+ module SkipDatabase
2
+ class Runner < Test::Unit::AutoRunner
3
+ class << self
4
+ attr_accessor :skipping_database
5
+
6
+ # run the test suite, disconnecting the database and requiring any files in test/skip_database if skip-database option set
7
+ def run(force_standalone=false, default_dir=nil, argv=ARGV, &block)
8
+ if self.skipping_database == true
9
+ require "unit_record"
10
+ ActiveRecord::Base.disconnect! :stub_associations => true, :strategy => :raise
11
+ dir = Rails.root.join("test/skip_database")
12
+ Dir["#{dir}/*"].each { |file| require "#{dir}/#{File.basename(file, File.extname(file))}" }
13
+ end
14
+ r = new(force_standalone || standalone?, &block)
15
+ r.base = default_dir
16
+ # adding this option here so that it will show up on the help menu
17
+ r.options.on("-s", "--skip-database", "Raise an exception when database calls are executed") {}
18
+ r.process_args(argv)
19
+ r.run
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ # redifining this file to remove the original at_exit block
2
+ require 'test/unit/testcase'
3
+ require 'test/unit/autorunner'
4
+
5
+ module Test # :nodoc:
6
+ module Unit
7
+ def self.run=(flag)
8
+ @run = flag
9
+ end
10
+
11
+ def self.run?
12
+ @run ||= false
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,42 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{skip_database}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Mike Bradford"]
9
+ s.date = %q{2010-02-16}
10
+ s.description = %q{Database skipping unility for Test::Unit and ActiveSupport::TestCase}
11
+ s.email = %q{mbradford@mumboe.com}
12
+ s.extra_rdoc_files = ["CHANGELOG", "README", "lib/active_support/test_case.rb", "lib/skip_database.rb", "lib/skip_database/runner.rb", "lib/test/unit/unit.rb"]
13
+ s.files = ["CHANGELOG", "Manifest", "README", "Rakefile", "lib/active_support/test_case.rb", "lib/skip_database.rb", "lib/skip_database/runner.rb", "lib/test/unit/unit.rb", "skip_database.gemspec"]
14
+ s.homepage = %q{http://development.mumboe.com/}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Skip_database", "--main", "README"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{skip_database}
18
+ s.rubygems_version = %q{1.3.5}
19
+ s.summary = %q{Database skipping unility for Test::Unit and ActiveSupport::TestCase}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
+ s.add_development_dependency(%q<rails>, [">= 0"])
27
+ s.add_development_dependency(%q<active_record>, [">= 0"])
28
+ s.add_development_dependency(%q<active_support>, [">= 0"])
29
+ s.add_development_dependency(%q<unit_record>, [">= 0"])
30
+ else
31
+ s.add_dependency(%q<rails>, [">= 0"])
32
+ s.add_dependency(%q<active_record>, [">= 0"])
33
+ s.add_dependency(%q<active_support>, [">= 0"])
34
+ s.add_dependency(%q<unit_record>, [">= 0"])
35
+ end
36
+ else
37
+ s.add_dependency(%q<rails>, [">= 0"])
38
+ s.add_dependency(%q<active_record>, [">= 0"])
39
+ s.add_dependency(%q<active_support>, [">= 0"])
40
+ s.add_dependency(%q<unit_record>, [">= 0"])
41
+ end
42
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: skip_database
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mike Bradford
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-16 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: active_record
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: active_support
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: unit_record
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ description: Database skipping unility for Test::Unit and ActiveSupport::TestCase
56
+ email: mbradford@mumboe.com
57
+ executables: []
58
+
59
+ extensions: []
60
+
61
+ extra_rdoc_files:
62
+ - CHANGELOG
63
+ - README
64
+ - lib/active_support/test_case.rb
65
+ - lib/skip_database.rb
66
+ - lib/skip_database/runner.rb
67
+ - lib/test/unit/unit.rb
68
+ files:
69
+ - CHANGELOG
70
+ - Manifest
71
+ - README
72
+ - Rakefile
73
+ - lib/active_support/test_case.rb
74
+ - lib/skip_database.rb
75
+ - lib/skip_database/runner.rb
76
+ - lib/test/unit/unit.rb
77
+ - skip_database.gemspec
78
+ has_rdoc: true
79
+ homepage: http://development.mumboe.com/
80
+ licenses: []
81
+
82
+ post_install_message:
83
+ rdoc_options:
84
+ - --line-numbers
85
+ - --inline-source
86
+ - --title
87
+ - Skip_database
88
+ - --main
89
+ - README
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: "0"
97
+ version:
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: "1.2"
103
+ version:
104
+ requirements: []
105
+
106
+ rubyforge_project: skip_database
107
+ rubygems_version: 1.3.5
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Database skipping unility for Test::Unit and ActiveSupport::TestCase
111
+ test_files: []
112
+