]> projects.mako.cc - selectricity/blob - vendor/plugins/yaml_db/spec/yaml_load_spec.rb
Added some minor TODO's for increased user funcitonality. Change email, beautify...
[selectricity] / vendor / plugins / yaml_db / spec / yaml_load_spec.rb
1 require File.dirname(__FILE__) + '/base'
2
3 describe YamlDb::Load do
4         before do
5                 YamlDb::Utils.stub!(:quote_table).with('mytable').and_return('mytable')
6
7                 ActiveRecord::Base = mock('ActiveRecord::Base', :null_object => true)
8                 ActiveRecord::Base.connection = mock('connection')
9                 ActiveRecord::Base.connection.stub!(:transaction).and_yield
10         end
11
12         before(:each) do
13                 @io = StringIO.new
14         end
15
16         it "should truncate the table" do
17                 ActiveRecord::Base.connection.stub!(:execute).with("TRUNCATE mytable").and_return(true)
18                 ActiveRecord::Base.connection.should_not_receive(:execute).with("DELETE FROM mytable")
19                 YamlDb::Load.truncate_table('mytable')
20         end
21
22         it "should delete the table if truncate throws an exception" do
23                 ActiveRecord::Base.connection.should_receive(:execute).with("TRUNCATE mytable").and_raise()
24                 ActiveRecord::Base.connection.should_receive(:execute).with("DELETE FROM mytable").and_return(true)
25                 YamlDb::Load.truncate_table('mytable')
26         end
27
28         it "should insert records into a table" do
29                 ActiveRecord::Base.connection.stub!(:quote_column_name).with('a').and_return('a')
30                 ActiveRecord::Base.connection.stub!(:quote_column_name).with('b').and_return('b')
31                 ActiveRecord::Base.connection.stub!(:quote).with(1).and_return("'1'")
32                 ActiveRecord::Base.connection.stub!(:quote).with(2).and_return("'2'")
33                 ActiveRecord::Base.connection.stub!(:quote).with(3).and_return("'3'")
34                 ActiveRecord::Base.connection.stub!(:quote).with(4).and_return("'4'")
35                 ActiveRecord::Base.connection.should_receive(:execute).with("INSERT INTO mytable (a,b) VALUES ('1','2')")
36                 ActiveRecord::Base.connection.should_receive(:execute).with("INSERT INTO mytable (a,b) VALUES ('3','4')")
37
38                 YamlDb::Load.load_records('mytable', ['a', 'b'], [[1, 2], [3, 4]])
39         end
40
41         it "should quote column names that correspond to sql keywords" do
42                 ActiveRecord::Base.connection.stub!(:quote_column_name).with('a').and_return('a')
43                 ActiveRecord::Base.connection.stub!(:quote_column_name).with('count').and_return('"count"')
44                 ActiveRecord::Base.connection.stub!(:quote).with(1).and_return("'1'")
45                 ActiveRecord::Base.connection.stub!(:quote).with(2).and_return("'2'")
46                 ActiveRecord::Base.connection.stub!(:quote).with(3).and_return("'3'")
47                 ActiveRecord::Base.connection.stub!(:quote).with(4).and_return("'4'")
48                 ActiveRecord::Base.connection.should_receive(:execute).with("INSERT INTO mytable (a,\"count\") VALUES ('1','2')")
49                 ActiveRecord::Base.connection.should_receive(:execute).with("INSERT INTO mytable (a,\"count\") VALUES ('3','4')")
50
51                 YamlDb::Load.load_records('mytable', ['a', 'count'], [[1, 2], [3, 4]])
52         end
53
54         it "should truncate the table and then load the records into the table" do
55                 YamlDb::Load.should_receive(:truncate_table).with('mytable')
56                 YamlDb::Load.should_receive(:load_records).with('mytable', ['a', 'b'], [[1, 2], [3, 4]])
57                 YamlDb::Load.should_receive(:reset_pk_sequence!).with('mytable')
58
59                 YamlDb::Load.load_table('mytable', { 'columns' => [ 'a', 'b' ], 'records' => [[1, 2], [3, 4]] })
60         end
61
62         it "should call load structure for each document in the file" do
63                 YAML.should_receive(:load_documents).with(@io).and_yield({ 'mytable' => { 
64                                         'columns' => [ 'a', 'b' ], 
65                                         'records' => [[1, 2], [3, 4]] 
66                                 } })
67                 YamlDb::Load.should_receive(:load_table).with('mytable', { 'columns' => [ 'a', 'b' ], 'records' => [[1, 2], [3, 4]] })
68                 YamlDb::Load.load(@io)
69         end
70
71         it "should not call load structure when the document in the file contains no records" do
72                 YAML.should_receive(:load_documents).with(@io).and_yield({ 'mytable' => nil })
73                 YamlDb::Load.should_not_receive(:load_table)
74                 YamlDb::Load.load(@io)
75         end
76
77         it "should call reset pk sequence if the connection adapter is postgres" do
78                 ActiveRecord::Base.connection.should_receive(:respond_to?).with(:reset_pk_sequence!).and_return(true)
79                 ActiveRecord::Base.connection.should_receive(:reset_pk_sequence!).with('mytable')
80                 YamlDb::Load.reset_pk_sequence!('mytable')
81         end
82
83         it "should not call reset pk sequence for other adapters" do
84                 ActiveRecord::Base.connection.should_receive(:respond_to?).with(:reset_pk_sequence!).and_return(false)
85                 ActiveRecord::Base.connection.should_not_receive(:reset_pk_sequence!)
86                 YamlDb::Load.reset_pk_sequence!('mytable')
87         end
88 end

Benjamin Mako Hill || Want to submit a patch?