disable_test_fixtures 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright (c) 2009 Karol Bucek
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,101 @@
1
+ DisableTestFixtures
2
+ ===================
3
+
4
+ Rails plugin that disables fixture loading during test setup.
5
+ Tested with the built-in *Test::Unit* as well as with *MiniTest*.
6
+
7
+ Unit tests should be fast, active records (unless testing a complicated
8
+ non data dependent business logic) should be connected to a database, but
9
+ every time such a test method runs Rails will make sure Your fixtures are
10
+ correctly loaded (and there's no way turning it off for particular tests).
11
+
12
+ Until NOW ...
13
+
14
+ If You don't need fixtures in a test, You might turn them off easily.
15
+
16
+ NOTE: The plugin assumes transactional fixtures are being used - it can't
17
+ really help with instantiated fixture data !
18
+
19
+
20
+ Install
21
+ =======
22
+
23
+ gem 'disable_test_fixtures'
24
+
25
+ or as a plain-old rails plugin :
26
+
27
+ script/plugin install git://github.com/kares/disable_test_fixtures.git
28
+
29
+
30
+ Example
31
+ =======
32
+
33
+ class UserTest < ActiveSupport::TestCase
34
+ include DisableTestFixtures
35
+
36
+ disable_fixtures_for %W{ test_create_a_user }
37
+
38
+ test "create a user" do
39
+ User.create! :name => 'Gregory House'
40
+ assert_equal 1, User.count
41
+ end
42
+
43
+ # other test methods that use fixtures
44
+ ...
45
+
46
+ end
47
+
48
+ Of course, You might disable fixtures "globally" for all tests :
49
+
50
+ # test_helper.rb
51
+
52
+ class ActiveSupport::TestCase
53
+ include DisableTestFixtures
54
+
55
+ disable_fixtures_for :all
56
+
57
+ # usual setup :
58
+ self.use_transactional_fixtures = true
59
+ self.use_instantiated_fixtures = false
60
+ ...
61
+
62
+ end
63
+
64
+ If You have existing test cases that depend on fixtures You might enable the
65
+ fixtures support "back" in the concrete test cases :
66
+
67
+ class FixturesDependentTest < ActiveSupport::TestCase
68
+
69
+ # assuming fixtures were disabled in test_helper.rb
70
+
71
+ enable_fixtures_for :all
72
+ ...
73
+
74
+ end
75
+
76
+
77
+ Sample speed improvement running a single test case from a fixture based setup
78
+ with fixtures disabled :
79
+
80
+ Finished in 12.705094 seconds.
81
+
82
+ 49 tests, 200 assertions, 0 failures, 0 errors
83
+
84
+
85
+ Finished in 3.015264 seconds.
86
+
87
+ 49 tests, 200 assertions, 0 failures, 0 errors
88
+
89
+
90
+ Your test will be faster and more readable if You don't use fixtures ...
91
+ Try the factory pattern for creating the data You need in every scenario.
92
+
93
+ [Test Factory](http://www.dcmanges.com/blog/38)
94
+
95
+ [Factory Girl](http://github.com/thoughtbot/factory_girl)
96
+
97
+ NOTE: BE CAREFUL WHEN MIXING TESTS THAT EXPECT AN EMPTY DB WITH TESTS THAT
98
+ DEPEND ON FIXTURES, YOU MIGHT NEED TO MAKE SURE YOUR DB IS CLEAN BEFORE EACH
99
+ AND EVERY CONSECUTIVE RUN (NO FIXTURES LEFT FROM A PREVIOUS TEST FAILURE).
100
+
101
+ <http://blog.kares.org/search/label/disable_test_fixtures>
@@ -0,0 +1,20 @@
1
+ require 'rake'
2
+
3
+ task :default => :test
4
+
5
+ require 'rake/testtask'
6
+ desc 'Run Unit Tests'
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.pattern = 'test/**/*_test.rb'
9
+ t.verbose = true
10
+ end
11
+
12
+ require 'rdoc/task'
13
+ desc 'Generate Documentation'
14
+ Rake::RDocTask.new(:rdoc) do |rdoc|
15
+ rdoc.rdoc_dir = 'rdoc'
16
+ rdoc.title = 'DisableTestFixtures'
17
+ rdoc.options << '--line-numbers' << '--inline-source'
18
+ #rdoc.rdoc_files.include('README.md')
19
+ #rdoc.rdoc_files.include('lib/**/*.rb')
20
+ end
@@ -0,0 +1,175 @@
1
+
2
+ require 'active_support/core_ext/module'
3
+
4
+ module DisableTestFixtures
5
+
6
+ @@last_test_loaded_fixtures = nil
7
+ mattr_accessor :last_test_loaded_fixtures
8
+
9
+ def self.included(base)
10
+ base.extend ClassMethods
11
+ end
12
+
13
+ module ClassMethods
14
+
15
+ # Disables fixtures for the particular tests in this test case, accepts :
16
+ #
17
+ # String/Symbol - test name
18
+ # Array of strings/symbols - test names
19
+ # Regexp - matching test names
20
+ # Proc - will get test name passed
21
+ # :all symbol
22
+ #
23
+ # NOTE: fixture disabling is inherited thru the inheritance chain
24
+ # thus if You've disabled them in super (e.g. ActiveSupport::TestCase)
25
+ # there's no need to disable them again.
26
+ def disable_fixtures_for(tests = nil, &block)
27
+ tests = tests.map { |e| e.to_s } if tests.is_a?(Array)
28
+ tests = block if tests.nil? && block_given?
29
+ @fixtures_disabled_tests = tests == :all ? true : tests
30
+ @fixtures_disabled_tests_negated = false
31
+ @fixtures_disabled_tests
32
+ end
33
+
34
+ # The opposite of #disable_fixtures_for.
35
+ #
36
+ # NOTE: fixture disabling is inherited thru the inheritance chain
37
+ # thus if You've disabled them in super (e.g. ActiveSupport::TestCase)
38
+ # there's no need to disable them again.
39
+ def enable_fixtures_for(tests = nil, &block)
40
+ outcome = disable_fixtures_for(tests, &block)
41
+ @fixtures_disabled_tests_negated = true
42
+ outcome
43
+ end
44
+
45
+ # #deprecated
46
+ def disable_fixtures(tests = true)
47
+ @fixtures_disabled_tests = tests
48
+ end
49
+
50
+ def enable_fixtures
51
+ @fixtures_disabled_tests = false
52
+ end
53
+
54
+ def fixtures_disabled?(test = nil)
55
+ fixtures_disabled_tests = @fixtures_disabled_tests
56
+ #fixtures_disabled_tests = self.instance_variable_get(:@fixtures_disabled_tests)
57
+ if fixtures_disabled_tests.nil?
58
+ superclass.respond_to?(:fixtures_disabled?) ?
59
+ superclass.fixtures_disabled?(test) : false
60
+ else
61
+ outcome = case fixtures_disabled_tests
62
+ when String then
63
+ test == fixtures_disabled_tests
64
+ when Symbol then
65
+ test == fixtures_disabled_tests.to_s
66
+ when Regexp then
67
+ !!(test =~ fixtures_disabled_tests)
68
+ when Array then
69
+ fixtures_disabled_tests.include?(test)
70
+ when Proc then
71
+ fixtures_disabled_tests.arity == 1 ?
72
+ fixtures_disabled_tests.call(test) :
73
+ fixtures_disabled_tests.call
74
+ else !! fixtures_disabled_tests
75
+ end
76
+ @fixtures_disabled_tests_negated ? ! outcome : outcome
77
+ end
78
+ end
79
+
80
+ end
81
+
82
+ Fixtures = defined?(ActiveRecord::Fixtures) ? ActiveRecord::Fixtures : Fixtures
83
+
84
+ def fixtures_disabled?
85
+ self.class.fixtures_disabled?(method_name)
86
+ end
87
+
88
+ def setup_fixtures
89
+ unless fixtures_disabled?
90
+ self.last_test_loaded_fixtures = true
91
+ return super # default (real) setup_fixtures from TestFixtures
92
+ end
93
+
94
+ if last_test_loaded_fixtures?
95
+ # need to reset all loaded fixtures - empty the tables :
96
+ unless (loaded_fixtures = already_loaded_fixtures).blank?
97
+ clear_loaded_fixtures(loaded_fixtures)
98
+ end
99
+ end
100
+
101
+ self.last_test_loaded_fixtures = false
102
+ @fixture_cache = {} # fixture helpers should work and return nothing
103
+ @fixture_connections = [] if enlist = respond_to?(:enlist_fixture_connections)
104
+
105
+ # begin transaction :
106
+ if use_transactional_fixtures? #run_in_transaction?
107
+ if enlist
108
+ @fixture_connections = enlist_fixture_connections
109
+ @fixture_connections.each do |connection|
110
+ connection.increment_open_transactions
111
+ connection.transaction_joinable = false
112
+ connection.begin_db_transaction
113
+ end
114
+ else
115
+ connection = ActiveRecord::Base.connection
116
+ connection.increment_open_transactions
117
+ connection.transaction_joinable = false if connection.respond_to?(:transaction_joinable=)
118
+ connection.begin_db_transaction
119
+ end
120
+ else
121
+ Fixtures.reset_cache # just to be sure ...
122
+ end
123
+ end
124
+
125
+ private
126
+
127
+ # Rails TestFixtures internals !
128
+ def already_loaded_fixtures
129
+ begin
130
+ self.class.send(:class_variable_get, :@@already_loaded_fixtures)
131
+ rescue NameError # might get here on first run in a single test
132
+ nil #{ self.class => {} }
133
+ end
134
+ end
135
+
136
+ # Rails TestFixtures internals !
137
+ def clear_loaded_fixtures(already_loaded_fixtures)
138
+ connection = ActiveRecord::Base.connection
139
+ connection.transaction(:requires_new => true) do
140
+ already_loaded_fixtures.values.each do |loaded_fixtures|
141
+ next if loaded_fixtures.nil?
142
+ loaded_fixtures.each_value do |fixtures|
143
+ if fixtures.respond_to?(:delete_existing_fixtures)
144
+ fixtures.delete_existing_fixtures
145
+ else
146
+ fixtures.table_rows.keys.each do |table|
147
+ connection.delete "DELETE FROM #{connection.quote_table_name(table)}", 'Fixture Delete'
148
+ end
149
+
150
+ end
151
+ end
152
+ end
153
+ # Cap primary key sequences to max(pk).
154
+ if connection.respond_to?(:reset_pk_sequence!)
155
+ fixture_table_names.each do |table_name|
156
+ connection.reset_pk_sequence!(table_name)
157
+ end
158
+ end
159
+ end
160
+
161
+ already_loaded_fixtures.clear
162
+ Fixtures.reset_cache # required to not break when multiple tests are run
163
+ end
164
+
165
+ #
166
+
167
+ def last_test_loaded_fixtures?
168
+ DisableTestFixtures.last_test_loaded_fixtures
169
+ end
170
+
171
+ def last_test_loaded_fixtures=(flag)
172
+ DisableTestFixtures.last_test_loaded_fixtures = flag
173
+ end
174
+
175
+ end
@@ -0,0 +1,110 @@
1
+ require File.expand_path('test_helper', File.dirname(__FILE__))
2
+
3
+ class NonDisabledFixturesTest < ActiveSupport::TestCase
4
+
5
+ def test_users
6
+ assert_equal 1, User.find_all_by_email('ferko@suska.net').size
7
+ end
8
+
9
+ def test_posts
10
+ assert_equal 3, Post.count
11
+ end
12
+
13
+ def test_comments
14
+ assert_equal 5, Comment.all.size
15
+ end
16
+
17
+ end
18
+
19
+ class DisableFixturesForAllTest < ActiveSupport::TestCase
20
+
21
+ disable_fixtures_for :all
22
+
23
+ def test_users
24
+ assert_equal 0, User.find_all_by_email('ferko@suska.net').size
25
+ end
26
+
27
+ def test_posts
28
+ assert_equal 0, Post.count
29
+ end
30
+
31
+ def test_comments
32
+ assert_equal 0, Comment.all.size
33
+ end
34
+
35
+ %W{User Post Comment}.each do |model|
36
+ test "no #{model.downcase}s found" do
37
+ clazz = Object.const_get(model.to_sym)
38
+ all_instances = clazz.find :all
39
+ #assert_equal Array, all_instances.class
40
+ assert_equal 0, all_instances.size
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ class DisableFixturesForRegexpTest < ActiveSupport::TestCase
47
+
48
+ disable_fixtures_for /users/
49
+
50
+ def test_users
51
+ assert_equal 0, User.find_all_by_email('ferko@suska.net').size
52
+ end
53
+
54
+ def test_posts
55
+ assert_equal 3, Post.count
56
+ end
57
+
58
+ def test_comments
59
+ assert_equal 5, Comment.all.size
60
+ end
61
+
62
+ test "no users found" do
63
+ all_instances = User.find :all
64
+ #assert_equal Array, all_instances.class
65
+ assert_equal 0, all_instances.size
66
+ end
67
+
68
+ end
69
+
70
+ class DisableFixturesForArrayOfNamesTest < ActiveSupport::TestCase
71
+
72
+ disable_fixtures_for %W{test_posts test_comments}
73
+
74
+ def test_users
75
+ assert_equal 1, User.find_all_by_email('ferko@suska.net').size
76
+ end
77
+
78
+ def test_posts
79
+ assert_equal 0, Post.count
80
+ end
81
+
82
+ def test_comments
83
+ assert_equal 0, Comment.all.size
84
+ end
85
+
86
+ end
87
+
88
+ class DisableFixturesForProcTest < ActiveSupport::TestCase
89
+
90
+ disable_fixtures_for do |test_name|
91
+ test_name == 'test_comments'
92
+ end
93
+
94
+ def setup
95
+ super
96
+ end
97
+
98
+ def test_users
99
+ assert_equal 1, User.find_all_by_email('ferko@suska.net').size
100
+ end
101
+
102
+ def test_posts
103
+ assert_equal 3, Post.count
104
+ end
105
+
106
+ def test_comments
107
+ assert_equal 0, Comment.all.size
108
+ end
109
+
110
+ end
@@ -0,0 +1,79 @@
1
+ require File.expand_path('test_helper', File.dirname(__FILE__))
2
+ require 'disable_test_fixtures'
3
+
4
+ class DisableTestFixturesTest < Test::Unit::TestCase
5
+
6
+ class TheTest
7
+ include DisableTestFixtures
8
+ end
9
+
10
+ def test_responds_to_disable_fixtures_for?
11
+ assert TheTest.respond_to? :disable_fixtures_for
12
+ end
13
+
14
+ def test_responds_to_enable_fixtures_for?
15
+ assert TheTest.respond_to? :enable_fixtures_for
16
+ end
17
+
18
+ class DisableAllTest
19
+ include DisableTestFixtures
20
+ disable_fixtures_for :all
21
+ end
22
+
23
+ def test_disable_all_fixtures_disabled?
24
+ assert DisableAllTest.fixtures_disabled?('test_method')
25
+ end
26
+
27
+ class EnableAllTest
28
+ include DisableTestFixtures
29
+ enable_fixtures_for :all
30
+ end
31
+
32
+ def test_enable_all_fixtures_disabled?
33
+ assert ! EnableAllTest.fixtures_disabled?('test_method')
34
+ end
35
+
36
+ class DisableAllInheritedTest < DisableAllTest; end
37
+
38
+ def test_inherited_disable_all_fixtures_disabled?
39
+ assert DisableAllInheritedTest.fixtures_disabled?('test_method')
40
+ end
41
+
42
+ class EnableAllInheritedTest < EnableAllTest; end
43
+
44
+ def test_inherited_enable_all_fixtures_disabled?
45
+ assert ! EnableAllInheritedTest.fixtures_disabled?('test_method')
46
+ end
47
+
48
+ class DisableEqualTest
49
+ include DisableTestFixtures
50
+ disable_fixtures_for [ 'test_method' ]
51
+ end
52
+
53
+ def test_disable_equal_fixtures_disabled?
54
+ assert DisableEqualTest.fixtures_disabled?('test_method')
55
+ assert ! DisableEqualTest.fixtures_disabled?('test_other_method')
56
+ end
57
+
58
+ class DisableMatchTest
59
+ include DisableTestFixtures
60
+ disable_fixtures_for /test_method/
61
+ end
62
+
63
+ def test_disable_match_fixtures_disabled?
64
+ assert DisableMatchTest.fixtures_disabled?('test_method')
65
+ assert DisableMatchTest.fixtures_disabled?('test_method_another')
66
+ assert ! DisableMatchTest.fixtures_disabled?('test_other_method')
67
+ end
68
+
69
+ class EnableEqualTest
70
+ include DisableTestFixtures
71
+ enable_fixtures_for [ 'test_method' ]
72
+ end
73
+
74
+ def test_disable_equal_fixtures_disabled?
75
+ assert ! EnableEqualTest.fixtures_disabled?('test_method')
76
+ assert EnableEqualTest.fixtures_disabled?('test_other_method')
77
+ end
78
+
79
+ end
@@ -0,0 +1,43 @@
1
+ require File.expand_path('test_helper', File.dirname(__FILE__))
2
+
3
+ class EnableFixturesForRegexpTest < ActiveSupport::TestCase
4
+
5
+ enable_fixtures_for /posts|comments/
6
+
7
+ def test_users
8
+ assert_equal 0, User.find_all_by_email('ferko@suska.net').size
9
+ end
10
+
11
+ def test_posts
12
+ assert_equal 3, Post.count
13
+ end
14
+
15
+ def test_comments
16
+ assert_equal 5, Comment.all.size
17
+ end
18
+
19
+ test "no users found" do
20
+ all_instances = User.find :all
21
+ #assert_equal Array, all_instances.class
22
+ assert_equal 0, all_instances.size
23
+ end
24
+
25
+ end
26
+
27
+ class EnableFixturesForArrayOfNamesTest < ActiveSupport::TestCase
28
+
29
+ enable_fixtures_for %W{ test_users }
30
+
31
+ def test_users
32
+ assert_equal 1, User.find_all_by_email('ferko@suska.net').size
33
+ end
34
+
35
+ def test_posts
36
+ assert_equal 0, Post.count
37
+ end
38
+
39
+ def test_comments
40
+ assert_equal 0, Comment.all.size
41
+ end
42
+
43
+ end
@@ -0,0 +1,52 @@
1
+
2
+ require 'rbconfig'
3
+
4
+ module MiniTestHelper
5
+
6
+ def self.enable_miniunit
7
+ File.symlink File.join(minidir, "minitest"), File.join(sitelib, "minitest")
8
+ File.symlink File.join(minidir, "test"), File.join(sitelib, "test")
9
+ puts "enabled miniunit - added links to #{sitelib}"
10
+ end
11
+
12
+ def self.disable_miniunit
13
+ File.unlink File.join(sitelib, "minitest")
14
+ File.unlink File.join(sitelib, "test")
15
+ puts "disabled miniunit - removed links from #{sitelib}"
16
+ end
17
+
18
+ private
19
+
20
+ def self.minidir
21
+ File.join(projdir, "lib")
22
+ end
23
+
24
+ def self.projdir
25
+ use_minitest_path = Gem.bin_path('minitest_tu_shim', 'use_minitest')
26
+ File.dirname(File.dirname(File.expand_path( use_minitest_path )))
27
+ end
28
+
29
+ def self.sitelib
30
+ Config::CONFIG["sitelibdir"]
31
+ end
32
+
33
+ end
34
+
35
+ require 'rubygems'
36
+
37
+ # NOTE: assuming testing on Ruby 1.8 !
38
+ #
39
+ # http://blog.floehopper.org/articles/2009/02/02/test-unit-and-minitest-with-different-ruby-versions
40
+ #
41
+ # sudo chmod a+rw /usr/local/lib/site_ruby/1.8 (sitelibdir)
42
+ #
43
+ def execute_with_minitest
44
+ raise 'no block given' unless block_given?
45
+ begin
46
+ MiniTestHelper.enable_miniunit
47
+ require 'test_helper'
48
+ yield
49
+ ensure
50
+ MiniTestHelper.disable_miniunit
51
+ end
52
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+ require 'mini_test_helper'
3
+
4
+ # run all tests with mini_test :
5
+ tests = Dir.glob('*_test.rb')
6
+ execute_with_minitest do
7
+ tests.each { |test| load test }
8
+ end
@@ -0,0 +1,104 @@
1
+ # enable testing with different version of rails via argv :
2
+ # ruby request_exception_handler_test.rb RAILS_VERSION=2.2.2
3
+
4
+ version =
5
+ if ARGV.find { |opt| /RAILS_VERSION=([\d\.]+)/ =~ opt }
6
+ $~[1] # rake test RAILS_VERSION=2.3.10
7
+ else
8
+ ENV['RAILS_VERSION']
9
+ end
10
+
11
+ if version
12
+ RAILS_VERSION = version
13
+ gem 'activesupport', "= #{RAILS_VERSION}"
14
+ gem 'activerecord', "= #{RAILS_VERSION}"
15
+ gem 'rails', "= #{RAILS_VERSION}"
16
+ else
17
+ gem 'activesupport'
18
+ gem 'activerecord'
19
+ gem 'rails'
20
+ end
21
+
22
+ require 'rails/version'
23
+ puts "emulating Rails.version = #{version = Rails::VERSION::STRING}"
24
+
25
+ require 'active_support'
26
+ require 'active_support/test_case'
27
+ if version >= '3.0.0'
28
+ # otherwise require 'active_record/fixtures' fails in 3.0.0 !
29
+ require 'active_support/core_ext/class/delegating_attributes'
30
+ end
31
+
32
+ require 'active_record'
33
+ require 'active_record/fixtures'
34
+ require 'active_record/test_case'
35
+
36
+ # Make double-sure the RAILS_ENV is set to test,
37
+ # so fixtures are loaded to the right database
38
+ silence_warnings { RAILS_ENV = "test" }
39
+ silence_warnings { RAILS_ROOT = File.dirname(__FILE__) }
40
+
41
+ module Rails
42
+ class << self
43
+
44
+ def initialized?
45
+ @initialized || false
46
+ end
47
+
48
+ def initialized=(initialized)
49
+ @initialized ||= initialized
50
+ end
51
+
52
+ def logger
53
+ if defined?(RAILS_DEFAULT_LOGGER)
54
+ RAILS_DEFAULT_LOGGER
55
+ else
56
+ nil
57
+ end
58
+ end
59
+
60
+ def backtrace_cleaner
61
+ @@backtrace_cleaner ||= begin
62
+ #require 'rails/gem_dependency' # backtrace_cleaner depends on this !
63
+ #require 'rails/backtrace_cleaner'
64
+ #Rails::BacktraceCleaner.new
65
+ require 'active_support/backtrace_cleaner'
66
+ ActiveSupport::BacktraceCleaner.new
67
+ end
68
+ end
69
+
70
+ def root
71
+ Pathname.new(RAILS_ROOT) if defined?(RAILS_ROOT)
72
+ end
73
+
74
+ def env
75
+ @_env ||= ActiveSupport::StringInquirer.new(RAILS_ENV)
76
+ end
77
+
78
+ def version
79
+ VERSION::STRING
80
+ end
81
+
82
+ def public_path
83
+ @@public_path ||= self.root ? File.join(self.root, "public") : "public"
84
+ end
85
+
86
+ def public_path=(path)
87
+ @@public_path = path
88
+ end
89
+ end
90
+ end
91
+
92
+ class ActiveSupport::TestCase
93
+ # only if defined e.g. Rails 2.2.3 monkey patches Test::Unit::TestCase :
94
+ include ActiveRecord::TestFixtures if defined? ActiveRecord::TestFixtures
95
+
96
+ #self.fixture_path = "#{RAILS_ROOT}/test/fixtures/"
97
+ #self.use_instantiated_fixtures = false
98
+ #self.use_transactional_fixtures = true
99
+
100
+ end
101
+
102
+ def create_fixtures(*table_names, &block)
103
+ Fixtures.create_fixtures(ActiveSupport::TestCase.fixture_path, table_names, {}, &block)
104
+ end
@@ -0,0 +1,61 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ DATA_DIR = File.join(File.dirname(__FILE__), 'data')
5
+ FIXTURES_ROOT = File.join(DATA_DIR, 'fixtures')
6
+
7
+ require File.join(File.dirname(__FILE__), 'rails_setup')
8
+
9
+ ActiveRecord::Migration.verbose = false # quiet down the migration engine
10
+
11
+ db =
12
+ if ARGV.find { |opt| /DB=([\d\.]+)/ =~ opt }
13
+ $~[1] # rake test DB=mysql
14
+ else
15
+ ENV['DB']
16
+ end || 'sqlite3'
17
+
18
+ load File.join(File.dirname(__FILE__), 'db', "#{db}_config.rb")
19
+
20
+ ActiveRecord::Base.establish_connection('test')
21
+ ActiveRecord::Base.silence do
22
+ load File.join(DATA_DIR, 'schema.rb')
23
+ end
24
+
25
+ require File.join(File.dirname(__FILE__), 'data/models')
26
+
27
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '../lib')
28
+ require 'disable_test_fixtures'
29
+
30
+ Fixtures = ActiveRecord::Fixtures unless defined?(Fixtures) # used in .ymls
31
+
32
+ class ActiveSupport::TestCase
33
+
34
+ include DisableTestFixtures
35
+
36
+ self.fixture_path = FIXTURES_ROOT
37
+
38
+ # Transactional fixtures accelerate your tests by wrapping each test method
39
+ # in a transaction that's rolled back on completion. This ensures that the
40
+ # test database remains unchanged so your fixtures don't have to be reloaded
41
+ # between every test method. Fewer database queries means faster tests.
42
+ #
43
+ # The only drawback to using transactional fixtures is when you actually
44
+ # need to test transactions. Since your test is bracketed by a transaction,
45
+ # any transactions started in your code will be automatically rolled back.
46
+ self.use_transactional_fixtures = true
47
+
48
+ # Instantiated fixtures are slow, but give you @david where otherwise you
49
+ # would need people(:david). If you don't want to migrate your existing
50
+ # test cases which use the @david style and don't mind the speed hit (each
51
+ # instantiated fixtures translates to a database query per test method),
52
+ # then set this back to true.
53
+ self.use_instantiated_fixtures = false
54
+
55
+ # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
56
+ #
57
+ # Note: You'll currently still have to declare fixtures explicitly in integration tests
58
+ # -- they do not yet inherit this setting
59
+ fixtures :all
60
+
61
+ end
@@ -0,0 +1,35 @@
1
+ require File.expand_path('test_helper', File.dirname(__FILE__))
2
+
3
+ class UserTest < ActiveSupport::TestCase
4
+
5
+ disable_fixtures_for :all
6
+
7
+ def test_users_1
8
+ User.create! :name => 'User 1', :email => 'user1@test.me'
9
+
10
+ assert_equal 1, User.all.size
11
+ end
12
+
13
+ def test_users_2
14
+ User.create! :name => 'User 1', :email => 'user1@test.me'
15
+ User.create! :name => 'User 2', :email => 'user2@test.me'
16
+
17
+ assert_equal 2, User.count
18
+ end
19
+
20
+ def test_users_3
21
+ User.create! :name => 'User 1', :email => 'user1@test.me'
22
+ User.create! :name => 'User 2', :email => 'user2@test.me'
23
+ User.create! :name => 'User 3', :email => 'user3@test.me'
24
+
25
+ assert_not_nil User.first
26
+ assert_not_equal User.first, User.last
27
+
28
+ assert_equal 3, User.count
29
+ end
30
+
31
+ def test_users_0
32
+ assert_equal [], User.find(:all)
33
+ end
34
+
35
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: disable_test_fixtures
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
+ platform: ruby
12
+ authors:
13
+ - Karol Bucek
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-04-22 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: actionpack
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 5
29
+ segments:
30
+ - 2
31
+ - 3
32
+ version: "2.3"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: mocha
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ description: Unit tests should be fast, active records (unless testing a complicated non data dependent business logic) should be connected to a database, but every time such a test method runs Rails will make sure Your fixtures are correctly loaded (and there's no way turning it off for particular tests).
50
+ email:
51
+ - self@kares.org
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files:
57
+ - README.md
58
+ files:
59
+ - lib/disable_test_fixtures.rb
60
+ - LICENSE
61
+ - README.md
62
+ - Rakefile
63
+ - test/disable_test_fixtures_test.rb
64
+ - test/user_test.rb
65
+ - test/mini_test_run.rb
66
+ - test/mini_test_helper.rb
67
+ - test/enable_fixtures_for_test.rb
68
+ - test/test_helper.rb
69
+ - test/disable_fixtures_for_test.rb
70
+ - test/rails_setup.rb
71
+ homepage: http://github.com/kares/disable_test_fixtures
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options: []
76
+
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 3
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ requirements: []
98
+
99
+ rubyforge_project: "[none]"
100
+ rubygems_version: 1.8.15
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Rails plugin that disables fixture loading during test setup.
104
+ test_files:
105
+ - test/disable_test_fixtures_test.rb
106
+ - test/user_test.rb
107
+ - test/mini_test_run.rb
108
+ - test/mini_test_helper.rb
109
+ - test/enable_fixtures_for_test.rb
110
+ - test/test_helper.rb
111
+ - test/disable_fixtures_for_test.rb
112
+ - test/rails_setup.rb