aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/concerns/configurable_spec.rb31
-rw-r--r--spec/fixtures/multiref.zipbin11432 -> 1086 bytes
-rw-r--r--spec/fixtures/nozip.zip1
-rw-r--r--spec/fixtures/ref1.zipbin0 -> 554 bytes
-rw-r--r--spec/fixtures/ref2.zipbin0 -> 554 bytes
-rw-r--r--spec/fixtures/singleref.zipbin0 -> 220 bytes
-rw-r--r--spec/fixtures/uniref.zipbin4904 -> 0 bytes
-rw-r--r--spec/requests/api/v1/netex_import_spec.rb2
-rw-r--r--spec/services/file_service_spec.rb16
-rw-r--r--spec/services/zip_service/zip_entry_data_spec.rb35
-rw-r--r--spec/services/zip_service/zip_entry_dirs_spec.rb33
-rw-r--r--spec/services/zip_service/zip_output_streams_spec.rb17
-rw-r--r--spec/support/fixtures_helper.rb15
-rw-r--r--spec/workers/workbench_import_worker_spec.rb2
14 files changed, 151 insertions, 1 deletions
diff --git a/spec/concerns/configurable_spec.rb b/spec/concerns/configurable_spec.rb
new file mode 100644
index 000000000..822f572c1
--- /dev/null
+++ b/spec/concerns/configurable_spec.rb
@@ -0,0 +1,31 @@
+RSpec.describe Configurable do
+
+ subject do
+ Class.new do
+ include Configurable
+ end
+ end
+
+ let( :something ){ double('something') }
+
+ it 'can be configured' do
+ expect{ subject.config.anything }.to raise_error(NoMethodError)
+
+ subject.config.something = something
+
+ expect( subject.config.something ).to eq(something)
+ expect( subject.new.send(:config).something ).to eq(something)
+ expect( subject.new.send(:config).something ).to eq(something)
+ end
+
+ it 'can be configured with a block' do
+
+ subject.config do | c |
+ c.something = something
+ end
+
+ expect( subject.config.something ).to eq(something)
+ expect( subject.new.send(:config).something ).to eq(something)
+ expect( subject.new.send(:config).something ).to eq(something)
+ end
+end
diff --git a/spec/fixtures/multiref.zip b/spec/fixtures/multiref.zip
index 5b28f7cad..28ddff198 100644
--- a/spec/fixtures/multiref.zip
+++ b/spec/fixtures/multiref.zip
Binary files differ
diff --git a/spec/fixtures/nozip.zip b/spec/fixtures/nozip.zip
new file mode 100644
index 000000000..505bd213a
--- /dev/null
+++ b/spec/fixtures/nozip.zip
@@ -0,0 +1 @@
+no zip file
diff --git a/spec/fixtures/ref1.zip b/spec/fixtures/ref1.zip
new file mode 100644
index 000000000..1cbd0268e
--- /dev/null
+++ b/spec/fixtures/ref1.zip
Binary files differ
diff --git a/spec/fixtures/ref2.zip b/spec/fixtures/ref2.zip
new file mode 100644
index 000000000..342353b07
--- /dev/null
+++ b/spec/fixtures/ref2.zip
Binary files differ
diff --git a/spec/fixtures/singleref.zip b/spec/fixtures/singleref.zip
new file mode 100644
index 000000000..4aee23614
--- /dev/null
+++ b/spec/fixtures/singleref.zip
Binary files differ
diff --git a/spec/fixtures/uniref.zip b/spec/fixtures/uniref.zip
deleted file mode 100644
index 86b688b51..000000000
--- a/spec/fixtures/uniref.zip
+++ /dev/null
Binary files differ
diff --git a/spec/requests/api/v1/netex_import_spec.rb b/spec/requests/api/v1/netex_import_spec.rb
index e67cef9a2..1c34d5544 100644
--- a/spec/requests/api/v1/netex_import_spec.rb
+++ b/spec/requests/api/v1/netex_import_spec.rb
@@ -4,7 +4,7 @@ RSpec.describe "NetexImport", type: :request do
let( :referential ){ create :referential }
- let( :file_path ){'spec/fixtures/neptune.zip'}
+ let( :file_path ){ fixtures_path 'singleref.zip' }
let( :file ){ fixture_file_upload( file_path ) }
let( :post_request ) do
diff --git a/spec/services/file_service_spec.rb b/spec/services/file_service_spec.rb
new file mode 100644
index 000000000..90e164408
--- /dev/null
+++ b/spec/services/file_service_spec.rb
@@ -0,0 +1,16 @@
+RSpec.describe FileService do
+
+ it 'computes a unique filename' do
+ expect( File ).to receive(:exists?).with('xxx/yyy_0').and_return( false )
+
+ expect(described_class.unique_filename('xxx/yyy')).to eq('xxx/yyy_0')
+ end
+
+ it 'handles duplicate names by means of a counter' do
+ expect( File ).to receive(:exists?).with('xxx/yyy_0').and_return( true )
+ expect( File ).to receive(:exists?).with('xxx/yyy_1').and_return( true )
+ expect( File ).to receive(:exists?).with('xxx/yyy_2').and_return( false )
+
+ expect(described_class.unique_filename('xxx/yyy')).to eq('xxx/yyy_2')
+ end
+end
diff --git a/spec/services/zip_service/zip_entry_data_spec.rb b/spec/services/zip_service/zip_entry_data_spec.rb
new file mode 100644
index 000000000..6bfaa8cc4
--- /dev/null
+++ b/spec/services/zip_service/zip_entry_data_spec.rb
@@ -0,0 +1,35 @@
+RSpec.describe ZipService do
+
+ subject{ described_class.new(read_fixture('multiref.zip')) }
+
+ let( :ref1_zipdata ){ read_fixture('ref1.zip') }
+ let( :ref2_zipdata ){ read_fixture('ref2.zip') }
+
+ it 'can group all entries' do
+ expect( subject.entry_groups.keys ).to eq(%w{ref1 ref2})
+ end
+
+ context 'creates correct zip data for each subdir' do
+ it 'e.g. ref1' do
+ ref1_stream = subject.entry_group_streams['ref1']
+ control_stream = Zip::InputStream.open( ref1_stream )
+ control_entries = described_class.entries(control_stream)
+ expect( control_entries.map{ |e| [e.name, e.get_input_stream.read]}.force ).to eq([
+ ["multiref/ref1/", ""],
+ ["multiref/ref1/datum-1", "multi-ref1-datum1\n"],
+ ["multiref/ref1/datum-2", "multi-ref1-datum2\n"]
+ ])
+ end
+ it 'e.g. ref2' do
+ ref2_stream = subject.entry_group_streams['ref2']
+ control_stream = Zip::InputStream.open( ref2_stream )
+ control_entries = described_class.entries(control_stream)
+ expect( control_entries.map{ |e| [e.name, e.get_input_stream.read]}.force ).to eq([
+ ["multiref/ref2/", ""],
+ ["multiref/ref2/datum-1", "multi-ref2-datum1\n"],
+ ["multiref/ref2/datum-2", "multi-ref2-datum2\n"]
+ ])
+ end
+ end
+
+end
diff --git a/spec/services/zip_service/zip_entry_dirs_spec.rb b/spec/services/zip_service/zip_entry_dirs_spec.rb
new file mode 100644
index 000000000..cf927855f
--- /dev/null
+++ b/spec/services/zip_service/zip_entry_dirs_spec.rb
@@ -0,0 +1,33 @@
+RSpec.describe ZipService do
+
+ let( :zip_service ){ described_class }
+
+ let( :zip_data ){ File.read zip_file }
+
+ shared_examples_for 'a correct zip entry reader' do
+ it 'gets all entries of the zip file' do
+ expect( zip_service.new(zip_data).entry_groups.keys ).to eq(expected)
+ end
+ end
+
+ context 'single entry' do
+ let( :zip_file ){ fixtures_path 'multiref.zip' }
+ let( :expected ){ %w{ref1 ref2} }
+
+ it_behaves_like 'a correct zip entry reader'
+ end
+
+ context 'more entries' do
+ let( :zip_file ){ fixtures_path 'singleref.zip' }
+ let( :expected ){ %w{ref} }
+
+ it_behaves_like 'a correct zip entry reader'
+ end
+
+ context 'illegal file' do
+ let( :zip_file ){ fixtures_path 'nozip.zip' }
+ let( :expected ){ [] }
+
+ it_behaves_like 'a correct zip entry reader'
+ end
+end
diff --git a/spec/services/zip_service/zip_output_streams_spec.rb b/spec/services/zip_service/zip_output_streams_spec.rb
new file mode 100644
index 000000000..0b7a1e99b
--- /dev/null
+++ b/spec/services/zip_service/zip_output_streams_spec.rb
@@ -0,0 +1,17 @@
+RSpec.describe ZipService do
+
+ subject{ described_class.new(read_fixture('multiref.zip')) }
+
+
+ it 'can write itself to a file' do
+ streams = subject.entry_group_streams
+ streams.each do | name, stream |
+ File.write("tmp/#{name}.zip", stream.string)
+ end
+ ref1_lines = %x(unzip -l tmp/ref1.zip).split("\n").grep(%r{multiref/ref}).map(&:strip).map(&:split).map(&:last)
+ ref2_lines = %x(unzip -l tmp/ref2.zip).split("\n").grep(%r{multiref/ref}).map(&:strip).map(&:split).map(&:last)
+
+ expect( ref1_lines ).to eq %w(multiref/ref1/ multiref/ref1/datum-1 multiref/ref1/datum-2)
+ expect( ref2_lines ).to eq %w(multiref/ref2/ multiref/ref2/datum-1 multiref/ref2/datum-2)
+ end
+end
diff --git a/spec/support/fixtures_helper.rb b/spec/support/fixtures_helper.rb
new file mode 100644
index 000000000..81f6ce838
--- /dev/null
+++ b/spec/support/fixtures_helper.rb
@@ -0,0 +1,15 @@
+module Support
+ module FixturesHelper
+ def fixtures_path *segments
+ Rails.root.join( fixture_path, *segments )
+ end
+
+ def read_fixture *segments
+ File.read(fixtures_path(*segments))
+ end
+ end
+end
+
+RSpec.configure do |c|
+ c.include Support::FixturesHelper
+end
diff --git a/spec/workers/workbench_import_worker_spec.rb b/spec/workers/workbench_import_worker_spec.rb
index 9a1df5bc3..d227c7610 100644
--- a/spec/workers/workbench_import_worker_spec.rb
+++ b/spec/workers/workbench_import_worker_spec.rb
@@ -30,6 +30,8 @@ RSpec.describe WorkbenchImportWorker, type: [:worker, :request] do
worker.perform import.id
+ require 'pry'
+ binding.pry
expect( File.read(worker.downloaded) ).to eq( result )
expect( worker ).not_to be_single_entry
end