Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / activerecord / test / fixtures_test.rb
blob6f11a337b1ef3a32088daec827e260f64d534cc4
1 require 'abstract_unit'
2 require 'fixtures/topic'
3 require 'fixtures/developer'
4 require 'fixtures/company'
5 require 'fixtures/task'
6 require 'fixtures/reply'
7 require 'fixtures/joke'
8 require 'fixtures/course'
9 require 'fixtures/category'
10 require 'fixtures/parrot'
11 require 'fixtures/pirate'
12 require 'fixtures/treasure'
14 class FixturesTest < Test::Unit::TestCase
15   self.use_instantiated_fixtures = true
16   self.use_transactional_fixtures = false
18   fixtures :topics, :developers, :accounts, :tasks, :categories, :funny_jokes, :binaries
20   FIXTURES = %w( accounts binaries companies customers
21                  developers developers_projects entrants
22                  movies projects subscribers topics tasks )
23   MATCH_ATTRIBUTE_NAME = /[a-zA-Z][-_\w]*/
25   BINARY_FIXTURE_PATH = File.dirname(__FILE__) + '/fixtures/flowers.jpg'
27   def test_clean_fixtures
28     FIXTURES.each do |name|
29       fixtures = nil
30       assert_nothing_raised { fixtures = create_fixtures(name) }
31       assert_kind_of(Fixtures, fixtures)
32       fixtures.each { |name, fixture|
33         fixture.each { |key, value|
34           assert_match(MATCH_ATTRIBUTE_NAME, key)
35         }
36       }
37     end
38   end
40   def test_multiple_clean_fixtures
41     fixtures_array = nil
42     assert_nothing_raised { fixtures_array = create_fixtures(*FIXTURES) }
43     assert_kind_of(Array, fixtures_array)
44     fixtures_array.each { |fixtures| assert_kind_of(Fixtures, fixtures) }
45   end
47   def test_attributes
48     topics = create_fixtures("topics")
49     assert_equal("The First Topic", topics["first"]["title"])
50     assert_nil(topics["second"]["author_email_address"])
51   end
53   def test_inserts
54     topics = create_fixtures("topics")
55     first_row = ActiveRecord::Base.connection.select_one("SELECT * FROM topics WHERE author_name = 'David'")
56     assert_equal("The First Topic", first_row["title"])
58     second_row = ActiveRecord::Base.connection.select_one("SELECT * FROM topics WHERE author_name = 'Mary'")
59     assert_nil(second_row["author_email_address"])
60   end
62   if ActiveRecord::Base.connection.supports_migrations?
63     def test_inserts_with_pre_and_suffix
64       # Reset cache to make finds on the new table work
65       Fixtures.reset_cache
67       ActiveRecord::Base.connection.create_table :prefix_topics_suffix do |t|
68         t.column :title, :string
69         t.column :author_name, :string
70         t.column :author_email_address, :string
71         t.column :written_on, :datetime
72         t.column :bonus_time, :time
73         t.column :last_read, :date
74         t.column :content, :string
75         t.column :approved, :boolean, :default => true
76         t.column :replies_count, :integer, :default => 0
77         t.column :parent_id, :integer
78         t.column :type, :string, :limit => 50
79       end
81       # Store existing prefix/suffix
82       old_prefix = ActiveRecord::Base.table_name_prefix
83       old_suffix = ActiveRecord::Base.table_name_suffix
85       # Set a prefix/suffix we can test against
86       ActiveRecord::Base.table_name_prefix = 'prefix_'
87       ActiveRecord::Base.table_name_suffix = '_suffix'
89       topics = create_fixtures("topics")
91       first_row = ActiveRecord::Base.connection.select_one("SELECT * FROM prefix_topics_suffix WHERE author_name = 'David'")
92       assert_equal("The First Topic", first_row["title"])
94       second_row = ActiveRecord::Base.connection.select_one("SELECT * FROM prefix_topics_suffix WHERE author_name = 'Mary'")
95       assert_nil(second_row["author_email_address"])
96     ensure
97       # Restore prefix/suffix to its previous values
98       ActiveRecord::Base.table_name_prefix = old_prefix
99       ActiveRecord::Base.table_name_suffix = old_suffix
101       ActiveRecord::Base.connection.drop_table :prefix_topics_suffix rescue nil
102     end
103   end
105   def test_insert_with_datetime
106     topics = create_fixtures("tasks")
107     first = Task.find(1)
108     assert first
109   end
111   def test_bad_format
112     path = File.join(File.dirname(__FILE__), 'fixtures', 'bad_fixtures')
113     Dir.entries(path).each do |file|
114       next unless File.file?(file) and file !~ Fixtures::DEFAULT_FILTER_RE
115       assert_raise(Fixture::FormatError) {
116         Fixture.new(bad_fixtures_path, file)
117       }
118     end
119   end
121   def test_deprecated_yaml_extension
122     assert_raise(Fixture::FormatError) {
123       Fixtures.new(nil, 'bad_extension', 'BadExtension', File.join(File.dirname(__FILE__), 'fixtures'))
124     }
125   end
127   def test_logger_level_invariant
128     level = ActiveRecord::Base.logger.level
129     create_fixtures('topics')
130     assert_equal level, ActiveRecord::Base.logger.level
131   end
133   def test_instantiation
134     topics = create_fixtures("topics")
135     assert_kind_of Topic, topics["first"].find
136   end
138   def test_complete_instantiation
139     assert_equal 2, @topics.size
140     assert_equal "The First Topic", @first.title
141   end
143   def test_fixtures_from_root_yml_with_instantiation
144     # assert_equal 2, @accounts.size
145     assert_equal 50, @unknown.credit_limit
146   end
148   def test_erb_in_fixtures
149     assert_equal 11, @developers.size
150     assert_equal "fixture_5", @dev_5.name
151   end
153   def test_empty_yaml_fixture
154     assert_not_nil Fixtures.new( Account.connection, "accounts", 'Account', File.dirname(__FILE__) + "/fixtures/naked/yml/accounts")
155   end
157   def test_empty_yaml_fixture_with_a_comment_in_it
158     assert_not_nil Fixtures.new( Account.connection, "companies", 'Company', File.dirname(__FILE__) + "/fixtures/naked/yml/companies")
159   end
161   def test_dirty_dirty_yaml_file
162     assert_raises(Fixture::FormatError) do
163       Fixtures.new( Account.connection, "courses", 'Course', File.dirname(__FILE__) + "/fixtures/naked/yml/courses")
164     end
165   end
167   def test_empty_csv_fixtures
168     assert_not_nil Fixtures.new( Account.connection, "accounts", 'Account', File.dirname(__FILE__) + "/fixtures/naked/csv/accounts")
169   end
171   def test_omap_fixtures
172     assert_nothing_raised do
173       fixtures = Fixtures.new(Account.connection, 'categories', 'Category', File.dirname(__FILE__) + '/fixtures/categories_ordered')
175       i = 0
176       fixtures.each do |name, fixture|
177         assert_equal "fixture_no_#{i}", name
178         assert_equal "Category #{i}", fixture['name']
179         i += 1
180       end
181     end
182   end
184   def test_yml_file_in_subdirectory
185     assert_equal(categories(:sub_special_1).name, "A special category in a subdir file")
186     assert_equal(categories(:sub_special_1).class, SpecialCategory)
187   end
189   def test_subsubdir_file_with_arbitrary_name
190     assert_equal(categories(:sub_special_3).name, "A special category in an arbitrarily named subsubdir file")
191     assert_equal(categories(:sub_special_3).class, SpecialCategory)
192   end
194   def test_binary_in_fixtures
195     assert_equal 1, @binaries.size
196     data = File.open(BINARY_FIXTURE_PATH, "rb").read.freeze
197     assert_equal data, @flowers.data
198   end
201 if Account.connection.respond_to?(:reset_pk_sequence!)
202   class FixturesResetPkSequenceTest < Test::Unit::TestCase
203     fixtures :accounts
204     fixtures :companies
206     def setup
207       @instances = [Account.new(:credit_limit => 50), Company.new(:name => 'RoR Consulting')]
208       Fixtures.reset_cache # make sure tables get reinitialized
209     end
211     def test_resets_to_min_pk_with_specified_pk_and_sequence
212       @instances.each do |instance|
213         model = instance.class
214         model.delete_all
215         model.connection.reset_pk_sequence!(model.table_name, model.primary_key, model.sequence_name)
217         instance.save!
218         assert_equal 1, instance.id, "Sequence reset for #{model.table_name} failed."
219       end
220     end
222     def test_resets_to_min_pk_with_default_pk_and_sequence
223       @instances.each do |instance|
224         model = instance.class
225         model.delete_all
226         model.connection.reset_pk_sequence!(model.table_name)
228         instance.save!
229         assert_equal 1, instance.id, "Sequence reset for #{model.table_name} failed."
230       end
231     end
233     def test_create_fixtures_resets_sequences_when_not_cached
234       @instances.each do |instance|
235         max_id = create_fixtures(instance.class.table_name).inject(0) do |max_id, (name, fixture)|
236           fixture_id = fixture['id'].to_i
237           fixture_id > max_id ? fixture_id : max_id
238         end
240         # Clone the last fixture to check that it gets the next greatest id.
241         instance.save!
242         assert_equal max_id + 1, instance.id, "Sequence reset for #{instance.class.table_name} failed."
243       end
244     end
245   end
248 class FixturesWithoutInstantiationTest < Test::Unit::TestCase
249   self.use_instantiated_fixtures = false
250   fixtures :topics, :developers, :accounts
252   def test_without_complete_instantiation
253     assert_nil @first
254     assert_nil @topics
255     assert_nil @developers
256     assert_nil @accounts
257   end
259   def test_fixtures_from_root_yml_without_instantiation
260     assert_nil @unknown
261   end
263   def test_accessor_methods
264     assert_equal "The First Topic", topics(:first).title
265     assert_equal "Jamis", developers(:jamis).name
266     assert_equal 50, accounts(:signals37).credit_limit
267   end
269   def test_accessor_methods_with_multiple_args
270     assert_equal 2, topics(:first, :second).size
271     assert_raise(StandardError) { topics([:first, :second]) }
272   end
274   uses_mocha 'reloading_fixtures_through_accessor_methods' do
275     def test_reloading_fixtures_through_accessor_methods
276       assert_equal "The First Topic", topics(:first).title
277       @loaded_fixtures['topics']['first'].expects(:find).returns(stub(:title => "Fresh Topic!"))
278       assert_equal "Fresh Topic!", topics(:first, true).title
279     end
280   end
283 class FixturesWithoutInstanceInstantiationTest < Test::Unit::TestCase
284   self.use_instantiated_fixtures = true
285   self.use_instantiated_fixtures = :no_instances
287   fixtures :topics, :developers, :accounts
289   def test_without_instance_instantiation
290     assert_nil @first
291     assert_not_nil @topics
292     assert_not_nil @developers
293     assert_not_nil @accounts
294   end
297 class TransactionalFixturesTest < Test::Unit::TestCase
298   self.use_instantiated_fixtures = true
299   self.use_transactional_fixtures = true
301   fixtures :topics
303   def test_destroy
304     assert_not_nil @first
305     @first.destroy
306   end
308   def test_destroy_just_kidding
309     assert_not_nil @first
310   end
313 class MultipleFixturesTest < Test::Unit::TestCase
314   fixtures :topics
315   fixtures :developers, :accounts
317   def test_fixture_table_names
318     assert_equal %w(topics developers accounts), fixture_table_names
319   end
322 class OverlappingFixturesTest < Test::Unit::TestCase
323   fixtures :topics, :developers
324   fixtures :developers, :accounts
326   def test_fixture_table_names
327     assert_equal %w(topics developers accounts), fixture_table_names
328   end
331 class ForeignKeyFixturesTest < Test::Unit::TestCase
332   fixtures :fk_test_has_pk, :fk_test_has_fk
334   # if foreign keys are implemented and fixtures
335   # are not deleted in reverse order then this test
336   # case will raise StatementInvalid
338   def test_number1
339     assert true
340   end
342   def test_number2
343     assert true
344   end
347 class SetTableNameFixturesTest < Test::Unit::TestCase
348   set_fixture_class :funny_jokes => 'Joke'
349   fixtures :funny_jokes
351   def test_table_method
352     assert_kind_of Joke, funny_jokes(:a_joke)
353   end
356 class CustomConnectionFixturesTest < Test::Unit::TestCase
357   set_fixture_class :courses => Course
358   fixtures :courses
360   def test_connection
361     assert_kind_of Course, courses(:ruby)
362     assert_equal Course.connection, courses(:ruby).connection
363   end
366 class InvalidTableNameFixturesTest < Test::Unit::TestCase
367   fixtures :funny_jokes
369   def test_raises_error
370     assert_raises FixtureClassNotFound do
371       funny_jokes(:a_joke)
372     end
373   end
376 class CheckEscapedYamlFixturesTest < Test::Unit::TestCase
377   set_fixture_class :funny_jokes => 'Joke'
378   fixtures :funny_jokes
380   def test_proper_escaped_fixture
381     assert_equal "The \\n Aristocrats\nAte the candy\n", funny_jokes(:another_joke).name
382   end
385 class DevelopersProject; end
386 class ManyToManyFixturesWithClassDefined < Test::Unit::TestCase
387   fixtures :developers_projects
389   def test_this_should_run_cleanly
390     assert true
391   end
394 class FixturesBrokenRollbackTest < Test::Unit::TestCase
395   def blank_setup; end
396   alias_method :ar_setup_with_fixtures, :setup_with_fixtures
397   alias_method :setup_with_fixtures, :blank_setup
398   alias_method :setup, :blank_setup
400   def blank_teardown; end
401   alias_method :ar_teardown_with_fixtures, :teardown_with_fixtures
402   alias_method :teardown_with_fixtures, :blank_teardown
403   alias_method :teardown, :blank_teardown
405   def test_no_rollback_in_teardown_unless_transaction_active
406     assert_equal 0, Thread.current['open_transactions']
407     assert_raise(RuntimeError) { ar_setup_with_fixtures }
408     assert_equal 0, Thread.current['open_transactions']
409     assert_nothing_raised { ar_teardown_with_fixtures }
410     assert_equal 0, Thread.current['open_transactions']
411   end
413   private
414     def load_fixtures
415       raise 'argh'
416     end
419 class LoadAllFixturesTest < Test::Unit::TestCase
420   self.fixture_path= File.join(File.dirname(__FILE__), '/fixtures/all')
421   fixtures :all
423   def test_all_there
424     assert_equal %w(developers people tasks), fixture_table_names.sort
425   end
428 class FasterFixturesTest < Test::Unit::TestCase
429   fixtures :categories, :authors
431   def load_extra_fixture(name)
432     fixture = create_fixtures(name)
433     assert fixture.is_a?(Fixtures)
434     @loaded_fixtures[fixture.table_name] = fixture
435   end
437   def test_cache
438     assert Fixtures.fixture_is_cached?(ActiveRecord::Base.connection, 'categories')
439     assert Fixtures.fixture_is_cached?(ActiveRecord::Base.connection, 'authors')
441     assert_no_queries do
442       create_fixtures('categories')
443       create_fixtures('authors')
444     end
446     load_extra_fixture('posts')
447     assert Fixtures.fixture_is_cached?(ActiveRecord::Base.connection, 'posts')
448     self.class.setup_fixture_accessors('posts')
449     assert_equal 'Welcome to the weblog', posts(:welcome).title
450   end
453 class FoxyFixturesTest < Test::Unit::TestCase
454   fixtures :parrots, :parrots_pirates, :pirates, :treasures
456   def test_identifies_strings
457     assert_equal(Fixtures.identify("foo"), Fixtures.identify("foo"))
458     assert_not_equal(Fixtures.identify("foo"), Fixtures.identify("FOO"))
459   end
461   def test_identifies_symbols
462     assert_equal(Fixtures.identify(:foo), Fixtures.identify(:foo))
463   end
465   TIMESTAMP_COLUMNS = %w(created_at created_on updated_at updated_on)
467   def test_populates_timestamp_columns
468     TIMESTAMP_COLUMNS.each do |property|
469       assert_not_nil(parrots(:george).send(property), "should set #{property}")
470     end
471   end
473   def test_populates_all_columns_with_the_same_time
474     last = nil
476     TIMESTAMP_COLUMNS.each do |property|
477       current = parrots(:george).send(property)
478       last ||= current
480       assert_equal(last, current)
481       last = current
482     end
483   end
485   def test_only_populates_columns_that_exist
486     assert_not_nil(pirates(:blackbeard).created_on)
487     assert_not_nil(pirates(:blackbeard).updated_on)
488   end
490   def test_preserves_existing_fixture_data
491     assert_equal(2.weeks.ago.to_date, pirates(:redbeard).created_on.to_date)
492     assert_equal(2.weeks.ago.to_date, pirates(:redbeard).updated_on.to_date)
493   end
495   def test_generates_unique_ids
496     assert_not_nil(parrots(:george).id)
497     assert_not_equal(parrots(:george).id, parrots(:louis).id)
498   end
500   def test_resolves_belongs_to_symbols
501     assert_equal(parrots(:george), pirates(:blackbeard).parrot)
502   end
504   def test_supports_join_tables
505     assert(pirates(:blackbeard).parrots.include?(parrots(:george)))
506     assert(pirates(:blackbeard).parrots.include?(parrots(:louis)))
507     assert(parrots(:george).pirates.include?(pirates(:blackbeard)))
508   end
510   def test_supports_inline_habtm
511     assert(parrots(:george).treasures.include?(treasures(:diamond)))
512     assert(parrots(:george).treasures.include?(treasures(:sapphire)))
513     assert(!parrots(:george).treasures.include?(treasures(:ruby)))
514   end
516   def test_supports_yaml_arrays
517     assert(parrots(:louis).treasures.include?(treasures(:diamond)))
518     assert(parrots(:louis).treasures.include?(treasures(:sapphire)))
519   end
521   def test_strips_DEFAULTS_key
522     assert_raise(StandardError) { parrots(:DEFAULTS) }
524     # this lets us do YAML defaults and not have an extra fixture entry
525     %w(sapphire ruby).each { |t| assert(parrots(:davey).treasures.include?(treasures(t))) }
526   end
528   def test_supports_label_interpolation
529     assert_equal("frederick", parrots(:frederick).name)
530   end
532   def test_supports_polymorphic_belongs_to
533     assert_equal(pirates(:redbeard), treasures(:sapphire).looter)
534     assert_equal(parrots(:louis), treasures(:ruby).looter)
535   end
538 class ActiveSupportSubclassWithFixturesTest < ActiveSupport::TestCase
539   fixtures :parrots
541   # This seemingly useless assertion catches a bug that caused the fixtures
542   # setup code call nil[]
543   def test_foo
544     assert_equal parrots(:louis), Parrot.find_by_name("King Louis")
545   end