Permalink
Browse files

Added overriding global config options

  • Loading branch information...
1 parent dae45b8 commit 2e4023c8d64ff055b76819e2068bc55b20d1a707 quaternion committed Feb 4, 2012
Showing with 64 additions and 33 deletions.
  1. +7 −4 lib/transfer.rb
  2. +22 −9 spec/transfer_spec.rb
  3. +35 −20 spec/transferer_spec.rb
View
@@ -35,12 +35,15 @@ def transfer *args, &block
when Hash
transfer :default, *args, &block
when Transfer::Config
- raise ArgumentError.new("second argument should be Hash") unless args[1].instance_of?(Hash)
- config = args[0]
- args[1].each do |key, value|
+ raise ArgumentError.new("second argument should be Hash!") unless args[1].instance_of?(Hash)
+ config, options = args[0], args[1]
+ process_keys = [:validate, :failure_strategy, :failure]
+ process_options = config.process_options.merge options.select{|key| process_keys.include?(key) }
+ sources = options.select{|key| !process_keys.include?(key) }
+ sources.each do |key, value|
dataset = config.connection[key]
transferer = Transfer::Transferer.new dataset, value, &block
- transferer.process config.process_options
+ transferer.process process_options
end
end
View
@@ -7,6 +7,9 @@
it { should respond_to :configs }
it { should respond_to :configure }
+ before do
+ Transfer.configs.clear
+ end
describe ".configs" do
subject { Transfer.configs }
@@ -17,12 +20,7 @@
end
end
-
describe ".configure" do
- after do
- Transfer.configs.clear
- end
-
context ":default" do
subject { Transfer.configs[:default] }
before do
@@ -42,10 +40,8 @@
it { should be_instance_of Transfer::Config }
its(:host) { should == "named" }
end
-
end
-
describe "transfer" do
def do_action &block
transfer :source_users => klass, &block
@@ -56,14 +52,15 @@ def do_action &block
end
context "with empty options" do
- before :all do
+ before do
Transfer.configure
end
it_should_behave_like "a transfer"
end
+
context "with options :validate => false" do
- before :all do
+ before do
Transfer.configure do |config|
config.validate = false
end
@@ -154,6 +151,22 @@ def do_action &block
end
end
+ describe "override global options" do
+ before do
+ Transfer.configure do |c|
+ c.validate = true
+ c.failure_strategy = :ignore
+ end
+ instance_of(Transfer::Transferer).process(:validate => false, :failure_strategy => :rollback).once
+ end
+
+ [SequelUser, ActiveRecordUser, MongoidUser].each do |klass|
+ it "for #{klass}" do
+ transfer :source_users => klass, :validate => false, :failure_strategy => :rollback
+ end
+ end
+ end
+
end
end
View
@@ -4,11 +4,11 @@
shared_examples "a transfer johnny hollow" do
let!(:user) { Fabricate :johnny_hollow }
- it "created one record" do
+ it "should change count records from 0 to 1" do
expect{ do_action }.to change{ klass.count }.from(0).to(1)
end
- describe "klass.first" do
+ describe "first record" do
subject { klass.first }
before do
@@ -75,6 +75,13 @@
end
end
+shared_examples "a not transfer johnny hollow" do
+ let!(:user) { Fabricate :johnny_hollow }
+
+ it "should count not changed" do
+ expect{ do_action }.to_not change{ klass.count }
+ end
+end
shared_examples "a transfer" do
context SequelUser do
@@ -91,6 +98,22 @@
end
end
+shared_examples "a not transfer" do
+ let!(:user) { Fabricate :johnny_hollow }
+
+ context SequelUser do
+ let!(:klass) { SequelUser }
+ it_should_behave_like "a not transfer johnny hollow"
+ end
+ context ActiveRecordUser do
+ let!(:klass) { ActiveRecordUser }
+ it_should_behave_like "a not transfer johnny hollow"
+ end
+ context MongoidUser do
+ let!(:klass) { MongoidUser }
+ it_should_behave_like "a not transfer johnny hollow"
+ end
+end
shared_examples "a transfer with false validation" do
context SequelUserWithFalseValidation do
@@ -107,32 +130,24 @@
end
end
-
-shared_examples "a not transfer" do
+shared_examples "a not transfer with false validation" do
let!(:user) { Fabricate :johnny_hollow }
- context SequelUser do
- let!(:klass) { SequelUser }
- it "should not crated new record" do
- expect{ do_action }.to_not change{ SequelUser.count }
- end
+ context SequelUserWithFalseValidation do
+ let!(:klass) { SequelUserWithFalseValidation }
+ it_should_behave_like "a not transfer johnny hollow"
end
- context ActiveRecordUser do
- let!(:klass) { ActiveRecordUser }
- it "should not crated new record" do
- expect{ do_action }.to_not change{ ActiveRecordUser.count }
- end
+ context ActiveRecordUserWithFalseValidation do
+ let!(:klass) { ActiveRecordUserWithFalseValidation }
+ it_should_behave_like "a not transfer johnny hollow"
end
- context MongoidUser do
- let!(:klass) { MongoidUser }
- it "should not crated new record" do
- expect{ do_action }.to_not change{ MongoidUser.count }
- end
+ context MongoidUserWithFalseValidation do
+ let!(:klass) { MongoidUserWithFalseValidation }
+ it_should_behave_like "a not transfer johnny hollow"
end
end
-
module Transfer
describe Transferer do
let!(:dataset) { SOURCE_DB[:source_users] }

0 comments on commit 2e4023c

Please sign in to comment.