require File.expand_path('../../spec_helper', __FILE__)
describe "Exportable Formats" do
before(:each) do
class User
make_exportable
end
clean_database!
User.create(:first_name => "user_1", :last_name => "Doe", :created_at => Time.at(0), :updated_at => Time.at(0))
User.create(:first_name => "user_2", :last_name => "Doe", :created_at => Time.at(0), :updated_at => Time.at(0))
end
context "csv format" do
it "should export the columns as csv" do
User.to_export( "csv", :only => [:first_name, "is_admin"]).should == ["First Name,Is Admin\nuser_1,false\nuser_2,false\n", "text/csv; charset=utf-8; header=present"]
end
it "should export the columns as csv and detect that there is no header" do
User.to_export( "csv", :only => [:first_name, "is_admin"], :headers => false).should == ["user_1,false\nuser_2,false\n", "text/csv; charset=utf-8; header=absent"]
end
end
context "excel format" do
it "should export the columns as xls" do
User.to_export( "xls", :only => [:first_name, "is_admin"]).should == ["
\n\t\n\t\tFirst Name | \n\t\tIs Admin | \n\t
\n\t\n\t\tuser_1 | \n\t\tfalse | \n\t
\n\t\n\t\tuser_2 | \n\t\tfalse | \n\t
\n
\n", "application/vnd.ms-excel; charset=utf-8; header=present"]
end
it "should export the columns as xls and detect no header" do
User.to_export( "xls", :only => [:first_name, "is_admin"], :headers => false).should == ["\n\t\n\t\tuser_1 | \n\t\tfalse | \n\t
\n\t\n\t\tuser_2 | \n\t\tfalse | \n\t
\n
\n", "application/vnd.ms-excel; charset=utf-8; header=absent"]
end
end
context "tsv format" do
it "should export the columns as tsv" do
User.to_export( "tsv", :only => [:first_name, "is_admin"]).should == ["First Name\tIs Admin\nuser_1\tfalse\nuser_2\tfalse\n", "text/tab-separated-values; charset=utf-8; header=present"]
end
it "should export the columns as tsv and detect no header" do
User.to_export( "tsv", :only => [:first_name, "is_admin"], :headers => false).should == ["user_1\tfalse\nuser_2\tfalse\n", "text/tab-separated-values; charset=utf-8; header=absent"]
end
end
context "html format" do
it "should export the columns as html" do
User.to_export( "html", :only => [:first_name, "is_admin"]).should == ["\n\t\n\t\tFirst Name | \n\t\tIs Admin | \n\t
\n\t\n\t\tuser_1 | \n\t\tfalse | \n\t
\n\t\n\t\tuser_2 | \n\t\tfalse | \n\t
\n
\n", "text/html; charset=utf-8; header=present"]
end
it "should export the columns as html and detect no header" do
User.to_export( "html", :only => [:first_name, "is_admin"], :headers => false).should == ["\n\t\n\t\tuser_1 | \n\t\tfalse | \n\t
\n\t\n\t\tuser_2 | \n\t\tfalse | \n\t
\n
\n", "text/html; charset=utf-8; header=absent"]
end
end
context "xml format" do
it "should export the columns as xml" do
User.to_export( "xml", :only => [:first_name, "is_admin"]).should == ["\n\n\t\n\t\tuser_1\n\t\tfalse\n\t\n\t\n\t\tuser_2\n\t\tfalse\n\t\n\n", "application/xml; header=present"]
end
it "should export the columns as xml and detect no header" do
User.to_export( "xml", :only => [:first_name, "is_admin"], :headers => false).should == ["\n\n\t\n\t\tuser_1\n\t\tfalse\n\t\n\t\n\t\tuser_2\n\t\tfalse\n\t\n\n", "application/xml; header=absent"]
end
end
context "json format" do
it "should export the columns as json" do
User.to_export( "json", :only => [:first_name]).should ==["[{\"first_name\":\"user_1\"},{\"first_name\":\"user_2\"}]", "application/json; charset=utf-8;"]
end
end
end