spec/csv2hash/validator/collection_spec.rb in csv2hash-0.5.0 vs spec/csv2hash/validator/collection_spec.rb in csv2hash-0.6.0

- old
+ new

@@ -1,78 +1,86 @@ require 'spec_helper' -describe Csv2hash::Validator::Collection do - let(:options) { {} } - let(:definition) do - Csv2hash::Definition.new([ { position: 0, key: 'name' } ], Csv2hash::Definition::COLLECTION, options).tap do |definition| - definition.validate! - definition.default! +module Csv2hash + describe Validator::Collection do + let(:options) { {} } + let(:definition) do + Main.generate_definition :foo do + set_type { Definition::COLLECTION } + mapping { cell position: 0, key: 'name' } + end.tap do |definition| + definition.validate! + definition.default! + end end - end - let(:ignore_blank_line) { false } + let(:ignore_blank_line) { false } - subject do - Csv2hash::Main.new(definition, data_source, ignore_blank_line: ignore_blank_line) - end + subject do + Main.new(definition, data_source, ignore_blank_line: ignore_blank_line) + end - before do - allow(subject).to receive(:break_on_failure) { true } - end + before do + allow(subject).to receive(:break_on_failure) { true } + end - context 'with valid data' do - let(:data_source) { [ [ 'John Doe' ] ]} + context 'with valid data' do + let(:data_source) { [ [ 'John Doe' ] ]} - it { expect { subject.validate_data! }.to_not raise_error } - context 'with header' do - let(:options) { { header_size: 1 } } - let(:data_source) { [ [ 'Name' ], [ 'John Doe' ] ]} it { expect { subject.validate_data! }.to_not raise_error } + context 'with header' do + let(:options) { { header_size: 1 } } + let(:data_source) { [ [ 'Name' ], [ 'John Doe' ] ]} + it { expect { subject.validate_data! }.to_not raise_error } + end end - end - context '#ignore_blank_line' do - let(:data_source) { [ [ ] ] } - let(:ignore_blank_line) { true } - it { expect { subject.validate_data! }.to_not raise_error } - context 'csv mode' do - before { subject.break_on_failure = false } - its(:errors) { should be_empty } + context '#ignore_blank_line' do + let(:data_source) { [ [ ] ] } + let(:ignore_blank_line) { true } + it { expect { subject.validate_data! }.to_not raise_error } + context 'csv mode' do + before { subject.break_on_failure = false } + its(:errors) { should be_empty } + end end - end - context 'with invalid data' do - let(:data_source) { [ [ ] ] } - it { expect { subject.validate_data! }.to raise_error('undefined name on [0, 0]') } - context 'with header' do - let(:options) { { header_size: 1 } } - let(:data_source) { [ [ 'Name' ], [ ] ]} - it { expect { subject.validate_data! }.to raise_error('undefined name on [1, 0]') } + context 'with invalid data' do + let(:data_source) { [ [ ] ] } + it { expect { subject.validate_data! }.to raise_error('undefined name on [0, 0]') } + context 'with header' do + let(:options) { { header_size: 1 } } + let(:data_source) { [ [ 'Name' ], [ ] ]} + it { expect { subject.validate_data! }.to raise_error('undefined name on [1, 0]') } + end end - end - context 'wihtout exception' do - let(:data_source) { [ [ ] ]} + context 'wihtout exception' do + let(:data_source) { [ [ ] ]} - before do - allow(subject).to receive(:break_on_failure) { false } - end + before do + allow(subject).to receive(:break_on_failure) { false } + end - it { expect(subject.parse.errors.to_csv).to eql ",\"undefined name on [0, 0]\"\n" } + it { expect(subject.parse.errors.to_csv).to eql ",\"undefined name on [0, 0]\"\n" } - context 'errors should be filled' do - before { subject.parse } - its(:errors) { should eql [{x: 0, y: 0, message: 'undefined name on [0, 0]', key: 'name'}] } - end + context 'errors should be filled' do + before { subject.parse } + its(:errors) { should eql [{x: 0, y: 0, message: 'undefined name on [0, 0]', key: 'name'}] } + end - context 'original csv + errors should returned' do - let(:definition) do - Csv2hash::Definition.new( - [{ position: 0, key: 'agree', values: ['yes', 'no'] }], Csv2hash::Definition::COLLECTION).tap do |d| - d.validate!; d.default! + context 'original csv + errors should returned' do + let(:definition) do + Main.generate_definition :foo do + set_type { Definition::COLLECTION } + mapping { cell position: 0, key: 'agree', values: ['yes', 'no'] } + end.tap do |definition| + definition.validate! + definition.default! + end end + let(:data_source) { [ [ 'what?' ], [ 'yes' ], [ 'no' ] ] } + it { expect(subject.parse.errors.to_csv).to eql "what?,\"agree not supported, please use one of [\"\"yes\"\", \"\"no\"\"]\"\n" } end - let(:data_source) { [ [ 'what?' ], [ 'yes' ], [ 'no' ] ] } - it { expect(subject.parse.errors.to_csv).to eql "what?,\"agree not supported, please use one of [\"\"yes\"\", \"\"no\"\"]\"\n" } end - end + end end