spec/csv2hash/parser/collection_spec.rb in csv2hash-0.6.8 vs spec/csv2hash/parser/collection_spec.rb in csv2hash-0.7.0
- old
+ new
@@ -32,9 +32,50 @@
{ data: [ { 'name' => 'John Doe' }, { 'name' => 'Jane Doe' } ] })
}
end
end
+ context 'discover' do
+
+ context 'when header keyword present' do
+ let(:data_source) { [ ['Name','Age'], [ 'John Doe',34 ], [ 'Jane Doe', 25] ] }
+ before do
+ subject.definition.header_size = 1
+ definition.cells << Cell.new({ position: /Age/, key: 'age' })
+ definition.cells << Cell.new({ position: /Name/, key: 'name' })
+ end
+ it {
+ expect(subject.tap { |c| c.parse! }.data).to eql(
+ { data: [ { 'name' => 'John Doe', 'age' => 34 }, { 'name' => 'Jane Doe', 'age' => 25} ] }
+ )
+ }
+ end
+
+ context 'when no header keyword found and no break on failure' do
+ let(:data_source) { [ [ 'John Doe',34 ], [ 'Jane Doe', 25] ] }
+ before do
+ subject.break_on_failure = false
+ definition.cells << Cell.new({ position: /Name/, key: 'name' })
+ subject.parse
+ end
+ it{ expect(subject.data).to eql(nil)}
+ it{ expect(subject.errors.first[:message]).to eql("Column doesn't found in foo")}
+ end
+
+ context 'when no header keyword found and break on failure' do
+ let(:data_source) { [ [ 'John Doe',34 ], [ 'Jane Doe', 25] ] }
+ before do
+ subject.break_on_failure = true
+ definition.cells << Cell.new({ position: /Name/, key: 'name' })
+ end
+ it 'should throw exception' do
+ expect {
+ subject.parse
+ }.to raise_error("Column doesn't found in foo")
+ end
+ end
+ end
+
context 'with nested' do
let(:data_source) { [ [ 'John Doe', 22 ], [ 'Jane Doe', 19 ] ] }
before do
definition.cells << Cell.new({ position: 1, key: 'age', nested: 'infos' })
end