Sha256: 0e814a75594a2591c525d21e4b772187a70c2fbc713e62c4f0dcd909b35f6598

Contents?: true

Size: 1.86 KB

Versions: 9

Compression:

Stored size: 1.86 KB

Contents

require File.join(File.dirname(__FILE__),'../../spec_helper')

describe IMW::Formats::Csv do
  # we don't test Tsv as the differences from Csv are trivial and
  # effect only code within the FasterCSV library

  before do
    @sample = IMW.open(File.join(IMWTest::DATA_DIR, 'formats/delimited/sample.csv'))
  end

  it "should be able to parse the CSV" do
    @sample.load[1].last.should == 'lemurinus'
  end

  it "should be able to write CSV" do
    data = [['foobar', 1, 2], ['bazbooz', 3, 4]]
    IMW.open!('test.csv') { |f| f << data }
    IMW.open('test.csv').load[1].last.should == "4"
  end

  it "should raise an error on an invalid schema" do
    lambda { @sample.schema = [{:name => :foobar, :has_many => {:associations => [:foo, :bar]}}] }.should raise_error(IMW::SchemaError)
  end

  it "should accept a valid schema" do
    @sample.schema = [:foo, :bar, :baz]
    @sample.schema.should == [{:name => 'foo'}, {:name => 'bar'}, {:name => 'baz'}]
  end

  describe "guessing a schema" do

    Dir[File.join(IMWTest::DATA_DIR, 'formats/delimited/with_schema/*')].each do |path|
      it "should correctly guess that with_schema/#{File.basename(path)} has headers in its first row" do
        IMW.open(path).headers_in_first_line?.should == true
      end
    end

    Dir[File.join(IMWTest::DATA_DIR, 'formats/delimited/without_schema/*')].each do |path|
      it "should correctly guess that without_schema/#{File.basename(path)} does not have headers in its first row" do
        IMW.open(path).headers_in_first_line?.should == false
      end
    end

    it "should automatically set the headers on a source with guessed headers" do
      resource = IMW.open(Dir[File.join(IMWTest::DATA_DIR, 'formats/delimited/with_schema/*')].first)
      resource.guess_schema!
      resource.delimited_options[:headers].class.should == Array
      resource.schema.should_not be_empty
    end

  end
  
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
imw-0.2.16 spec/imw/formats/delimited_spec.rb
imw-0.2.15 spec/imw/formats/delimited_spec.rb
imw-0.2.14 spec/imw/formats/delimited_spec.rb
imw-0.2.13 spec/imw/formats/delimited_spec.rb
imw-0.2.12 spec/imw/formats/delimited_spec.rb
imw-0.2.11 spec/imw/formats/delimited_spec.rb
imw-0.2.10 spec/imw/formats/delimited_spec.rb
imw-0.2.9 spec/imw/formats/delimited_spec.rb
imw-0.2.8 spec/imw/formats/delimited_spec.rb