From e00e9d7d5925d42ee454d29134fa73435d8b0b39 Mon Sep 17 00:00:00 2001 From: Zach Dennis Date: Sat, 19 Mar 2011 15:49:26 -0400 Subject: [PATCH] Adding in support for :synchronization option for #import. Pulled in from ar-extensions. Currently only works with MySQL since that is the only database that supports on duplicate key update functionality. https://github.com/zdennis/activerecord-import/issues#issue/6 --- test/support/mysql/import_examples.rb | 26 ++++++++++++++++++++++++++ test/synchronize_test.rb | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/test/support/mysql/import_examples.rb b/test/support/mysql/import_examples.rb index f455089..6e716ca 100644 --- a/test/support/mysql/import_examples.rb +++ b/test/support/mysql/import_examples.rb @@ -142,4 +142,30 @@ def should_support_mysql_import_functionality end end + + describe "#import with :synchronization option" do + let(:topics){ Array.new } + let(:values){ [ [topics.first.id, "Jerry Carter"], [topics.last.id, "Chad Fowler"] ]} + let(:columns){ %W(id author_name) } + + setup do + topics << Topic.create!(:title=>"LDAP", :author_name=>"Big Bird") + topics << Topic.create!(:title=>"Rails Recipes", :author_name=>"Elmo") + end + + it "synchronizes passed in ActiveRecord model instances with the data just imported" do + columns2update = [ 'author_name' ] + + expected_count = Topic.count + Topic.import( columns, values, + :validate=>false, + :on_duplicate_key_update=>columns2update, + :synchronize=>topics ) + + assert_equal expected_count, Topic.count, "no new records should have been created!" + assert_equal "Jerry Carter", topics.first.author_name, "wrong author!" + assert_equal "Chad Fowler", topics.last.author_name, "wrong author!" + end + end + end \ No newline at end of file diff --git a/test/synchronize_test.rb b/test/synchronize_test.rb index 30e548f..02eb14f 100644 --- a/test/synchronize_test.rb +++ b/test/synchronize_test.rb @@ -19,4 +19,4 @@ describe ".synchronize" do assert_equal "#{titles[1]}_haha", actual_titles[1], "the second record was not correctly updated" assert_equal "#{titles[2]}_haha", actual_titles[2], "the third record was not correctly updated" end -end \ No newline at end of file +end