]> projects.mako.cc - selectricity/blob - vendor/plugins/yaml_db/spec/yaml_dump_spec.rb
Added some minor TODO's for increased user funcitonality. Change email, beautify...
[selectricity] / vendor / plugins / yaml_db / spec / yaml_dump_spec.rb
1 require File.dirname(__FILE__) + '/base'
2
3 describe YamlDb::Dump do
4         before do
5                 File.stub!(:new).with('dump.yml', 'w').and_return(StringIO.new)
6
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')
14         end
15
16         before(:each) do
17                 @io = StringIO.new
18         end
19
20         it "should return a formatted string" do
21                 YamlDb::Dump.table_record_header(@io)
22                 @io.rewind
23                 @io.read.should == "  records: \n"
24         end
25
26         it "should return a list of column names" do
27                 YamlDb::Dump.table_column_names('mytable').should == [ 'a', 'b' ]
28         end
29
30         it "should return a list of tables without the rails schema table" do
31                 YamlDb::Dump.tables.should == ['mytable']
32         end
33
34         it "should return the total number of records in a table" do
35                 YamlDb::Dump.table_record_count('mytable').should == 2
36         end
37
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')
41                 @io.rewind
42                 @io.read.should == <<EOYAML
43
44 --- 
45 mytable: 
46   columns: 
47   - a
48   - b
49 EOYAML
50         end
51
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 } ]
55                 end
56         end
57
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 } ])
60
61                 records = [ ]
62                 YamlDb::Dump.each_table_page('mytable', 1) do |page|
63                         page.size.should == 1
64                         records.concat(page)
65                 end
66
67                 records.should == [ { 'a' => 1, 'b' => 2 }, { 'a' => 3, 'b' => 4 } ]
68         end
69
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')
72                 @io.rewind
73                 @io.read.should == <<EOYAML
74   records: 
75   - - 1
76     - 2
77   - - 3
78     - 4
79 EOYAML
80         end
81
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')
86         end
87
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')
93         end
94 end

Benjamin Mako Hill || Want to submit a patch?