spec/csv2hash/structure_validator_spec.rb in csv2hash-0.5.0 vs spec/csv2hash/structure_validator_spec.rb in csv2hash-0.6.0

- old
+ new

@@ -1,89 +1,90 @@ require 'spec_helper' -describe Csv2hash::StructureValidator do - - let(:rules) { [ { position: [0,0], key: 'name' } ] } - let(:options) { {} } - let(:definition) do - Csv2hash::Definition.new(rules, Csv2hash::Definition::MAPPING, options).tap do |definition| - definition.validate! - definition.default! +module Csv2hash + describe StructureValidator do + let(:definition) do + Main.generate_definition :foo do + set_type { Definition::MAPPING } + mapping { cell position: [0,0], key: 'name' } + end.tap do |definition| + definition.validate! + definition.default! + end end - end - subject do - Csv2hash::Main.new(definition, data_source, ignore_blank_line: false) - end - - context 'the csv with errors' do - let(:options){ { structure_rules: { 'MaxColumns' => 2 } } } - before { subject.parse } - let(:data_source) do - [ - [ 'John', 'Doe' ], - [ 'Jane', 'Doe', 'extra field' ] - ] + subject do + Main.new(definition, data_source, ignore_blank_line: false) end - its(:csv_with_errors) { should be_kind_of CsvArray } - it "adds structure error in first cell" do - expect(subject.csv_with_errors.first[:message]).to eq 'Too many columns (max. 2) on line 1' - end - end - - - context '#MaxColumns' do - let(:options){ { structure_rules: { 'MaxColumns' => 2 } } } - - before do - allow(subject).to receive(:break_on_failure) { true } - end - - context 'valid data' do - let(:data_source) do - [ - [ 'John', 'Doe' ] - ] + context 'the csv with errors' do + before do + allow(definition).to receive(:structure_rules) {{ 'MaxColumns' => 2 }} + subject.parse end - it { expect { subject.validate_structure! }.to_not raise_error } - end - - context 'invalid data' do let(:data_source) do [ [ 'John', 'Doe' ], [ 'Jane', 'Doe', 'extra field' ] ] end - it { expect { subject.validate_structure! }.to raise_error 'Too many columns (max. 2) on line 1' } + + its(:csv_with_errors) { should be_kind_of CsvArray } + it "adds structure error in first cell" do + expect(subject.csv_with_errors.first[:message]).to eq 'Too many columns (max. 2) on line 1' + end end - end - context '#MinColumns' do - let(:options){ { structure_rules: { 'MinColumns' => 2 } } } + context '#MaxColumns' do + before do + allow(definition).to receive(:structure_rules) {{ 'MaxColumns' => 2 }} + allow(subject).to receive(:break_on_failure) { true } + end - before do - allow(subject).to receive(:break_on_failure) { true } - end + context 'valid data' do + let(:data_source) do + [ + [ 'John', 'Doe' ] + ] + end + it { expect { subject.validate_structure! }.to_not raise_error } + end - context 'valid data' do - let(:data_source) do - [ - [ 'John', 'Doe', 'foo' ] - ] + context 'invalid data' do + let(:data_source) do + [ + [ 'John', 'Doe' ], + [ 'Jane', 'Doe', 'extra field' ] + ] + end + it { expect { subject.validate_structure! }.to raise_error 'Too many columns (max. 2) on line 1' } end - it { expect { subject.validate_structure! }.to_not raise_error } end - context 'invalid data' do - let(:data_source) do - [ - [ 'John' ], - [ 'Jane', 'Doe' ] - ] + context '#MinColumns' do + before do + allow(definition).to receive(:structure_rules) {{ 'MinColumns' => 2 }} + allow(subject).to receive(:break_on_failure) { true } end - it { expect { subject.validate_structure! }.to raise_error 'Not enough columns (min. 2) on line 0' } + + context 'valid data' do + let(:data_source) do + [ + [ 'John', 'Doe', 'foo' ] + ] + end + it { expect { subject.validate_structure! }.to_not raise_error } + end + + context 'invalid data' do + let(:data_source) do + [ + [ 'John' ], + [ 'Jane', 'Doe' ] + ] + end + it { expect { subject.validate_structure! }.to raise_error 'Not enough columns (min. 2) on line 0' } + end end - end + end end