diff --git a/test/active_record_test.rb b/test/active_record_test.rb index 70dae2a..c43e43a 100644 --- a/test/active_record_test.rb +++ b/test/active_record_test.rb @@ -55,6 +55,16 @@ end create_table "namespaced", :force => true + + create_table "clubs", :force => true do |t| + t.integer "id" + t.string "name" + end + + create_table "clubs_users", :id => false, :force => true do |t| + t.integer "club_id" + t.integer "user_id" + end end # models @@ -62,6 +72,7 @@ class User < ActiveRecord::Base has_one :profile, :dependent => :destroy has_many :emails, :dependent => :destroy, :order => 'id' has_many :notes, :as => :notable + has_and_belongs_to_many :clubs replicate_natural_key :login end @@ -93,6 +104,10 @@ class User::Namespaced < ActiveRecord::Base self.table_name = "namespaced" end +class Club < ActiveRecord::Base + has_and_belongs_to_many :users +end + # The test case loads some fixture data once and uses transaction rollback to # reset fixture state for each test's setup. class ActiveRecordTest < Test::Unit::TestCase @@ -123,10 +138,13 @@ def self.fixtures user.create_profile :name => 'Ryan Tomayko', :homepage => 'http://tomayko.com' user.emails.create! :email => 'ryan@github.com' user.emails.create! :email => 'rtomayko@gmail.com' + user.clubs.create! :name => 'chess' + battlebots = user.clubs.create! :name => 'battlebots' user = User.create! :login => 'kneath' user.create_profile :name => 'Kyle Neath', :homepage => 'http://warpspire.com' user.emails.create! :email => 'kyle@github.com' + user.clubs << battlebots user = User.create! :login => 'tmm1' user.create_profile :name => 'tmm1', :homepage => 'https://github.com/tmm1' @@ -352,6 +370,51 @@ def test_omit_attributes_at_dump_time assert_nil attrs['created_at'] end + def test_dumping_and_loading_has_and_belongs_to_many_associations + objects = [] + @dumper.listen { |type, id, attrs, obj| objects << [type, id, attrs, obj] } + + User.replicate_associations :clubs + rtomayko = User.find_by_login('rtomayko') + kneath = User.find_by_login('kneath') + @dumper.dump rtomayko + @dumper.dump kneath + + User.destroy_all + Club.destroy_all + objects.each { |type, id, attrs, obj| @loader.feed type, id, attrs } + + rtomayko = User.find_by_login('rtomayko') + kneath = User.find_by_login('kneath') + battlebots = Club.find_by_name('battlebots') + chess = Club.find_by_name('chess') + assert_equal [chess, battlebots], rtomayko.clubs.to_a + assert_equal [battlebots], kneath.clubs.to_a + assert_equal [rtomayko, kneath], battlebots.users.to_a + end + + def test_has_and_belongs_to_many_associations_at_dump_time + objects = [] + @dumper.listen { |type, id, attrs, obj| objects << [type, id, attrs, obj] } + + rtomayko = User.find_by_login('rtomayko') + kneath = User.find_by_login('kneath') + @dumper.dump rtomayko, :associations => [:clubs] + @dumper.dump kneath, :associations => [:clubs] + + User.destroy_all + Club.destroy_all + objects.each { |type, id, attrs, obj| @loader.feed type, id, attrs } + + rtomayko = User.find_by_login('rtomayko') + kneath = User.find_by_login('kneath') + battlebots = Club.find_by_name('battlebots') + chess = Club.find_by_name('chess') + assert_equal [chess, battlebots], rtomayko.clubs.to_a + assert_equal [battlebots], kneath.clubs.to_a + assert_equal [rtomayko, kneath], battlebots.users.to_a + end + def test_dumping_polymorphic_associations objects = [] @dumper.listen { |type, id, attrs, obj| objects << [type, id, attrs, obj] }