aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuc Donnet2017-09-06 09:11:59 +0200
committerLuc Donnet2017-09-06 09:11:59 +0200
commitbcea52e515f1eef544da5cfcbfb5e900c69b1081 (patch)
tree469460f07121fe0880f2eeed00596aca76ab06c3
parentbed9e47d020a43aff40dd31033554501b550aab0 (diff)
parent954fca7c0857f80b3398e9a843dcb689accb9817 (diff)
downloadchouette-core-bcea52e515f1eef544da5cfcbfb5e900c69b1081.tar.bz2
Merge branch 'master' of github.com:AF83/stif-boiv
-rw-r--r--app/workers/workbench_import_worker.rb2
-rw-r--r--spec/workers/workbench_import_worker_spec.rb81
2 files changed, 48 insertions, 35 deletions
diff --git a/app/workers/workbench_import_worker.rb b/app/workers/workbench_import_worker.rb
index 706c3fc63..37308571e 100644
--- a/app/workers/workbench_import_worker.rb
+++ b/app/workers/workbench_import_worker.rb
@@ -9,7 +9,7 @@ class WorkbenchImportWorker
def perform(import_id)
@workbench_import = WorkbenchImport.find(import_id)
@response = nil
- @workbench_import.update_attributes(status: 'running', started_at: Time.now)
+ @workbench_import.update(status: 'running', started_at: Time.now)
downloaded = download
zip_service = ZipService.new(downloaded)
upload zip_service
diff --git a/spec/workers/workbench_import_worker_spec.rb b/spec/workers/workbench_import_worker_spec.rb
index be07e301a..a349b3433 100644
--- a/spec/workers/workbench_import_worker_spec.rb
+++ b/spec/workers/workbench_import_worker_spec.rb
@@ -1,8 +1,4 @@
-RSpec.describe WorkbenchImportWorker,
- type: [:worker, :request],
- skip: "ZipService has been refactored and RetryService was removed. These
- tests need to be changed to reflect the new state of the code. Skipping
- them because imports need to be ready for QA testing within a day." do
+RSpec.describe WorkbenchImportWorker, type: [:worker, :request] do
let( :worker ) { described_class.new }
let( :import ){ build_stubbed :import, token_download: download_token, file: zip_file }
@@ -10,11 +6,6 @@ RSpec.describe WorkbenchImportWorker,
let( :workbench ){ import.workbench }
let( :referential ){ import.referential }
let( :api_key ){ build_stubbed :api_key, referential: referential, token: "#{referential.id}-#{SecureRandom.hex}" }
- let( :params ) do
- { netex_import:
- { referential_id: referential.id, workbench_id: workbench.id }
- }
- end
# http://www.example.com/workbenches/:workbench_id/imports/:id/download
let( :host ){ Rails.configuration.rails_host }
@@ -27,12 +18,12 @@ RSpec.describe WorkbenchImportWorker,
let( :upload_path ) { api_v1_netex_imports_path(format: :json) }
- let( :entry_group_streams ) do
- entry_count.times.map{ |i| double( "entry group stream #{i}" ) }
- end
- let( :entry_groups ) do
- entry_count.times.map do | i |
- {"entry_group_name#{i}" => entry_group_streams[i] }
+ let( :subdirs ) do
+ entry_count.times.map do |i|
+ ZipService::Subdir.new(
+ "subdir #{i}",
+ double("subdir #{i}", rewind: 0, read: '')
+ )
end
end
@@ -42,6 +33,8 @@ RSpec.describe WorkbenchImportWorker,
let( :post_response_ok ){ double(status: 201, body: "{}") }
before do
+ Timecop.freeze(Time.now)
+
# Silence Logger
allow_any_instance_of(Logger).to receive(:info)
allow_any_instance_of(Logger).to receive(:warn)
@@ -51,8 +44,15 @@ RSpec.describe WorkbenchImportWorker,
allow(Api::V1::ApiKey).to receive(:from).and_return(api_key)
allow(ZipService).to receive(:new).with(downloaded_zip).and_return zip_service
- expect(zip_service).to receive(:entry_group_streams).and_return(entry_groups)
- expect( import ).to receive(:update).with(status: 'running')
+ expect(zip_service).to receive(:subdirs).and_return(subdirs)
+ expect( import ).to receive(:update).with(
+ status: 'running',
+ started_at: Time.now
+ )
+ end
+
+ after do
+ Timecop.return
end
@@ -65,13 +65,14 @@ RSpec.describe WorkbenchImportWorker,
.with(host: host, path: path, params: {token: download_token})
.and_return( download_zip_response )
- entry_groups.each do | entry_group_name, entry_group_stream |
- mock_post entry_group_name, entry_group_stream, post_response_ok
+ subdirs.each do |subdir|
+ mock_post subdir, post_response_ok
end
expect( import ).to receive(:update).with(total_steps: 2)
expect( import ).to receive(:update).with(current_step: 1)
expect( import ).to receive(:update).with(current_step: 2)
+ expect( import ).to receive(:update).with(ended_at: Time.now)
worker.perform import.id
@@ -87,14 +88,14 @@ RSpec.describe WorkbenchImportWorker,
.with(host: host, path: path, params: {token: download_token})
.and_return( download_zip_response )
- # First entry_group succeeds
- entry_groups[0..0].each do | entry_group_name, entry_group_stream |
- mock_post entry_group_name, entry_group_stream, post_response_ok
+ # First subdir succeeds
+ subdirs[0..0].each do |subdir|
+ mock_post subdir, post_response_ok
end
- # Second entry_group fails (M I S E R A B L Y)
- entry_groups[1..1].each do | entry_group_name, entry_group_stream |
- mock_post entry_group_name, entry_group_stream, post_response_failure
+ # Second subdir fails (M I S E R A B L Y)
+ subdirs[1..1].each do |subdir|
+ mock_post subdir, post_response_failure
end
expect( import ).to receive(:update).with(total_steps: 3)
@@ -102,18 +103,30 @@ RSpec.describe WorkbenchImportWorker,
expect( import ).to receive(:update).with(current_step: 2)
expect( import ).to receive(:update).with(current_step: 3, status: 'failed')
- worker.perform import.id
+ expect { worker.perform import.id }.to raise_error(StopIteration)
end
end
- def mock_post entry_group_name, entry_group_stream, response
+ def mock_post subdir, response
+ allow(HTTPService).to receive(:upload)
expect( HTTPService ).to receive(:post_resource)
- .with(host: host,
- path: upload_path,
- token: api_key.token,
- params: params,
- upload: {file: [entry_group_stream, 'application/zip', entry_group_name]})
- .and_return(response)
+ .with(
+ host: host,
+ path: upload_path,
+ params: {
+ netex_import: {
+ parent_id: import.id,
+ parent_type: import.class.name,
+ workbench_id: workbench.id,
+ name: subdir.name,
+ file: HTTPService.upload(
+ subdir.stream,
+ 'application/zip',
+ "#{subdir.name}.zip"
+ )
+ }
+ }
+ ).and_return(response)
end
end