From 777c57133b48e8b746f94ae0819eeb7a3c9b2764 Mon Sep 17 00:00:00 2001 From: Ian Young Date: Mon, 1 Oct 2012 17:02:13 -0700 Subject: [PATCH 1/6] Habtm dumping test --- test/active_record_test.rb | 58 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/test/active_record_test.rb b/test/active_record_test.rb index 70dae2a..0b425db 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", :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,46 @@ def test_omit_attributes_at_dump_time assert_nil attrs['created_at'] end + def test_dumping_has_and_belongs_to_many_associations + objects = [] + @dumper.listen { |type, id, attrs, obj| objects << [type, id, attrs, obj] } + + User.replicate_associations :clubs + Profile.delete_all + rtomayko = User.find_by_login('rtomayko') + kneath = User.find_by_login('kneath') + @dumper.dump rtomayko + @dumper.dump kneath + + assert_equal 6, objects.size + + type, id, attrs, obj = objects.shift + assert_equal 'User', type + assert_equal rtomayko.id, id + + type, id, attrs, obj = objects.shift + assert_equal 'Club', type + assert_equal 'chess', attrs['name'] + + type, id, attrs, obj = objects.shift + assert_equal 'Club', type + assert_equal 'battlebots', attrs['name'] + + type, id, attrs, obj = objects.shift + assert_equal 'Replicate::AR::Habtm', type + assert_equal [:id, 'User', rtomayko.id], obj.attributes["id"] + assert_equal [:id, 'Club', rtomayko.club_ids], obj.attributes["collection"] + + type, id, attrs, obj = objects.shift + assert_equal 'User', type + assert_equal kneath.id, id + + type, id, attrs, obj = objects.shift + assert_equal 'Replicate::AR::Habtm', type + assert_equal [:id, 'User', kneath.id], obj.attributes["id"] + assert_equal [:id, 'Club', kneath.club_ids], obj.attributes["collection"] + end + def test_dumping_polymorphic_associations objects = [] @dumper.listen { |type, id, attrs, obj| objects << [type, id, attrs, obj] } From 0e73ba81844c0e20b47b5b078d955c5f124a2166 Mon Sep 17 00:00:00 2001 From: Ian Young Date: Tue, 9 Oct 2012 13:12:53 -0700 Subject: [PATCH 2/6] Test loading of HABTM associations --- test/active_record_test.rb | 39 +++++++++++--------------------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/test/active_record_test.rb b/test/active_record_test.rb index 0b425db..5ad7df1 100644 --- a/test/active_record_test.rb +++ b/test/active_record_test.rb @@ -370,44 +370,27 @@ def test_omit_attributes_at_dump_time assert_nil attrs['created_at'] end - def test_dumping_has_and_belongs_to_many_associations + 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 - Profile.delete_all rtomayko = User.find_by_login('rtomayko') kneath = User.find_by_login('kneath') @dumper.dump rtomayko @dumper.dump kneath - assert_equal 6, objects.size - - type, id, attrs, obj = objects.shift - assert_equal 'User', type - assert_equal rtomayko.id, id - - type, id, attrs, obj = objects.shift - assert_equal 'Club', type - assert_equal 'chess', attrs['name'] - - type, id, attrs, obj = objects.shift - assert_equal 'Club', type - assert_equal 'battlebots', attrs['name'] - - type, id, attrs, obj = objects.shift - assert_equal 'Replicate::AR::Habtm', type - assert_equal [:id, 'User', rtomayko.id], obj.attributes["id"] - assert_equal [:id, 'Club', rtomayko.club_ids], obj.attributes["collection"] - - type, id, attrs, obj = objects.shift - assert_equal 'User', type - assert_equal kneath.id, id + User.destroy_all + Club.destroy_all + objects.each { |type, id, attrs, obj| @loader.feed type, id, attrs } - type, id, attrs, obj = objects.shift - assert_equal 'Replicate::AR::Habtm', type - assert_equal [:id, 'User', kneath.id], obj.attributes["id"] - assert_equal [:id, 'Club', kneath.club_ids], obj.attributes["collection"] + 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 rtomayko.clubs, [chess, battlebots] + assert_equal kneath.clubs, [battlebots] + assert_equal battlebots.users, [rtomayko, kneath] end def test_dumping_polymorphic_associations From 62d06b84043762bc3fa8301af10589657b7bda52 Mon Sep 17 00:00:00 2001 From: Ian Young Date: Tue, 9 Oct 2012 14:03:06 -0700 Subject: [PATCH 3/6] Now the test is failing --- test/active_record_test.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/active_record_test.rb b/test/active_record_test.rb index 5ad7df1..a7094ab 100644 --- a/test/active_record_test.rb +++ b/test/active_record_test.rb @@ -374,11 +374,12 @@ 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 rtomayko.clubs @dumper.dump kneath + @dumper.dump kneath.clubs User.destroy_all Club.destroy_all @@ -388,9 +389,9 @@ def test_dumping_and_loading_has_and_belongs_to_many_associations kneath = User.find_by_login('kneath') battlebots = Club.find_by_name('battlebots') chess = Club.find_by_name('chess') - assert_equal rtomayko.clubs, [chess, battlebots] - assert_equal kneath.clubs, [battlebots] - assert_equal battlebots.users, [rtomayko, kneath] + 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 From 9f22912036126e512342a5bc689f5bb36284d1e0 Mon Sep 17 00:00:00 2001 From: Ian Young Date: Tue, 9 Oct 2012 14:36:54 -0700 Subject: [PATCH 4/6] Test both forms of HABTM --- test/active_record_test.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/active_record_test.rb b/test/active_record_test.rb index a7094ab..325576b 100644 --- a/test/active_record_test.rb +++ b/test/active_record_test.rb @@ -374,6 +374,29 @@ 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_without_replicate_directive + 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 From 1c6abc2b33404170a0753f4d10f86b5b35463057 Mon Sep 17 00:00:00 2001 From: Ian Young Date: Mon, 15 Oct 2012 14:03:50 -0700 Subject: [PATCH 5/6] Test right --- test/active_record_test.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/active_record_test.rb b/test/active_record_test.rb index 325576b..155952c 100644 --- a/test/active_record_test.rb +++ b/test/active_record_test.rb @@ -393,16 +393,14 @@ def test_dumping_and_loading_has_and_belongs_to_many_associations assert_equal [rtomayko, kneath], battlebots.users.to_a end - def test_has_and_belongs_to_many_associations_without_replicate_directive + 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 - @dumper.dump rtomayko.clubs - @dumper.dump kneath - @dumper.dump kneath.clubs + @dumper.dump rtomayko, :associations => [:clubs] + @dumper.dump kneath, :associations => [:clubs] User.destroy_all Club.destroy_all From d98ba27825ac1c6e2147a43d283d3846a5c62d88 Mon Sep 17 00:00:00 2001 From: Ian Young Date: Tue, 24 Sep 2013 13:59:32 -0700 Subject: [PATCH 6/6] Don't define an id on join table --- test/active_record_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/active_record_test.rb b/test/active_record_test.rb index 155952c..c43e43a 100644 --- a/test/active_record_test.rb +++ b/test/active_record_test.rb @@ -61,7 +61,7 @@ t.string "name" end - create_table "clubs_users", :force => true do |t| + create_table "clubs_users", :id => false, :force => true do |t| t.integer "club_id" t.integer "user_id" end