1 require File.dirname(__FILE__) + '/base'
3 describe YamlDb::Load do
5 YamlDb::Utils.stub!(:quote_table).with('mytable').and_return('mytable')
7 ActiveRecord::Base = mock('ActiveRecord::Base', :null_object => true)
8 ActiveRecord::Base.connection = mock('connection')
9 ActiveRecord::Base.connection.stub!(:transaction).and_yield
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')
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')
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')")
38 YamlDb::Load.load_records('mytable', ['a', 'b'], [[1, 2], [3, 4]])
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')")
51 YamlDb::Load.load_records('mytable', ['a', 'count'], [[1, 2], [3, 4]])
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')
59 YamlDb::Load.load_table('mytable', { 'columns' => [ 'a', 'b' ], 'records' => [[1, 2], [3, 4]] })
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]]
67 YamlDb::Load.should_receive(:load_table).with('mytable', { 'columns' => [ 'a', 'b' ], 'records' => [[1, 2], [3, 4]] })
68 YamlDb::Load.load(@io)
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)
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')
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')