X-Git-Url: https://projects.mako.cc/source/selectricity-live/blobdiff_plain/5637ec97922038fa7a7cfe9f8aa13b1e3002abcd..90a5a1b95e6ec0b5f9aa2db522215463a2909920:/vendor/plugins/yaml_db/spec/yaml_dump_spec.rb diff --git a/vendor/plugins/yaml_db/spec/yaml_dump_spec.rb b/vendor/plugins/yaml_db/spec/yaml_dump_spec.rb new file mode 100644 index 0000000..dd47a9f --- /dev/null +++ b/vendor/plugins/yaml_db/spec/yaml_dump_spec.rb @@ -0,0 +1,94 @@ +require File.dirname(__FILE__) + '/base' + +describe YamlDb::Dump do + before do + File.stub!(:new).with('dump.yml', 'w').and_return(StringIO.new) + + ActiveRecord::Base = mock('ActiveRecord::Base', :null_object => true) + ActiveRecord::Base.connection = mock('connection') + ActiveRecord::Base.connection.stub!(:tables).and_return([ 'mytable', 'schema_info', 'schema_migrations' ]) + ActiveRecord::Base.connection.stub!(:columns).with('mytable').and_return([ mock('a',:name => 'a'), mock('b', :name => 'b') ]) + ActiveRecord::Base.connection.stub!(:select_one).and_return({"count"=>"2"}) + ActiveRecord::Base.connection.stub!(:select_all).and_return([ { 'a' => 1, 'b' => 2 }, { 'a' => 3, 'b' => 4 } ]) + YamlDb::Utils.stub!(:quote_table).with('mytable').and_return('mytable') + end + + before(:each) do + @io = StringIO.new + end + + it "should return a formatted string" do + YamlDb::Dump.table_record_header(@io) + @io.rewind + @io.read.should == " records: \n" + end + + it "should return a list of column names" do + YamlDb::Dump.table_column_names('mytable').should == [ 'a', 'b' ] + end + + it "should return a list of tables without the rails schema table" do + YamlDb::Dump.tables.should == ['mytable'] + end + + it "should return the total number of records in a table" do + YamlDb::Dump.table_record_count('mytable').should == 2 + end + + it "should return a yaml string that contains a table header and column names" do + YamlDb::Dump.stub!(:table_column_names).with('mytable').and_return([ 'a', 'b' ]) + YamlDb::Dump.dump_table_columns(@io, 'mytable') + @io.rewind + @io.read.should == < 1, 'b' => 2 }, { 'a' => 3, 'b' => 4 } ] + end + end + + it "should paginate records from the database and return them" do + ActiveRecord::Base.connection.stub!(:select_all).and_return([ { 'a' => 1, 'b' => 2 } ], [ { 'a' => 3, 'b' => 4 } ]) + + records = [ ] + YamlDb::Dump.each_table_page('mytable', 1) do |page| + page.size.should == 1 + records.concat(page) + end + + records.should == [ { 'a' => 1, 'b' => 2 }, { 'a' => 3, 'b' => 4 } ] + end + + it "should return dump the records for a table in yaml to a given io stream" do + YamlDb::Dump.dump_table_records(@io, 'mytable') + @io.rewind + @io.read.should == <