td-client 0.8.85 → 0.9.0dev1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/lib/td/client.rb +8 -16
  3. data/lib/td/client/api.rb +46 -62
  4. data/lib/td/client/api/bulk_import.rb +2 -1
  5. data/lib/td/client/api/bulk_load.rb +3 -3
  6. data/lib/td/client/api/export.rb +0 -12
  7. data/lib/td/client/api/import.rb +2 -3
  8. data/lib/td/client/api/job.rb +71 -145
  9. data/lib/td/client/api/schedule.rb +1 -1
  10. data/lib/td/client/api_error.rb +0 -5
  11. data/lib/td/client/model.rb +28 -91
  12. data/lib/td/client/version.rb +1 -1
  13. data/spec/spec_helper.rb +5 -5
  14. data/spec/td/client/account_api_spec.rb +5 -5
  15. data/spec/td/client/api_spec.rb +51 -69
  16. data/spec/td/client/api_ssl_connection_spec.rb +1 -1
  17. data/spec/td/client/bulk_import_spec.rb +29 -28
  18. data/spec/td/client/bulk_load_spec.rb +35 -60
  19. data/spec/td/client/db_api_spec.rb +1 -1
  20. data/spec/td/client/export_api_spec.rb +1 -11
  21. data/spec/td/client/import_api_spec.rb +10 -85
  22. data/spec/td/client/job_api_spec.rb +61 -567
  23. data/spec/td/client/model_job_spec.rb +10 -27
  24. data/spec/td/client/model_schedule_spec.rb +2 -2
  25. data/spec/td/client/partial_delete_api_spec.rb +1 -1
  26. data/spec/td/client/result_api_spec.rb +3 -3
  27. data/spec/td/client/sched_api_spec.rb +4 -12
  28. data/spec/td/client/server_status_api_spec.rb +2 -2
  29. data/spec/td/client/spec_resources.rb +0 -1
  30. data/spec/td/client/table_api_spec.rb +14 -14
  31. data/spec/td/client/user_api_spec.rb +12 -12
  32. data/spec/td/client_sched_spec.rb +6 -31
  33. data/spec/td/client_spec.rb +0 -1
  34. metadata +97 -42
  35. data/spec/td/client/api_error_spec.rb +0 -77
  36. data/spec/td/client/model_schema_spec.rb +0 -134
@@ -8,47 +8,24 @@ describe 'Import API' do
8
8
  include_context 'common helper'
9
9
 
10
10
  let :api do
11
- API.new(nil, :endpoint => endpoint)
11
+ API.new(nil, :endpoint => 'https://api.treasuredata.com')
12
12
  end
13
13
 
14
14
  let :api_old do
15
- API.new(nil, :endpoint => endpoint_old)
15
+ API.new(nil, :endpoint => 'http://api.treasure-data.com')
16
16
  end
17
17
 
18
- let :api_default do
19
- API.new(nil)
20
- end
21
-
22
- let :api_default_http do
23
- API.new(nil, :ssl => false)
24
- end
25
-
26
- let :api_unknown_host do
27
- API.new(nil, :endpoint => endpoint_unknown)
28
- end
29
-
30
- let :api_unknown_host_http do
31
- API.new(nil, :endpoint => endpoint_unknown, :ssl => false)
32
- end
33
-
34
- let(:endpoint) { 'api.treasuredata.com' }
35
- let(:endpoint_old) { TreasureData::API::OLD_ENDPOINT }
36
- let(:endpoint_unknown) { "example.com" }
37
- let(:endpoint_import) { "api-import.treasuredata.com" }
38
- let(:endpoint_import_old) { "api-import.treasure-data.com" }
39
- let(:endpoint_import_unknown) { endpoint_unknown }
40
-
41
18
  describe 'import' do
42
19
  it 'runs with unique_id' do
43
20
  t = Tempfile.new('import_api_spec')
44
21
  File.open(t.path, 'w') do |f|
45
22
  f << '12345'
46
23
  end
47
- stub_request(:put, "https://#{endpoint_import}/v3/table/import_with_id/db/table/unique_id/format").
24
+ stub_request(:put, "https://api-import.treasuredata.com/v3/table/import_with_id/db/table/unique_id/format").
48
25
  with(:body => '12345').
49
26
  to_return(:body => '{"elapsed_time":"1.23"}')
50
27
  File.open(t.path) do |f|
51
- expect(api.import('db', 'table', 'format', f, 5, 'unique_id')).to eq(1.23)
28
+ api.import('db', 'table', 'format', f, 5, 'unique_id').should == 1.23
52
29
  end
53
30
  end
54
31
 
@@ -57,76 +34,24 @@ describe 'Import API' do
57
34
  File.open(t.path, 'w') do |f|
58
35
  f << '12345'
59
36
  end
60
- stub_request(:put, "https://#{endpoint_import}/v3/table/import/db/table/format").
61
- with(:body => '12345').
62
- to_return(:body => '{"elapsed_time":"1.23"}')
63
- File.open(t.path) do |f|
64
- expect(api.import('db', 'table', 'format', f, 5)).to eq(1.23)
65
- end
66
- end
67
-
68
- it 'runs for old endpoint (force "http" instead of "https" for compatibility)' do
69
- t = Tempfile.new('import_api_spec')
70
- File.open(t.path, 'w') do |f|
71
- f << '12345'
72
- end
73
- stub_request(:put, "http://#{endpoint_import_old}/v3/table/import/db/table/format").
74
- with(:body => '12345').
75
- to_return(:body => '{"elapsed_time":"1.23"}')
76
- File.open(t.path) do |f|
77
- expect(api_old.import('db', 'table', 'format', f, 5)).to eq(1.23)
78
- end
79
- end
80
-
81
- it 'runs for no endpoint specified (default behavior)' do
82
- t = Tempfile.new('import_api_spec')
83
- File.open(t.path, 'w') do |f|
84
- f << '12345'
85
- end
86
- stub_request(:put, "https://#{endpoint_import}/v3/table/import/db/table/format").
87
- with(:body => '12345').
88
- to_return(:body => '{"elapsed_time":"1.23"}')
89
- File.open(t.path) do |f|
90
- expect(api_default.import('db', 'table', 'format', f, 5)).to eq 1.23
91
- end
92
- end
93
-
94
- it 'runs for no endpoint specified with ssl: false' do
95
- t = Tempfile.new('import_api_spec')
96
- File.open(t.path, 'w') do |f|
97
- f << '12345'
98
- end
99
- stub_request(:put, "http://#{endpoint_import}/v3/table/import/db/table/format").
100
- with(:body => '12345').
101
- to_return(:body => '{"elapsed_time":"1.23"}')
102
- File.open(t.path) do |f|
103
- expect(api_default_http.import('db', 'table', 'format', f, 5)).to eq 1.23
104
- end
105
- end
106
-
107
- it 'runs for unknown endpoint specified' do
108
- t = Tempfile.new('import_api_spec')
109
- File.open(t.path, 'w') do |f|
110
- f << '12345'
111
- end
112
- stub_request(:put, "https://#{endpoint_unknown}/v3/table/import/db/table/format").
37
+ stub_request(:put, "https://api-import.treasuredata.com/v3/table/import/db/table/format").
113
38
  with(:body => '12345').
114
39
  to_return(:body => '{"elapsed_time":"1.23"}')
115
40
  File.open(t.path) do |f|
116
- expect(api_unknown_host.import('db', 'table', 'format', f, 5)).to eq 1.23
41
+ api.import('db', 'table', 'format', f, 5).should == 1.23
117
42
  end
118
43
  end
119
44
 
120
- it 'runs for unknown endpoint with ssl=false specified' do
45
+ it 'runs for old endpoint' do
121
46
  t = Tempfile.new('import_api_spec')
122
47
  File.open(t.path, 'w') do |f|
123
48
  f << '12345'
124
49
  end
125
- stub_request(:put, "http://#{endpoint_unknown}/v3/table/import/db/table/format").
50
+ stub_request(:put, "http://api-import.treasure-data.com/v3/table/import/db/table/format").
126
51
  with(:body => '12345').
127
52
  to_return(:body => '{"elapsed_time":"1.23"}')
128
53
  File.open(t.path) do |f|
129
- expect(api_unknown_host_http.import('db', 'table', 'format', f, 5)).to eq 1.23
54
+ api_old.import('db', 'table', 'format', f, 5).should == 1.23
130
55
  end
131
56
  end
132
57
 
@@ -135,7 +60,7 @@ describe 'Import API' do
135
60
  File.open(t.path, 'w') do |f|
136
61
  f << '12345'
137
62
  end
138
- stub_request(:put, "https://#{endpoint_import}/v3/table/import/db/table/format").
63
+ stub_request(:put, "https://api-import.treasuredata.com/v3/table/import/db/table/format").
139
64
  with(:body => '12345').
140
65
  to_return(:status => 500)
141
66
  File.open(t.path) do |f|
@@ -20,7 +20,7 @@ describe 'Job API' do
20
20
  it 'should returns 20 jobs by default' do
21
21
  stub_api_request(:get, "/v3/job/list", :query => {'from' => '0'}).to_return(:body => {'jobs' => raw_jobs}.to_json)
22
22
  jobs = api.list_jobs
23
- expect(jobs.size).to eq(20)
23
+ jobs.size.should == 20
24
24
  end
25
25
 
26
26
  (0...MAX_JOB).each {|i|
@@ -31,22 +31,21 @@ describe 'Job API' do
31
31
  jobs = api.list_jobs
32
32
  jobs[i..i].map {|job_id, type, status, query, start_at, end_at, cpu_time,
33
33
  result_size, result_url, priority, retry_limit, org, db,
34
- duration, num_records|
35
- expect(job_id).to eq(job['job_id'])
36
- expect(type).to eq(job['type'])
37
- expect(status).to eq(job['status'])
38
- expect(query).to eq(job['query'])
39
- expect(start_at).to eq(job['start_at'])
40
- expect(end_at).to eq(job['end_at'])
41
- expect(cpu_time).to eq(job['cpu_time'])
42
- expect(result_size).to eq(job['result_size'])
43
- expect(result_url).to eq(job['result_url'])
44
- expect(priority).to eq(job['priority'])
45
- expect(retry_limit).to eq(job['retry_limit'])
46
- expect(org).to eq(job['organization'])
47
- expect(db).to eq(job['database'])
48
- expect(duration).to eq(job['duration'])
49
- expect(num_records).to eq(job['num_records'])
34
+ duration|
35
+ job_id.should == job['job_id']
36
+ type.should == job['type']
37
+ status.should == job['status']
38
+ query.should == job['query']
39
+ start_at.should == job['start_at']
40
+ end_at.should == job['end_at']
41
+ cpu_time.should == job['cpu_time']
42
+ result_size.should == job['result_size']
43
+ result_url.should == job['result_url']
44
+ priority.should == job['priority']
45
+ retry_limit.should == job['retry_limit']
46
+ org.should == job['organization']
47
+ db.should == job['database']
48
+ duration.should == job['duration']
50
49
  }
51
50
  end
52
51
  }
@@ -54,14 +53,14 @@ describe 'Job API' do
54
53
  it 'should returns 10 jobs with to parameter' do
55
54
  stub_api_request(:get, "/v3/job/list", :query => {'from' => '0', 'to' => '10'}).to_return(:body => {'jobs' => raw_jobs[0...10]}.to_json)
56
55
  jobs = api.list_jobs(0, 10)
57
- expect(jobs.size).to eq(10)
56
+ jobs.size.should == 10
58
57
  end
59
58
 
60
59
  it 'should returns 10 jobs with to status parameter' do
61
60
  error_jobs = raw_jobs.select { |j| j['status'] == 'error' }
62
61
  stub_api_request(:get, "/v3/job/list", :query => {'from' => '0', 'status' => 'error'}).to_return(:body => {'jobs' => error_jobs}.to_json)
63
62
  jobs = api.list_jobs(0, nil, 'error')
64
- expect(jobs.size).to eq(error_jobs.size)
63
+ jobs.size.should == error_jobs.size
65
64
  end
66
65
 
67
66
  #it 'should contain the result_size field' do
@@ -75,23 +74,22 @@ describe 'Job API' do
75
74
  stub_api_request(:get, "/v3/job/show/#{e(i)}").to_return(:body => job.to_json)
76
75
 
77
76
  type, query, status, url, debug, start_at, end_at, cpu_time,
78
- result_size, result_url, hive_result_schema, priority, retry_limit, org, db, num_records = api.show_job(i)
79
- expect(type).to eq(job['type'])
80
- expect(query).to eq(job['query'])
81
- expect(status).to eq(job['status'])
82
- expect(url).to eq(job['url'])
83
- expect(debug).to eq(job['debug'])
84
- expect(start_at).to eq(job['start_at'])
85
- expect(end_at).to eq(job['end_at'])
86
- expect(cpu_time).to eq(job['cpu_time'])
87
- expect(result_size).to eq(job['result_size'])
88
- expect(result_url).to eq(job['result_url'])
89
- expect(hive_result_schema).to eq(job['hive_result_schema'])
90
- expect(result_url).to eq(job['result_url'])
91
- expect(priority).to eq(job['priority'])
92
- expect(org).to eq(job['organization'])
93
- expect(db).to eq(job['database'])
94
- expect(num_records).to eq(job['num_records'])
77
+ result_size, result_url, hive_result_schema, priority, retry_limit, org, db = api.show_job(i)
78
+ type.should == job['type']
79
+ query.should == job['query']
80
+ status.should == job['status']
81
+ url.should == job['url']
82
+ debug.should == job['debug']
83
+ start_at.should == job['start_at']
84
+ end_at.should == job['end_at']
85
+ cpu_time.should == job['cpu_time']
86
+ result_size.should == job['result_size']
87
+ result_url.should == job['result_url']
88
+ hive_result_schema.should == job['hive_result_schema']
89
+ result_url.should == job['result_url']
90
+ priority.should == job['priority']
91
+ org.should == job['organization']
92
+ db.should == job['database']
95
93
  end
96
94
  }
97
95
 
@@ -131,7 +129,7 @@ describe 'Job API' do
131
129
  stub_api_request(:get, "/v3/job/status/#{e(job_id)}").to_return(:body => result_job.to_json)
132
130
 
133
131
  status = api.job_status(job_id)
134
- expect(status).to eq(i.odd? ? 'success' : 'error')
132
+ status.should == (i.odd? ? 'success' : 'error')
135
133
  end
136
134
  }
137
135
  end
@@ -146,7 +144,7 @@ describe 'Job API' do
146
144
  stub_api_request(:post, "/v3/job/issue/hive/#{e(db_name)}").with(:body => params).to_return(return_body)
147
145
 
148
146
  job_id = api.hive_query(query, db_name)
149
- expect(job_id).to eq('1')
147
+ job_id.should == '1'
150
148
  end
151
149
 
152
150
  it 'issue a query with result_url' do
@@ -154,7 +152,7 @@ describe 'Job API' do
154
152
  stub_api_request(:post, "/v3/job/issue/hive/#{e(db_name)}").with(:body => params).to_return(return_body)
155
153
 
156
154
  job_id = api.hive_query(query, db_name, 'td://@/test/table')
157
- expect(job_id).to eq('1')
155
+ job_id.should == '1'
158
156
  end
159
157
 
160
158
  it 'issue a query with priority' do
@@ -162,13 +160,13 @@ describe 'Job API' do
162
160
  stub_api_request(:post, "/v3/job/issue/hive/#{e(db_name)}").with(:body => params).to_return(return_body)
163
161
 
164
162
  job_id = api.hive_query(query, db_name, nil, 1)
165
- expect(job_id).to eq('1')
163
+ job_id.should == '1'
166
164
  end
167
165
  end
168
166
 
169
167
  describe 'job_result' do
170
168
  let :packed do
171
- s = StringIO.new(String.new)
169
+ s = StringIO.new
172
170
  pk = MessagePack::Packer.new(s)
173
171
  pk.write('hello')
174
172
  pk.write('world')
@@ -180,167 +178,13 @@ describe 'Job API' do
180
178
  stub_api_request(:get, '/v3/job/result/12345').
181
179
  with(:query => {'format' => 'msgpack'}).
182
180
  to_return(:body => packed)
183
- expect(api.job_result(12345)).to eq(['hello', 'world'])
181
+ api.job_result(12345).should == ['hello', 'world']
184
182
  end
185
-
186
- it '200->200 cannot resume' do
187
- sz = packed.bytesize / 3
188
- stub_api_request(:get, '/v3/job/result/12345').
189
- with(:query => {'format' => 'msgpack'}).
190
- to_return(
191
- :headers => {
192
- 'Content-Length' => packed.bytesize,
193
- 'Etag' => '"abcdefghijklmn"',
194
- },
195
- :body => packed[0, sz]
196
- )
197
- stub_api_request(:get, '/v3/job/result/12345').
198
- with(
199
- :headers => {
200
- 'If-Range' => '"abcdefghijklmn"',
201
- 'Range' => "bytes=#{sz}-",
202
- },
203
- :query => {'format' => 'msgpack'}
204
- ).
205
- to_return(
206
- :status => 200,
207
- :headers => {
208
- 'Content-Length' => packed.bytesize - sz,
209
- 'Content-Range' => "bytes #{sz}-#{packed.bytesize-1}/#{packed.bytesize}",
210
- 'Etag' => '"abcdefghijklmn"',
211
- },
212
- :body => packed
213
- )
214
- expect(api).to receive(:sleep).once
215
- expect($stderr).to receive(:print)
216
- expect($stderr).to receive(:puts)
217
- expect{api.job_result(12345)}.to raise_error(TreasureData::APIError)
218
- end
219
-
220
- it '200->403 cannot resume' do
221
- sz = packed.bytesize / 3
222
- stub_api_request(:get, '/v3/job/result/12345').
223
- with(:query => {'format' => 'msgpack'}).
224
- to_return(
225
- :headers => {
226
- 'Content-Length' => packed.bytesize,
227
- 'Etag' => '"abcdefghijklmn"',
228
- },
229
- :body => packed[0, sz]
230
- )
231
- stub_api_request(:get, '/v3/job/result/12345').
232
- with(
233
- :headers => {
234
- 'If-Range' => '"abcdefghijklmn"',
235
- 'Range' => "bytes=#{sz}-",
236
- },
237
- :query => {'format' => 'msgpack'}
238
- ).
239
- to_return(
240
- :status => 403,
241
- :headers => {
242
- 'Content-Length' => packed.bytesize - sz,
243
- 'Content-Range' => "bytes #{sz}-#{packed.bytesize-1}/#{packed.bytesize}",
244
- 'Etag' => '"abcdefghijklmn"',
245
- },
246
- :body => packed
247
- )
248
- expect(api).to receive(:sleep).once
249
- expect($stderr).to receive(:print)
250
- expect($stderr).to receive(:puts)
251
- expect{api.job_result(12345)}.to raise_error(TreasureData::APIError)
252
- end
253
-
254
- it 'can resume' do
255
- sz = packed.bytesize / 3
256
- stub_api_request(:get, '/v3/job/result/12345').
257
- with(:query => {'format' => 'msgpack'}).
258
- to_return(
259
- :headers => {
260
- 'Content-Length' => packed.bytesize,
261
- 'Etag' => '"abcdefghijklmn"',
262
- },
263
- :body => packed[0, sz]
264
- )
265
- stub_api_request(:get, '/v3/job/result/12345').
266
- with(
267
- :headers => {
268
- 'If-Range' => '"abcdefghijklmn"',
269
- 'Range' => "bytes=#{sz}-",
270
- },
271
- :query => {'format' => 'msgpack'}
272
- ).
273
- to_return(
274
- :status => 206,
275
- :headers => {
276
- 'Content-Length' => packed.bytesize - sz,
277
- 'Content-Range' => "bytes #{sz}-#{packed.bytesize-1}/#{packed.bytesize}",
278
- 'Etag' => '"abcdefghijklmn"',
279
- },
280
- :body => packed[sz, packed.bytesize - sz]
281
- )
282
- expect(api).to receive(:sleep).once
283
- expect($stderr).to receive(:print)
284
- expect($stderr).to receive(:puts)
285
- expect(api.job_result(12345)).to eq ['hello', 'world']
286
- end
287
-
288
- it '200->500->206 can resume' do
289
- sz = packed.bytesize / 3
290
- stub_api_request(:get, '/v3/job/result/12345').
291
- with(:query => {'format' => 'msgpack'}).
292
- to_return(
293
- :headers => {
294
- 'Content-Length' => packed.bytesize,
295
- 'Etag' => '"abcdefghijklmn"',
296
- },
297
- :body => packed[0, sz]
298
- )
299
- stub_api_request(:get, '/v3/job/result/12345').
300
- with(
301
- :headers => {
302
- 'If-Range' => '"abcdefghijklmn"',
303
- 'Range' => "bytes=#{sz}-",
304
- },
305
- :query => {'format' => 'msgpack'}
306
- ).
307
- to_return(
308
- :status => 500,
309
- :headers => {
310
- 'Content-Length' => packed.bytesize - sz,
311
- 'Content-Range' => "bytes #{sz}-#{packed.bytesize-1}/#{packed.bytesize}",
312
- 'Etag' => '"abcdefghijklmn"',
313
- },
314
- :body => packed
315
- )
316
- stub_api_request(:get, '/v3/job/result/12345').
317
- with(
318
- :headers => {
319
- 'If-Range' => '"abcdefghijklmn"',
320
- 'Range' => "bytes=#{sz}-",
321
- },
322
- :query => {'format' => 'msgpack'}
323
- ).
324
- to_return(
325
- :status => 206,
326
- :headers => {
327
- 'Content-Length' => packed.bytesize - sz,
328
- 'Content-Range' => "bytes #{sz}-#{packed.bytesize-1}/#{packed.bytesize}",
329
- 'Etag' => '"abcdefghijklmn"',
330
- },
331
- :body => packed[sz, packed.bytesize - sz]
332
- )
333
- expect(api).to receive(:sleep).once
334
- expect($stderr).to receive(:print)
335
- expect($stderr).to receive(:puts)
336
- expect(api.job_result(12345)).to eq ['hello', 'world']
337
- end
338
-
339
183
  end
340
184
 
341
185
  describe 'job_result_format' do
342
186
  let :packed do
343
- s = StringIO.new(String.new)
187
+ s = StringIO.new
344
188
  Zlib::GzipWriter.wrap(s) do |f|
345
189
  f << ['hello', 'world'].to_json
346
190
  end
@@ -359,8 +203,8 @@ describe 'Job API' do
359
203
  total_size = 0
360
204
  api.job_result_format(12345, 'json', io) {|size| total_size += size }
361
205
 
362
- expect(io.string).to eq(json)
363
- expect(total_size).to eq(json.size)
206
+ io.string.should == json
207
+ total_size.should == json.size
364
208
  end
365
209
  end
366
210
 
@@ -372,7 +216,7 @@ describe 'Job API' do
372
216
  :headers => {'Content-Encoding' => 'gzip'},
373
217
  :body => packed
374
218
  )
375
- expect(api.job_result_format(12345, 'json')).to eq(['hello', 'world'].to_json)
219
+ api.job_result_format(12345, 'json').should == ['hello', 'world'].to_json
376
220
  end
377
221
 
378
222
  it 'writes formatted job result' do
@@ -384,66 +228,24 @@ describe 'Job API' do
384
228
  )
385
229
  s = StringIO.new
386
230
  api.job_result_format(12345, 'json', s)
387
- expect(s.string).to eq(['hello', 'world'].to_json)
388
- end
389
-
390
- context 'can resume' do
391
- before do
392
- sz = packed.bytesize / 3
393
- stub_api_request(:get, '/v3/job/result/12345').
394
- with(:query => {'format' => 'json'}).
395
- to_return(
396
- :headers => {
397
- 'Content-Encoding' => 'gzip',
398
- 'Content-Length' => packed.bytesize,
399
- 'Etag' => '"abcdefghijklmn"',
400
- },
401
- :body => packed[0, sz]
402
- )
403
- stub_api_request(:get, '/v3/job/result/12345').
404
- with(
405
- :headers => {
406
- 'If-Range' => '"abcdefghijklmn"',
407
- 'Range' => "bytes=#{sz}-",
408
- },
409
- :query => {'format' => 'json'}
410
- ).
411
- to_return(
412
- :status => 206,
413
- :headers => {
414
- 'Content-Encoding' => 'gzip',
415
- 'Content-Length' => packed.bytesize-sz,
416
- 'Content-Range' => "bytes #{sz}-#{packed.bytesize-1}/#{packed.bytesize}",
417
- 'Etag' => '"abcdefghijklmn"',
418
- },
419
- :body => packed[sz, packed.bytesize-sz]
420
- )
421
- expect(api).to receive(:sleep).once
422
- expect($stderr).to receive(:print)
423
- expect($stderr).to receive(:puts)
424
- end
425
- it 'can work with io' do
426
- s = StringIO.new
427
- api.job_result_format(12345, 'json', s)
428
- expect(s.string).to eq ['hello', 'world'].to_json
429
- end
430
- it 'can work without block' do
431
- expect(api.job_result_format(12345, 'json')).to eq ['hello', 'world'].to_json
432
- end
231
+ s.string.should == ['hello', 'world'].to_json
433
232
  end
434
233
  end
435
234
  end
436
235
 
437
236
  describe 'job_result_each' do
438
237
  let :packed do
439
- s = StringIO.new(String.new)
440
- Zlib::GzipWriter.wrap(s) do |f|
441
- pk = MessagePack::Packer.new(f)
442
- pk.write('hello')
443
- pk.write('world')
444
- pk.flush
238
+ s = StringIO.new
239
+ pk = MessagePack::Packer.new(s)
240
+ pk.write('hello')
241
+ pk.write('world')
242
+ pk.flush
243
+ s.rewind
244
+ out = StringIO.new
245
+ Zlib::GzipWriter.wrap(out) do |f|
246
+ f.write s.read
445
247
  end
446
- s.string
248
+ out.string
447
249
  end
448
250
 
449
251
  it 'yields job result for each row' do
@@ -457,46 +259,7 @@ describe 'Job API' do
457
259
  api.job_result_each(12345) do |row|
458
260
  result << row
459
261
  end
460
- expect(result).to eq(['hello', 'world'])
461
- end
462
-
463
- it 'can resume' do
464
- sz= packed.bytesize / 3
465
- stub_api_request(:get, '/v3/job/result/12345').
466
- with(:query => {'format' => 'msgpack'}).
467
- to_return(
468
- :headers => {
469
- 'Content-Encoding' => 'gzip',
470
- 'Content-Length' => packed.bytesize,
471
- 'Etag' => '"abcdefghijklmn"',
472
- },
473
- :body => packed[0, sz]
474
- )
475
- stub_api_request(:get, '/v3/job/result/12345').
476
- with(
477
- :headers => {
478
- 'If-Range' => '"abcdefghijklmn"',
479
- 'Range' => "bytes=#{sz}-",
480
- },
481
- :query => {'format' => 'msgpack'}
482
- ).
483
- to_return(
484
- :status => 206,
485
- :headers => {
486
- 'Content-Length' => packed.bytesize-sz,
487
- 'Content-Range' => "bytes #{sz}-#{packed.bytesize-1}/#{packed.bytesize}",
488
- 'Etag' => '"abcdefghijklmn"',
489
- },
490
- :body => packed[sz, packed.bytesize-sz]
491
- )
492
- expect(api).to receive(:sleep).once
493
- expect($stderr).to receive(:print)
494
- expect($stderr).to receive(:puts)
495
- result = []
496
- api.job_result_each(12345) do |row|
497
- result << row
498
- end
499
- expect(result).to eq ['hello', 'world']
262
+ result.should == ['hello', 'world']
500
263
  end
501
264
  end
502
265
 
@@ -511,7 +274,7 @@ describe 'Job API' do
511
274
  # pk.flush
512
275
  # end
513
276
  # s.string
514
- "\x1F\x8B\b\x00#\xA1\x93T\x00\x03[\x9A\x91\x9A\x93\x93\xBF\xB4<\xBF('\x05\x00e 0\xB3\f\x00\x00\x00".force_encoding(Encoding::ASCII_8BIT)
277
+ "\u001F\x8B\b\u0000#\xA1\x93T\u0000\u0003[\x9A\x91\x9A\x93\x93\xBF\xB4<\xBF('\u0005\u0000e 0\xB3\f\u0000\u0000\u0000"
515
278
  end
516
279
 
517
280
  it 'yields job result for each row with progress' do
@@ -525,46 +288,7 @@ describe 'Job API' do
525
288
  api.job_result_each_with_compr_size(12345) do |row, size|
526
289
  result << [row, size]
527
290
  end
528
- expect(result).to eq([['hello', 32], ['world', 32]])
529
- end
530
-
531
- it 'can resume' do
532
- sz = packed.bytesize / 3
533
- stub_api_request(:get, '/v3/job/result/12345').
534
- with(:query => {'format' => 'msgpack'}).
535
- to_return(
536
- :headers => {
537
- 'Content-Encoding' => 'gzip',
538
- 'Content-Length' => packed.bytesize,
539
- 'Etag' => '"abcdefghijklmn"',
540
- },
541
- :body => packed[0, sz]
542
- )
543
- stub_api_request(:get, '/v3/job/result/12345').
544
- with(
545
- :headers => {
546
- 'If-Range' => '"abcdefghijklmn"',
547
- 'Range' => "bytes=#{sz}-",
548
- },
549
- :query => {'format' => 'msgpack'}
550
- ).
551
- to_return(
552
- :status => 206,
553
- :headers => {
554
- 'Content-Length' => packed.bytesize - sz,
555
- 'Content-Range' => "bytes #{sz}-#{packed.bytesize-1}/#{packed.bytesize}",
556
- 'Etag' => '"abcdefghijklmn"',
557
- },
558
- :body => packed[sz, packed.bytesize - sz]
559
- )
560
- expect(api).to receive(:sleep).once
561
- expect($stderr).to receive(:print)
562
- expect($stderr).to receive(:puts)
563
- result = []
564
- api.job_result_each_with_compr_size(12345) do |row, size|
565
- result << [row, size]
566
- end
567
- expect(result).to eq [['hello', 32], ['world', 32]]
291
+ result.should == [['hello', 32], ['world', 32]]
568
292
  end
569
293
  end
570
294
 
@@ -578,7 +302,7 @@ describe 'Job API' do
578
302
  to_return(:body => 'raw binary')
579
303
  api.job_result_raw(12345, 'json', io)
580
304
 
581
- expect(io.string).to eq('raw binary')
305
+ io.string.should == 'raw binary'
582
306
  end
583
307
  end
584
308
 
@@ -587,246 +311,16 @@ describe 'Job API' do
587
311
  stub_api_request(:get, '/v3/job/result/12345').
588
312
  with(:query => {'format' => 'json'}).
589
313
  to_return(:body => 'raw binary')
590
- expect(api.job_result_raw(12345, 'json')).to eq('raw binary')
314
+ api.job_result_raw(12345, 'json').should == 'raw binary'
591
315
  end
592
316
  end
593
-
594
- let :packed do
595
- # Hard code fixture data to make the size stable
596
- # s = StringIO.new
597
- # Zlib::GzipWriter.wrap(s) do |f|
598
- # pk = MessagePack::Packer.new(f)
599
- # pk.write('hello')
600
- # pk.write('world')
601
- # pk.flush
602
- # end
603
- # s.string
604
- "\x1F\x8B\b\x00#\xA1\x93T\x00\x03[\x9A\x91\x9A\x93\x93\xBF\xB4<\xBF('\x05\x00e 0\xB3\f\x00\x00\x00".force_encoding(Encoding::ASCII_8BIT)
605
- end
606
-
607
- it 'can resume' do
608
- sz = packed.bytesize / 3
609
- stub_api_request(:get, '/v3/job/result/12345').
610
- with(:query => {'format' => 'msgpack.gz'}).
611
- to_return(
612
- :headers => {
613
- 'Content-Length' => packed.bytesize,
614
- 'Etag' => '"abcdefghijklmn"',
615
- },
616
- :body => packed[0, sz]
617
- )
618
- stub_api_request(:get, '/v3/job/result/12345').
619
- with(
620
- :headers => {
621
- 'If-Range' => '"abcdefghijklmn"',
622
- 'Range' => "bytes=#{sz}-",
623
- },
624
- :query => {'format' => 'msgpack.gz'}
625
- ).
626
- to_return(
627
- :status => 206,
628
- :headers => {
629
- 'Content-Length' => packed.bytesize - sz,
630
- 'Content-Range' => "bytes #{sz}-#{packed.bytesize-1}/#{packed.bytesize}",
631
- 'Etag' => '"abcdefghijklmn"',
632
- },
633
- :body => packed[sz, packed.bytesize - sz]
634
- )
635
- expect(api).to receive(:sleep).once
636
- expect($stderr).to receive(:print)
637
- expect($stderr).to receive(:puts)
638
- sio = StringIO.new(String.new)
639
- api.job_result_raw(12345, 'msgpack.gz', sio)
640
- expect(sio.string).to eq(packed)
641
- end
642
317
  end
643
318
 
644
319
  describe 'kill' do
645
320
  it 'kills a job' do
646
321
  stub_api_request(:post, '/v3/job/kill/12345').
647
322
  to_return(:body => {'former_status' => 'status'}.to_json)
648
- expect(api.kill(12345)).to eq('status')
649
- end
650
- end
651
-
652
- describe 'job_result_download' do
653
- let (:data){ [[1, 'hello', nil], [2, 'world', true], [3, '!', false]] }
654
- let :formatted do
655
- case format
656
- when 'json'
657
- data.map{|a| JSON(a) }.join("\n")
658
- when 'msgpack'
659
- pk = MessagePack::Packer.new
660
- data.each{|x| pk.write(x) }
661
- pk.to_str
662
- else
663
- raise
664
- end
665
- end
666
- let :gziped do
667
- s = StringIO.new(String.new)
668
- Zlib::GzipWriter.wrap(s) do |f|
669
- f.write formatted
670
- end
671
- s.string
672
- end
673
- let :deflated do
674
- Zlib::Deflate.deflate(formatted)
675
- end
676
- subject do
677
- str = ''
678
- api.__send__(:job_result_download, 12345, format){|x| str << x }
679
- str
680
- end
681
- context '200' do
682
- before do
683
- sz = packed.bytesize / 3
684
- stub_api_request(:get, '/v3/job/result/12345').
685
- with(:query => {'format' => format}).
686
- to_return(
687
- :headers => {
688
- 'Content-Encoding' => content_encoding,
689
- 'Content-Length' => packed.bytesize,
690
- 'Etag' => '"abcdefghijklmn"',
691
- },
692
- :body => packed
693
- )
694
- expect(api).not_to receive(:sleep)
695
- expect($stderr).not_to receive(:print)
696
- expect($stderr).not_to receive(:puts)
697
- end
698
- context 'Content-Encoding: gzip' do
699
- let (:content_encoding){ 'gzip' }
700
- let (:packed){ gziped }
701
- context 'msgpack' do
702
- let (:format){ 'msgpack' }
703
- it { is_expected.to eq formatted }
704
- end
705
- context 'json' do
706
- let (:format){ 'json' }
707
- it { is_expected.to eq formatted }
708
- end
709
- end
710
- context 'Content-Encoding: deflate' do
711
- let (:content_encoding){ 'deflate' }
712
- let (:packed){ deflated }
713
- context 'msgpack' do
714
- let (:format){ 'msgpack' }
715
- it { is_expected.to eq formatted }
716
- end
717
- context 'json' do
718
- let (:format){ 'json' }
719
- it { is_expected.to eq formatted }
720
- end
721
- end
722
- end
723
-
724
- context '200 -> 206' do
725
- before do
726
- sz = packed.bytesize / 3
727
- stub_api_request(:get, '/v3/job/result/12345').
728
- with(:query => {'format' => format}).
729
- to_return(
730
- :headers => {
731
- 'Content-Encoding' => content_encoding,
732
- 'Content-Length' => packed.bytesize,
733
- 'Etag' => '"abcdefghijklmn"',
734
- },
735
- :body => packed[0, sz]
736
- )
737
- stub_api_request(:get, '/v3/job/result/12345').
738
- with(
739
- :headers => {
740
- 'If-Range' => '"abcdefghijklmn"',
741
- 'Range' => "bytes=#{sz}-",
742
- },
743
- :query => {'format' => format}
744
- ).
745
- to_return(
746
- :status => 206,
747
- :headers => {
748
- 'Content-Encoding' => content_encoding,
749
- 'Content-Length' => packed.bytesize - sz,
750
- 'Content-Range' => "bytes #{sz}-#{packed.bytesize-1}/#{packed.bytesize}",
751
- 'Etag' => '"abcdefghijklmn"',
752
- },
753
- :body => packed[sz, packed.bytesize - sz]
754
- )
755
- expect(api).to receive(:sleep).once
756
- allow($stderr).to receive(:print)
757
- allow($stderr).to receive(:puts)
758
- end
759
- context 'Content-Encoding: gzip' do
760
- let (:content_encoding){ 'gzip' }
761
- let (:packed){ gziped }
762
- context 'msgpack' do
763
- let (:format){ 'msgpack' }
764
- it { is_expected.to eq formatted }
765
- end
766
- context 'json' do
767
- let (:format){ 'json' }
768
- it { is_expected.to eq formatted }
769
- end
770
- end
771
- context 'Content-Encoding: deflate' do
772
- let (:content_encoding){ 'deflate' }
773
- let (:packed){ deflated }
774
- context 'msgpack' do
775
- let (:format){ 'msgpack' }
776
- it { is_expected.to eq formatted }
777
- end
778
- context 'json' do
779
- let (:format){ 'json' }
780
- it { is_expected.to eq formatted }
781
- end
782
- end
783
- end
784
-
785
- context 'without autodecode' do
786
- before do
787
- sz = packed.bytesize / 3
788
- stub_api_request(:get, '/v3/job/result/12345').
789
- with(:query => {'format' => format}).
790
- to_return(
791
- :headers => {
792
- 'Content-Length' => packed.bytesize,
793
- 'Etag' => '"abcdefghijklmn"',
794
- },
795
- :body => packed
796
- )
797
- expect(api).not_to receive(:sleep)
798
- expect($stderr).not_to receive(:print)
799
- expect($stderr).not_to receive(:puts)
800
- end
801
- subject do
802
- str = ''
803
- api.__send__(:job_result_download, 12345, format, false){|x| str << x }
804
- str
805
- end
806
- context 'Content-Encoding: gzip' do
807
- let (:content_encoding){ 'gzip' }
808
- let (:packed){ gziped }
809
- context 'msgpack' do
810
- let (:format){ 'msgpack' }
811
- it { is_expected.to eq packed }
812
- end
813
- context 'json' do
814
- let (:format){ 'json' }
815
- it { is_expected.to eq packed }
816
- end
817
- end
818
- context 'Content-Encoding: deflate' do
819
- let (:content_encoding){ 'deflate' }
820
- let (:packed){ deflated }
821
- context 'msgpack' do
822
- let (:format){ 'msgpack' }
823
- it { is_expected.to eq packed }
824
- end
825
- context 'json' do
826
- let (:format){ 'json' }
827
- it { is_expected.to eq packed }
828
- end
829
- end
323
+ api.kill(12345).should == 'status'
830
324
  end
831
325
  end
832
326
  end