Sha256: 884cc3107b1970f2c8724930b80ff4bfa3a48d4acf90a055b53c78a6bb62c168

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

require 'pathname'
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'

describe DataMapper::Serialize, '#to_csv' do
  #
  # ==== blah, it's CSV
  #

  before(:all) do
    query = DataMapper::Query.new(DataMapper::repository(:default), Cow)

    @collection = DataMapper::Collection.new(query) do |c|
      c.load([1, 2, 'Betsy', 'Jersey'])
      c.load([10, 20, 'Berta', 'Guernsey'])
    end

    @empty_collection = DataMapper::Collection.new(query) {}
  end

  it "should serialize a resource to CSV" do
    peter = Cow.new
    peter.id = 44
    peter.composite = 344
    peter.name = 'Peter'
    peter.breed = 'Long Horn'
    peter.to_csv.chomp.split(',')[0..3].should == ['44','344','Peter','Long Horn']
  end

  it "should serialize a collection to CSV" do
    result = @collection.to_csv.gsub(/[[:space:]]+\n/, "\n")
    result.to_a[0].split(',')[0..3].should == ['1','2','Betsy','Jersey']
    result.to_a[1].split(',')[0..3].should == ['10','20','Berta','Guernsey']
  end

  describe "multiple repositories" do
    before(:all) do
      QuanTum::Cat.auto_migrate!
      repository(:alternate){QuanTum::Cat.auto_migrate!}
    end

    it "should use the repsoitory for the model" do
      gerry = QuanTum::Cat.create(:name => "gerry")
      george = repository(:alternate){QuanTum::Cat.create(:name => "george", :is_dead => false)}
      gerry.to_csv.should_not match(/false/)
      george.to_csv.should match(/false/)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dm-serializer-0.9.9 spec/public/to_csv_spec.rb
dm-serializer-0.9.8 spec/public/to_csv_spec.rb