Skip to content
This repository
Newer
Older
100644 922 lines (821 sloc) 30.262 kb
db045dbb » dhh
2004-11-24 Initial
1 require 'erb'
59f32184 » tenderlove
2011-01-21 load and prefer psych as the YAML parser when it is available
2
3 begin
4 require 'psych'
5 rescue LoadError
6 end
7
db045dbb » dhh
2004-11-24 Initial
8 require 'yaml'
87adecfe » metaskills
2009-05-11 Reimplement Fixtures.identify so that it consistently generates ident…
9 require 'zlib'
21901e93 » josh
2008-11-24 fixtures depends on dependencies
10 require 'active_support/dependencies'
2538ef0d » jeremy
2010-04-24 Use Array.wrap to quiet 1.8.8 deprecation
11 require 'active_support/core_ext/array/wrap'
76f024ac » fxn
2010-03-28 adds missing requires for Object#blank? and Object#present?
12 require 'active_support/core_ext/object/blank'
2538ef0d » jeremy
2010-04-24 Use Array.wrap to quiet 1.8.8 deprecation
13 require 'active_support/core_ext/logger'
83dd6e1e » tenderlove
2011-02-11 favor composition over inheritence. use AS::OrderedHash rather than omap
14 require 'active_support/ordered_hash'
ad3f0eec » tenderlove
2011-05-10 adding AR::Fixtures::File class to wrap a fixture file
15 require 'active_record/fixtures/file'
88c3416f » jeremy
2005-10-13 r3581@sedna: jeremy | 2005-10-13 12:20:42 -0700
16
77de690a » jeremy
2007-12-09 Be careful not to reference ActiveRecord if it isn't loaded, fixing r…
17 if defined? ActiveRecord
18 class FixtureClassNotFound < ActiveRecord::ActiveRecordError #:nodoc:
19 end
20 else
21 class FixtureClassNotFound < StandardError #:nodoc:
22 end
2383a604 » technoweenie
2006-03-06 Allow set_fixture_class to take Classes instead of strings for a clas…
23 end
24
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
25 module ActiveRecord
0a38e2a5 » fxn
2011-09-03 restores the API docs of AR::Fixtures, made a quick pass over them, r…
26 # \Fixtures are a way of organizing data that you want to test against; in short, sample data.
27 #
28 # They are stored in YAML files, one file per model, which are placed in the directory
29 # appointed by <tt>ActiveSupport::TestCase.fixture_path=(path)</tt> (this is automatically
30 # configured for Rails, so you can just put your files in <tt><your-rails-app>/test/fixtures/</tt>).
31 # The fixture file ends with the <tt>.yml</tt> file extension (Rails example:
32 # <tt><your-rails-app>/test/fixtures/web_sites.yml</tt>). The format of a fixture file looks
33 # like this:
34 #
35 # rubyonrails:
36 # id: 1
37 # name: Ruby on Rails
38 # url: http://www.rubyonrails.org
39 #
40 # google:
41 # id: 2
42 # name: Google
43 # url: http://www.google.com
44 #
45 # This fixture file includes two fixtures. Each YAML fixture (ie. record) is given a name and
46 # is followed by an indented list of key/value pairs in the "key: value" format. Records are
47 # separated by a blank line for your viewing pleasure.
48 #
49 # Note that fixtures are unordered. If you want ordered fixtures, use the omap YAML type.
50 # See http://yaml.org/type/omap.html
51 # for the specification. You will need ordered fixtures when you have foreign key constraints
52 # on keys in the same table. This is commonly needed for tree structures. Example:
53 #
54 # --- !omap
55 # - parent:
56 # id: 1
57 # parent_id: NULL
58 # title: Parent
59 # - child:
60 # id: 2
61 # parent_id: 1
62 # title: Child
63 #
64 # = Using Fixtures in Test Cases
65 #
66 # Since fixtures are a testing construct, we use them in our unit and functional tests. There
67 # are two ways to use the fixtures, but first let's take a look at a sample unit test:
68 #
69 # require 'test_helper'
70 #
71 # class WebSiteTest < ActiveSupport::TestCase
72 # test "web_site_count" do
73 # assert_equal 2, WebSite.count
74 # end
75 # end
76 #
77 # By default, <tt>test_helper.rb</tt> will load all of your fixtures into your test database,
78 # so this test will succeed.
79 #
80 # The testing environment will automatically load the all fixtures into the database before each
81 # test. To ensure consistent data, the environment deletes the fixtures before running the load.
82 #
83 # In addition to being available in the database, the fixture's data may also be accessed by
84 # using a special dynamic method, which has the same name as the model, and accepts the
85 # name of the fixture to instantiate:
86 #
87 # test "find" do
88 # assert_equal "Ruby on Rails", web_sites(:rubyonrails).name
89 # end
90 #
91 # Alternatively, you may enable auto-instantiation of the fixture data. For instance, take the
92 # following tests:
93 #
94 # test "find_alt_method_1" do
95 # assert_equal "Ruby on Rails", @web_sites['rubyonrails']['name']
96 # end
97 #
98 # test "find_alt_method_2" do
99 # assert_equal "Ruby on Rails", @rubyonrails.news
100 # end
101 #
102 # In order to use these methods to access fixtured data within your testcases, you must specify one of the
103 # following in your <tt>ActiveSupport::TestCase</tt>-derived class:
104 #
105 # - to fully enable instantiated fixtures (enable alternate methods #1 and #2 above)
106 # self.use_instantiated_fixtures = true
107 #
108 # - create only the hash for the fixtures, do not 'find' each instance (enable alternate method #1 only)
109 # self.use_instantiated_fixtures = :no_instances
110 #
111 # Using either of these alternate methods incurs a performance hit, as the fixtured data must be fully
112 # traversed in the database to create the fixture hash and/or instance variables. This is expensive for
113 # large sets of fixtured data.
114 #
115 # = Dynamic fixtures with ERB
116 #
117 # Some times you don't care about the content of the fixtures as much as you care about the volume.
118 # In these cases, you can mix ERB in with your YAML fixtures to create a bunch of fixtures for load
119 # testing, like:
120 #
121 # <% 1.upto(1000) do |i| %>
122 # fix_<%= i %>:
123 # id: <%= i %>
124 # name: guy_<%= 1 %>
125 # <% end %>
126 #
127 # This will create 1000 very simple fixtures.
128 #
129 # Using ERB, you can also inject dynamic values into your fixtures with inserts like
130 # <tt><%= Date.today.strftime("%Y-%m-%d") %></tt>.
131 # This is however a feature to be used with some caution. The point of fixtures are that they're
132 # stable units of predictable sample data. If you feel that you need to inject dynamic values, then
133 # perhaps you should reexamine whether your application is properly testable. Hence, dynamic values
134 # in fixtures are to be considered a code smell.
135 #
136 # = Transactional Fixtures
137 #
138 # Test cases can use begin+rollback to isolate their changes to the database instead of having to
139 # delete+insert for every test case.
140 #
141 # class FooTest < ActiveSupport::TestCase
142 # self.use_transactional_fixtures = true
143 #
144 # test "godzilla" do
145 # assert !Foo.all.empty?
146 # Foo.destroy_all
147 # assert Foo.all.empty?
148 # end
149 #
150 # test "godzilla aftermath" do
151 # assert !Foo.all.empty?
152 # end
153 # end
154 #
155 # If you preload your test database with all fixture data (probably in the rake task) and use
156 # transactional fixtures, then you may omit all fixtures declarations in your test cases since
157 # all the data's already there and every case rolls back its changes.
158 #
159 # In order to use instantiated fixtures with preloaded data, set +self.pre_loaded_fixtures+ to
160 # true. This will provide access to fixture data for every table that has been loaded through
161 # fixtures (depending on the value of +use_instantiated_fixtures+).
162 #
163 # When *not* to use transactional fixtures:
164 #
165 # 1. You're testing whether a transaction works correctly. Nested transactions don't commit until
166 # all parent transactions commit, particularly, the fixtures transaction which is begun in setup
167 # and rolled back in teardown. Thus, you won't be able to verify
168 # the results of your transaction until Active Record supports nested transactions or savepoints (in progress).
169 # 2. Your database does not support transactions. Every Active Record database supports transactions except MySQL MyISAM.
170 # Use InnoDB, MaxDB, or NDB instead.
171 #
172 # = Advanced Fixtures
173 #
174 # Fixtures that don't specify an ID get some extra features:
175 #
176 # * Stable, autogenerated IDs
177 # * Label references for associations (belongs_to, has_one, has_many)
178 # * HABTM associations as inline lists
179 # * Autofilled timestamp columns
180 # * Fixture label interpolation
181 # * Support for YAML defaults
182 #
183 # == Stable, Autogenerated IDs
184 #
185 # Here, have a monkey fixture:
186 #
187 # george:
188 # id: 1
189 # name: George the Monkey
190 #
191 # reginald:
192 # id: 2
193 # name: Reginald the Pirate
194 #
195 # Each of these fixtures has two unique identifiers: one for the database
196 # and one for the humans. Why don't we generate the primary key instead?
197 # Hashing each fixture's label yields a consistent ID:
198 #
199 # george: # generated id: 503576764
200 # name: George the Monkey
201 #
202 # reginald: # generated id: 324201669
203 # name: Reginald the Pirate
204 #
205 # Active Record looks at the fixture's model class, discovers the correct
206 # primary key, and generates it right before inserting the fixture
207 # into the database.
208 #
209 # The generated ID for a given label is constant, so we can discover
210 # any fixture's ID without loading anything, as long as we know the label.
211 #
212 # == Label references for associations (belongs_to, has_one, has_many)
213 #
214 # Specifying foreign keys in fixtures can be very fragile, not to
215 # mention difficult to read. Since Active Record can figure out the ID of
216 # any fixture from its label, you can specify FK's by label instead of ID.
217 #
218 # === belongs_to
219 #
220 # Let's break out some more monkeys and pirates.
221 #
222 # ### in pirates.yml
223 #
224 # reginald:
225 # id: 1
226 # name: Reginald the Pirate
227 # monkey_id: 1
228 #
229 # ### in monkeys.yml
230 #
231 # george:
232 # id: 1
233 # name: George the Monkey
234 # pirate_id: 1
235 #
236 # Add a few more monkeys and pirates and break this into multiple files,
237 # and it gets pretty hard to keep track of what's going on. Let's
238 # use labels instead of IDs:
239 #
240 # ### in pirates.yml
241 #
242 # reginald:
243 # name: Reginald the Pirate
244 # monkey: george
245 #
246 # ### in monkeys.yml
247 #
248 # george:
249 # name: George the Monkey
250 # pirate: reginald
251 #
252 # Pow! All is made clear. Active Record reflects on the fixture's model class,
253 # finds all the +belongs_to+ associations, and allows you to specify
254 # a target *label* for the *association* (monkey: george) rather than
255 # a target *id* for the *FK* (<tt>monkey_id: 1</tt>).
256 #
257 # ==== Polymorphic belongs_to
258 #
259 # Supporting polymorphic relationships is a little bit more complicated, since
260 # Active Record needs to know what type your association is pointing at. Something
261 # like this should look familiar:
262 #
263 # ### in fruit.rb
264 #
265 # belongs_to :eater, :polymorphic => true
266 #
267 # ### in fruits.yml
268 #
269 # apple:
270 # id: 1
271 # name: apple
272 # eater_id: 1
273 # eater_type: Monkey
274 #
275 # Can we do better? You bet!
276 #
277 # apple:
278 # eater: george (Monkey)
279 #
280 # Just provide the polymorphic target type and Active Record will take care of the rest.
281 #
282 # === has_and_belongs_to_many
283 #
284 # Time to give our monkey some fruit.
285 #
286 # ### in monkeys.yml
287 #
288 # george:
289 # id: 1
290 # name: George the Monkey
291 #
292 # ### in fruits.yml
293 #
294 # apple:
295 # id: 1
296 # name: apple
297 #
298 # orange:
299 # id: 2
300 # name: orange
301 #
302 # grape:
303 # id: 3
304 # name: grape
305 #
306 # ### in fruits_monkeys.yml
307 #
308 # apple_george:
309 # fruit_id: 1
310 # monkey_id: 1
311 #
312 # orange_george:
313 # fruit_id: 2
314 # monkey_id: 1
315 #
316 # grape_george:
317 # fruit_id: 3
318 # monkey_id: 1
319 #
320 # Let's make the HABTM fixture go away.
321 #
322 # ### in monkeys.yml
323 #
324 # george:
325 # id: 1
326 # name: George the Monkey
327 # fruits: apple, orange, grape
328 #
329 # ### in fruits.yml
330 #
331 # apple:
332 # name: apple
333 #
334 # orange:
335 # name: orange
336 #
337 # grape:
338 # name: grape
339 #
340 # Zap! No more fruits_monkeys.yml file. We've specified the list of fruits
341 # on George's fixture, but we could've just as easily specified a list
342 # of monkeys on each fruit. As with +belongs_to+, Active Record reflects on
343 # the fixture's model class and discovers the +has_and_belongs_to_many+
344 # associations.
345 #
346 # == Autofilled Timestamp Columns
347 #
348 # If your table/model specifies any of Active Record's
349 # standard timestamp columns (+created_at+, +created_on+, +updated_at+, +updated_on+),
350 # they will automatically be set to <tt>Time.now</tt>.
351 #
352 # If you've set specific values, they'll be left alone.
353 #
354 # == Fixture label interpolation
355 #
356 # The label of the current fixture is always available as a column value:
357 #
358 # geeksomnia:
359 # name: Geeksomnia's Account
360 # subdomain: $LABEL
361 #
362 # Also, sometimes (like when porting older join table fixtures) you'll need
363 # to be able to get a hold of the identifier for a given label. ERB
364 # to the rescue:
365 #
366 # george_reginald:
367 # monkey_id: <%= ActiveRecord::Fixtures.identify(:reginald) %>
368 # pirate_id: <%= ActiveRecord::Fixtures.identify(:george) %>
369 #
370 # == Support for YAML defaults
371 #
372 # You probably already know how to use YAML to set and reuse defaults in
373 # your <tt>database.yml</tt> file. You can use the same technique in your fixtures:
374 #
375 # DEFAULTS: &DEFAULTS
376 # created_on: <%= 3.weeks.ago.to_s(:db) %>
377 #
378 # first:
379 # name: Smurf
380 # *DEFAULTS
381 #
382 # second:
383 # name: Fraggle
384 # *DEFAULTS
385 #
386 # Any fixture labeled "DEFAULTS" is safely ignored.
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
387 class Fixtures
388 MAX_ID = 2 ** 30 - 1
c7589559 » dhh
2004-12-09 Tidied up Fixtures for better readability and some error checking [bi…
389
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
390 @@all_cached_fixtures = Hash.new { |h,k| h[k] = {} }
b095ce63 » jeremy
2007-09-28 Fixtures test fixes and general cleanup. Closes #9682 [norbert]
391
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
392 def self.find_table_name(table_name) # :nodoc:
393 ActiveRecord::Base.pluralize_table_names ?
394 table_name.to_s.singularize.camelize :
395 table_name.to_s.camelize
396 end
fb09d025 » tenderlove
2011-02-11 refactor fixtures to do less work in the constructor
397
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
398 def self.reset_cache
399 @@all_cached_fixtures.clear
400 end
b095ce63 » jeremy
2007-09-28 Fixtures test fixes and general cleanup. Closes #9682 [norbert]
401
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
402 def self.cache_for_connection(connection)
403 @@all_cached_fixtures[connection]
404 end
b095ce63 » jeremy
2007-09-28 Fixtures test fixes and general cleanup. Closes #9682 [norbert]
405
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
406 def self.fixture_is_cached?(connection, table_name)
407 cache_for_connection(connection)[table_name]
408 end
078bd051 » dhh
2007-09-28 Added fixture caching thatll speed up a normal fixture-powered test s…
409
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
410 def self.cached_fixtures(connection, keys_to_fetch = nil)
411 if keys_to_fetch
412 cache_for_connection(connection).values_at(*keys_to_fetch)
413 else
414 cache_for_connection(connection).values
415 end
078bd051 » dhh
2007-09-28 Added fixture caching thatll speed up a normal fixture-powered test s…
416 end
417
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
418 def self.cache_fixtures(connection, fixtures_map)
419 cache_for_connection(connection).update(fixtures_map)
420 end
b095ce63 » jeremy
2007-09-28 Fixtures test fixes and general cleanup. Closes #9682 [norbert]
421
f02e7dc9 » alexeymuranov
2012-05-09 Deprecate ::instantiate_fixtures unused parameter
422 #--
423 # TODO:NOTE: in the next version, the __with_new_arity suffix and
424 # the method with the old arity will be removed.
425 #++
426 def self.instantiate_fixtures__with_new_arity(object, fixture_set, load_instances = true) # :nodoc:
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
427 if load_instances
f02e7dc9 » alexeymuranov
2012-05-09 Deprecate ::instantiate_fixtures unused parameter
428 fixture_set.each do |fixture_name, fixture|
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
429 begin
f02e7dc9 » alexeymuranov
2012-05-09 Deprecate ::instantiate_fixtures unused parameter
430 object.instance_variable_set "@#{fixture_name}", fixture.find
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
431 rescue FixtureClassNotFound
432 nil
433 end
b2cfbc4c » dhh
2005-03-20 Added with additional settings for working with transactional fixture…
434 end
9b0fd9d0 » dhh
2005-02-07 Added that the 'fixture :posts' syntax can be used for has_and_belong…
435 end
db045dbb » dhh
2004-11-24 Initial
436 end
a2f0ae76 » jeremy
2005-10-23 r2718@asus: jeremy | 2005-10-23 14:45:30 -0700
437
f02e7dc9 » alexeymuranov
2012-05-09 Deprecate ::instantiate_fixtures unused parameter
438 # The use with parameters <tt>(object, fixture_set_name, fixture_set, load_instances = true)</tt> is deprecated, +fixture_set_name+ parameter is not used.
439 # Use as:
440 #
441 # instantiate_fixtures(object, fixture_set, load_instances = true)
442 def self.instantiate_fixtures(object, fixture_set, load_instances = true, rails_3_2_compatibility_argument = true)
443 unless load_instances == true || load_instances == false
444 ActiveSupport::Deprecation.warn(
445 "ActiveRecord::Fixtures.instantiate_fixtures with parameters (object, fixture_set_name, fixture_set, load_instances = true) is deprecated and shall be removed from future releases. Use it with parameters (object, fixture_set, load_instances = true) instead (skip fixture_set_name).",
446 caller)
447 fixture_set = load_instances
448 load_instances = rails_3_2_compatibility_argument
449 end
450 instantiate_fixtures__with_new_arity(object, fixture_set, load_instances)
451 end
452
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
453 def self.instantiate_all_loaded_fixtures(object, load_instances = true)
98105554 » alexeymuranov
2012-05-09 Remove deprecated use of ::instantiate_fixtures
454 all_loaded_fixtures.each_value do |fixture_set|
455 ActiveRecord::Fixtures.instantiate_fixtures(object, fixture_set, load_instances)
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
456 end
f8783abf » dhh
2005-04-03 Made eager loading work even more
457 end
b095ce63 » jeremy
2007-09-28 Fixtures test fixes and general cleanup. Closes #9682 [norbert]
458
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
459 cattr_accessor :all_loaded_fixtures
460 self.all_loaded_fixtures = {}
c7589559 » dhh
2004-12-09 Tidied up Fixtures for better readability and some error checking [bi…
461
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
462 def self.create_fixtures(fixtures_directory, table_names, class_names = {})
463 table_names = [table_names].flatten.map { |n| n.to_s }
464 table_names.each { |n|
465 class_names[n.tr('/', '_').to_sym] = n.classify if n.include?('/')
466 }
87b7b5b3 » dhh
2007-02-25 Added fixtures :all to test_helper.rb to assume that most people just…
467
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
468 # FIXME: Apparently JK uses this.
469 connection = block_given? ? yield : ActiveRecord::Base.connection
b095ce63 » jeremy
2007-09-28 Fixtures test fixes and general cleanup. Closes #9682 [norbert]
470
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
471 files_to_read = table_names.reject { |table_name|
472 fixture_is_cached?(connection, table_name)
473 }
87b7b5b3 » dhh
2007-02-25 Added fixtures :all to test_helper.rb to assume that most people just…
474
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
475 unless files_to_read.empty?
476 connection.disable_referential_integrity do
477 fixtures_map = {}
87b7b5b3 » dhh
2007-02-25 Added fixtures :all to test_helper.rb to assume that most people just…
478
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
479 fixture_files = files_to_read.map do |path|
480 table_name = path.tr '/', '_'
ca938c0f » tenderlove
2011-02-11 fixing variable names
481
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
482 fixtures_map[path] = ActiveRecord::Fixtures.new(
483 connection,
484 table_name,
485 class_names[table_name.to_sym] || table_name.classify,
74c5f65b » tenderlove
2011-05-10 use top level file constant for join, etc
486 ::File.join(fixtures_directory, path))
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
487 end
87b7b5b3 » dhh
2007-02-25 Added fixtures :all to test_helper.rb to assume that most people just…
488
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
489 all_loaded_fixtures.update(fixtures_map)
c7589559 » dhh
2004-12-09 Tidied up Fixtures for better readability and some error checking [bi…
490
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
491 connection.transaction(:requires_new => true) do
492 fixture_files.each do |ff|
493 conn = ff.model_class.respond_to?(:connection) ? ff.model_class.connection : connection
494 table_rows = ff.table_rows
a2f0ae76 » jeremy
2005-10-23 r2718@asus: jeremy | 2005-10-23 14:45:30 -0700
495
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
496 table_rows.keys.each do |table|
497 conn.delete "DELETE FROM #{conn.quote_table_name(table)}", 'Fixture Delete'
498 end
fd81c700 » tenderlove
2011-02-14 extract database activity out of Fixtures instances
499
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
500 table_rows.each do |table_name,rows|
501 rows.each do |row|
502 conn.insert_fixture(row, table_name)
503 end
49eafd8c » jeremy
2007-10-26 Foxy fixtures. Adapter#disable_referential_integrity. Closes #9981.
504 end
078bd051 » dhh
2007-09-28 Added fixture caching thatll speed up a normal fixture-powered test s…
505 end
b095ce63 » jeremy
2007-09-28 Fixtures test fixes and general cleanup. Closes #9682 [norbert]
506
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
507 # Cap primary key sequences to max(pk).
508 if connection.respond_to?(:reset_pk_sequence!)
509 table_names.each do |table_name|
510 connection.reset_pk_sequence!(table_name.tr('/', '_'))
511 end
078bd051 » dhh
2007-09-28 Added fixture caching thatll speed up a normal fixture-powered test s…
512 end
a2f0ae76 » jeremy
2005-10-23 r2718@asus: jeremy | 2005-10-23 14:45:30 -0700
513 end
77a6e255 » tenderlove
2011-02-11 use hash defaults to dry up ||= calls
514
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
515 cache_fixtures(connection, fixtures_map)
516 end
7117fdb8 » jeremy
2005-10-16 r3616@asus: jeremy | 2005-09-26 23:09:28 -0700
517 end
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
518 cached_fixtures(connection, table_names)
a2f0ae76 » jeremy
2005-10-23 r2718@asus: jeremy | 2005-10-23 14:45:30 -0700
519 end
520
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
521 # Returns a consistent, platform-independent identifier for +label+.
522 # Identifiers are positive integers less than 2^32.
523 def self.identify(label)
524 Zlib.crc32(label.to_s) % MAX_ID
0ebbf6be » tenderlove
2011-02-11 calculate model class on construction
525 end
526
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
527 attr_reader :table_name, :name, :fixtures, :model_class
79e85edd » dhh
2004-12-01 Added reverse order of deleting fixtures, so referential keys can be …
528
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
529 def initialize(connection, table_name, class_name, fixture_path)
530 @connection = connection
531 @table_name = table_name
532 @fixture_path = fixture_path
533 @name = table_name # preserve fixture base name
534 @class_name = class_name
83dd6e1e » tenderlove
2011-02-11 favor composition over inheritence. use AS::OrderedHash rather than omap
535
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
536 @fixtures = ActiveSupport::OrderedHash.new
537 @table_name = "#{ActiveRecord::Base.table_name_prefix}#{@table_name}#{ActiveRecord::Base.table_name_suffix}"
83dd6e1e » tenderlove
2011-02-11 favor composition over inheritence. use AS::OrderedHash rather than omap
538
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
539 # Should be an AR::Base type class
540 if class_name.is_a?(Class)
541 @table_name = class_name.table_name
542 @connection = class_name.connection
543 @model_class = class_name
544 else
545 @model_class = class_name.constantize rescue nil
546 end
83dd6e1e » tenderlove
2011-02-11 favor composition over inheritence. use AS::OrderedHash rather than omap
547
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
548 read_fixture_files
549 end
79e85edd » dhh
2004-12-01 Added reverse order of deleting fixtures, so referential keys can be …
550
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
551 def [](x)
552 fixtures[x]
553 end
49eafd8c » jeremy
2007-10-26 Foxy fixtures. Adapter#disable_referential_integrity. Closes #9981.
554
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
555 def []=(k,v)
556 fixtures[k] = v
557 end
49eafd8c » jeremy
2007-10-26 Foxy fixtures. Adapter#disable_referential_integrity. Closes #9981.
558
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
559 def each(&block)
560 fixtures.each(&block)
561 end
49eafd8c » jeremy
2007-10-26 Foxy fixtures. Adapter#disable_referential_integrity. Closes #9981.
562
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
563 def size
564 fixtures.size
565 end
49eafd8c » jeremy
2007-10-26 Foxy fixtures. Adapter#disable_referential_integrity. Closes #9981.
566
0034b782 » smartinez87
2011-05-23 Remove extra white spaces on ActiveRecord docs.
567 # Return a hash of rows to be inserted. The key is the table, the value is
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
568 # a list of rows to insert to that table.
569 def table_rows
570 now = ActiveRecord::Base.default_timezone == :utc ? Time.now.utc : Time.now
571 now = now.to_s(:db)
49eafd8c » jeremy
2007-10-26 Foxy fixtures. Adapter#disable_referential_integrity. Closes #9981.
572
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
573 # allow a standard key to be used for doing defaults in YAML
574 fixtures.delete('DEFAULTS')
49eafd8c » jeremy
2007-10-26 Foxy fixtures. Adapter#disable_referential_integrity. Closes #9981.
575
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
576 # track any join tables we need to insert later
577 rows = Hash.new { |h,table| h[table] = [] }
49eafd8c » jeremy
2007-10-26 Foxy fixtures. Adapter#disable_referential_integrity. Closes #9981.
578
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
579 rows[table_name] = fixtures.map do |label, fixture|
580 row = fixture.to_hash
581
582 if model_class && model_class < ActiveRecord::Base
583 # fill in timestamp columns if they aren't specified and the model is set to record_timestamps
584 if model_class.record_timestamps
585 timestamp_column_names.each do |name|
586 row[name] = now unless row.key?(name)
587 end
c7e39691 » jeremy
2007-11-26 Foxy fixtures: support single-table inheritance. Closes #10234.
588 end
589
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
590 # interpolate the fixture label
591 row.each do |key, value|
592 row[key] = label if value == "$LABEL"
593 end
8b3f8310 » jeremy
2007-11-26 Foxy fixtures: allow mixed usage to make migration easier and more at…
594
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
595 # generate a primary key if necessary
596 if has_primary_key_column? && !row.include?(primary_key_name)
597 row[primary_key_name] = ActiveRecord::Fixtures.identify(label)
598 end
c95f066b » NZKoz
2007-11-20 Add 'foxy' support for fixtures of polymorphic associations. Closes #…
599
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
600 # If STI is used, find the correct subclass for association reflection
601 reflection_class =
602 if row.include?(inheritance_column_name)
603 row[inheritance_column_name].constantize rescue model_class
604 else
605 model_class
49eafd8c » jeremy
2007-10-26 Foxy fixtures. Adapter#disable_referential_integrity. Closes #9981.
606 end
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
607
608 reflection_class.reflect_on_all_associations.each do |association|
609 case association.macro
610 when :belongs_to
611 # Do not replace association name with association foreign key if they are named the same
612 fk_name = (association.options[:foreign_key] || "#{association.name}_id").to_s
613
614 if association.name.to_s != fk_name && value = row.delete(association.name.to_s)
615 if association.options[:polymorphic] && value.sub!(/\s*\(([^\)]*)\)\s*$/, "")
616 # support polymorphic belongs_to as "label (Type)"
617 row[association.foreign_type] = $1
618 end
619
620 row[fk_name] = ActiveRecord::Fixtures.identify(value)
621 end
622 when :has_and_belongs_to_many
623 if (targets = row.delete(association.name.to_s))
624 targets = targets.is_a?(Array) ? targets : targets.split(/\s*,\s*/)
625 table_name = association.options[:join_table]
626 rows[table_name].concat targets.map { |target|
627 { association.foreign_key => row[primary_key_name],
628 association.association_foreign_key => ActiveRecord::Fixtures.identify(target) }
629 }
630 end
49eafd8c » jeremy
2007-10-26 Foxy fixtures. Adapter#disable_referential_integrity. Closes #9981.
631 end
632 end
633 end
634
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
635 row
636 end
637 rows
49eafd8c » jeremy
2007-10-26 Foxy fixtures. Adapter#disable_referential_integrity. Closes #9981.
638 end
639
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
640 private
641 def primary_key_name
64747654 » jonleighton
2011-10-05 Revert "Raise error on unknown primary key."
642 @primary_key_name ||= model_class && model_class.primary_key
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
643 end
8b3f8310 » jeremy
2007-11-26 Foxy fixtures: allow mixed usage to make migration easier and more at…
644
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
645 def has_primary_key_column?
646 @has_primary_key_column ||= primary_key_name &&
647 model_class.columns.any? { |c| c.name == primary_key_name }
648 end
49eafd8c » jeremy
2007-10-26 Foxy fixtures. Adapter#disable_referential_integrity. Closes #9981.
649
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
650 def timestamp_column_names
651 @timestamp_column_names ||=
652 %w(created_at created_on updated_at updated_on) & column_names
653 end
c7e39691 » jeremy
2007-11-26 Foxy fixtures: support single-table inheritance. Closes #10234.
654
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
655 def inheritance_column_name
656 @inheritance_column_name ||= model_class && model_class.inheritance_column
657 end
49eafd8c » jeremy
2007-10-26 Foxy fixtures. Adapter#disable_referential_integrity. Closes #9981.
658
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
659 def column_names
660 @column_names ||= @connection.columns(@table_name).collect { |c| c.name }
db045dbb » dhh
2004-11-24 Initial
661 end
662
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
663 def read_fixture_files
1716da07 » tenderlove
2011-05-10 removing CSV fixture support
664 yaml_files = Dir["#{@fixture_path}/**/*.yml"].select { |f|
665 ::File.file?(f)
666 } + [yaml_file_path]
667
668 yaml_files.each do |file|
669 Fixtures::File.open(file) do |fh|
670 fh.each do |name, row|
671 fixtures[name] = ActiveRecord::Fixture.new(row, model_class)
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
672 end
b095ce63 » jeremy
2007-09-28 Fixtures test fixes and general cleanup. Closes #9682 [norbert]
673 end
674 end
675 end
c7589559 » dhh
2004-12-09 Tidied up Fixtures for better readability and some error checking [bi…
676
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
677 def yaml_file_path
678 "#{@fixture_path}.yml"
679 end
5e3eaff5 » dhh
2004-12-07 Added CSV format for fixtures #272 [what-a-day]
680
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
681 end
db045dbb » dhh
2004-11-24 Initial
682
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
683 class Fixture #:nodoc:
684 include Enumerable
b095ce63 » jeremy
2007-09-28 Fixtures test fixes and general cleanup. Closes #9682 [norbert]
685
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
686 class FixtureError < StandardError #:nodoc:
687 end
b095ce63 » jeremy
2007-09-28 Fixtures test fixes and general cleanup. Closes #9682 [norbert]
688
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
689 class FormatError < FixtureError #:nodoc:
690 end
db045dbb » dhh
2004-11-24 Initial
691
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
692 attr_reader :model_class, :fixture
33e96f3c » jeremy
2007-05-26 Oracle binary fixtures; pull fixture insertion into the adapters. Clo…
693
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
694 def initialize(fixture, model_class)
695 @fixture = fixture
696 @model_class = model_class
697 end
83f65594 » jeremy
2008-01-05 Prefer to instantiate fixtures with model classes instead of their na…
698
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
699 def class_name
700 model_class.name if model_class
701 end
db045dbb » dhh
2004-11-24 Initial
702
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
703 def each
704 fixture.each { |item| yield item }
705 end
db045dbb » dhh
2004-11-24 Initial
706
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
707 def [](key)
708 fixture[key]
709 end
db045dbb » dhh
2004-11-24 Initial
710
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
711 alias :to_hash :fixture
c7589559 » dhh
2004-12-09 Tidied up Fixtures for better readability and some error checking [bi…
712
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
713 def find
714 if model_class
715 model_class.find(fixture[model_class.primary_key])
716 else
717 raise FixtureClassNotFound, "No class attached to find."
718 end
9b0fd9d0 » dhh
2005-02-07 Added that the 'fixture :posts' syntax can be used for has_and_belong…
719 end
db045dbb » dhh
2004-11-24 Initial
720 end
721 end
722
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
723 module ActiveRecord
724 module TestFixtures
4e50a35f » josh
2009-05-28 Break up DependencyModule's dual function of providing a "depend_on" …
725 extend ActiveSupport::Concern
a2875bec » brynary
2009-05-11 Use DependencyModule for included hooks in ActiveRecord
726
727 included do
728 setup :setup_fixtures
729 teardown :teardown_fixtures
730
d8db5714 » neerajdotname
2010-07-28 replacing superclass_delegating_accessor with class_attribute
731 class_attribute :fixture_path
732 class_attribute :fixture_table_names
733 class_attribute :fixture_class_names
734 class_attribute :use_transactional_fixtures
735 class_attribute :use_instantiated_fixtures # true, false, or :no_instances
736 class_attribute :pre_loaded_fixtures
a2875bec » brynary
2009-05-11 Use DependencyModule for included hooks in ActiveRecord
737
738 self.fixture_table_names = []
894ce155 » packagethief
2010-01-06 Use better defaults for fixtures: transactional=true, instantiated=false
739 self.use_transactional_fixtures = true
740 self.use_instantiated_fixtures = false
a2875bec » brynary
2009-05-11 Use DependencyModule for included hooks in ActiveRecord
741 self.pre_loaded_fixtures = false
87b7b5b3 » dhh
2007-02-25 Added fixtures :all to test_helper.rb to assume that most people just…
742
fb09d025 » tenderlove
2011-02-11 refactor fixtures to do less work in the constructor
743 self.fixture_class_names = Hash.new do |h, table_name|
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
744 h[table_name] = ActiveRecord::Fixtures.find_table_name(table_name)
fb09d025 » tenderlove
2011-02-11 refactor fixtures to do less work in the constructor
745 end
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
746 end
747
748 module ClassMethods
749 def set_fixture_class(class_names = {})
750 self.fixture_class_names = self.fixture_class_names.merge(class_names)
751 end
bc1aa7d9 » dhh
2004-12-14 fixtures :models will now also attempt to include the model.rb file
752
f8321856 » tenderlove
2011-02-15 fixing variable names, removing FixtureFile instances from test ivars
753 def fixtures(*fixture_names)
754 if fixture_names.first == :all
1716da07 » tenderlove
2011-05-10 removing CSV fixture support
755 fixture_names = Dir["#{fixture_path}/**/*.{yml}"]
f8321856 » tenderlove
2011-02-15 fixing variable names, removing FixtureFile instances from test ivars
756 fixture_names.map! { |f| f[(fixture_path.size + 1)..-5] }
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
757 else
f8321856 » tenderlove
2011-02-15 fixing variable names, removing FixtureFile instances from test ivars
758 fixture_names = fixture_names.flatten.map { |n| n.to_s }
139b9249 » jeremy
2008-01-05 * Continue evolution toward ActiveSupport::TestCase and friends. #10679
759 end
760
f8321856 » tenderlove
2011-02-15 fixing variable names, removing FixtureFile instances from test ivars
761 self.fixture_table_names |= fixture_names
762 require_fixture_classes(fixture_names)
763 setup_fixture_accessors(fixture_names)
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
764 end
765
766 def try_to_load_dependency(file_name)
767 require_dependency file_name
768 rescue LoadError => e
769 # Let's hope the developer has included it himself
3b0e1d90 » josh
2008-05-14 Prefer string core_ext inflector methods over directly accessing Infl…
770
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
771 # Let's warn in case this is a subdependency, otherwise
772 # subdependency error messages are totally cryptic
773 if ActiveRecord::Base.logger
774 ActiveRecord::Base.logger.warn("Unable to load #{file_name}, underlying cause #{e.message} \n\n #{e.backtrace.join("\n")}")
fbf41c72 » dhh
2008-03-17 Added logging for dependency load errors with fixtures (closes #11056)…
775 end
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
776 end
3b0e1d90 » josh
2008-05-14 Prefer string core_ext inflector methods over directly accessing Infl…
777
f8321856 » tenderlove
2011-02-15 fixing variable names, removing FixtureFile instances from test ivars
778 def require_fixture_classes(fixture_names = nil)
779 (fixture_names || fixture_table_names).each do |fixture_name|
780 file_name = fixture_name.to_s
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
781 file_name = file_name.singularize if ActiveRecord::Base.pluralize_table_names
782 try_to_load_dependency(file_name)
f389a8fb » dhh
2004-12-16 Updated and added more documentation
783 end
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
784 end
c7589559 » dhh
2004-12-09 Tidied up Fixtures for better readability and some error checking [bi…
785
8c4c07e4 » tenderlove
2011-02-15 fixing variable names to reflect reality
786 def setup_fixture_accessors(fixture_names = nil)
787 fixture_names = Array.wrap(fixture_names || fixture_table_names)
8276a240 » tenderlove
2011-02-15 fixture methods can be overridden and super() is useful
788 methods = Module.new do
789 fixture_names.each do |fixture_name|
790 fixture_name = fixture_name.to_s.tr('./', '_')
673daeab » jeremy
2007-06-05 Fixtures: people(:technomancy, :josh) returns both fixtures. Closes #…
791
8276a240 » tenderlove
2011-02-15 fixture methods can be overridden and super() is useful
792 define_method(fixture_name) do |*fixtures|
793 force_reload = fixtures.pop if fixtures.last == true || fixtures.last == :reload
23e24c59 » dhh
2007-02-25 Setting fixtures automatically for integration tests is a bit harder …
794
8276a240 » tenderlove
2011-02-15 fixture methods can be overridden and super() is useful
795 @fixture_cache[fixture_name] ||= {}
23e24c59 » dhh
2007-02-25 Setting fixtures automatically for integration tests is a bit harder …
796
8276a240 » tenderlove
2011-02-15 fixture methods can be overridden and super() is useful
797 instances = fixtures.map do |fixture|
798 @fixture_cache[fixture_name].delete(fixture) if force_reload
673daeab » jeremy
2007-06-05 Fixtures: people(:technomancy, :josh) returns both fixtures. Closes #…
799
8276a240 » tenderlove
2011-02-15 fixture methods can be overridden and super() is useful
800 if @loaded_fixtures[fixture_name][fixture.to_s]
0b702ba3 » miloops
2011-02-18 Merge remote branch 'rails/master' into identity_map
801 ActiveRecord::IdentityMap.without do
802 @fixture_cache[fixture_name][fixture] ||= @loaded_fixtures[fixture_name][fixture.to_s].find
803 end
8276a240 » tenderlove
2011-02-15 fixture methods can be overridden and super() is useful
804 else
805 raise StandardError, "No fixture with name '#{fixture}' found for table '#{fixture_name}'"
ce66bfdc » swistak
2010-08-28 IdentityMap - misc fixes
806 end
673daeab » jeremy
2007-06-05 Fixtures: people(:technomancy, :josh) returns both fixtures. Closes #…
807 end
6d9ee4a3 » jamis
2005-06-03 Added fixture accessors that can be used when fixture instantiation i…
808
8276a240 » tenderlove
2011-02-15 fixture methods can be overridden and super() is useful
809 instances.size == 1 ? instances.first : instances
810 end
811 private fixture_name
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
812 end
139b9249 » jeremy
2008-01-05 * Continue evolution toward ActiveSupport::TestCase and friends. #10679
813 end
8276a240 » tenderlove
2011-02-15 fixture methods can be overridden and super() is useful
814 include methods
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
815 end
2c0fa320 » jamis
2005-06-10 Do not use instantiated fixtures (in general) in tests. Also, support…
816
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
817 def uses_transaction(*methods)
818 @uses_transaction = [] unless defined?(@uses_transaction)
a7eb8d97 » sikachu
2010-08-14 Removing most of the symbol to proc usage in Active Record
819 @uses_transaction.concat methods.map { |m| m.to_s }
2c0fa320 » jamis
2005-06-10 Do not use instantiated fixtures (in general) in tests. Also, support…
820 end
821
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
822 def uses_transaction?(method)
823 @uses_transaction = [] unless defined?(@uses_transaction)
824 @uses_transaction.include?(method.to_s)
2c0fa320 » jamis
2005-06-10 Do not use instantiated fixtures (in general) in tests. Also, support…
825 end
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
826 end
2c0fa320 » jamis
2005-06-10 Do not use instantiated fixtures (in general) in tests. Also, support…
827
ad93212f » jeremy
2008-11-25 Rename use_transactional_fixtures? so it doesn't collide with the sup…
828 def run_in_transaction?
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
829 use_transactional_fixtures &&
830 !self.class.uses_transaction?(method_name)
831 end
5b866f75 » jeremy
2006-06-29 r4720@asus: jeremy | 2006-06-28 18:12:57 -0700
832
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
833 def setup_fixtures
13e72db7 » tenderlove
2011-02-15 this is in the AR namespace, so AR must exist
834 return unless !ActiveRecord::Base.configurations.blank?
b2cfbc4c » dhh
2005-03-20 Added with additional settings for working with transactional fixture…
835
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
836 if pre_loaded_fixtures && !use_transactional_fixtures
837 raise RuntimeError, 'pre_loaded_fixtures requires use_transactional_fixtures'
838 end
17d3fd31 » jeremy
2005-10-25 r2736@asus: jeremy | 2005-10-24 17:08:12 -0700
839
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
840 @fixture_cache = {}
d102791d » tenderlove
2011-12-10 ensure @fixture_connections is initialized in case an exception happe…
841 @fixture_connections = []
d9f460a3 » lifo
2008-11-16 Ensure @@already_loaded_fixtures is initialized before use
842 @@already_loaded_fixtures ||= {}
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
843
844 # Load fixtures once and begin transaction.
ad93212f » jeremy
2008-11-25 Rename use_transactional_fixtures? so it doesn't collide with the sup…
845 if run_in_transaction?
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
846 if @@already_loaded_fixtures[self.class]
847 @loaded_fixtures = @@already_loaded_fixtures[self.class]
903ef71b » dhh
2005-03-06 Added transactional fixtures that uses rollback to undo changes to fi…
848 else
e0802fa1 » tenderlove
2011-02-11 avoid side effects from method calls, localize ivar assignment
849 @loaded_fixtures = load_fixtures
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
850 @@already_loaded_fixtures[self.class] = @loaded_fixtures
903ef71b » dhh
2005-03-06 Added transactional fixtures that uses rollback to undo changes to fi…
851 end
999b7ed9 » jeremy
2011-10-05 Transactional fixtures enlist all active database connections.
852 @fixture_connections = enlist_fixture_connections
853 @fixture_connections.each do |connection|
854 connection.increment_open_transactions
855 connection.transaction_joinable = false
856 connection.begin_db_transaction
857 end
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
858 # Load fixtures for every test.
859 else
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
860 ActiveRecord::Fixtures.reset_cache
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
861 @@already_loaded_fixtures[self.class] = nil
e0802fa1 » tenderlove
2011-02-11 avoid side effects from method calls, localize ivar assignment
862 @loaded_fixtures = load_fixtures
903ef71b » dhh
2005-03-06 Added transactional fixtures that uses rollback to undo changes to fi…
863 end
864
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
865 # Instantiate fixtures for every test if requested.
866 instantiate_fixtures if use_instantiated_fixtures
867 end
868
869 def teardown_fixtures
870 return unless defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank?
5b866f75 » jeremy
2006-06-29 r4720@asus: jeremy | 2006-06-28 18:12:57 -0700
871
ad93212f » jeremy
2008-11-25 Rename use_transactional_fixtures? so it doesn't collide with the sup…
872 unless run_in_transaction?
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
873 ActiveRecord::Fixtures.reset_cache
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
874 end
b095ce63 » jeremy
2007-09-28 Fixtures test fixes and general cleanup. Closes #9682 [norbert]
875
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
876 # Rollback changes if a transaction is active.
999b7ed9 » jeremy
2011-10-05 Transactional fixtures enlist all active database connections.
877 if run_in_transaction?
878 @fixture_connections.each do |connection|
879 if connection.open_transactions != 0
880 connection.rollback_db_transaction
881 connection.decrement_open_transactions
882 end
883 end
884 @fixture_connections.clear
db045dbb » dhh
2004-11-24 Initial
885 end
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
886 ActiveRecord::Base.clear_active_connections!
887 end
c7589559 » dhh
2004-12-09 Tidied up Fixtures for better readability and some error checking [bi…
888
999b7ed9 » jeremy
2011-10-05 Transactional fixtures enlist all active database connections.
889 def enlist_fixture_connections
890 ActiveRecord::Base.connection_handler.connection_pools.values.map(&:connection)
891 end
892
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
893 private
894 def load_fixtures
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
895 fixtures = ActiveRecord::Fixtures.create_fixtures(fixture_path, fixture_table_names, fixture_class_names)
e0802fa1 » tenderlove
2011-02-11 avoid side effects from method calls, localize ivar assignment
896 Hash[fixtures.map { |f| [f.name, f] }]
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
897 end
03097d3a » dhh
2005-04-18 Fixed that fixtures were being deleted in the same order as inserts c…
898
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
899 # for pre_loaded_fixtures, only require the classes once. huge speed improvement
900 @@required_fixture_classes = false
03097d3a » dhh
2005-04-18 Fixed that fixtures were being deleted in the same order as inserts c…
901
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
902 def instantiate_fixtures
903 if pre_loaded_fixtures
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
904 raise RuntimeError, 'Load fixtures before instantiating them.' if ActiveRecord::Fixtures.all_loaded_fixtures.empty?
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
905 unless @@required_fixture_classes
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
906 self.class.require_fixture_classes ActiveRecord::Fixtures.all_loaded_fixtures.keys
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
907 @@required_fixture_classes = true
908 end
7db2ef47 » jasonnoble
2011-05-07 Namespace Fixtures in ActiveRecord
909 ActiveRecord::Fixtures.instantiate_all_loaded_fixtures(self, load_instances?)
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
910 else
911 raise RuntimeError, 'Load fixtures before instantiating them.' if @loaded_fixtures.nil?
98105554 » alexeymuranov
2012-05-09 Remove deprecated use of ::instantiate_fixtures
912 @loaded_fixtures.each_value do |fixture_set|
913 ActiveRecord::Fixtures.instantiate_fixtures(self, fixture_set, load_instances?)
903ef71b » dhh
2005-03-06 Added transactional fixtures that uses rollback to undo changes to fi…
914 end
f389a8fb » dhh
2004-12-16 Updated and added more documentation
915 end
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
916 end
03097d3a » dhh
2005-04-18 Fixed that fixtures were being deleted in the same order as inserts c…
917
b0ee1bdf » jeremy
2008-11-07 Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures in…
918 def load_instances?
919 use_instantiated_fixtures != :no_instances
920 end
f389a8fb » dhh
2004-12-16 Updated and added more documentation
921 end
daf3e92a » dhh
2005-01-01 Added a db2 adapter that only depends on the Ruby/DB2 bindings (http:…
922 end
Something went wrong with that request. Please try again.