Skip to content

Instantly share code, notes, and snippets.

@pillowfactory
Created April 9, 2011 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pillowfactory/911780 to your computer and use it in GitHub Desktop.
Save pillowfactory/911780 to your computer and use it in GitHub Desktop.
describe ".import" do
context "with no mapping block" do
context "given a single record" do
it "returns an empty collection" do
CsvMapper.import(nil).should be_empty
end
end
context "given a collection with only one record" do
it "returns an empty collection" do
CsvMapper.import([['John', 'Doe']]).should be_empty
end
end
context "given multiple records" do
let(:header_record) {['attr_name_0', 'attr_name_1']}
let(:first_record) {['Foo', 'Bar']}
context "where the first record has 0 values" do
it "returns an empty collection" do
header_record = []
results = CsvMapper.import([header_record, first_record])
results.should be_empty
end
end
context "where the first record has 2 values" do
let(:records) {[header_record, first_record]}
it "uses the first record to define 2 accessors" do
results = CsvMapper.import(records)
results.first.should respond_to :attr_name_0
results.first.should respond_to :attr_name_1
end
it "maps the 2 attribute values to each object attribute" do
results = CsvMapper.import(records)
results.first.attr_name_0.should == first_record[0]
end
end
context "where the first record has any number of values" do
let(:n) { rand(10) + 1 }
let(:header_record) { (0...n).map {|i| "attr_name_#{i}" } }
let(:first_record) { (0...n).map {|i| "attr_value_#{i}" } }
it "uses the first record to define n accessors" do
results = CsvMapper.import([header_record, first_record])
header_record.each do |attr_name|
results.first.should respond_to attr_name.to_sym
end
end
it "maps n attribute values to each object attribute" do
results = CsvMapper.import([header_record, first_record])
header_record.each_with_index do |attr_name, i|
results.first.send(attr_name.to_sym).should == first_record[i]
end
end
end
end
end
context "with a mapping block" do
context "given a single 'record'"
context "given multiple records"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment