1 require File.dirname(__FILE__) + '/base'
3 describe YamlDb::Dump do
5 File.stub!(:new).with('dump.yml', 'w').and_return(StringIO.new)
7 ActiveRecord::Base = mock('ActiveRecord::Base', :null_object => true)
8 ActiveRecord::Base.connection = mock('connection')
9 ActiveRecord::Base.connection.stub!(:tables).and_return([ 'mytable', 'schema_info', 'schema_migrations' ])
10 ActiveRecord::Base.connection.stub!(:columns).with('mytable').and_return([ mock('a',:name => 'a'), mock('b', :name => 'b') ])
11 ActiveRecord::Base.connection.stub!(:select_one).and_return({"count"=>"2"})
12 ActiveRecord::Base.connection.stub!(:select_all).and_return([ { 'a' => 1, 'b' => 2 }, { 'a' => 3, 'b' => 4 } ])
13 YamlDb::Utils.stub!(:quote_table).with('mytable').and_return('mytable')
20 it "should return a formatted string" do
21 YamlDb::Dump.table_record_header(@io)
23 @io.read.should == " records: \n"
26 it "should return a list of column names" do
27 YamlDb::Dump.table_column_names('mytable').should == [ 'a', 'b' ]
30 it "should return a list of tables without the rails schema table" do
31 YamlDb::Dump.tables.should == ['mytable']
34 it "should return the total number of records in a table" do
35 YamlDb::Dump.table_record_count('mytable').should == 2
38 it "should return a yaml string that contains a table header and column names" do
39 YamlDb::Dump.stub!(:table_column_names).with('mytable').and_return([ 'a', 'b' ])
40 YamlDb::Dump.dump_table_columns(@io, 'mytable')
42 @io.read.should == <<EOYAML
52 it "should return all records from the database and return them when there is only 1 page" do
53 YamlDb::Dump.each_table_page('mytable') do |records|
54 records.should == [ { 'a' => 1, 'b' => 2 }, { 'a' => 3, 'b' => 4 } ]
58 it "should paginate records from the database and return them" do
59 ActiveRecord::Base.connection.stub!(:select_all).and_return([ { 'a' => 1, 'b' => 2 } ], [ { 'a' => 3, 'b' => 4 } ])
62 YamlDb::Dump.each_table_page('mytable', 1) do |page|
67 records.should == [ { 'a' => 1, 'b' => 2 }, { 'a' => 3, 'b' => 4 } ]
70 it "should return dump the records for a table in yaml to a given io stream" do
71 YamlDb::Dump.dump_table_records(@io, 'mytable')
73 @io.read.should == <<EOYAML
82 it "should dump a table's contents to yaml" do
83 YamlDb::Dump.should_receive(:dump_table_columns)
84 YamlDb::Dump.should_receive(:dump_table_records)
85 YamlDb::Dump.dump_table(@io, 'mytable')
88 it "should not dump a table's contents when the record count is zero" do
89 YamlDb::Dump.stub!(:table_record_count).with('mytable').and_return(0)
90 YamlDb::Dump.should_not_receive(:dump_table_columns)
91 YamlDb::Dump.should_not_receive(:dump_table_records)
92 YamlDb::Dump.dump_table(@io, 'mytable')