Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
Conflicts:
	lib/salesforce_bulk_api/job.rb
  • Loading branch information
jthibeaux committed Aug 25, 2014
2 parents 8d57963 + eb6c8d4 commit fbcd7a6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 32 deletions.
3 changes: 2 additions & 1 deletion example_auth_credentials.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ salesforce:
client_id: client_id_here
client_secret: client_secret_here
user: [email protected]
passwordandtoken: passandtokenhere
passwordandtoken: passandtokenhere
test_account_id: 0013000000ymMBh
20 changes: 19 additions & 1 deletion lib/salesforce_bulk_api/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def create_job(batch_size, send_nulls, no_null_list)
raise SalesforceException.new("#{response_parsed['exceptionMessage'][0]} (#{response_parsed['exceptionCode'][0]})") if response_parsed['exceptionCode']

@job_id = response_parsed['id'][0]

end

def close_job()
Expand Down Expand Up @@ -92,10 +93,27 @@ def add_batch(keys, batch)
response_parsed['id'][0] if response_parsed['id']
end

def build_sobject(data)
xml = '<sObject>'
data.keys.each do |k|
if k.is_a?(Hash)
xml += build_sobject(k)
elsif data[k] != :type
#xml += "<type>#{data[:type]}</type>"
xml += "<#{k}>#{data[k]}</#{k}>"
end
end
xml += '</sObject>'
end

def create_sobject(keys, r)
sobject_xml = '<sObject>'
keys.each do |k|
if !r[k].to_s.empty?
if r[k].is_a?(Hash)
sobject_xml += "<#{k}>"
sobject_xml += build_sobject(r[k])
sobject_xml += "</#{k}>"
elsif !r[k].to_s.empty?
sobject_xml += "<#{k}>"
if r[k].respond_to?(:encode)
sobject_xml += r[k].encode(:xml => :text)
Expand Down
2 changes: 1 addition & 1 deletion lib/salesforce_bulk_api/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SalesforceBulkApi
VERSION = '0.0.8'
VERSION = '0.0.10'
end
81 changes: 52 additions & 29 deletions spec/salesforce_bulk_api/salesforce_bulk_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,66 @@
@sf_client = Databasedotcom::Client.new(:client_id => auth_hash['salesforce']['client_id'],
:client_secret => auth_hash['salesforce']['client_secret'])
@sf_client.authenticate(:username => auth_hash['salesforce']['user'], :password => auth_hash['salesforce']['passwordandtoken'])

@account_id = auth_hash['salesforce']['test_account_id']

@api = SalesforceBulkApi::Api.new(@sf_client)
end

after :each do

end

describe 'upsert' do

context 'when not passed get_result' do
it "doesn't return the batches array" do
res = @api.upsert('Account', [{:Id => '0013000000ymMBh', :Website => 'www.test.com'}], 'Id')
it "doesn't return the batches array" do
res = @api.upsert('Account', [{:Id => @account_id, :Website => 'www.test.com'}], 'Id')
res['batches'].should be_nil
end
end

context 'when passed get_result = true' do
it 'returns the batches array' do
res = @api.upsert('Account', [{:Id => '0013000000ymMBh', :Website => 'www.test.com'}], 'Id', true)
res = @api.upsert('Account', [{:Id => @account_id, :Website => 'www.test.com'}], 'Id', true)
res['batches'][0]['response'].is_a? Array
res['batches'][0]['response'][0].should eq({'id'=>['0013000000ymMBhAAM'], 'success'=>['true'], 'created'=>['false']})

res['batches'][0]['response'][0]['id'][0].should start_with(@account_id)
res['batches'][0]['response'][0]['success'].should eq ['true']
res['batches'][0]['response'][0]['created'].should eq ['false']

end
end

context 'when passed send_nulls = true' do
it 'sets the nil and empty attributes to NULL' do
@api.update('Account', [{:Id => '0013000000ymMBh', :Website => 'abc123', :Other_Phone__c => '5678', :Gold_Star__c => true}], true)
res = @api.query('Account', "SELECT Website, Other_Phone__c From Account WHERE Id = '0013000000ymMBh'")
@api.update('Account', [{:Id => @account_id, :Website => 'abc123', :Phone => '5678'}], true)
res = @api.query('Account', "SELECT Website, Phone From Account WHERE Id = '#{@account_id}'")
res['batches'][0]['response'][0]['Website'][0].should eq 'abc123'
res['batches'][0]['response'][0]['Other_Phone__c'][0].should eq '5678'
res = @api.upsert('Account', [{:Id => '0013000000ymMBh', :Website => '', :Other_Phone__c => nil, :Gold_Star__c => false, :CRM_Last_Modified__c => nil}], 'Id', true, true)
res['batches'][0]['response'][0].should eq({'id'=>['0013000000ymMBhAAM'], 'success'=>['true'], 'created'=>['false']})
res = @api.query('Account', "SELECT Website, Other_Phone__c, Gold_Star__c, CRM_Last_Modified__c From Account WHERE Id = '0013000000ymMBh'")
res['batches'][0]['response'][0]['Phone'][0].should eq '5678'
res = @api.upsert('Account', [{:Id => @account_id, :Website => '', :Phone => nil}], 'Id', true, true)
res['batches'][0]['response'][0]['id'][0].should start_with(@account_id)
res['batches'][0]['response'][0]['success'].should eq ['true']
res['batches'][0]['response'][0]['created'].should eq ['false']
res = @api.query('Account', "SELECT Website, Phone From Account WHERE Id = '#{@account_id}'")
res['batches'][0]['response'][0]['Website'][0].should eq({"xsi:nil" => "true"})
res['batches'][0]['response'][0]['Other_Phone__c'][0].should eq({"xsi:nil" => "true"})
res['batches'][0]['response'][0]['Gold_Star__c'][0].should eq('false')
res['batches'][0]['response'][0]['CRM_Last_Modified__c'][0].should eq({"xsi:nil" => "true"})
res['batches'][0]['response'][0]['Phone'][0].should eq({"xsi:nil" => "true"})
end
end

context 'when passed send_nulls = true and an array of fields not to null' do
it 'sets the nil and empty attributes to NULL, except for those included in the list of fields to ignore' do
@api.update('Account', [{:Id => '0013000000ymMBh', :Website => 'abc123', :Other_Phone__c => '5678', :Gold_Star__c => true}], true)
res = @api.query('Account', "SELECT Website, Other_Phone__c From Account WHERE Id = '0013000000ymMBh'")
@api.update('Account', [{:Id => @account_id, :Website => 'abc123', :Phone => '5678'}], true)
res = @api.query('Account', "SELECT Website, Phone From Account WHERE Id = '#{@account_id}'")
res['batches'][0]['response'][0]['Website'][0].should eq 'abc123'
res['batches'][0]['response'][0]['Other_Phone__c'][0].should eq '5678'
res = @api.upsert('Account', [{:Id => '0013000000ymMBh', :Website => '', :Other_Phone__c => nil, :Gold_Star__c => false, :CRM_Last_Modified__c => nil}], 'Id', true, true, [:Website, :Other_Phone__c])
res['batches'][0]['response'][0].should eq({'id'=>['0013000000ymMBhAAM'], 'success'=>['true'], 'created'=>['false']})
res = @api.query('Account', "SELECT Website, Other_Phone__c, Gold_Star__c, CRM_Last_Modified__c From Account WHERE Id = '0013000000ymMBh'")
res['batches'][0]['response'][0]['Phone'][0].should eq '5678'
res = @api.upsert('Account', [{:Id => @account_id, :Website => '', :Phone => nil}], 'Id', true, true, [:Website, :Phone])
res['batches'][0]['response'][0]['id'][0].should start_with(@account_id)
res['batches'][0]['response'][0]['success'].should eq ['true']
res['batches'][0]['response'][0]['created'].should eq ['false']
res = @api.query('Account', "SELECT Website, Phone From Account WHERE Id = '#{@account_id}'")
res['batches'][0]['response'][0]['Website'][0].should eq('abc123')
res['batches'][0]['response'][0]['Other_Phone__c'][0].should eq('5678')
res['batches'][0]['response'][0]['Gold_Star__c'][0].should eq('false')
res['batches'][0]['response'][0]['CRM_Last_Modified__c'][0].should eq({"xsi:nil" => "true"})
res['batches'][0]['response'][0]['Phone'][0].should eq('5678')
end
end

Expand All @@ -67,33 +78,45 @@
context 'when there is not an error' do
context 'when not passed get_result' do
it "doesnt return the batches array" do
res = @api.update('Account', [{:Id => '0013000000ymMBh', :Website => 'www.test.com'}])
res = @api.update('Account', [{:Id => @account_id, :Website => 'www.test.com'}])
res['batches'].should be_nil
end
end

context 'when passed get_result = true' do
it 'returns the batches array' do
res = @api.update('Account', [{:Id => '0013000000ymMBh', :Website => 'www.test.com'}], true)
res = @api.update('Account', [{:Id => @account_id, :Website => 'www.test.com'}], true)
res['batches'][0]['response'].is_a? Array
res['batches'][0]['response'][0].should eq({'id'=>['0013000000ymMBhAAM'], 'success'=>['true'], 'created'=>['false']})
res['batches'][0]['response'][0]['id'][0].should start_with(@account_id)
res['batches'][0]['response'][0]['success'].should eq ['true']
res['batches'][0]['response'][0]['created'].should eq ['false']
end
end
end

context 'when there is an error' do
context 'when not passed get_result' do
it "doesn't return the results array" do
res = @api.update('Account', [{:Id => '0013000000ymMBh', :Website => 'www.test.com'},{:Id => 'abc123', :Website => 'www.test.com'}])
res = @api.update('Account', [{:Id => @account_id, :Website => 'www.test.com'},{:Id => 'abc123', :Website => 'www.test.com'}])
res['batches'].should be_nil
end
end

context 'when passed get_result = true with batches' do
it 'returns the results array' do
res = @api.update('Account', [{:Id => '0013000000ymMBh', :Website => 'www.test.com'}, {:Id => '0013000000ymMBh', :Website => 'www.test.com'}, {:Id => '0013000000ymMBh', :Website => 'www.test.com'}, {:Id => 'abc123', :Website => 'www.test.com'}], true, false, [], 2)
res['batches'][0]['response'].should eq([{"id"=>["0013000000ymMBhAAM"], "success"=>["true"], "created"=>["false"]}, {"errors"=>[{"fields"=>["Id"], "message"=>["Account ID: id value of incorrect type: abc123"], "statusCode"=>["MALFORMED_ID"]}], "success"=>["false"], "created"=>["false"]}])
res['batches'][1]['response'].should eq([{"id"=>["0013000000ymMBhAAM"], "success"=>["true"], "created"=>["false"]},{"id"=>["0013000000ymMBhAAM"], "success"=>["true"], "created"=>["false"]}])
res = @api.update('Account', [{:Id => @account_id, :Website => 'www.test.com'}, {:Id => @account_id, :Website => 'www.test.com'}, {:Id => @account_id, :Website => 'www.test.com'}, {:Id => 'abc123', :Website => 'www.test.com'}], true, false, [], 2)
res['batches'][0]['response'][0]['id'][0].should start_with(@account_id)
res['batches'][0]['response'][0]['success'].should eq ['true']
res['batches'][0]['response'][0]['created'].should eq ['false']

res['batches'][0]['response'][1].should eq({"errors"=>[{"fields"=>["Id"], "message"=>["Account ID: id value of incorrect type: abc123"], "statusCode"=>["MALFORMED_ID"]}], "success"=>["false"], "created"=>["false"]})

res['batches'][1]['response'][0]['id'][0].should start_with(@account_id)
res['batches'][1]['response'][0]['success'].should eq ['true']
res['batches'][1]['response'][0]['created'].should eq ['false']
res['batches'][1]['response'][1]['id'][0].should start_with(@account_id)
res['batches'][1]['response'][1]['success'].should eq ['true']
res['batches'][1]['response'][1]['created'].should eq ['false']
end
end
end
Expand Down

0 comments on commit fbcd7a6

Please sign in to comment.