Sha256: a1aaae8d4b21b92106a899b9fe1da9e97bae3f69134380af0f8933e9abbdd0d0

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

require 'spec_helper'
require_relative '../test/lib/person.rb'

describe CsvToObject do

  before(:each) do
    @test_csv_path = 'test/csv/person.csv'
    @source_file = File.open(@test_csv_path)
    @c2o = CsvToObject::CsvToObject.new(@test_csv_path)
    @number_of_data_lines = (@source_file.count - 1)
    @source_file.rewind
    @attribute_names = @source_file.first
    @source_file.rewind
  end

  it "returns an array" do
    @c2o.to_objects.class.should == [].class
  end
  
  it "populates the objects array with objects defined by the source file name" do
    @c2o.to_objects.first.class.to_s.should == File.basename(@source_file.path).gsub('.csv','').capitalize
  end
  
  it "returns as many objects as there are data lines in the csv file" do
    @c2o.to_objects.count.should == @number_of_data_lines
  end
  
  it "instantiate objects defined by its source file name, using the data line as parameters" do
    expected_paramaters = CSV.table(File.open(@test_csv_path),{header_converters: lambda { |h| h.encode(CSV::ConverterEncoding).downcase.gsub(/\s+/, "_") }})
    expected_paramaters.each do |x|
      @c2o.should_receive(:new_object).with(x.to_hash)
    end
    @c2o.to_objects
  end
  
  it "should use the data line to populate attributes" do
    @c2o.to_objects.first.id.should == 0    
    @c2o.to_objects.first.first_name.should == "Billie Lindsey"
    @c2o.to_objects.first.last_name.should == "Austino"
  end
  
  it "replaces spaces in attribute names with underscores" do
    @attribute_names.include?('last name').should == true
    @c2o.should_receive(:new_object).with(include_key('last_name')).exactly(@number_of_data_lines).times
    @c2o.to_objects
  end

  it "downcases attribute names and does not remove non-word characters" do
    @attribute_names.include?('SCORE_[person_id|date]').should == true
    @c2o.should_receive(:new_object).with(include_key('score_[person_id|date]')).exactly(@number_of_data_lines).times
    @c2o.to_objects
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
csv_to_object-0.0.3 spec/csv_to_object_spec.rb