Skip to content

Commit 1255e19

Browse files
committed
added disable indexes while restoring unit tests
1 parent 3bf5b75 commit 1255e19

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

spec/lib/dump_rake/dump_reader_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,14 @@ def create_entry(rows_count)
490490
expect(@dump).to receive(:insert_into_table).with('`first`', '(`id`, `name`)', @rows.map(&:inspect))
491491
@dump.read_table('first', 100)
492492
end
493+
494+
it "should remove indexes around reading/writing table" do
495+
create_entry(100)
496+
allow(@dump).to receive(:clear_table)
497+
allow(@dump).to receive(:insert_into_table)
498+
expect(@dump).to receive(:with_disabled_indexes).with('first')
499+
@dump.read_table('first', 100)
500+
end
493501
end
494502
end
495503

spec/lib/dump_rake/table_manipulation_spec.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,59 @@
4848
end
4949
end
5050

51+
describe "with_disabled_indexes" do
52+
it "should call ActiveRecord::Base.connection.indexes for table" do
53+
expect(ActiveRecord::Base.connection).to receive(:indexes).with('table').and_return([])
54+
with_disabled_indexes 'table' do
55+
end
56+
end
57+
58+
it "should remove indexes first add indexes then" do
59+
expect(ActiveRecord::Base.connection).to receive(:indexes).with('table').and_return([])
60+
expect(self).to receive(:remove_indexes).with([]).ordered
61+
expect(self).to receive(:add_indexes).with([]).ordered
62+
with_disabled_indexes 'table' do
63+
end
64+
end
65+
end
66+
67+
describe "remove_indexes" do
68+
it "should call ActiveRecord::Base.connection.remove_index for each passed index" do
69+
indexes = [
70+
OpenStruct.new(:table => 'table1', :name => 'index1'),
71+
OpenStruct.new(:table => 'table2', :name => 'index2')
72+
]
73+
expect(ActiveRecord::Base.connection).to receive(:remove_index).with('table1', { :name => 'index1' }).ordered
74+
expect(ActiveRecord::Base.connection).to receive(:remove_index).with('table2', { :name => 'index2' }).ordered
75+
remove_indexes indexes
76+
end
77+
end
78+
79+
describe "index_options" do
80+
it "should return non-empty valid index options" do
81+
index = OpenStruct.new(
82+
:unique => nil,
83+
:name => 'index1',
84+
:unknown_key => 1,
85+
86+
:members => [:unique, :order, :name, :where, :length, :internal, :using, :algorithm, :type]
87+
)
88+
expect(index_options(index)).to eq(:name => 'index1')
89+
end
90+
end
91+
92+
describe "add_indexes" do
93+
it "should call ActiveRecord::Base.connection.add_index for each passed index" do
94+
indexes = [
95+
OpenStruct.new(:table => 'table1', :columns => [:column1], :name => 'index1', :unknown_key => 1, :members => [:name]),
96+
OpenStruct.new(:table => 'table2', :columns => [:column2], :name => 'index2', :unknown_key => 1, :members => [:name])
97+
]
98+
expect(ActiveRecord::Base.connection).to receive(:add_index).with('table1', [:column1], { :name => 'index1' }).ordered
99+
expect(ActiveRecord::Base.connection).to receive(:add_index).with('table2', [:column2], { :name => 'index2' }).ordered
100+
add_indexes indexes
101+
end
102+
end
103+
51104
describe "insert_into_table" do
52105
it "should call ActiveRecord::Base.connection.insert with sql for insert if values is string" do
53106
expect(ActiveRecord::Base.connection).to receive(:insert).with("INSERT INTO `table` (`c1`,`c2`) VALUES (`v1`,`v2`)", anything)

0 commit comments

Comments
 (0)