require "spec_helper" describe ActiveReport::Hash do sample_header = ["Id", "Name", "Speed", "Hp", "Crash safety rated", "Created at"] sample_header_alt = ["No.", "Model", "Speed", "Horse Power", "Crash Safety Rated", "Driven On"] sample_content = { id: "1", name: "Porche", speed: "225", hp: "430", crash_safety_rated: "true", created_at: "2014-08-22T20:59:34.000Z" } sample_content_alt = [ { id: "1", name: "Ferrari", speed: "235", hp: "630", crash_safety_rated: "true", created_at: "2014-08-23T20:59:34.000Z" }, { id: "2", name: "Lamborghini", speed: "245", hp: "720", crash_safety_rated: "true", created_at: "2014-08-24T20:59:34.000Z" }, { id: "3", name: "Bugatti", speed: "256", hp: "1001", crash_safety_rated: "false", created_at: "2014-08-25T20:59:34.000Z" } ] context 'export to csv all data for an' do it 'array of hashes' do sample_csv = File.read('spec/sample/multi_all.csv') constructed_csv = ActiveReport::Hash.export(sample_content_alt) expect(constructed_csv).to eq(sample_csv) end it 'hash' do sample_csv = File.read('spec/sample/solo_all.csv') constructed_csv = ActiveReport::Hash.export(sample_content) expect(constructed_csv).to eq(sample_csv) end end context 'export to csv only values for an' do it 'array of hashes' do sample_csv = File.read('spec/sample/multi_only.csv') constructed_csv = ActiveReport::Hash.export(sample_content_alt, only: [:id, :name]) expect(constructed_csv).to eq(sample_csv) end it 'hash' do sample_csv = File.read('spec/sample/solo_only.csv') constructed_csv = ActiveReport::Hash.export(sample_content, only: :name) expect(constructed_csv).to eq(sample_csv) end end context 'export to csv except values for an' do it 'array of hashes' do sample_csv = File.read('spec/sample/multi_except.csv') constructed_csv = ActiveReport::Hash.export(sample_content_alt, except: [:id, :name]) expect(constructed_csv).to eq(sample_csv) end it 'hash' do sample_csv = File.read('spec/sample/solo_except.csv') constructed_csv = ActiveReport::Hash.export(sample_content, except: :name) expect(constructed_csv).to eq(sample_csv) end end context 'export to csv with headers for an' do it 'array of hashes' do sample_csv = File.read('spec/sample/multi_headers.csv') constructed_csv = ActiveReport::Hash.export(sample_content_alt, headers: sample_header_alt) expect(constructed_csv).to eq(sample_csv) end it 'hash' do sample_csv = File.read('spec/sample/solo_headers.csv') constructed_csv = ActiveReport::Hash.export(sample_content, headers: sample_header_alt) expect(constructed_csv).to eq(sample_csv) end end context 'export to csv with options for an' do it 'array of hashes' do sample_csv = File.read('spec/sample/multi_options.csv') constructed_csv = ActiveReport::Hash.export(sample_content_alt, options: { col_sep: ";" }) expect(constructed_csv).to eq(sample_csv) end it 'hash' do sample_csv = File.read('spec/sample/solo_options.csv') constructed_csv = ActiveReport::Hash.export(sample_content, options: { col_sep: ";" }) expect(constructed_csv).to eq(sample_csv) end end context 'import csv with headers returns an' do it 'array of arrays' do constructed_array = ActiveReport::Hash.import('spec/sample/multi_headerless.csv', headers: sample_header) expect(constructed_array).to eq(sample_content_alt) end it 'array with a hash' do constructed_array = ActiveReport::Hash.import('spec/sample/solo_headerless.csv', headers: sample_header) expect(constructed_array).to eq([].push(sample_content)) end end context 'import csv only values returns an' do it 'array of arrays' do sample_array = sample_content_alt.dup.map { |v| v.dup.keep_if { |k,v| [:id, :name].include?(k) } } constructed_array = ActiveReport::Hash.import('spec/sample/multi_headerless.csv', headers: sample_header, only: [:id, :name]) expect(constructed_array).to eq(sample_array) end it 'array with a hash' do sample_array = sample_content.dup.keep_if { |k,v| [:name].include?(k) } constructed_array = ActiveReport::Hash.import('spec/sample/solo_headerless.csv', headers: sample_header, only: :name) expect(constructed_array).to eq([].push(sample_array)) end end context 'import csv except values returns an' do it 'array of arrays' do sample_array = sample_content_alt.dup.map { |v| v.dup.delete_if { |k,v| [:id, :name].include?(k) } } constructed_array = ActiveReport::Hash.import('spec/sample/multi_headerless.csv', headers: sample_header, except: [:id, :name]) expect(constructed_array).to eq(sample_array) end it 'array with a hash' do sample_array = sample_content.dup.delete_if { |k,v| [:name].include?(k) } constructed_array = ActiveReport::Hash.import('spec/sample/solo_headerless.csv', headers: sample_header, except: :name) expect(constructed_array).to eq([].push(sample_array)) end end context 'import csv with options returns an' do it 'array of hashes' do constructed_array = ActiveReport::Hash.import('spec/sample/multi_headerless_options.csv', headers: sample_header, options: { col_sep: ";" }) expect(constructed_array).to eq(sample_content_alt) end it 'array with a hash' do constructed_array = ActiveReport::Hash.import('spec/sample/solo_headerless_options.csv', headers: sample_header, options: { col_sep: ";" }) expect(constructed_array).to eq([].push(sample_content)) end end end