Skip to content

Commit

Permalink
job_id was not being parsed properly.
Browse files Browse the repository at this point in the history
removed Rails dependency
changed spec test, so it's no longer Salesforce account-specific
  • Loading branch information
patcheng committed May 20, 2014
1 parent 1387fff commit 8132635
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 35 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: sf_user@example.com
passwordandtoken: passandtokenhere
passwordandtoken: passandtokenhere
test_account_id: 0013000000ymMBh
7 changes: 2 additions & 5 deletions lib/salesforce_bulk_api/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ def create_job(batch_size, send_nulls, no_null_list)
response = @connection.post_xml(nil, path, xml, headers)
response_parsed = XmlSimple.xml_in(response)

begin
@job_id = response_parsed['jobInfo']['id']
rescue NoMethodError => e
Rails.logger.error "SalesforceBulkApi: #{response_parsed['error']['exceptionCode']}: #{response_parsed['error']['exceptionMessage']}"
end
@job_id = response_parsed['id'][0]

end

def close_job()
Expand Down
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 8132635

Please sign in to comment.