activerecord 3.1.0 → 3.1.1.rc1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- data/CHANGELOG +36 -1
- data/lib/active_record.rb +0 -7
- data/lib/active_record/associations/alias_tracker.rb +12 -6
- data/lib/active_record/associations/belongs_to_association.rb +4 -0
- data/lib/active_record/associations/collection_association.rb +7 -3
- data/lib/active_record/associations/collection_proxy.rb +1 -1
- data/lib/active_record/associations/through_association.rb +1 -1
- data/lib/active_record/base.rb +2 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +1 -1
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +1 -1
- data/lib/active_record/connection_adapters/mysql_adapter.rb +36 -4
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +47 -6
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +57 -5
- data/lib/active_record/connection_adapters/statement_pool.rb +40 -0
- data/lib/active_record/fixtures.rb +370 -374
- data/lib/active_record/migration/command_recorder.rb +1 -1
- data/lib/active_record/railties/databases.rake +7 -5
- data/lib/active_record/reflection.rb +1 -1
- data/lib/active_record/relation/batches.rb +1 -1
- data/lib/active_record/relation/calculations.rb +1 -0
- data/lib/active_record/relation/finder_methods.rb +9 -3
- data/lib/active_record/relation/query_methods.rb +2 -7
- data/lib/active_record/version.rb +2 -2
- metadata +24 -16
@@ -25,378 +25,368 @@ end
|
|
25
25
|
|
26
26
|
class FixturesFileNotFound < StandardError; end
|
27
27
|
|
28
|
-
# Fixtures are a way of organizing data that you want to test against; in short, sample data.
|
29
|
-
#
|
30
|
-
# = Fixture formats
|
31
|
-
#
|
32
|
-
# Fixtures come in 1 flavor:
|
33
|
-
#
|
34
|
-
# 1. YAML fixtures
|
35
|
-
#
|
36
|
-
# == YAML fixtures
|
37
|
-
#
|
38
|
-
# This type of fixture is in YAML format and the preferred default. YAML is a file format which describes data structures
|
39
|
-
# in a non-verbose, human-readable format. It ships with Ruby 1.8.1+.
|
40
|
-
#
|
41
|
-
# Unlike single-file fixtures, YAML fixtures are stored in a single file per model, which are placed
|
42
|
-
# in the directory appointed by <tt>ActiveSupport::TestCase.fixture_path=(path)</tt> (this is
|
43
|
-
# automatically configured for Rails, so you can just put your files in <tt><your-rails-app>/test/fixtures/</tt>).
|
44
|
-
# The fixture file ends with the <tt>.yml</tt> file extension (Rails example:
|
45
|
-
# <tt><your-rails-app>/test/fixtures/web_sites.yml</tt>). The format of a YAML fixture file looks like this:
|
46
|
-
#
|
47
|
-
# rubyonrails:
|
48
|
-
# id: 1
|
49
|
-
# name: Ruby on Rails
|
50
|
-
# url: http://www.rubyonrails.org
|
51
|
-
#
|
52
|
-
# google:
|
53
|
-
# id: 2
|
54
|
-
# name: Google
|
55
|
-
# url: http://www.google.com
|
56
|
-
#
|
57
|
-
# This YAML fixture file includes two fixtures. Each YAML fixture (ie. record) is given a name and is followed by an
|
58
|
-
# indented list of key/value pairs in the "key: value" format. Records are separated by a blank line for your viewing
|
59
|
-
# pleasure.
|
60
|
-
#
|
61
|
-
# Note that YAML fixtures are unordered. If you want ordered fixtures, use the omap YAML type.
|
62
|
-
# See http://yaml.org/type/omap.html
|
63
|
-
# for the specification. You will need ordered fixtures when you have foreign key constraints on keys in the same table.
|
64
|
-
# This is commonly needed for tree structures. Example:
|
65
|
-
#
|
66
|
-
# --- !omap
|
67
|
-
# - parent:
|
68
|
-
# id: 1
|
69
|
-
# parent_id: NULL
|
70
|
-
# title: Parent
|
71
|
-
# - child:
|
72
|
-
# id: 2
|
73
|
-
# parent_id: 1
|
74
|
-
# title: Child
|
75
|
-
#
|
76
|
-
# = Using fixtures in testcases
|
77
|
-
#
|
78
|
-
# Since fixtures are a testing construct, we use them in our unit and functional tests. There are two ways to use the
|
79
|
-
# fixtures, but first let's take a look at a sample unit test:
|
80
|
-
#
|
81
|
-
# require 'test_helper'
|
82
|
-
#
|
83
|
-
# class WebSiteTest < ActiveSupport::TestCase
|
84
|
-
# test "web_site_count" do
|
85
|
-
# assert_equal 2, WebSite.count
|
86
|
-
# end
|
87
|
-
# end
|
88
|
-
#
|
89
|
-
# By default, the <tt>test_helper module</tt> will load all of your fixtures into your test database,
|
90
|
-
# so this test will succeed.
|
91
|
-
# The testing environment will automatically load the all fixtures into the database before each test.
|
92
|
-
# To ensure consistent data, the environment deletes the fixtures before running the load.
|
93
|
-
#
|
94
|
-
# In addition to being available in the database, the fixture's data may also be accessed by
|
95
|
-
# using a special dynamic method, which has the same name as the model, and accepts the
|
96
|
-
# name of the fixture to instantiate:
|
97
|
-
#
|
98
|
-
# test "find" do
|
99
|
-
# assert_equal "Ruby on Rails", web_sites(:rubyonrails).name
|
100
|
-
# end
|
101
|
-
#
|
102
|
-
# Alternatively, you may enable auto-instantiation of the fixture data. For instance, take the following tests:
|
103
|
-
#
|
104
|
-
# test "find_alt_method_1" do
|
105
|
-
# assert_equal "Ruby on Rails", @web_sites['rubyonrails']['name']
|
106
|
-
# end
|
107
|
-
#
|
108
|
-
# test "find_alt_method_2" do
|
109
|
-
# assert_equal "Ruby on Rails", @rubyonrails.news
|
110
|
-
# end
|
111
|
-
#
|
112
|
-
# In order to use these methods to access fixtured data within your testcases, you must specify one of the
|
113
|
-
# following in your <tt>ActiveSupport::TestCase</tt>-derived class:
|
114
|
-
#
|
115
|
-
# - to fully enable instantiated fixtures (enable alternate methods #1 and #2 above)
|
116
|
-
# self.use_instantiated_fixtures = true
|
117
|
-
#
|
118
|
-
# - create only the hash for the fixtures, do not 'find' each instance (enable alternate method #1 only)
|
119
|
-
# self.use_instantiated_fixtures = :no_instances
|
120
|
-
#
|
121
|
-
# Using either of these alternate methods incurs a performance hit, as the fixtured data must be fully
|
122
|
-
# traversed in the database to create the fixture hash and/or instance variables. This is expensive for
|
123
|
-
# large sets of fixtured data.
|
124
|
-
#
|
125
|
-
# = Dynamic fixtures with ERB
|
126
|
-
#
|
127
|
-
# Some times you don't care about the content of the fixtures as much as you care about the volume. In these cases, you can
|
128
|
-
# mix ERB in with your YAML fixtures to create a bunch of fixtures for load testing, like:
|
129
|
-
#
|
130
|
-
# <% for i in 1..1000 %>
|
131
|
-
# fix_<%= i %>:
|
132
|
-
# id: <%= i %>
|
133
|
-
# name: guy_<%= 1 %>
|
134
|
-
# <% end %>
|
135
|
-
#
|
136
|
-
# This will create 1000 very simple YAML fixtures.
|
137
|
-
#
|
138
|
-
# Using ERB, you can also inject dynamic values into your fixtures with inserts like <tt><%= Date.today.strftime("%Y-%m-%d") %></tt>.
|
139
|
-
# This is however a feature to be used with some caution. The point of fixtures are that they're
|
140
|
-
# stable units of predictable sample data. If you feel that you need to inject dynamic values, then
|
141
|
-
# perhaps you should reexamine whether your application is properly testable. Hence, dynamic values
|
142
|
-
# in fixtures are to be considered a code smell.
|
143
|
-
#
|
144
|
-
# = Transactional fixtures
|
145
|
-
#
|
146
|
-
# TestCases can use begin+rollback to isolate their changes to the database instead of having to
|
147
|
-
# delete+insert for every test case.
|
148
|
-
#
|
149
|
-
# class FooTest < ActiveSupport::TestCase
|
150
|
-
# self.use_transactional_fixtures = true
|
151
|
-
#
|
152
|
-
# test "godzilla" do
|
153
|
-
# assert !Foo.find(:all).empty?
|
154
|
-
# Foo.destroy_all
|
155
|
-
# assert Foo.find(:all).empty?
|
156
|
-
# end
|
157
|
-
#
|
158
|
-
# test "godzilla aftermath" do
|
159
|
-
# assert !Foo.find(:all).empty?
|
160
|
-
# end
|
161
|
-
# end
|
162
|
-
#
|
163
|
-
# If you preload your test database with all fixture data (probably in the Rakefile task) and use transactional fixtures,
|
164
|
-
# then you may omit all fixtures declarations in your test cases since all the data's already there
|
165
|
-
# and every case rolls back its changes.
|
166
|
-
#
|
167
|
-
# In order to use instantiated fixtures with preloaded data, set +self.pre_loaded_fixtures+ to true. This will provide
|
168
|
-
# access to fixture data for every table that has been loaded through fixtures (depending on the
|
169
|
-
# value of +use_instantiated_fixtures+)
|
170
|
-
#
|
171
|
-
# When *not* to use transactional fixtures:
|
172
|
-
#
|
173
|
-
# 1. You're testing whether a transaction works correctly. Nested transactions don't commit until
|
174
|
-
# all parent transactions commit, particularly, the fixtures transaction which is begun in setup
|
175
|
-
# and rolled back in teardown. Thus, you won't be able to verify
|
176
|
-
# the results of your transaction until Active Record supports nested transactions or savepoints (in progress).
|
177
|
-
# 2. Your database does not support transactions. Every Active Record database supports transactions except MySQL MyISAM.
|
178
|
-
# Use InnoDB, MaxDB, or NDB instead.
|
179
|
-
#
|
180
|
-
# = Advanced YAML Fixtures
|
181
|
-
#
|
182
|
-
# YAML fixtures that don't specify an ID get some extra features:
|
183
|
-
#
|
184
|
-
# * Stable, autogenerated IDs
|
185
|
-
# * Label references for associations (belongs_to, has_one, has_many)
|
186
|
-
# * HABTM associations as inline lists
|
187
|
-
# * Autofilled timestamp columns
|
188
|
-
# * Fixture label interpolation
|
189
|
-
# * Support for YAML defaults
|
190
|
-
#
|
191
|
-
# == Stable, autogenerated IDs
|
192
|
-
#
|
193
|
-
# Here, have a monkey fixture:
|
194
|
-
#
|
195
|
-
# george:
|
196
|
-
# id: 1
|
197
|
-
# name: George the Monkey
|
198
|
-
#
|
199
|
-
# reginald:
|
200
|
-
# id: 2
|
201
|
-
# name: Reginald the Pirate
|
202
|
-
#
|
203
|
-
# Each of these fixtures has two unique identifiers: one for the database
|
204
|
-
# and one for the humans. Why don't we generate the primary key instead?
|
205
|
-
# Hashing each fixture's label yields a consistent ID:
|
206
|
-
#
|
207
|
-
# george: # generated id: 503576764
|
208
|
-
# name: George the Monkey
|
209
|
-
#
|
210
|
-
# reginald: # generated id: 324201669
|
211
|
-
# name: Reginald the Pirate
|
212
|
-
#
|
213
|
-
# Active Record looks at the fixture's model class, discovers the correct
|
214
|
-
# primary key, and generates it right before inserting the fixture
|
215
|
-
# into the database.
|
216
|
-
#
|
217
|
-
# The generated ID for a given label is constant, so we can discover
|
218
|
-
# any fixture's ID without loading anything, as long as we know the label.
|
219
|
-
#
|
220
|
-
# == Label references for associations (belongs_to, has_one, has_many)
|
221
|
-
#
|
222
|
-
# Specifying foreign keys in fixtures can be very fragile, not to
|
223
|
-
# mention difficult to read. Since Active Record can figure out the ID of
|
224
|
-
# any fixture from its label, you can specify FK's by label instead of ID.
|
225
|
-
#
|
226
|
-
# === belongs_to
|
227
|
-
#
|
228
|
-
# Let's break out some more monkeys and pirates.
|
229
|
-
#
|
230
|
-
# ### in pirates.yml
|
231
|
-
#
|
232
|
-
# reginald:
|
233
|
-
# id: 1
|
234
|
-
# name: Reginald the Pirate
|
235
|
-
# monkey_id: 1
|
236
|
-
#
|
237
|
-
# ### in monkeys.yml
|
238
|
-
#
|
239
|
-
# george:
|
240
|
-
# id: 1
|
241
|
-
# name: George the Monkey
|
242
|
-
# pirate_id: 1
|
243
|
-
#
|
244
|
-
# Add a few more monkeys and pirates and break this into multiple files,
|
245
|
-
# and it gets pretty hard to keep track of what's going on. Let's
|
246
|
-
# use labels instead of IDs:
|
247
|
-
#
|
248
|
-
# ### in pirates.yml
|
249
|
-
#
|
250
|
-
# reginald:
|
251
|
-
# name: Reginald the Pirate
|
252
|
-
# monkey: george
|
253
|
-
#
|
254
|
-
# ### in monkeys.yml
|
255
|
-
#
|
256
|
-
# george:
|
257
|
-
# name: George the Monkey
|
258
|
-
# pirate: reginald
|
259
|
-
#
|
260
|
-
# Pow! All is made clear. Active Record reflects on the fixture's model class,
|
261
|
-
# finds all the +belongs_to+ associations, and allows you to specify
|
262
|
-
# a target *label* for the *association* (monkey: george) rather than
|
263
|
-
# a target *id* for the *FK* (<tt>monkey_id: 1</tt>).
|
264
|
-
#
|
265
|
-
# ==== Polymorphic belongs_to
|
266
|
-
#
|
267
|
-
# Supporting polymorphic relationships is a little bit more complicated, since
|
268
|
-
# Active Record needs to know what type your association is pointing at. Something
|
269
|
-
# like this should look familiar:
|
270
|
-
#
|
271
|
-
# ### in fruit.rb
|
272
|
-
#
|
273
|
-
# belongs_to :eater, :polymorphic => true
|
274
|
-
#
|
275
|
-
# ### in fruits.yml
|
276
|
-
#
|
277
|
-
# apple:
|
278
|
-
# id: 1
|
279
|
-
# name: apple
|
280
|
-
# eater_id: 1
|
281
|
-
# eater_type: Monkey
|
282
|
-
#
|
283
|
-
# Can we do better? You bet!
|
284
|
-
#
|
285
|
-
# apple:
|
286
|
-
# eater: george (Monkey)
|
287
|
-
#
|
288
|
-
# Just provide the polymorphic target type and Active Record will take care of the rest.
|
289
|
-
#
|
290
|
-
# === has_and_belongs_to_many
|
291
|
-
#
|
292
|
-
# Time to give our monkey some fruit.
|
293
|
-
#
|
294
|
-
# ### in monkeys.yml
|
295
|
-
#
|
296
|
-
# george:
|
297
|
-
# id: 1
|
298
|
-
# name: George the Monkey
|
299
|
-
#
|
300
|
-
# ### in fruits.yml
|
301
|
-
#
|
302
|
-
# apple:
|
303
|
-
# id: 1
|
304
|
-
# name: apple
|
305
|
-
#
|
306
|
-
# orange:
|
307
|
-
# id: 2
|
308
|
-
# name: orange
|
309
|
-
#
|
310
|
-
# grape:
|
311
|
-
# id: 3
|
312
|
-
# name: grape
|
313
|
-
#
|
314
|
-
# ### in fruits_monkeys.yml
|
315
|
-
#
|
316
|
-
# apple_george:
|
317
|
-
# fruit_id: 1
|
318
|
-
# monkey_id: 1
|
319
|
-
#
|
320
|
-
# orange_george:
|
321
|
-
# fruit_id: 2
|
322
|
-
# monkey_id: 1
|
323
|
-
#
|
324
|
-
# grape_george:
|
325
|
-
# fruit_id: 3
|
326
|
-
# monkey_id: 1
|
327
|
-
#
|
328
|
-
# Let's make the HABTM fixture go away.
|
329
|
-
#
|
330
|
-
# ### in monkeys.yml
|
331
|
-
#
|
332
|
-
# george:
|
333
|
-
# id: 1
|
334
|
-
# name: George the Monkey
|
335
|
-
# fruits: apple, orange, grape
|
336
|
-
#
|
337
|
-
# ### in fruits.yml
|
338
|
-
#
|
339
|
-
# apple:
|
340
|
-
# name: apple
|
341
|
-
#
|
342
|
-
# orange:
|
343
|
-
# name: orange
|
344
|
-
#
|
345
|
-
# grape:
|
346
|
-
# name: grape
|
347
|
-
#
|
348
|
-
# Zap! No more fruits_monkeys.yml file. We've specified the list of fruits
|
349
|
-
# on George's fixture, but we could've just as easily specified a list
|
350
|
-
# of monkeys on each fruit. As with +belongs_to+, Active Record reflects on
|
351
|
-
# the fixture's model class and discovers the +has_and_belongs_to_many+
|
352
|
-
# associations.
|
353
|
-
#
|
354
|
-
# == Autofilled timestamp columns
|
355
|
-
#
|
356
|
-
# If your table/model specifies any of Active Record's
|
357
|
-
# standard timestamp columns (+created_at+, +created_on+, +updated_at+, +updated_on+),
|
358
|
-
# they will automatically be set to <tt>Time.now</tt>.
|
359
|
-
#
|
360
|
-
# If you've set specific values, they'll be left alone.
|
361
|
-
#
|
362
|
-
# == Fixture label interpolation
|
363
|
-
#
|
364
|
-
# The label of the current fixture is always available as a column value:
|
365
|
-
#
|
366
|
-
# geeksomnia:
|
367
|
-
# name: Geeksomnia's Account
|
368
|
-
# subdomain: $LABEL
|
369
|
-
#
|
370
|
-
# Also, sometimes (like when porting older join table fixtures) you'll need
|
371
|
-
# to be able to get a hold of the identifier for a given label. ERB
|
372
|
-
# to the rescue:
|
373
|
-
#
|
374
|
-
# george_reginald:
|
375
|
-
# monkey_id: <%= ActiveRecord::Fixtures.identify(:reginald) %>
|
376
|
-
# pirate_id: <%= ActiveRecord::Fixtures.identify(:george) %>
|
377
|
-
#
|
378
|
-
# == Support for YAML defaults
|
379
|
-
#
|
380
|
-
# You probably already know how to use YAML to set and reuse defaults in
|
381
|
-
# your <tt>database.yml</tt> file. You can use the same technique in your fixtures:
|
382
|
-
#
|
383
|
-
# DEFAULTS: &DEFAULTS
|
384
|
-
# created_on: <%= 3.weeks.ago.to_s(:db) %>
|
385
|
-
#
|
386
|
-
# first:
|
387
|
-
# name: Smurf
|
388
|
-
# <<: *DEFAULTS
|
389
|
-
#
|
390
|
-
# second:
|
391
|
-
# name: Fraggle
|
392
|
-
# <<: *DEFAULTS
|
393
|
-
#
|
394
|
-
# Any fixture labeled "DEFAULTS" is safely ignored.
|
395
|
-
|
396
|
-
Fixture = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Fixture', 'ActiveRecord::Fixture')
|
397
|
-
Fixtures = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Fixtures', 'ActiveRecord::Fixtures')
|
398
|
-
|
399
28
|
module ActiveRecord
|
29
|
+
# \Fixtures are a way of organizing data that you want to test against; in short, sample data.
|
30
|
+
#
|
31
|
+
# They are stored in YAML files, one file per model, which are placed in the directory
|
32
|
+
# appointed by <tt>ActiveSupport::TestCase.fixture_path=(path)</tt> (this is automatically
|
33
|
+
# configured for Rails, so you can just put your files in <tt><your-rails-app>/test/fixtures/</tt>).
|
34
|
+
# The fixture file ends with the <tt>.yml</tt> file extension (Rails example:
|
35
|
+
# <tt><your-rails-app>/test/fixtures/web_sites.yml</tt>). The format of a fixture file looks
|
36
|
+
# like this:
|
37
|
+
#
|
38
|
+
# rubyonrails:
|
39
|
+
# id: 1
|
40
|
+
# name: Ruby on Rails
|
41
|
+
# url: http://www.rubyonrails.org
|
42
|
+
#
|
43
|
+
# google:
|
44
|
+
# id: 2
|
45
|
+
# name: Google
|
46
|
+
# url: http://www.google.com
|
47
|
+
#
|
48
|
+
# This fixture file includes two fixtures. Each YAML fixture (ie. record) is given a name and
|
49
|
+
# is followed by an indented list of key/value pairs in the "key: value" format. Records are
|
50
|
+
# separated by a blank line for your viewing pleasure.
|
51
|
+
#
|
52
|
+
# Note that fixtures are unordered. If you want ordered fixtures, use the omap YAML type.
|
53
|
+
# See http://yaml.org/type/omap.html
|
54
|
+
# for the specification. You will need ordered fixtures when you have foreign key constraints
|
55
|
+
# on keys in the same table. This is commonly needed for tree structures. Example:
|
56
|
+
#
|
57
|
+
# --- !omap
|
58
|
+
# - parent:
|
59
|
+
# id: 1
|
60
|
+
# parent_id: NULL
|
61
|
+
# title: Parent
|
62
|
+
# - child:
|
63
|
+
# id: 2
|
64
|
+
# parent_id: 1
|
65
|
+
# title: Child
|
66
|
+
#
|
67
|
+
# = Using Fixtures in Test Cases
|
68
|
+
#
|
69
|
+
# Since fixtures are a testing construct, we use them in our unit and functional tests. There
|
70
|
+
# are two ways to use the fixtures, but first let's take a look at a sample unit test:
|
71
|
+
#
|
72
|
+
# require 'test_helper'
|
73
|
+
#
|
74
|
+
# class WebSiteTest < ActiveSupport::TestCase
|
75
|
+
# test "web_site_count" do
|
76
|
+
# assert_equal 2, WebSite.count
|
77
|
+
# end
|
78
|
+
# end
|
79
|
+
#
|
80
|
+
# By default, <tt>test_helper.rb</tt> will load all of your fixtures into your test database,
|
81
|
+
# so this test will succeed.
|
82
|
+
#
|
83
|
+
# The testing environment will automatically load the all fixtures into the database before each
|
84
|
+
# test. To ensure consistent data, the environment deletes the fixtures before running the load.
|
85
|
+
#
|
86
|
+
# In addition to being available in the database, the fixture's data may also be accessed by
|
87
|
+
# using a special dynamic method, which has the same name as the model, and accepts the
|
88
|
+
# name of the fixture to instantiate:
|
89
|
+
#
|
90
|
+
# test "find" do
|
91
|
+
# assert_equal "Ruby on Rails", web_sites(:rubyonrails).name
|
92
|
+
# end
|
93
|
+
#
|
94
|
+
# Alternatively, you may enable auto-instantiation of the fixture data. For instance, take the
|
95
|
+
# following tests:
|
96
|
+
#
|
97
|
+
# test "find_alt_method_1" do
|
98
|
+
# assert_equal "Ruby on Rails", @web_sites['rubyonrails']['name']
|
99
|
+
# end
|
100
|
+
#
|
101
|
+
# test "find_alt_method_2" do
|
102
|
+
# assert_equal "Ruby on Rails", @rubyonrails.news
|
103
|
+
# end
|
104
|
+
#
|
105
|
+
# In order to use these methods to access fixtured data within your testcases, you must specify one of the
|
106
|
+
# following in your <tt>ActiveSupport::TestCase</tt>-derived class:
|
107
|
+
#
|
108
|
+
# - to fully enable instantiated fixtures (enable alternate methods #1 and #2 above)
|
109
|
+
# self.use_instantiated_fixtures = true
|
110
|
+
#
|
111
|
+
# - create only the hash for the fixtures, do not 'find' each instance (enable alternate method #1 only)
|
112
|
+
# self.use_instantiated_fixtures = :no_instances
|
113
|
+
#
|
114
|
+
# Using either of these alternate methods incurs a performance hit, as the fixtured data must be fully
|
115
|
+
# traversed in the database to create the fixture hash and/or instance variables. This is expensive for
|
116
|
+
# large sets of fixtured data.
|
117
|
+
#
|
118
|
+
# = Dynamic fixtures with ERB
|
119
|
+
#
|
120
|
+
# Some times you don't care about the content of the fixtures as much as you care about the volume.
|
121
|
+
# In these cases, you can mix ERB in with your YAML fixtures to create a bunch of fixtures for load
|
122
|
+
# testing, like:
|
123
|
+
#
|
124
|
+
# <% 1.upto(1000) do |i| %>
|
125
|
+
# fix_<%= i %>:
|
126
|
+
# id: <%= i %>
|
127
|
+
# name: guy_<%= 1 %>
|
128
|
+
# <% end %>
|
129
|
+
#
|
130
|
+
# This will create 1000 very simple fixtures.
|
131
|
+
#
|
132
|
+
# Using ERB, you can also inject dynamic values into your fixtures with inserts like
|
133
|
+
# <tt><%= Date.today.strftime("%Y-%m-%d") %></tt>.
|
134
|
+
# This is however a feature to be used with some caution. The point of fixtures are that they're
|
135
|
+
# stable units of predictable sample data. If you feel that you need to inject dynamic values, then
|
136
|
+
# perhaps you should reexamine whether your application is properly testable. Hence, dynamic values
|
137
|
+
# in fixtures are to be considered a code smell.
|
138
|
+
#
|
139
|
+
# = Transactional Fixtures
|
140
|
+
#
|
141
|
+
# Test cases can use begin+rollback to isolate their changes to the database instead of having to
|
142
|
+
# delete+insert for every test case.
|
143
|
+
#
|
144
|
+
# class FooTest < ActiveSupport::TestCase
|
145
|
+
# self.use_transactional_fixtures = true
|
146
|
+
#
|
147
|
+
# test "godzilla" do
|
148
|
+
# assert !Foo.all.empty?
|
149
|
+
# Foo.destroy_all
|
150
|
+
# assert Foo.all.empty?
|
151
|
+
# end
|
152
|
+
#
|
153
|
+
# test "godzilla aftermath" do
|
154
|
+
# assert !Foo.all.empty?
|
155
|
+
# end
|
156
|
+
# end
|
157
|
+
#
|
158
|
+
# If you preload your test database with all fixture data (probably in the rake task) and use
|
159
|
+
# transactional fixtures, then you may omit all fixtures declarations in your test cases since
|
160
|
+
# all the data's already there and every case rolls back its changes.
|
161
|
+
#
|
162
|
+
# In order to use instantiated fixtures with preloaded data, set +self.pre_loaded_fixtures+ to
|
163
|
+
# true. This will provide access to fixture data for every table that has been loaded through
|
164
|
+
# fixtures (depending on the value of +use_instantiated_fixtures+).
|
165
|
+
#
|
166
|
+
# When *not* to use transactional fixtures:
|
167
|
+
#
|
168
|
+
# 1. You're testing whether a transaction works correctly. Nested transactions don't commit until
|
169
|
+
# all parent transactions commit, particularly, the fixtures transaction which is begun in setup
|
170
|
+
# and rolled back in teardown. Thus, you won't be able to verify
|
171
|
+
# the results of your transaction until Active Record supports nested transactions or savepoints (in progress).
|
172
|
+
# 2. Your database does not support transactions. Every Active Record database supports transactions except MySQL MyISAM.
|
173
|
+
# Use InnoDB, MaxDB, or NDB instead.
|
174
|
+
#
|
175
|
+
# = Advanced Fixtures
|
176
|
+
#
|
177
|
+
# Fixtures that don't specify an ID get some extra features:
|
178
|
+
#
|
179
|
+
# * Stable, autogenerated IDs
|
180
|
+
# * Label references for associations (belongs_to, has_one, has_many)
|
181
|
+
# * HABTM associations as inline lists
|
182
|
+
# * Autofilled timestamp columns
|
183
|
+
# * Fixture label interpolation
|
184
|
+
# * Support for YAML defaults
|
185
|
+
#
|
186
|
+
# == Stable, Autogenerated IDs
|
187
|
+
#
|
188
|
+
# Here, have a monkey fixture:
|
189
|
+
#
|
190
|
+
# george:
|
191
|
+
# id: 1
|
192
|
+
# name: George the Monkey
|
193
|
+
#
|
194
|
+
# reginald:
|
195
|
+
# id: 2
|
196
|
+
# name: Reginald the Pirate
|
197
|
+
#
|
198
|
+
# Each of these fixtures has two unique identifiers: one for the database
|
199
|
+
# and one for the humans. Why don't we generate the primary key instead?
|
200
|
+
# Hashing each fixture's label yields a consistent ID:
|
201
|
+
#
|
202
|
+
# george: # generated id: 503576764
|
203
|
+
# name: George the Monkey
|
204
|
+
#
|
205
|
+
# reginald: # generated id: 324201669
|
206
|
+
# name: Reginald the Pirate
|
207
|
+
#
|
208
|
+
# Active Record looks at the fixture's model class, discovers the correct
|
209
|
+
# primary key, and generates it right before inserting the fixture
|
210
|
+
# into the database.
|
211
|
+
#
|
212
|
+
# The generated ID for a given label is constant, so we can discover
|
213
|
+
# any fixture's ID without loading anything, as long as we know the label.
|
214
|
+
#
|
215
|
+
# == Label references for associations (belongs_to, has_one, has_many)
|
216
|
+
#
|
217
|
+
# Specifying foreign keys in fixtures can be very fragile, not to
|
218
|
+
# mention difficult to read. Since Active Record can figure out the ID of
|
219
|
+
# any fixture from its label, you can specify FK's by label instead of ID.
|
220
|
+
#
|
221
|
+
# === belongs_to
|
222
|
+
#
|
223
|
+
# Let's break out some more monkeys and pirates.
|
224
|
+
#
|
225
|
+
# ### in pirates.yml
|
226
|
+
#
|
227
|
+
# reginald:
|
228
|
+
# id: 1
|
229
|
+
# name: Reginald the Pirate
|
230
|
+
# monkey_id: 1
|
231
|
+
#
|
232
|
+
# ### in monkeys.yml
|
233
|
+
#
|
234
|
+
# george:
|
235
|
+
# id: 1
|
236
|
+
# name: George the Monkey
|
237
|
+
# pirate_id: 1
|
238
|
+
#
|
239
|
+
# Add a few more monkeys and pirates and break this into multiple files,
|
240
|
+
# and it gets pretty hard to keep track of what's going on. Let's
|
241
|
+
# use labels instead of IDs:
|
242
|
+
#
|
243
|
+
# ### in pirates.yml
|
244
|
+
#
|
245
|
+
# reginald:
|
246
|
+
# name: Reginald the Pirate
|
247
|
+
# monkey: george
|
248
|
+
#
|
249
|
+
# ### in monkeys.yml
|
250
|
+
#
|
251
|
+
# george:
|
252
|
+
# name: George the Monkey
|
253
|
+
# pirate: reginald
|
254
|
+
#
|
255
|
+
# Pow! All is made clear. Active Record reflects on the fixture's model class,
|
256
|
+
# finds all the +belongs_to+ associations, and allows you to specify
|
257
|
+
# a target *label* for the *association* (monkey: george) rather than
|
258
|
+
# a target *id* for the *FK* (<tt>monkey_id: 1</tt>).
|
259
|
+
#
|
260
|
+
# ==== Polymorphic belongs_to
|
261
|
+
#
|
262
|
+
# Supporting polymorphic relationships is a little bit more complicated, since
|
263
|
+
# Active Record needs to know what type your association is pointing at. Something
|
264
|
+
# like this should look familiar:
|
265
|
+
#
|
266
|
+
# ### in fruit.rb
|
267
|
+
#
|
268
|
+
# belongs_to :eater, :polymorphic => true
|
269
|
+
#
|
270
|
+
# ### in fruits.yml
|
271
|
+
#
|
272
|
+
# apple:
|
273
|
+
# id: 1
|
274
|
+
# name: apple
|
275
|
+
# eater_id: 1
|
276
|
+
# eater_type: Monkey
|
277
|
+
#
|
278
|
+
# Can we do better? You bet!
|
279
|
+
#
|
280
|
+
# apple:
|
281
|
+
# eater: george (Monkey)
|
282
|
+
#
|
283
|
+
# Just provide the polymorphic target type and Active Record will take care of the rest.
|
284
|
+
#
|
285
|
+
# === has_and_belongs_to_many
|
286
|
+
#
|
287
|
+
# Time to give our monkey some fruit.
|
288
|
+
#
|
289
|
+
# ### in monkeys.yml
|
290
|
+
#
|
291
|
+
# george:
|
292
|
+
# id: 1
|
293
|
+
# name: George the Monkey
|
294
|
+
#
|
295
|
+
# ### in fruits.yml
|
296
|
+
#
|
297
|
+
# apple:
|
298
|
+
# id: 1
|
299
|
+
# name: apple
|
300
|
+
#
|
301
|
+
# orange:
|
302
|
+
# id: 2
|
303
|
+
# name: orange
|
304
|
+
#
|
305
|
+
# grape:
|
306
|
+
# id: 3
|
307
|
+
# name: grape
|
308
|
+
#
|
309
|
+
# ### in fruits_monkeys.yml
|
310
|
+
#
|
311
|
+
# apple_george:
|
312
|
+
# fruit_id: 1
|
313
|
+
# monkey_id: 1
|
314
|
+
#
|
315
|
+
# orange_george:
|
316
|
+
# fruit_id: 2
|
317
|
+
# monkey_id: 1
|
318
|
+
#
|
319
|
+
# grape_george:
|
320
|
+
# fruit_id: 3
|
321
|
+
# monkey_id: 1
|
322
|
+
#
|
323
|
+
# Let's make the HABTM fixture go away.
|
324
|
+
#
|
325
|
+
# ### in monkeys.yml
|
326
|
+
#
|
327
|
+
# george:
|
328
|
+
# id: 1
|
329
|
+
# name: George the Monkey
|
330
|
+
# fruits: apple, orange, grape
|
331
|
+
#
|
332
|
+
# ### in fruits.yml
|
333
|
+
#
|
334
|
+
# apple:
|
335
|
+
# name: apple
|
336
|
+
#
|
337
|
+
# orange:
|
338
|
+
# name: orange
|
339
|
+
#
|
340
|
+
# grape:
|
341
|
+
# name: grape
|
342
|
+
#
|
343
|
+
# Zap! No more fruits_monkeys.yml file. We've specified the list of fruits
|
344
|
+
# on George's fixture, but we could've just as easily specified a list
|
345
|
+
# of monkeys on each fruit. As with +belongs_to+, Active Record reflects on
|
346
|
+
# the fixture's model class and discovers the +has_and_belongs_to_many+
|
347
|
+
# associations.
|
348
|
+
#
|
349
|
+
# == Autofilled Timestamp Columns
|
350
|
+
#
|
351
|
+
# If your table/model specifies any of Active Record's
|
352
|
+
# standard timestamp columns (+created_at+, +created_on+, +updated_at+, +updated_on+),
|
353
|
+
# they will automatically be set to <tt>Time.now</tt>.
|
354
|
+
#
|
355
|
+
# If you've set specific values, they'll be left alone.
|
356
|
+
#
|
357
|
+
# == Fixture label interpolation
|
358
|
+
#
|
359
|
+
# The label of the current fixture is always available as a column value:
|
360
|
+
#
|
361
|
+
# geeksomnia:
|
362
|
+
# name: Geeksomnia's Account
|
363
|
+
# subdomain: $LABEL
|
364
|
+
#
|
365
|
+
# Also, sometimes (like when porting older join table fixtures) you'll need
|
366
|
+
# to be able to get a hold of the identifier for a given label. ERB
|
367
|
+
# to the rescue:
|
368
|
+
#
|
369
|
+
# george_reginald:
|
370
|
+
# monkey_id: <%= ActiveRecord::Fixtures.identify(:reginald) %>
|
371
|
+
# pirate_id: <%= ActiveRecord::Fixtures.identify(:george) %>
|
372
|
+
#
|
373
|
+
# == Support for YAML defaults
|
374
|
+
#
|
375
|
+
# You probably already know how to use YAML to set and reuse defaults in
|
376
|
+
# your <tt>database.yml</tt> file. You can use the same technique in your fixtures:
|
377
|
+
#
|
378
|
+
# DEFAULTS: &DEFAULTS
|
379
|
+
# created_on: <%= 3.weeks.ago.to_s(:db) %>
|
380
|
+
#
|
381
|
+
# first:
|
382
|
+
# name: Smurf
|
383
|
+
# *DEFAULTS
|
384
|
+
#
|
385
|
+
# second:
|
386
|
+
# name: Fraggle
|
387
|
+
# *DEFAULTS
|
388
|
+
#
|
389
|
+
# Any fixture labeled "DEFAULTS" is safely ignored.
|
400
390
|
class Fixtures
|
401
391
|
MAX_ID = 2 ** 30 - 1
|
402
392
|
|
@@ -558,7 +548,7 @@ module ActiveRecord
|
|
558
548
|
fixtures.size
|
559
549
|
end
|
560
550
|
|
561
|
-
# Return a hash of rows to be inserted.
|
551
|
+
# Return a hash of rows to be inserted. The key is the table, the value is
|
562
552
|
# a list of rows to insert to that table.
|
563
553
|
def table_rows
|
564
554
|
now = ActiveRecord::Base.default_timezone == :utc ? Time.now.utc : Time.now
|
@@ -715,10 +705,16 @@ module ActiveRecord
|
|
715
705
|
File.basename(@fixture_path).split(".").first
|
716
706
|
end
|
717
707
|
|
708
|
+
RESCUE_ERRORS = [ ArgumentError ]
|
709
|
+
|
710
|
+
if defined?(Psych) && defined?(Psych::SyntaxError)
|
711
|
+
RESCUE_ERRORS << Psych::SyntaxError
|
712
|
+
end
|
713
|
+
|
718
714
|
def parse_yaml_string(fixture_content)
|
719
715
|
YAML::load(erb_render(fixture_content))
|
720
|
-
rescue => error
|
721
|
-
raise Fixture::FormatError, "a YAML error occurred parsing #{yaml_file_path}. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.html\nThe exact error was:\n #{error.class}: #{error}"
|
716
|
+
rescue *RESCUE_ERRORS => error
|
717
|
+
raise Fixture::FormatError, "a YAML error occurred parsing #{yaml_file_path}. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.html\nThe exact error was:\n #{error.class}: #{error}", error.backtrace
|
722
718
|
end
|
723
719
|
|
724
720
|
def erb_render(fixture_content)
|