salesforcebulk 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/.gitignore +4 -0
  2. data/.rbenv-version +1 -0
  3. data/Gemfile +2 -0
  4. data/README.md +143 -0
  5. data/Rakefile +22 -0
  6. data/lib/salesforce_bulk.rb +15 -0
  7. data/lib/salesforce_bulk/batch.rb +50 -0
  8. data/lib/salesforce_bulk/batch_result.rb +39 -0
  9. data/lib/salesforce_bulk/batch_result_collection.rb +29 -0
  10. data/lib/salesforce_bulk/client.rb +254 -0
  11. data/lib/salesforce_bulk/core_extensions/string.rb +14 -0
  12. data/lib/salesforce_bulk/job.rb +70 -0
  13. data/lib/salesforce_bulk/query_result_collection.rb +48 -0
  14. data/lib/salesforce_bulk/salesforce_error.rb +30 -0
  15. data/lib/salesforce_bulk/version.rb +3 -0
  16. data/salesforcebulk.gemspec +28 -0
  17. data/test/fixtures/batch_create_request.csv +3 -0
  18. data/test/fixtures/batch_create_response.xml +13 -0
  19. data/test/fixtures/batch_info_list_response.xml +27 -0
  20. data/test/fixtures/batch_info_response.xml +13 -0
  21. data/test/fixtures/batch_result_list_response.csv +3 -0
  22. data/test/fixtures/config.yml +7 -0
  23. data/test/fixtures/invalid_batch_error.xml +5 -0
  24. data/test/fixtures/invalid_error.xml +5 -0
  25. data/test/fixtures/invalid_job_error.xml +5 -0
  26. data/test/fixtures/invalid_session_error.xml +5 -0
  27. data/test/fixtures/job_abort_request.xml +1 -0
  28. data/test/fixtures/job_abort_response.xml +25 -0
  29. data/test/fixtures/job_close_request.xml +1 -0
  30. data/test/fixtures/job_close_response.xml +25 -0
  31. data/test/fixtures/job_create_request.xml +1 -0
  32. data/test/fixtures/job_create_response.xml +25 -0
  33. data/test/fixtures/job_info_response.xml +25 -0
  34. data/test/fixtures/login_error.xml +1 -0
  35. data/test/fixtures/login_request.xml +1 -0
  36. data/test/fixtures/login_response.xml +39 -0
  37. data/test/fixtures/query_result_list_response.xml +1 -0
  38. data/test/fixtures/query_result_response.csv +5 -0
  39. data/test/lib/test_batch.rb +258 -0
  40. data/test/lib/test_batch_result.rb +40 -0
  41. data/test/lib/test_core_extensions.rb +15 -0
  42. data/test/lib/test_initialization.rb +86 -0
  43. data/test/lib/test_job.rb +256 -0
  44. data/test/lib/test_query_result_collection.rb +87 -0
  45. data/test/test_helper.rb +32 -0
  46. metadata +222 -0
@@ -0,0 +1 @@
1
+ <?xml version="1.0" encoding="UTF-8"?><result-list xmlns="http://www.force.com/2009/06/asyncapi/dataload"><result>752E0000000TNaq</result></result-list>
@@ -0,0 +1,5 @@
1
+ "Id__c","Title__c","IsPreview__c"
2
+ "23456.0","A second test!","true"
3
+ "12345.0","This is a test video","false"
4
+ "3456.0","Some long title","false"
5
+ "4567.0","Another test title for testing","true"
@@ -0,0 +1,258 @@
1
+ require 'test_helper'
2
+
3
+ class TestBatch < Test::Unit::TestCase
4
+
5
+ def setup
6
+ options = {
7
+ :username => 'myusername',
8
+ :password => 'mypassword',
9
+ :token => "somelongtoken"
10
+ }
11
+
12
+ @client = SalesforceBulk::Client.new(options)
13
+ bypass_authentication(@client)
14
+ @batch = SalesforceBulk::Batch.new
15
+ @headers = {"Content-Type" => "text/csv; charset=UTF-8", 'X-Sfdc-Session' => '123456789'}
16
+ @headersWithXml = {'Content-Type' => 'application/xml', 'X-Sfdc-Session' => '123456789'}
17
+ end
18
+
19
+ test "initialize from XML" do
20
+ xml = fixture("batch_info_response.xml")
21
+ batch = SalesforceBulk::Batch.new_from_xml(XmlSimple.xml_in(xml, 'ForceArray' => false))
22
+
23
+ assert_equal batch.id, '751E00000004ZRbIAM'
24
+ assert_equal batch.job_id, '750E00000004N97IAE'
25
+ assert_equal batch.state, 'Completed'
26
+ assert_equal batch.created_at, DateTime.parse('2012-05-31T01:22:47.000Z')
27
+ assert_equal batch.completed_at, DateTime.parse('2012-05-31T01:22:47.000Z')
28
+ assert_equal batch.processed_records, 2
29
+ assert_equal batch.failed_records, 0
30
+ assert_equal batch.total_processing_time, 72
31
+ assert_equal batch.api_active_processing_time, 28
32
+ assert_equal batch.apex_processing_time, 0
33
+ end
34
+
35
+ test "state?" do
36
+ @batch.state = "Completed"
37
+ assert @batch.state?('Completed')
38
+
39
+ @batch.state = "COMPLETED"
40
+ assert @batch.state?('completed')
41
+
42
+ @batch.state = "Failed"
43
+ assert !@batch.state?('Queued')
44
+ end
45
+
46
+ test "state is marked queued" do
47
+ @batch.state = "Queued"
48
+ assert @batch.queued?
49
+
50
+ @batch.state = nil
51
+ assert !@batch.queued?
52
+ end
53
+
54
+ test "state is marked in progress" do
55
+ @batch.state = "InProgress"
56
+ assert @batch.in_progress?
57
+
58
+ @batch.state = nil
59
+ assert !@batch.in_progress?
60
+ end
61
+
62
+ test "state is marked completed" do
63
+ @batch.state = "Completed"
64
+ assert @batch.completed?
65
+
66
+ @batch.state = nil
67
+ assert !@batch.completed?
68
+ end
69
+
70
+ test "state is marked failed" do
71
+ @batch.state = "Failed"
72
+ assert @batch.failed?
73
+
74
+ @batch.state = nil
75
+ assert !@batch.failed?
76
+ end
77
+
78
+ test "should add a batch to a job and return a successful response" do
79
+ request = fixture("batch_create_request.csv")
80
+ response = fixture("batch_create_response.xml")
81
+ job_id = "750E00000004NRfIAM"
82
+ batch_id = "751E00000004ZmUIAU"
83
+ data = [
84
+ {:Id__c => '12345', :Title__c => "This is a test video", :IsPreview__c => nil},
85
+ {:Id__c => '23456', :Title__c => "A second test!", :IsPreview__c => true}
86
+ ]
87
+
88
+ stub_request(:post, "#{api_url(@client)}job/#{job_id}/batch")
89
+ .with(:body => request, :headers => @headers)
90
+ .to_return(:body => response, :status => 200)
91
+
92
+ batch = @client.add_batch(job_id, data)
93
+
94
+ assert_requested :post, "#{api_url(@client)}job/#{job_id}/batch", :body => request, :headers => @headers, :times => 1
95
+
96
+ assert_equal batch.id, batch_id
97
+ assert_equal batch.job_id, job_id
98
+ assert_equal batch.state, 'Queued'
99
+ assert_equal batch.created_at, DateTime.parse('2012-06-02T21:03:56.000Z')
100
+ assert_equal batch.completed_at, DateTime.parse('2012-06-02T21:03:56.000Z')
101
+ assert_equal batch.processed_records, 0
102
+ assert_equal batch.failed_records, 0
103
+ assert_equal batch.total_processing_time, 0
104
+ assert_equal batch.api_active_processing_time, 0
105
+ assert_equal batch.apex_processing_time, 0
106
+ end
107
+
108
+ test "should raise ArgumentError when given data array length exceeding 10000" do
109
+ data = (1..10100).map { |index| {:Id => index} }
110
+
111
+ assert_raise ArgumentError do
112
+ @client.add_batch("750E00000004NRfIAM", data)
113
+ end
114
+ end
115
+
116
+ test "should retrieve info for all batches in a job in a single request" do
117
+ response = fixture("batch_info_list_response.xml")
118
+ job_id = "750E00000004N97IAE"
119
+
120
+ stub_request(:get, "#{api_url(@client)}job/#{job_id}/batch").with(:headers => @headersWithXml).to_return(:body => response, :status => 200)
121
+
122
+ batches = @client.batch_info_list(job_id)
123
+
124
+ assert_requested :get, "#{api_url(@client)}job/#{job_id}/batch", :headers => @headersWithXml, :times => 1
125
+
126
+ assert_kind_of Array, batches
127
+ assert_kind_of SalesforceBulk::Batch, batches.first
128
+ assert_equal batches.length, 2
129
+
130
+ batch = batches.first
131
+ assert_equal batch.id, "751E00000004ZRbIAM"
132
+ assert_equal batch.job_id, job_id
133
+ assert_equal batch.state, 'Completed'
134
+ assert_equal batch.created_at, DateTime.parse('2012-05-31T01:22:47.000Z')
135
+ assert_equal batch.completed_at, DateTime.parse('2012-05-31T01:22:47.000Z')
136
+ assert_equal batch.processed_records, 2
137
+ assert_equal batch.failed_records, 0
138
+ assert_equal batch.total_processing_time, 72
139
+ assert_equal batch.api_active_processing_time, 28
140
+ assert_equal batch.apex_processing_time, 0
141
+
142
+ batch = batches.last
143
+ assert_equal batch.id, "751E00000004ZQsIAM"
144
+ assert_equal batch.job_id, job_id
145
+ assert_equal batch.state, 'Completed'
146
+ assert_equal batch.created_at, DateTime.parse('2012-05-31T01:23:20.000Z')
147
+ assert_equal batch.completed_at, DateTime.parse('2012-05-31T01:23:20.000Z')
148
+ assert_equal batch.processed_records, 2
149
+ assert_equal batch.failed_records, 0
150
+ assert_equal batch.total_processing_time, 72
151
+ assert_equal batch.api_active_processing_time, 28
152
+ assert_equal batch.apex_processing_time, 0
153
+ end
154
+
155
+ test "should retrieve batch info" do
156
+ response = fixture("batch_info_response.xml")
157
+ job_id = "750E00000004N97IAE"
158
+ batch_id = "751E00000004ZRbIAM"
159
+
160
+ stub_request(:get, "#{api_url(@client)}job/#{job_id}/batch/#{batch_id}")
161
+ .with(:headers => @headersWithXml)
162
+ .to_return(:body => response, :status => 200)
163
+
164
+ batch = @client.batch_info(job_id, batch_id)
165
+
166
+ assert_requested :get, "#{api_url(@client)}job/#{job_id}/batch/#{batch_id}", :headers => @headersWithXml, :times => 1
167
+
168
+ assert_equal batch.id, batch_id
169
+ assert_equal batch.job_id, job_id
170
+ assert_equal batch.state, 'Completed'
171
+ assert_equal batch.created_at, DateTime.parse('2012-05-31T01:22:47.000Z')
172
+ assert_equal batch.completed_at, DateTime.parse('2012-05-31T01:22:47.000Z')
173
+ assert_equal batch.processed_records, 2
174
+ assert_equal batch.failed_records, 0
175
+ assert_equal batch.total_processing_time, 72
176
+ assert_equal batch.api_active_processing_time, 28
177
+ assert_equal batch.apex_processing_time, 0
178
+ end
179
+
180
+ test "should return batch result for a non-querying job" do
181
+ response = fixture("batch_result_list_response.csv")
182
+ job_id = "750E00000004NRa"
183
+ batch_id = "751E00000004ZmK"
184
+
185
+ # Batches that are created using CSV will always return
186
+ # results in CSV format despite requesting with XML content type.
187
+ # Thus the content type header is ignored.
188
+
189
+ stub_request(:get, "#{api_url(@client)}job/#{job_id}/batch/#{batch_id}/result").to_return(:body => response, :status => 200)
190
+
191
+ results = @client.batch_result(job_id, batch_id)
192
+
193
+ assert_requested :get, "#{api_url(@client)}job/#{job_id}/batch/#{batch_id}/result", :times => 1
194
+
195
+ assert_kind_of SalesforceBulk::BatchResultCollection, results
196
+ assert_kind_of Array, results
197
+ assert_equal results.length, 2
198
+ assert_equal results.job_id, job_id
199
+ assert_equal results.batch_id, batch_id
200
+ assert_equal results.first.success, true
201
+ assert_equal results.first.created, false
202
+ assert_equal results.first.error?, false
203
+ assert_equal results.first.error, ''
204
+ end
205
+
206
+ test "retrieve result id for a query operation" do
207
+ response = fixture("query_result_list_response.xml")
208
+ job_id = "750E00000004NnR"
209
+ batch_id = "751E00000004aEY"
210
+ result_id = "752E0000000TNaq"
211
+
212
+ stub_request(:get, "#{api_url(@client)}job/#{job_id}/batch/#{batch_id}/result")
213
+ .with(:headers => @headersWithXml)
214
+ .to_return(:body => response, :status => 200)
215
+
216
+ @client.expects(:query_result).with(job_id, batch_id, result_id).returns([])
217
+
218
+ result = @client.batch_result(job_id, batch_id)
219
+
220
+ assert_requested :get, "#{api_url(@client)}job/#{job_id}/batch/#{batch_id}/result", :headers => @headersWithXml, :times => 1
221
+ assert_kind_of SalesforceBulk::QueryResultCollection, result
222
+ assert_equal result.job_id, job_id
223
+ assert_equal result.batch_id, batch_id
224
+ assert_equal result.result_id, result_id
225
+ end
226
+
227
+ test "retrieve and parse a query result successfully" do
228
+ response = fixture("query_result_response.csv")
229
+ job_id = "750E00000004NnR"
230
+ batch_id = "751E00000004aEY"
231
+ result_id = "752E0000000TNaq"
232
+
233
+ stub_request(:get, "#{api_url(@client)}job/#{job_id}/batch/#{batch_id}/result/#{result_id}")
234
+ .with(:headers => @headers)
235
+ .to_return(:body => response, :status => 200)
236
+
237
+ result = @client.query_result(job_id, batch_id, result_id)
238
+
239
+ assert_requested :get, "#{api_url(@client)}job/#{job_id}/batch/#{batch_id}/result/#{result_id}", :headers => @headers, :times => 1
240
+ assert_kind_of Array, result
241
+ assert result.length == 4
242
+ end
243
+
244
+ test "should raise SalesforceError on invalid batch response" do
245
+ response = fixture("invalid_batch_error.xml")
246
+ job_id = "750E00000004NnR"
247
+ batch_id = "751E00000004aEY"
248
+
249
+ stub_request(:get, "#{api_url(@client)}job/#{job_id}/batch/#{batch_id}/result")
250
+ .with(:headers => @headersWithXml)
251
+ .to_return(:body => response, :status => 400)
252
+
253
+ assert_raise SalesforceBulk::SalesforceError do
254
+ @client.batch_result(job_id, batch_id)
255
+ end
256
+ end
257
+
258
+ end
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ class TestBatchResult < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @result_created = SalesforceBulk::BatchResult.new('123', true, true, '')
7
+ @result_updated = SalesforceBulk::BatchResult.new('123', true, false, '')
8
+ @result_failed = SalesforceBulk::BatchResult.new('123', false, false, 'Some Error Message This Is')
9
+ end
10
+
11
+ test "basic initialization" do
12
+ assert_equal @result_created.id, '123'
13
+ assert_equal @result_created.success, true
14
+ assert_equal @result_created.successful?, true
15
+ assert_equal @result_created.created, true
16
+ assert_equal @result_created.created?, true
17
+ assert_equal @result_created.error, ''
18
+ assert_equal @result_created.error?, false
19
+ assert_equal @result_created.updated?, false
20
+ end
21
+
22
+ test "initialization from CSV row" do
23
+ #@br = SalesforceBulk::BatchResult.new_from_csv()
24
+ end
25
+
26
+ test "error?" do
27
+ assert @result_failed.error?
28
+
29
+ assert !@result_created.error?
30
+ assert !@result_updated.error?
31
+ end
32
+
33
+ test "updated?" do
34
+ assert @result_updated.updated?
35
+
36
+ assert !@result_created.updated?
37
+ assert !@result_failed.updated?
38
+ end
39
+
40
+ end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ class TestCoreExtensions < Test::Unit::TestCase
4
+
5
+ test "to_b" do
6
+ assert_equal "true".to_b, true
7
+ assert_equal "TRUE".to_b, true
8
+ assert_equal "false".to_b, false
9
+ assert_equal "FALSE".to_b, false
10
+ assert_equal " true ".to_b, true
11
+ assert_equal " false ".to_b, false
12
+ assert_equal "Any true value".to_b, "Any true value"
13
+ end
14
+
15
+ end
@@ -0,0 +1,86 @@
1
+ require 'test_helper'
2
+
3
+ class TestInitialization < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @options = {
7
+ :username => 'MyUsername',
8
+ :password => 'MyPassword',
9
+ :token => 'MySecurityToken'
10
+ }
11
+
12
+ @client = SalesforceBulk::Client.new(@options)
13
+ end
14
+
15
+ test "initialization with default values" do
16
+ assert_not_nil @client
17
+ assert_equal @client.username, @options[:username]
18
+ assert_equal @client.password, "#{@options[:password]}#{@options[:token]}"
19
+ assert_equal @client.token, @options[:token]
20
+ assert_equal @client.host, 'login.salesforce.com'
21
+ assert_equal @client.version, 24.0
22
+ assert_equal @client.debugging, false
23
+ end
24
+
25
+ test "initialization overriding all default values" do
26
+ @options.merge!({:debugging => true, :host => 'newhost.salesforce.com', :version => 1.0})
27
+
28
+ client = SalesforceBulk::Client.new(@options)
29
+
30
+ assert_equal client.username, @options[:username]
31
+ assert_equal client.password, "#{@options[:password]}#{@options[:token]}"
32
+ assert_equal client.token, @options[:token]
33
+ assert_equal client.debugging, @options[:debugging]
34
+ assert_equal client.host, @options[:host]
35
+ assert_equal client.version, @options[:version]
36
+ end
37
+
38
+ test "initialization with a YAML file" do
39
+ client = SalesforceBulk::Client.new(fixture_path('config.yml'))
40
+
41
+ assert_equal client.username, 'MyUsername'
42
+ assert_equal client.password, 'MyPasswordMySecurityToken'
43
+ assert_equal client.token, 'MySecurityToken'
44
+ assert_equal client.debugging, true
45
+ assert_equal client.host, 'myhost.mydomain.com'
46
+ assert_equal client.version, 88.0
47
+ end
48
+
49
+ test "initialization with invalid key raises ArgumentError" do
50
+ assert_raise ArgumentError do
51
+ SalesforceBulk::Client.new(:non_existing_key => '')
52
+ end
53
+ end
54
+
55
+ test "authentication" do
56
+ headers = {'Content-Type' => 'text/xml', 'SOAPAction' => 'login'}
57
+ request = fixture("login_request.xml")
58
+ response = fixture("login_response.xml")
59
+
60
+ stub_request(:post, "https://#{@client.host}/services/Soap/u/24.0")
61
+ .with(:body => request, :headers => headers)
62
+ .to_return(:body => response, :status => 200)
63
+
64
+ @client.authenticate()
65
+
66
+ assert_requested :post, "https://#{@client.host}/services/Soap/u/24.0", :body => request, :headers => headers, :times => 1
67
+
68
+ assert_equal @client.instance_host, 'na9-api.salesforce.com'
69
+ assert_equal @client.instance_variable_get('@session_id'), '00DE0000000YSKp!AQ4AQNQhDKLMORZx2NwZppuKfure.ChCmdI3S35PPxpNA5MHb3ZVxhYd5STM3euVJTI5.39s.jOBT.3mKdZ3BWFDdIrddS8O'
70
+ end
71
+
72
+ test "parsing instance id from server url" do
73
+ assert_equal @client.instance_id('https://na1-api.salesforce.com'), 'na1-api'
74
+ assert_equal @client.instance_id('https://na23-api.salesforce.com'), 'na23-api'
75
+ assert_equal @client.instance_id('https://na345-api.salesforce.com'), 'na345-api'
76
+
77
+ # protocol shouldn't matter, its just part of the host name we are after
78
+ assert_equal @client.instance_id('://na1-api.salesforce.com'), 'na1-api'
79
+ assert_equal @client.instance_id('://na23-api.salesforce.com'), 'na23-api'
80
+
81
+ # in fact the .com portion shouldn't matter either
82
+ assert_equal @client.instance_id('://na1-api.salesforce'), 'na1-api'
83
+ assert_equal @client.instance_id('://na23-api.salesforce'), 'na23-api'
84
+ end
85
+
86
+ end
@@ -0,0 +1,256 @@
1
+ require 'test_helper'
2
+
3
+ class TestJob < Test::Unit::TestCase
4
+
5
+ def setup
6
+ options = {
7
+ :username => 'myusername',
8
+ :password => 'mypassword',
9
+ :token => "somelongtoken"
10
+ }
11
+
12
+ @client = SalesforceBulk::Client.new(options)
13
+ @job = SalesforceBulk::Job.new
14
+ @headers = {'Content-Type' => 'application/xml', 'X-Sfdc-Session' => '123456789'}
15
+
16
+ bypass_authentication(@client)
17
+ end
18
+
19
+ test "initialization from XML" do
20
+ xml = fixture("job_info_response.xml")
21
+ job = SalesforceBulk::Job.new_from_xml(XmlSimple.xml_in(xml, 'ForceArray' => false))
22
+
23
+ assert_equal job.id, '750E00000004N1mIAE'
24
+ assert_equal job.operation, 'upsert'
25
+ assert_equal job.sobject, 'VideoEvent__c'
26
+ assert_equal job.created_by, '005E00000017spfIAA'
27
+ assert_equal job.created_at, DateTime.parse('2012-05-30T04:08:30.000Z')
28
+ assert_equal job.completed_at, DateTime.parse('2012-05-30T04:08:30.000Z')
29
+ assert_equal job.state, 'Open'
30
+ assert_equal job.external_id_field_name, 'Id__c'
31
+ assert_equal job.concurrency_mode, 'Parallel'
32
+ assert_equal job.content_type, 'CSV'
33
+ assert_equal job.queued_batches, 0
34
+ assert_equal job.in_progress_batches, 0
35
+ assert_equal job.completed_batches, 0
36
+ assert_equal job.failed_batches, 0
37
+ assert_equal job.total_batches, 0
38
+ assert_equal job.processed_records, 0
39
+ assert_equal job.failed_records, 0
40
+ assert_equal job.retries, 0
41
+ assert_equal job.api_active_processing_time, 0
42
+ assert_equal job.apex_processing_time, 0
43
+ assert_equal job.total_processing_time, 0
44
+ assert_equal job.api_version, 24.0
45
+ end
46
+
47
+ test "state?" do
48
+ @job.state = 'Closed'
49
+ assert @job.state?('closed')
50
+
51
+ @job.state = 'Closed'
52
+ assert @job.state?('CLOSED')
53
+
54
+ @job.state = nil
55
+ assert !@job.state?('closed')
56
+ end
57
+
58
+ test "aborted?" do
59
+ @job.state = 'Aborted'
60
+ assert @job.aborted?
61
+
62
+ @job.state = nil
63
+ assert !@job.aborted?
64
+ end
65
+
66
+ test "closed?" do
67
+ @job.state = 'Closed'
68
+ assert @job.closed?
69
+
70
+ @job.state = nil
71
+ assert !@job.closed?
72
+ end
73
+
74
+ test "open?" do
75
+ @job.state = 'Open'
76
+ assert @job.open?
77
+
78
+ @job.state = nil
79
+ assert !@job.open?
80
+ end
81
+
82
+ test "add_job returns successful response" do
83
+ request = fixture("job_create_request.xml")
84
+ response = fixture("job_create_response.xml")
85
+
86
+ stub_request(:post, "#{api_url(@client)}job")
87
+ .with(:body => request, :headers => @headers)
88
+ .to_return(:body => response, :status => 200)
89
+
90
+ job = @client.add_job(:upsert, :VideoEvent__c, :external_id_field_name => :Id__c)
91
+
92
+ assert_requested :post, "#{api_url(@client)}job", :body => request, :headers => @headers, :times => 1
93
+
94
+ assert_equal job.id, '750E00000004MzbIAE'
95
+ assert_equal job.operation, 'upsert'
96
+ assert_equal job.sobject, 'VideoEvent__c'
97
+ assert_equal job.created_by, '005E00000017spfIAA'
98
+ assert_equal job.created_at, DateTime.parse('2012-05-29T21:50:47.000Z')
99
+ assert_equal job.completed_at, DateTime.parse('2012-05-29T21:50:47.000Z')
100
+ assert_equal job.state, 'Open'
101
+ assert_equal job.external_id_field_name, 'Id__c'
102
+ assert_equal job.concurrency_mode, 'Parallel'
103
+ assert_equal job.content_type, 'CSV'
104
+ assert_equal job.queued_batches, 0
105
+ assert_equal job.in_progress_batches, 0
106
+ assert_equal job.completed_batches, 0
107
+ assert_equal job.failed_batches, 0
108
+ assert_equal job.total_batches, 0
109
+ assert_equal job.processed_records, 0
110
+ assert_equal job.failed_records, 0
111
+ assert_equal job.retries, 0
112
+ assert_equal job.api_active_processing_time, 0
113
+ assert_equal job.apex_processing_time, 0
114
+ assert_equal job.total_processing_time, 0
115
+ assert_equal job.api_version, 24.0
116
+ end
117
+
118
+ test "add_job raises ArgumentError if provided with invalid operation" do
119
+ assert_raise ArgumentError do
120
+ job = @client.add_job(:SomeOtherOperation, nil)
121
+ end
122
+ end
123
+
124
+ test "add_job raises ArgumentError if provided with invalid key for options" do
125
+ assert_raise ArgumentError do
126
+ job = @client.add_job(:upsert, :VideoEvent__c, :non_existing_key => '')
127
+ end
128
+ end
129
+
130
+ test "add_job raises ArgumentError if provided with invalid concurrency mode" do
131
+ assert_raise ArgumentError do
132
+ job = @client.add_job(:upsert, :VideoEvent__c, :concurrency_mode => 'SomeMode')
133
+ end
134
+ end
135
+
136
+ test "should close job and return successful response" do
137
+ request = fixture("job_close_request.xml")
138
+ response = fixture("job_close_response.xml")
139
+ job_id = "750E00000004MzbIAE"
140
+
141
+ stub_request(:post, "#{api_url(@client)}job/#{job_id}")
142
+ .with(:body => request, :headers => @headers)
143
+ .to_return(:body => response, :status => 200)
144
+
145
+ job = @client.close_job(job_id)
146
+
147
+ assert_requested :post, "#{api_url(@client)}job/#{job_id}", :body => request, :headers => @headers, :times => 1
148
+
149
+ assert_equal job.id, job_id
150
+ assert_equal job.operation, 'upsert'
151
+ assert_equal job.sobject, 'VideoEvent__c'
152
+ assert_equal job.created_by, '005E00000017spfIAA'
153
+ assert_equal job.created_at, DateTime.parse('2012-05-29T23:51:53.000Z')
154
+ assert_equal job.completed_at, DateTime.parse('2012-05-29T23:51:53.000Z')
155
+ assert_equal job.state, 'Closed'
156
+ assert_equal job.external_id_field_name, 'Id__c'
157
+ assert_equal job.concurrency_mode, 'Parallel'
158
+ assert_equal job.content_type, 'CSV'
159
+ assert_equal job.queued_batches, 0
160
+ assert_equal job.in_progress_batches, 0
161
+ assert_equal job.completed_batches, 0
162
+ assert_equal job.failed_batches, 0
163
+ assert_equal job.total_batches, 0
164
+ assert_equal job.processed_records, 0
165
+ assert_equal job.failed_records, 0
166
+ assert_equal job.retries, 0
167
+ assert_equal job.api_active_processing_time, 0
168
+ assert_equal job.apex_processing_time, 0
169
+ assert_equal job.total_processing_time, 0
170
+ assert_equal job.api_version, 24.0
171
+ end
172
+
173
+ test "should abort job and return successful response" do
174
+ request = fixture("job_abort_request.xml")
175
+ response = fixture("job_abort_response.xml")
176
+ job_id = "750E00000004N1NIAU"
177
+
178
+ stub_request(:post, "#{api_url(@client)}job/#{job_id}")
179
+ .with(:body => request, :headers => @headers)
180
+ .to_return(:body => response, :status => 200)
181
+
182
+ job = @client.abort_job(job_id)
183
+
184
+ assert_requested :post, "#{api_url(@client)}job/#{job_id}", :body => request, :headers => @headers, :times => 1
185
+
186
+ assert_equal job.id, job_id
187
+ assert_equal job.operation, 'upsert'
188
+ assert_equal job.sobject, 'VideoEvent__c'
189
+ assert_equal job.created_by, '005E00000017spfIAA'
190
+ assert_equal job.created_at, DateTime.parse('2012-05-30T00:16:04.000Z')
191
+ assert_equal job.completed_at, DateTime.parse('2012-05-30T00:16:04.000Z')
192
+ assert_equal job.state, 'Aborted'
193
+ assert_equal job.external_id_field_name, 'Id__c'
194
+ assert_equal job.concurrency_mode, 'Parallel'
195
+ assert_equal job.content_type, 'CSV'
196
+ assert_equal job.queued_batches, 0
197
+ assert_equal job.in_progress_batches, 0
198
+ assert_equal job.completed_batches, 0
199
+ assert_equal job.failed_batches, 0
200
+ assert_equal job.total_batches, 0
201
+ assert_equal job.processed_records, 0
202
+ assert_equal job.failed_records, 0
203
+ assert_equal job.retries, 0
204
+ assert_equal job.api_active_processing_time, 0
205
+ assert_equal job.apex_processing_time, 0
206
+ assert_equal job.total_processing_time, 0
207
+ assert_equal job.api_version, 24.0
208
+ end
209
+
210
+ test "should return job info" do
211
+ response = fixture("job_info_response.xml")
212
+ job_id = "750E00000004N1mIAE"
213
+
214
+ stub_request(:get, "#{api_url(@client)}job/#{job_id}")
215
+ .with(:body => '', :headers => @headers)
216
+ .to_return(:body => response, :status => 200)
217
+
218
+ job = @client.job_info(job_id)
219
+
220
+ assert_requested :get, "#{api_url(@client)}job/#{job_id}", :body => '', :headers => @headers, :times => 1
221
+
222
+ assert_equal job.id, job_id
223
+ assert_equal job.operation, 'upsert'
224
+ assert_equal job.sobject, 'VideoEvent__c'
225
+ assert_equal job.created_by, '005E00000017spfIAA'
226
+ assert_equal job.created_at, DateTime.parse('2012-05-30T04:08:30.000Z')
227
+ assert_equal job.completed_at, DateTime.parse('2012-05-30T04:08:30.000Z')
228
+ assert_equal job.state, 'Open'
229
+ assert_equal job.external_id_field_name, 'Id__c'
230
+ assert_equal job.concurrency_mode, 'Parallel'
231
+ assert_equal job.content_type, 'CSV'
232
+ assert_equal job.queued_batches, 0
233
+ assert_equal job.in_progress_batches, 0
234
+ assert_equal job.completed_batches, 0
235
+ assert_equal job.failed_batches, 0
236
+ assert_equal job.total_batches, 0
237
+ assert_equal job.processed_records, 0
238
+ assert_equal job.failed_records, 0
239
+ assert_equal job.retries, 0
240
+ assert_equal job.api_active_processing_time, 0
241
+ assert_equal job.apex_processing_time, 0
242
+ assert_equal job.total_processing_time, 0
243
+ assert_equal job.api_version, 24.0
244
+ end
245
+
246
+ test "should raise SalesforceError on invalid job" do
247
+ response = fixture("invalid_job_error.xml")
248
+
249
+ stub_request(:post, "#{api_url(@client)}job").to_return(:body => response, :status => 500)
250
+
251
+ assert_raise SalesforceBulk::SalesforceError do
252
+ job = @client.add_job(:upsert, :SomeNonExistingObject__c, :external_id_field_name => :Id__c)
253
+ end
254
+ end
255
+
256
+ end