From 8b3f83105ce719642a8f079f72c4f03c97cfc321 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 26 Nov 2007 22:45:43 +0000 Subject: Foxy fixtures: allow mixed usage to make migration easier and more attractive. Closes #10004. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8218 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/aggregations_test.rb | 2 +- .../test/fixtures/db_definitions/schema.rb | 10 ++++++- activerecord/test/fixtures/joke.rb | 3 -- activerecord/test/fixtures/matey.rb | 2 +- activerecord/test/fixtures/parrots.yml | 5 ++++ activerecord/test/fixtures/ship.rb | 3 ++ activerecord/test/fixtures/ships.yml | 5 ++++ activerecord/test/fixtures_test.rb | 32 ++++++++++++++++++++-- activerecord/test/mixin_test.rb | 13 +++++++-- 9 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 activerecord/test/fixtures/ship.rb create mode 100644 activerecord/test/fixtures/ships.yml (limited to 'activerecord/test') diff --git a/activerecord/test/aggregations_test.rb b/activerecord/test/aggregations_test.rb index 299e0484e3..ea0339c63a 100644 --- a/activerecord/test/aggregations_test.rb +++ b/activerecord/test/aggregations_test.rb @@ -20,7 +20,7 @@ class AggregationsTest < Test::Unit::TestCase def test_change_single_value_object customers(:david).balance = Money.new(100) customers(:david).save - assert_equal 100, Customer.find(1).balance.amount + assert_equal 100, customers(:david).reload.balance.amount end def test_immutable_value_objects diff --git a/activerecord/test/fixtures/db_definitions/schema.rb b/activerecord/test/fixtures/db_definitions/schema.rb index 9b6b6987d2..f6bd0efa5d 100644 --- a/activerecord/test/fixtures/db_definitions/schema.rb +++ b/activerecord/test/fixtures/db_definitions/schema.rb @@ -335,10 +335,18 @@ ActiveRecord::Schema.define do t.column :parrot_id, :integer t.column :treasure_id, :integer end - + create_table :mateys, :id => false, :force => true do |t| t.column :pirate_id, :integer t.column :target_id, :integer t.column :weight, :integer end + + create_table :ships, :force => true do |t| + t.string :name + t.datetime :created_at + t.datetime :created_on + t.datetime :updated_at + t.datetime :updated_on + end end diff --git a/activerecord/test/fixtures/joke.rb b/activerecord/test/fixtures/joke.rb index 8006a43bd7..3978abc2ba 100644 --- a/activerecord/test/fixtures/joke.rb +++ b/activerecord/test/fixtures/joke.rb @@ -1,6 +1,3 @@ class Joke < ActiveRecord::Base set_table_name 'funny_jokes' end -class Joke < ActiveRecord::Base - set_table_name 'funny_jokes' -end \ No newline at end of file diff --git a/activerecord/test/fixtures/matey.rb b/activerecord/test/fixtures/matey.rb index 86442e0470..47b0baa974 100644 --- a/activerecord/test/fixtures/matey.rb +++ b/activerecord/test/fixtures/matey.rb @@ -1,4 +1,4 @@ class Matey < ActiveRecord::Base belongs_to :pirate - belongs_to :target, :class_name => 'Pirate', :foreign_key => 'target_id' + belongs_to :target, :class_name => 'Pirate' end diff --git a/activerecord/test/fixtures/parrots.yml b/activerecord/test/fixtures/parrots.yml index 74bc6b4e78..dd2c9548e7 100644 --- a/activerecord/test/fixtures/parrots.yml +++ b/activerecord/test/fixtures/parrots.yml @@ -9,6 +9,11 @@ louis: frederick: name: $LABEL +polly: + id: 4 + name: $LABEL + treasures: sapphire, ruby + DEFAULTS: &DEFAULTS treasures: sapphire, ruby diff --git a/activerecord/test/fixtures/ship.rb b/activerecord/test/fixtures/ship.rb new file mode 100644 index 0000000000..05b09fc1b9 --- /dev/null +++ b/activerecord/test/fixtures/ship.rb @@ -0,0 +1,3 @@ +class Ship < ActiveRecord::Base + self.record_timestamps = false +end \ No newline at end of file diff --git a/activerecord/test/fixtures/ships.yml b/activerecord/test/fixtures/ships.yml new file mode 100644 index 0000000000..137055aad1 --- /dev/null +++ b/activerecord/test/fixtures/ships.yml @@ -0,0 +1,5 @@ +black_pearl: + name: "Black Pearl" +interceptor: + id: 2 + name: "Interceptor" diff --git a/activerecord/test/fixtures_test.rb b/activerecord/test/fixtures_test.rb index de0321fbf4..d51a4029bc 100755 --- a/activerecord/test/fixtures_test.rb +++ b/activerecord/test/fixtures_test.rb @@ -1,5 +1,8 @@ require 'abstract_unit' +require 'fixtures/post' +require 'fixtures/binary' require 'fixtures/topic' +require 'fixtures/computer' require 'fixtures/developer' require 'fixtures/company' require 'fixtures/task' @@ -11,6 +14,7 @@ require 'fixtures/parrot' require 'fixtures/pirate' require 'fixtures/treasure' require 'fixtures/matey' +require 'fixtures/ship' class FixturesTest < Test::Unit::TestCase self.use_instantiated_fixtures = true @@ -452,7 +456,7 @@ class FasterFixturesTest < Test::Unit::TestCase end class FoxyFixturesTest < Test::Unit::TestCase - fixtures :parrots, :parrots_pirates, :pirates, :treasures, :mateys + fixtures :parrots, :parrots_pirates, :pirates, :treasures, :mateys, :ships, :computers, :developers def test_identifies_strings assert_equal(Fixtures.identify("foo"), Fixtures.identify("foo")) @@ -471,6 +475,12 @@ class FoxyFixturesTest < Test::Unit::TestCase end end + def test_does_not_populate_timestamp_columns_if_model_has_set_record_timestamps_to_false + TIMESTAMP_COLUMNS.each do |property| + assert_nil(ships(:black_pearl).send(property), "should not set #{property}") + end + end + def test_populates_all_columns_with_the_same_time last = nil @@ -498,10 +508,22 @@ class FoxyFixturesTest < Test::Unit::TestCase assert_not_equal(parrots(:george).id, parrots(:louis).id) end + def test_automatically_sets_primary_key + assert_not_nil(ships(:black_pearl)) + end + + def test_preserves_existing_primary_key + assert_equal(2, ships(:interceptor).id) + end + def test_resolves_belongs_to_symbols assert_equal(parrots(:george), pirates(:blackbeard).parrot) end + def test_ignores_belongs_to_symbols_if_association_and_foreign_key_are_named_the_same + assert_equal(developers(:david), computers(:workstation).developer) + end + def test_supports_join_tables assert(pirates(:blackbeard).parrots.include?(parrots(:george))) assert(pirates(:blackbeard).parrots.include?(parrots(:louis))) @@ -514,6 +536,12 @@ class FoxyFixturesTest < Test::Unit::TestCase assert(!parrots(:george).treasures.include?(treasures(:ruby))) end + def test_supports_inline_habtm_with_specified_id + assert(parrots(:polly).treasures.include?(treasures(:ruby))) + assert(parrots(:polly).treasures.include?(treasures(:sapphire))) + assert(!parrots(:polly).treasures.include?(treasures(:diamond))) + end + def test_supports_yaml_arrays assert(parrots(:louis).treasures.include?(treasures(:diamond))) assert(parrots(:louis).treasures.include?(treasures(:sapphire))) @@ -550,4 +578,4 @@ class ActiveSupportSubclassWithFixturesTest < ActiveSupport::TestCase def test_foo assert_equal parrots(:louis), Parrot.find_by_name("King Louis") end -end \ No newline at end of file +end diff --git a/activerecord/test/mixin_test.rb b/activerecord/test/mixin_test.rb index f8d9edc646..d2118e6764 100644 --- a/activerecord/test/mixin_test.rb +++ b/activerecord/test/mixin_test.rb @@ -78,10 +78,17 @@ class TouchTest < Test::Unit::TestCase def test_create_turned_off Mixin.record_timestamps = false - assert_nil mixins(:set_1).updated_at - mixins(:set_1).save - assert_nil mixins(:set_1).updated_at + mixin = Mixin.new + assert_nil mixin.updated_at + mixin.save + assert_nil mixin.updated_at + + # Make sure Mixin.record_timestamps gets reset, even if this test fails, + # so that other tests do not fail because Mixin.record_timestamps == false + rescue Exception => e + raise e + ensure Mixin.record_timestamps = true end -- cgit v1.2.3