From 49eafd8c3620bf8e46d21d447fc634a12c8280ab Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 26 Oct 2007 05:56:46 +0000 Subject: Foxy fixtures. Adapter#disable_referential_integrity. Closes #9981. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8036 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/associations/eager_test.rb | 4 +- activerecord/test/associations_test.rb | 2 +- .../test/fixtures/db_definitions/schema.rb | 29 ++++++++ activerecord/test/fixtures/parrot.rb | 4 ++ activerecord/test/fixtures/parrots.yml | 16 +++++ activerecord/test/fixtures/parrots_pirates.yml | 7 ++ activerecord/test/fixtures/pirate.rb | 4 ++ activerecord/test/fixtures/pirates.yml | 9 +++ activerecord/test/fixtures/treasure.rb | 3 + activerecord/test/fixtures/treasures.yml | 8 +++ activerecord/test/fixtures_test.rb | 83 ++++++++++++++++++++++ 11 files changed, 166 insertions(+), 3 deletions(-) create mode 100644 activerecord/test/fixtures/parrot.rb create mode 100644 activerecord/test/fixtures/parrots.yml create mode 100644 activerecord/test/fixtures/parrots_pirates.yml create mode 100644 activerecord/test/fixtures/pirate.rb create mode 100644 activerecord/test/fixtures/pirates.yml create mode 100644 activerecord/test/fixtures/treasure.rb create mode 100644 activerecord/test/fixtures/treasures.yml (limited to 'activerecord/test') diff --git a/activerecord/test/associations/eager_test.rb b/activerecord/test/associations/eager_test.rb index 2b02971238..75948ab59f 100644 --- a/activerecord/test/associations/eager_test.rb +++ b/activerecord/test/associations/eager_test.rb @@ -252,9 +252,9 @@ class EagerAssociationTest < Test::Unit::TestCase end def test_eager_with_scoped_order_using_association_limiting_without_explicit_scope - posts_with_explicit_order = Post.find(:all, :conditions => 'comments.id', :include => :comments, :order => 'posts.id DESC', :limit => 2) + posts_with_explicit_order = Post.find(:all, :conditions => 'comments.id is not null', :include => :comments, :order => 'posts.id DESC', :limit => 2) posts_with_scoped_order = Post.with_scope(:find => {:order => 'posts.id DESC'}) do - Post.find(:all, :conditions => 'comments.id', :include => :comments, :limit => 2) + Post.find(:all, :conditions => 'comments.id is not null', :include => :comments, :limit => 2) end assert_equal posts_with_explicit_order, posts_with_scoped_order end diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index 9f155dd4ab..121a8c5ac4 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -548,7 +548,7 @@ class HasManyAssociationsTest < Test::Unit::TestCase client_ary = firm.clients_using_finder_sql.find("2", "3") assert_kind_of Array, client_ary assert_equal 2, client_ary.size - assert_equal client, client_ary.first + assert client_ary.include?(client) end def test_find_all diff --git a/activerecord/test/fixtures/db_definitions/schema.rb b/activerecord/test/fixtures/db_definitions/schema.rb index dcb6222cd3..9cadc5bdb8 100644 --- a/activerecord/test/fixtures/db_definitions/schema.rb +++ b/activerecord/test/fixtures/db_definitions/schema.rb @@ -295,4 +295,33 @@ ActiveRecord::Schema.define do t.column :city, :string, :null => false t.column :type, :string end + + create_table :parrots, :force => true do |t| + t.column :name, :string + t.column :created_at, :datetime + t.column :created_on, :datetime + t.column :updated_at, :datetime + t.column :updated_on, :datetime + end + + create_table :pirates, :force => true do |t| + t.column :catchphrase, :string + t.column :parrot_id, :integer + t.column :created_on, :datetime + t.column :updated_on, :datetime + end + + create_table :parrots_pirates, :id => false, :force => true do |t| + t.column :parrot_id, :integer + t.column :pirate_id, :integer + end + + create_table :treasures, :force => true do |t| + t.column :name, :string + end + + create_table :parrots_treasures, :id => false, :force => true do |t| + t.column :parrot_id, :integer + t.column :treasure_id, :integer + end end diff --git a/activerecord/test/fixtures/parrot.rb b/activerecord/test/fixtures/parrot.rb new file mode 100644 index 0000000000..340c73b907 --- /dev/null +++ b/activerecord/test/fixtures/parrot.rb @@ -0,0 +1,4 @@ +class Parrot < ActiveRecord::Base + has_and_belongs_to_many :pirates + has_and_belongs_to_many :treasures +end diff --git a/activerecord/test/fixtures/parrots.yml b/activerecord/test/fixtures/parrots.yml new file mode 100644 index 0000000000..74bc6b4e78 --- /dev/null +++ b/activerecord/test/fixtures/parrots.yml @@ -0,0 +1,16 @@ +george: + name: "Curious George" + treasures: diamond, sapphire + +louis: + name: "King Louis" + treasures: [diamond, sapphire] + +frederick: + name: $LABEL + +DEFAULTS: &DEFAULTS + treasures: sapphire, ruby + +davey: + <<: *DEFAULTS diff --git a/activerecord/test/fixtures/parrots_pirates.yml b/activerecord/test/fixtures/parrots_pirates.yml new file mode 100644 index 0000000000..6b17a37d68 --- /dev/null +++ b/activerecord/test/fixtures/parrots_pirates.yml @@ -0,0 +1,7 @@ +george_blackbeard: + parrot_id: <%= Fixtures.identify(:george) %> + pirate_id: <%= Fixtures.identify(:blackbeard) %> + +louis_blackbeard: + parrot_id: <%= Fixtures.identify(:louis) %> + pirate_id: <%= Fixtures.identify(:blackbeard) %> diff --git a/activerecord/test/fixtures/pirate.rb b/activerecord/test/fixtures/pirate.rb new file mode 100644 index 0000000000..d22d66bac9 --- /dev/null +++ b/activerecord/test/fixtures/pirate.rb @@ -0,0 +1,4 @@ +class Pirate < ActiveRecord::Base + belongs_to :parrot + has_and_belongs_to_many :parrots +end diff --git a/activerecord/test/fixtures/pirates.yml b/activerecord/test/fixtures/pirates.yml new file mode 100644 index 0000000000..abb91101da --- /dev/null +++ b/activerecord/test/fixtures/pirates.yml @@ -0,0 +1,9 @@ +blackbeard: + catchphrase: "Yar." + parrot: george + +redbeard: + catchphrase: "Avast!" + parrot: louis + created_on: <%= 2.weeks.ago.to_s(:db) %> + updated_on: <%= 2.weeks.ago.to_s(:db) %> diff --git a/activerecord/test/fixtures/treasure.rb b/activerecord/test/fixtures/treasure.rb new file mode 100644 index 0000000000..cbb3ec3c08 --- /dev/null +++ b/activerecord/test/fixtures/treasure.rb @@ -0,0 +1,3 @@ +class Treasure < ActiveRecord::Base + has_and_belongs_to_many :parrots +end diff --git a/activerecord/test/fixtures/treasures.yml b/activerecord/test/fixtures/treasures.yml new file mode 100644 index 0000000000..c6fe30d0e3 --- /dev/null +++ b/activerecord/test/fixtures/treasures.yml @@ -0,0 +1,8 @@ +diamond: + name: $LABEL + +sapphire: + name: $LABEL + +ruby: + name: $LABEL diff --git a/activerecord/test/fixtures_test.rb b/activerecord/test/fixtures_test.rb index f35e1ef2c7..2bc72d0b4e 100755 --- a/activerecord/test/fixtures_test.rb +++ b/activerecord/test/fixtures_test.rb @@ -7,6 +7,9 @@ require 'fixtures/reply' require 'fixtures/joke' require 'fixtures/course' require 'fixtures/category' +require 'fixtures/parrot' +require 'fixtures/pirate' +require 'fixtures/treasure' class FixturesTest < Test::Unit::TestCase self.use_instantiated_fixtures = true @@ -446,3 +449,83 @@ class FasterFixturesTest < Test::Unit::TestCase assert_equal 'Welcome to the weblog', posts(:welcome).title end end + +class FoxyFixturesTest < Test::Unit::TestCase + fixtures :parrots, :parrots_pirates, :pirates, :treasures + + def test_identifies_strings + assert_equal(Fixtures.identify("foo"), Fixtures.identify("foo")) + assert_not_equal(Fixtures.identify("foo"), Fixtures.identify("FOO")) + end + + def test_identifies_symbols + assert_equal(Fixtures.identify(:foo), Fixtures.identify(:foo)) + end + + TIMESTAMP_COLUMNS = %w(created_at created_on updated_at updated_on) + + def test_populates_timestamp_columns + TIMESTAMP_COLUMNS.each do |property| + assert_not_nil(parrots(:george).send(property), "should set #{property}") + end + end + + def test_populates_all_columns_with_the_same_time + last = nil + + TIMESTAMP_COLUMNS.each do |property| + current = parrots(:george).send(property) + last ||= current + + assert_equal(last, current) + last = current + end + end + + def test_only_populates_columns_that_exist + assert_not_nil(pirates(:blackbeard).created_on) + assert_not_nil(pirates(:blackbeard).updated_on) + end + + def test_preserves_existing_fixture_data + assert_equal(2.weeks.ago.to_date, pirates(:redbeard).created_on.to_date) + assert_equal(2.weeks.ago.to_date, pirates(:redbeard).updated_on.to_date) + end + + def test_generates_unique_ids + assert_not_nil(parrots(:george).id) + assert_not_equal(parrots(:george).id, parrots(:louis).id) + end + + def test_resolves_belongs_to_symbols + assert_equal(parrots(:george), pirates(:blackbeard).parrot) + end + + def test_supports_join_tables + assert(pirates(:blackbeard).parrots.include?(parrots(:george))) + assert(pirates(:blackbeard).parrots.include?(parrots(:louis))) + assert(parrots(:george).pirates.include?(pirates(:blackbeard))) + end + + def test_supports_inline_habtm + assert(parrots(:george).treasures.include?(treasures(:diamond))) + assert(parrots(:george).treasures.include?(treasures(:sapphire))) + assert(!parrots(:george).treasures.include?(treasures(:ruby))) + end + + def test_supports_yaml_arrays + assert(parrots(:louis).treasures.include?(treasures(:diamond))) + assert(parrots(:louis).treasures.include?(treasures(:sapphire))) + end + + def test_strips_DEFAULTS_key + assert_raise(StandardError) { parrots(:DEFAULTS) } + + # this lets us do YAML defaults and not have an extra fixture entry + %w(sapphire ruby).each { |t| assert(parrots(:davey).treasures.include?(treasures(t))) } + end + + def test_supports_label_interpolation + assert_equal("frederick", parrots(:frederick).name) + end +end -- cgit v1.2.3